2012-10-30 60 views
4

我想设置一个批处理页面进行处理,我需要一个例子。示例模块中给出的示例位于表单内,我需要一个可以独立于处理批处理请求的表单运行的页面。Drupal 7批处理页面示例

例如:

function mymodule_batch_2() { 
$operations[] = array('mymodule_onefunction','mymodule_anotherfunction') 
$batch = array(
    'operations' => $operations, 
    'finished' => 'mymodule_finished', 
    // We can define custom messages instead of the default ones. 
    'title' => t('Processing batch 2'), 
    'init_message' => t('Batch 2 is starting.'), 
    'progress_message' => t('Processed @current out of @total.'), 
    'error_message' => t('Batch 2 has encountered an error.'), 
); 
    batch_set($batch); 
    batch_process(''); 
} 

凡批处理功能将调用等功能于$操作的形式。

+0

删除batch_process( ''); – milkovsky

回答

4

你需要给批处理一个ID工作。 batch_process('mybatch')否则yourmexample是正确的。你有这个策略的特殊问题吗?

+0

我的问题是我试图从不在包含路径中的单独文件调用整个批处理过程。我只是将批处理函数添加到mymodule.module和参数>> file <<中以调用其余函数。 –

+0

或者,您可以将'文件'添加到批量定义中...确保使用drupal_get_path构建包含文件的完整路径 – Dave

2

在这里你可以看到我的批处理relization的样品与形式,调用批处理:

function my_module_menu() { 
     $items['admin/commerce/import'] = array(
     'title' => t('Import'), 
     'page callback' => 'drupal_get_form', 
     'page arguments' => array('my_module_settings_form'), 
     'access arguments' => array('administer site settings'), 
     'type' => MENU_NORMAL_ITEM, 
    ); 
     return $items; 
    } 

    /** 
    * Import form 
    */ 
    function my_module_settings_form() { 
     $form = array(); 
     $form['import'] = array(
     '#type' => 'fieldset', 
     '#title' => t('Import'), 
     '#collapsible' => TRUE, 
     '#collapsed' => FALSE, 
    ); 

     $form['import']['submit'] = array(
     '#type' => 'submit', 
     '#value' => t('Import'), 
    ); 
     return $form; 
    } 

    function my_module_settings_form_submit($form, &$form_state) { 
     batch_my_module_import_start(); 
    } 


    /** 
    * Batch start function 
    */ 
    function batch_my_module_import_start() { 

     $batch = array(
     'title' => t('Import products'), 
     'operations' => array(
      array('_batch_my_module_import', array()), 
     ), 
     'progress_message' => t('Import. Operation @current out of @total.'), 
     'error_message' => t('Error!'), 
     'finished' => 'my_module_batch_finished', 
    ); 

     batch_set($batch); 
    } 

    /** 
    * Import from 1C operation. Deletes Products 
    */ 
    function _batch_my_module_import(&$context) { 
     // Your iterms. In my case select all products 
     $pids = db_select('commerce_product', 'p') 
      ->fields('p', array('sku', 'product_id', 'title')) 
      ->condition('p.type', 'product') 
      ->execute() 
      ->fetchAll(); 

     // Get Count of products 
     if (empty($context['sandbox']['progress'])) { 
     $context['sandbox']['progress'] = 0; 
     $context['sandbox']['max'] = count($pids); 
     watchdog('import', 'import products'); 
     } 
     // Create Iteration variable 
     if (empty($context['sandbox']['iteration'])) { 
     $context['sandbox']['iteration'] = 0; 
     } 
     // Check for the end of cycle 
     if ($context['sandbox']['iteration'] < $context['sandbox']['max']) { 
     // Count of operation in one batch step 
     $limit = 10; 
     // Counts completed operations in one batch step 
     $counter = 0; 
     if ($context['sandbox']['progress'] != 0) { 
      $context['sandbox']['iteration'] = $context['sandbox']['iteration'] + $limit; 
     } 
     // Loop all Product items in xml 
     for ($i = $context['sandbox']['iteration']; $i < $context['sandbox']['max'] && $counter < $limit; $i++) { 

      /* Loop items here */ 
      /* ... */ 
      /* ... */ 
      $context['results']['added']['products'][] = $product_item->title; 


      // Update Progress 
      $context['sandbox']['progress']++; 
      $counter++; 
      // Messages 
      $context['message'] = t('Now processing product %name. Product %progress of %count', array('%name' => $product_item->title, '%progress' => $context['sandbox']['progress'], '%count' => $context['sandbox']['max'])); 
      $context['results']['processed'] = $context['sandbox']['progress']; 
     } 
     } 

     if ($context['sandbox']['progress'] != $context['sandbox']['max']) { 
     $context['finished'] = $context['sandbox']['progress']/$context['sandbox']['max']; 
     } 
    } 


