Projet

Général

Profil

Paste
Télécharger (5,02 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / system / form.api.php @ 76597ebf

1
<?php
2

    
3
/**
4
 * @file
5
 * Callbacks provided by the form system.
6
 */
7

    
8
/**
9
 * @addtogroup callbacks
10
 * @{
11
 */
12

    
13
/**
14
 * Perform a single batch operation.
15
 *
16
 * Callback for batch_set().
17
 *
18
 * @param $MULTIPLE_PARAMS
19
 *   Additional parameters specific to the batch. These are specified in the
20
 *   array passed to batch_set().
21
 * @param $context
22
 *   The batch context array, passed by reference. This contains the following
23
 *   properties:
24
 *   - 'finished': A float number between 0 and 1 informing the processing
25
 *     engine of the completion level for the operation. 1 (or no value
26
 *     explicitly set) means the operation is finished: the operation will not
27
 *     be called again, and execution passes to the next operation or the
28
 *     callback_batch_finished() implementation. Any other value causes this
29
 *     operation to be called again; however it should be noted that the value
30
 *     set here does not persist between executions of this callback: each time
31
 *     it is set to 1 by default by the batch system.
32
 *   - 'sandbox': This may be used by operations to persist data between
33
 *     successive calls to the current operation. Any values set in
34
 *     $context['sandbox'] will be there the next time this function is called
35
 *     for the current operation. For example, an operation may wish to store a
36
 *     pointer in a file or an offset for a large query. The 'sandbox' array key
37
 *     is not initially set when this callback is first called, which makes it
38
 *     useful for determining whether it is the first call of the callback or
39
 *     not:
40
 *     @code
41
 *       if (empty($context['sandbox'])) {
42
 *         // Perform set-up steps here.
43
 *       }
44
 *     @endcode
45
 *     The values in the sandbox are stored and updated in the database between
46
 *     http requests until the batch finishes processing. This avoids problems
47
 *     if the user navigates away from the page before the batch finishes.
48
 *   - 'message': A text message displayed in the progress page.
49
 *   - 'results': The array of results gathered so far by the batch processing.
50
 *     This array is highly useful for passing data between operations. After
51
 *     all operations have finished, this is passed to callback_batch_finished()
52
 *     where results may be referenced to display information to the end-user,
53
 *     such as how many total items were processed.
54
 */
55
function callback_batch_operation($MULTIPLE_PARAMS, &$context) {
56
  if (!isset($context['sandbox']['progress'])) {
57
    $context['sandbox']['progress'] = 0;
58
    $context['sandbox']['current_node'] = 0;
59
    $context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')->fetchField();
60
  }
61

    
62
  // For this example, we decide that we can safely process
63
  // 5 nodes at a time without a timeout.
64
  $limit = 5;
65

    
66
  // With each pass through the callback, retrieve the next group of nids.
67
  $result = db_query_range("SELECT nid FROM {node} WHERE nid > %d ORDER BY nid ASC", $context['sandbox']['current_node'], 0, $limit);
68
  while ($row = db_fetch_array($result)) {
69

    
70
    // Here we actually perform our processing on the current node.
71
    $node = node_load($row['nid'], NULL, TRUE);
72
    $node->value1 = $options1;
73
    $node->value2 = $options2;
74
    node_save($node);
75

    
76
    // Store some result for post-processing in the finished callback.
77
    $context['results'][] = check_plain($node->title);
78

    
79
    // Update our progress information.
80
    $context['sandbox']['progress']++;
81
    $context['sandbox']['current_node'] = $node->nid;
82
    $context['message'] = t('Now processing %node', array('%node' => $node->title));
83
  }
84

    
85
  // Inform the batch engine that we are not finished,
86
  // and provide an estimation of the completion level we reached.
87
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
88
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
89
  }
90
}
91

    
92
/**
93
 * Complete a batch process.
94
 *
95
 * Callback for batch_set().
96
 *
97
 * This callback may be specified in a batch to perform clean-up operations, or
98
 * to analyze the results of the batch operations.
99
 *
100
 * @param $success
101
 *   A boolean indicating whether the batch has completed successfully.
102
 * @param $results
103
 *   The value set in $context['results'] by callback_batch_operation().
104
 * @param $operations
105
 *   If $success is FALSE, contains the operations that remained unprocessed.
106
 */
107
function callback_batch_finished($success, $results, $operations) {
108
  if ($success) {
109
    // Here we do something meaningful with the results.
110
    $message = t("!count items were processed.", array(
111
      '!count' => count($results),
112
      ));
113
    $message .= theme('item_list', array('items' => $results));
114
    drupal_set_message($message);
115
  }
116
  else {
117
    // An error occurred.
118
    // $operations contains the operations that remained unprocessed.
119
    $error_operation = reset($operations);
120
    $message = t('An error occurred while processing %error_operation with arguments: @arguments', array(
121
      '%error_operation' => $error_operation[0],
122
      '@arguments' => print_r($error_operation[1], TRUE)
123
    ));
124
    drupal_set_message($message, 'error');
125
  }
126
}