form.api.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @file
  4. * Callbacks provided by the form system.
  5. */
  6. /**
  7. * @addtogroup callbacks
  8. * @{
  9. */
  10. /**
  11. * Perform a single batch operation.
  12. *
  13. * Callback for batch_set().
  14. *
  15. * @param $MULTIPLE_PARAMS
  16. * Additional parameters specific to the batch. These are specified in the
  17. * array passed to batch_set().
  18. * @param $context
  19. * The batch context array, passed by reference. This contains the following
  20. * properties:
  21. * - 'finished': A float number between 0 and 1 informing the processing
  22. * engine of the completion level for the operation. 1 (or no value
  23. * explicitly set) means the operation is finished: the operation will not
  24. * be called again, and execution passes to the next operation or the
  25. * callback_batch_finished() implementation. Any other value causes this
  26. * operation to be called again; however it should be noted that the value
  27. * set here does not persist between executions of this callback: each time
  28. * it is set to 1 by default by the batch system.
  29. * - 'sandbox': This may be used by operations to persist data between
  30. * successive calls to the current operation. Any values set in
  31. * $context['sandbox'] will be there the next time this function is called
  32. * for the current operation. For example, an operation may wish to store a
  33. * pointer in a file or an offset for a large query. The 'sandbox' array key
  34. * is not initially set when this callback is first called, which makes it
  35. * useful for determining whether it is the first call of the callback or
  36. * not:
  37. * @code
  38. * if (empty($context['sandbox'])) {
  39. * // Perform set-up steps here.
  40. * }
  41. * @endcode
  42. * The values in the sandbox are stored and updated in the database between
  43. * http requests until the batch finishes processing. This avoids problems
  44. * if the user navigates away from the page before the batch finishes.
  45. * - 'message': A text message displayed in the progress page.
  46. * - 'results': The array of results gathered so far by the batch processing.
  47. * This array is highly useful for passing data between operations. After
  48. * all operations have finished, this is passed to callback_batch_finished()
  49. * where results may be referenced to display information to the end-user,
  50. * such as how many total items were processed.
  51. */
  52. function callback_batch_operation($MULTIPLE_PARAMS, &$context) {
  53. if (!isset($context['sandbox']['progress'])) {
  54. $context['sandbox']['progress'] = 0;
  55. $context['sandbox']['current_node'] = 0;
  56. $context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')->fetchField();
  57. }
  58. // For this example, we decide that we can safely process
  59. // 5 nodes at a time without a timeout.
  60. $limit = 5;
  61. // With each pass through the callback, retrieve the next group of nids.
  62. $result = db_query_range("SELECT nid FROM {node} WHERE nid > %d ORDER BY nid ASC", $context['sandbox']['current_node'], 0, $limit);
  63. while ($row = db_fetch_array($result)) {
  64. // Here we actually perform our processing on the current node.
  65. $node = node_load($row['nid'], NULL, TRUE);
  66. $node->value1 = $options1;
  67. $node->value2 = $options2;
  68. node_save($node);
  69. // Store some result for post-processing in the finished callback.
  70. $context['results'][] = check_plain($node->title);
  71. // Update our progress information.
  72. $context['sandbox']['progress']++;
  73. $context['sandbox']['current_node'] = $node->nid;
  74. $context['message'] = t('Now processing %node', array('%node' => $node->title));
  75. }
  76. // Inform the batch engine that we are not finished,
  77. // and provide an estimation of the completion level we reached.
  78. if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
  79. $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  80. }
  81. }
  82. /**
  83. * Complete a batch process.
  84. *
  85. * Callback for batch_set().
  86. *
  87. * This callback may be specified in a batch to perform clean-up operations, or
  88. * to analyze the results of the batch operations.
  89. *
  90. * @param $success
  91. * A boolean indicating whether the batch has completed successfully.
  92. * @param $results
  93. * The value set in $context['results'] by callback_batch_operation().
  94. * @param $operations
  95. * If $success is FALSE, contains the operations that remained unprocessed.
  96. */
  97. function callback_batch_finished($success, $results, $operations) {
  98. if ($success) {
  99. // Here we do something meaningful with the results.
  100. $message = t("!count items were processed.", array(
  101. '!count' => count($results),
  102. ));
  103. $message .= theme('item_list', array('items' => $results));
  104. drupal_set_message($message);
  105. }
  106. else {
  107. // An error occurred.
  108. // $operations contains the operations that remained unprocessed.
  109. $error_operation = reset($operations);
  110. $message = t('An error occurred while processing %error_operation with arguments: @arguments', array(
  111. '%error_operation' => $error_operation[0],
  112. '@arguments' => print_r($error_operation[1], TRUE)
  113. ));
  114. drupal_set_message($message, 'error');
  115. }
  116. }