Projet

Général

Profil

Paste
Télécharger (3,78 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / taxonomy_menu / taxonomy_menu.batch.inc @ a2baadd1

1
<?php
2

    
3
/**
4
 * @file
5
 * Batch API Code
6
 */
7

    
8
/**
9
 * The $batch can include the following values. Only 'operations'
10
 * and 'finished' are required, all others will be set to default values.
11
 *
12
 * @param operations
13
 *   An array of callbacks and arguments for the callbacks.
14
 *   There can be one callback called one time, one callback
15
 *   called repeatedly with different arguments, different
16
 *   callbacks with the same arguments, one callback with no
17
 *   arguments, etc.
18
 *
19
 * @param finished
20
 *   A callback to be used when the batch finishes.
21
 *
22
 * @param title
23
 *   A title to be displayed to the end user when the batch starts.
24
 *
25
 * @param init_message
26
 *   An initial message to be displayed to the end user when the batch starts.
27
 *
28
 * @param progress_message
29
 *   A progress message for the end user. Placeholders are available.
30
 *   Placeholders note the progression by operation, i.e. if there are
31
 *   2 operations, the message will look like:
32
 *    'Processed 1 out of 2.'
33
 *    'Processed 2 out of 2.'
34
 *   Placeholders include:
35
 *     @current, @remaining, @total and @percentage
36
 *
37
 * @param error_message
38
 *   The error message that will be displayed to the end user if the batch
39
 *   fails.
40
 *
41
 */
42
function _taxonomy_menu_insert_link_items_batch($vid) {
43
  $terms = taxonomy_get_tree($vid, 0, NULL, TRUE);
44
  $menu_name = variable_get(_taxonomy_menu_build_variable('vocab_menu', $vid), FALSE);
45

    
46
  $batch = array(
47
    'operations' => array(
48
      array('_taxonomy_menu_insert_link_items_process', array($terms, $menu_name)),
49
    ),
50
    'finished' => '_taxonomy_menu_insert_link_items_success',
51
    'title' => t('Rebuilding Taxonomy Menu'),
52
    'init_message' => t('The menu items have been deleted, and are about to be regenerated.'),
53
    'progress_message' => t('Import progress: Completed @current of @total stages.'),
54
    'redirect' => 'admin/structure/taxonomy',
55
    'error_message' => t('The Taxonomy Menu rebuild process encountered an error.'),
56
  );
57
  batch_set($batch);
58
  batch_process();
59
}
60

    
61

    
62
/*
63
 * Insert 10 menu link items
64
 */
65
function _taxonomy_menu_insert_link_items_process($terms, $menu_name, &$context) {
66
  _taxonomy_menu_batch_init_context($context, $start, $end, 10);
67

    
68
  // Loop through $terms to process each term.
69
  for ($i=$start; $i<count($terms) && $i<$end; $i++) {
70
    $args = array(
71
      'term' => $terms[$i],
72
      'menu_name' => $menu_name,
73
    );
74
    $mlid = taxonomy_menu_handler('insert', $args);
75
  }
76

    
77
  _taxonomy_menu_batch_update_context($context, $end, count($terms), 'Creating Menu Items');
78
}
79

    
80

    
81

    
82
/*
83
 * Set a message stating the menu has been updated
84
 */
85
function _taxonomy_menu_insert_link_items_success() {
86
  // TODO state menu name here.
87
  drupal_set_message(t('The Taxonomy Menu has been updated.'));
88
}
89

    
90
/*
91
 * Initialise the batch context
92
 * @param array $context Batch context array.
93
 * @param int $start The item to start on in this pass
94
 * @param int $end The end item of this pass
95
 * @param int $items The number of items to process in this pass
96
 */
97
function _taxonomy_menu_batch_init_context(&$context, &$start, &$end, $items) {
98
  // Initialize sandbox the first time through.
99
  if (!isset($context['sandbox']['progress'])) {
100
    $context['sandbox']['progress'] = 0;
101
  }
102

    
103
  $start = $context['sandbox']['progress'];
104
  $end = $start + $items;
105
}
106

    
107

    
108
/*
109
 * Update the batch context
110
 *
111
 * @param array $context Batch context array.
112
 * @param int $end The end point of the most recent pass
113
 * @param int $total The total number of items to process in this batch
114
 * @param str $msg Message for the progress bar
115
 */
116
function _taxonomy_menu_batch_update_context(&$context, $end, $total, $msg) {
117
  //Update context array
118
  if ($end > $total) {
119
    $context['finished'] = 1;
120
    return;
121
  }
122
  $context['message'] = "{$msg}: {$end} of {$total}";
123
  $context['sandbox']['progress'] = $end;
124
  $context['finished'] = $end/$total;
125
}