2012-10-30 86 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