/** 
* Finish of batch. Messagess 
*/ 
function my_module_batch_finished($success, $results, $operations) { 
    if ($success) { 
    drupal_set_message(t('@count products added.', array('@count' => isset($results['added']) ? count($results['added']) : 0))); 
    } 
    else { 
    $error_operation = reset($operations); 
    drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE)))); 
    } 
    watchdog('import', 'import finished'); 
} 
+0

此页面是否自动提交? –

+0

你应该推送提交按钮。你是什​​么意思autosubmit?您没有提及此问题 – milkovsky

-1
function module_name_import_form_submit($form, $form_state) { 
    // Check to make sure that the file was uploaded to the server properly 
    $uri = db_query("SELECT uri FROM {file_managed} WHERE fid = :fid", array(
    ':fid' => $form_state['input']['import']['fid'], 
))->fetchField(); 
    if(!empty($uri)) { 
    if(file_exists(drupal_realpath($uri))) { 
     // Open the csv 
     $handle = fopen(drupal_realpath($uri), "r"); 
     // Go through each row in the csv and run a function on it. In this case we are parsing by '|' (pipe) characters. 
     // If you want commas are any other character, replace the pipe with it. 
     while (($data = fgetcsv($handle, 0, '|', '"')) !== FALSE) { 
     $operations[] = array(
      'module_name_import_batch_processing', // The function to run on each row 
      array($data), // The row in the csv 
     ); 
     } 

     // Once everything is gathered and ready to be processed... well... process it! 
     $batch = array(
     'title' => t('Importing CSV...'), 
     'operations' => $operations, // Runs all of the queued processes from the while loop above. 
     'finished' => 'module_name_import_finished', // Function to run when the import is successful 
     'error_message' => t('The installation has encountered an error.'), 
     'progress_message' => t('Imported @current of @total products.'), 
    ); 
     batch_set($batch); 
     fclose($handle);  
    } 
    } 
    else { 
    drupal_set_message(t('There was an error uploading your file. Please contact a System administator.'), 'error'); 
    } 
} 

/** 
* This function runs the batch processing and creates nodes with then given information 
* @see 
* module_name_import_form_submit() 
*/ 
function module_name_import_batch_processing($data) { 
    // Lets make the variables more readable. 
    $title = $data[0]; 
    $body = $data[1]; 
    $serial_num = $data[2]; 
    // Find out if the node already exists by looking up its serial number. Each serial number should be unique. You can use whatever you want. 
    $nid = db_query("SELECT DISTINCT n.nid FROM {node} n " . 
    "INNER JOIN {field_data_field_serial_number} s ON s.revision_id = n.vid AND s.entity_id = n.nid " . 
    "WHERE field_serial_number_value = :serial", array(
     ':serial' => $serial_num, 
    ))->fetchField(); 
    if(!empty($nid)) { 
    // The node exists! Load it. 
    $node = node_load($nid); 

    // Change the values. No need to update the serial number though. 
    $node->title = $title; 
    $node->body['und'][0]['value'] = $body; 
    $node->body['und'][0]['safe_value'] = check_plain($body); 
    node_save($node); 
    } 
    else { 
    // The node does not exist! Create it. 
    global $user; 
    $node = new StdClass(); 
    $node->type = 'page'; // Choose your type 
    $node->status = 1; // Sets to published automatically, 0 will be unpublished 
    $node->title = $title; 
    $node->uid = $user->uid;   
    $node->body['und'][0]['value'] = $body; 
    $node->body['und'][0]['safe_value'] = check_plain($body); 
    $node->language = 'und'; 

    $node->field_serial_number['und'][0]['value'] = $serial_num; 
    $node->field_serial_number['und'][0]['safe_value'] = check_plain($serial_num); 
    node_save($node); 
    } 
} 

/** 
* This function runs when the batch processing is complete 
* 
* @see 
* module_name_import_form_submit() 
*/ 
function module_name_import_finished() { 
    drupal_set_message(t('Import Completed Successfully')); 
} 
+0

您应该在工作方式以及如何回答问题上添加更多信息和/或说明,以便其他人可以轻松理解,而无需询问更多细节。无论如何,欢迎来到StackOverflow :) – koceeng