Projet

Général

Profil

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

root / drupal7 / sites / all / modules / taxonomy_menu / taxonomy_menu.batch.inc @ 6d8023f2

1
<?php
2

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

    
8
/**
9
 *Starts a batch operation.
10
 *
11
 * @param int $vid
12
 *   Vocabulary ID.
13
 */
14
function _taxonomy_menu_insert_link_items_batch($vid) {
15
  $depth = variable_get(_taxonomy_menu_build_variable('max_depth', $vid), 0);
16
  if ($depth == 0) {
17
    $depth = NULL;
18
  }
19
  $terms = taxonomy_get_tree($vid, 0, $depth, TRUE);
20
  $menu_name = variable_get(_taxonomy_menu_build_variable('vocab_menu', $vid), FALSE);
21

    
22
  $batch = array(
23
    // An array of callbacks and arguments for the callbacks.
24
    'operations' => array(
25
      array('_taxonomy_menu_insert_link_items_process', array($terms, $menu_name)),
26
    ),
27
    // A callback to be used when the batch finishes.
28
    'finished' => '_taxonomy_menu_insert_link_items_success',
29
    // A title to be displayed to the end user when the batch starts.
30
    'title' => t('Rebuilding Taxonomy Menu'),
31
    // An initial message to be displayed to the end user when the batch starts.
32
    'init_message' => t('The menu items have been deleted, and are about to be regenerated.'),
33
    // A progress message for the end user. Placeholders are available.
34
    // Placeholders include: @current, @remaining, @total and @percentage
35
    'progress_message' => t('Import progress: Completed @current of @total stages.'),
36
    // The message that will be displayed to the end user if the batch fails.
37
    'error_message' => t('The Taxonomy Menu rebuild process encountered an error.'),
38
    'redirect' => 'admin/structure/taxonomy',
39
  );
40
  batch_set($batch);
41
  batch_process();
42
}
43

    
44
/**
45
 * Batch operation callback function: inserts 10 menu link items.
46
 *
47
 * @param array $terms
48
 *   Taxonomy terms as from taxonomy_get_tree().
49
 * @param string $menu_name
50
 *   Setting for the menu item name assigned to the vocabulary.
51
 * @param array $context
52
 *   Batch context array.
53
 *
54
 * @see _taxonomy_menu_insert_link_items_batch().
55
 */
56
function _taxonomy_menu_insert_link_items_process($terms, $menu_name, &$context) {
57
  _taxonomy_menu_batch_init_context($context, $start, $end, 10);
58

    
59
  // Loop through $terms to process each term.
60
  for ($i=$start; $i<count($terms) && $i<$end; $i++) {
61
    $args = array(
62
      'term' => $terms[$i],
63
      'menu_name' => $menu_name,
64
    );
65
    $mlid = taxonomy_menu_handler('insert', $args);
66
  }
67

    
68
  _taxonomy_menu_batch_update_context($context, $end, count($terms), 'Creating Menu Items');
69
}
70

    
71

    
72

    
73
/**
74
 * Batch finished callback: sets a message stating the menu has been updated.
75
 *
76
 * @see _taxonomy_menu_insert_link_items_batch().
77
 */
78
function _taxonomy_menu_insert_link_items_success() {
79
  // TODO state menu name here.
80
  drupal_set_message(t('The Taxonomy Menu has been updated.'));
81
}
82

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

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

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