Advertisement
Guest User

Untitled

a guest
May 24th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.63 KB | None | 0 0
  1. <?php
  2. /**
  3.  * The $batch can include the following values. Only 'operations'
  4.  * and 'finished' are required, all others will be set to default values.
  5.  *
  6.  * @param operations
  7.  *   An array of callbacks and arguments for the callbacks.
  8.  *   There can be one callback called one time, one callback
  9.  *   called repeatedly with different arguments, different
  10.  *   callbacks with the same arguments, one callback with no
  11.  *   arguments, etc.
  12.  *
  13.  * @param finished
  14.  *   A callback to be used when the batch finishes.
  15.  *
  16.  * @param title
  17.  *   A title to be displayed to the end user when the batch starts.
  18.  *
  19.  * @param init_message
  20.  *   An initial message to be displayed to the end user when the batch starts.
  21.  *
  22.  * @param progress_message
  23.  *   A progress message for the end user. Placeholders are available.
  24.  *   Placeholders note the progression by operation, i.e. if there are
  25.  *   2 operations, the message will look like:
  26.  *    'Processed 1 out of 2.'
  27.  *    'Processed 2 out of 2.'
  28.  *   Placeholders include:
  29.  *     @current, @remaining, @total and @percentage
  30.  *
  31.  * @param error_message
  32.  *   The error message that will be displayed to the end user if the batch
  33.  *   fails.
  34.  *
  35.  * @param file
  36.  *   Path to file containing the callbacks declared above. Always needed when
  37.  *   the callbacks are not in a .module file.
  38.  *
  39.  */
  40. function unlock_order_export($month = '', $year = '') {
  41.     $oldfile = file_directory_path().'/'.file_create_filename('export.csv.tmp', 'unlock_order');
  42.     $newfile = file_directory_path().'/'.file_create_filename('export.csv', 'unlock_order');
  43.     $folder = file_directory_path().'/unlock_order';
  44.     file_delete($oldfile);
  45.     file_delete($newfile);
  46.     file_check_directory($folder, FILE_CREATE_DIRECTORY);
  47.     $fp = fopen($oldfile, 'a+');
  48.     fputcsv($fp, array('Date Submitted', 'Date Completed', 'Tool ID', 'Username', 'User Email', 'Order ID', 'Status', 'Remote REF', 'IMEI', 'Model', 'Email', 'Cost', 'Sell Price', 'Profit', 'Notes'));
  49.     fclose($fp);
  50.  
  51.   $batch = array(
  52.     'operations' => array(
  53.         array('unlock_order_export_process', array($month, $year, $oldfile, $newfile)),
  54.       ),
  55.     'finished' => 'unlock_order_export_save',
  56.     'title' => t('Exporting Orders'),
  57.     'init_message' => t('Gathering Data'),
  58.     'progress_message' => '',
  59.     'error_message' => t('Order Export has encountered an error. Contact DA Desigers for troubleshooting.'),
  60.     'file' => drupal_get_path('module', 'unlock_order') . '/batch_export.inc',
  61.   );
  62.   batch_set($batch);
  63.  
  64.   // If this function was called from a form submit handler, stop here,
  65.   // FAPI will handle calling batch_process().
  66.  
  67.   // If not called from a submit handler, add the following,
  68.   // noting the url the user should be sent to once the batch
  69.   // is finished.
  70.  
  71.   batch_process($newfile);
  72. }
  73.  
  74. /**
  75.  * Batch Operation Callback
  76.  *
  77.  * Each batch operation callback will iterate over and over until
  78.  * $context['finished'] is set to 1. After each pass, batch.inc will
  79.  * check its timer and see if it is time for a new http request,
  80.  * i.e. when more than 1 minute has elapsed since the last request.
  81.  *
  82.  * An entire batch that processes very quickly might only need a single
  83.  * http request even if it iterates through the callback several times,
  84.  * while slower processes might initiate a new http request on every
  85.  * iteration of the callback.
  86.  *
  87.  * This means you should set your processing up to do in each iteration
  88.  * only as much as you can do without a php timeout, then let batch.inc
  89.  * decide if it needs to make a fresh http request.
  90.  *
  91.  * @param options1, options2
  92.  *   If any arguments were sent to the operations callback, they
  93.  *   will be the first argments available to the callback.
  94.  *
  95.  * @param context
  96.  *   $context is an array that will contain information about the
  97.  *   status of the batch. The values in $context will retain their
  98.  *   values as the batch progresses.
  99.  *
  100.  * @param $context['sandbox']
  101.  *   Use the $context['sandbox'] rather than $_SESSION to store the
  102.  *   information needed to track information between successive calls.
  103.  *   The values in the sandbox will be stored and updated in the database
  104.  *   between http requests until the batch finishes processing. This will
  105.  *   avoid problems if the user navigates away from the page before the
  106.  *   batch finishes.
  107.  *
  108.  * @param $context['results']
  109.  *   The array of results gathered so far by the batch processing.
  110.  *   The current operation can append its own.
  111.  *
  112.  * @param $context['message']
  113.  *   A text message displayed in the progress page.
  114.  *
  115.  * @param $context['finished']
  116.  *   A float number between 0 and 1 informing the processing engine
  117.  *   of the completion level for the operation.
  118.  *
  119.  *   1 (or no value explicitly set) means the operation is finished
  120.  *   and the batch processing can continue to the next operation.
  121.  */
  122. function unlock_order_export_process($month, $year, $oldfile, $newfile,  &$context) {
  123.   if (!isset($context['sandbox']['progress'])) {
  124.     $context['sandbox']['progress'] = 0;
  125.     $context['sandbox']['current_node'] = 0;
  126.     $context['sandbox']['max'] = db_result(db_query("SELECT COUNT(*) FROM {unlock_orders} uo WHERE uo.status IN (".UNLOCK_FOUND.", ".UNLOCK_NOT_FOUND_NR.", ".UNLOCK_NOT_FOUND_R.")"));
  127.  }
  128.  
  129.   // For this example, we decide that we can safely process
  130.   // 5 nodes at a time without a timeout.
  131.     $limit = 5;
  132.     $fp = fopen($oldfile, 'a+');
  133.   // With each pass through the callback, retrieve the next group of nids.
  134.   $unlock_orders = db_query_range("SELECT * FROM {unlock_orders} uo WHERE uo.status IN (".UNLOCK_FOUND.", ".UNLOCK_NOT_FOUND_NR.", ".UNLOCK_NOT_FOUND_R.") AND uo.order_id > %d ORDER BY uo.order_id ASC", $context['sandbox']['current_node'], 0, $limit);
  135.   while ($order = db_fetch_object($unlock_orders)) {
  136.     $user = user_load($order->user_id);
  137.  
  138.     // Store some result for post-processing in the finished callback.
  139.     $context['results'][] = $order->order_id;
  140.     $row =  array(unlock_order_format_timestamp($order->timestamp), unlock_order_format_timestamp($order->ctimestamp), $order->tool_id, $user->name, $user->mail, $order->order_id, filter_xss(unlock_order_status($order->status)), $order->remote_order_id, $order->imei, $order->model, $order->cust_email, number_format($order->cost, 2), number_format($order->price, 2), number_format($order->price-$order->cost, 2), $order->notes);
  141.     fputcsv($fp, $row);
  142.     // Update our progress information.
  143.     $context['sandbox']['progress']++;
  144.     $context['sandbox']['current_node'] = $order->order_id;
  145.     $context['message'] = t('Now Exporting Order #%order', array('%order' => $order->order_id));
  146.   }
  147.   fclose($fp);
  148.   // Inform the batch engine that we are not finished,
  149.   // and provide an estimation of the completion level we reached.
  150.   if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
  151.     $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  152.   }
  153. }
  154.  
  155. /**
  156.  * Batch 'finished' callback
  157.  */
  158. function unlock_order_export_save($success, $results, $operations) {
  159.     $oldfile = file_directory_path().'/'.file_create_filename('export.csv.tmp', 'unlock_order');
  160.     $newfile = file_directory_path().'/'.file_create_filename('export.csv', 'unlock_order');
  161.   if ($success) {
  162.     // Here we do something meaningful with the results.
  163.     $message = count($results) .' orders exported.';
  164.   } else {
  165.     // An error occurred.
  166.     // $operations contains the operations that remained unprocessed.
  167.     $error_operation = reset($operations);
  168.     $message = 'An error occured while exporting orders. Please speak to a DA Designers Tech Support agent for troubleshooting.';
  169.   }
  170.   file_move($oldfile, $newfile);
  171.   drupal_set_message($message);
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement