Projet

Général

Profil

Paste
Télécharger (9,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_bulk_operations / views_bulk_operations.rules.inc @ 7547bb19

1
<?php
2

    
3
/**
4
 * @file
5
 * Views Bulk Operations conditions and actions for Rules.
6
 */
7

    
8
/**
9
 * Implements hook_rules_condition_info().
10
 */
11
function views_bulk_operations_rules_condition_info() {
12
  $conditions = array();
13
  $conditions['views_bulk_operations_condition_result_count'] = array(
14
    'label' => t('Check number of results returned by a VBO View'),
15
    'parameter' => array(
16
      'view' => array(
17
        'type' => 'text',
18
        'label' => t('View and display'),
19
        'options list' => 'views_bulk_operations_views_list',
20
        'description' => t('Select the VBO view and display you want to check'),
21
        'restriction' => 'input',
22
      ),
23
      'args' => array(
24
        'type' => 'text',
25
        'label' => t('Arguments'),
26
        'description' => t('Any arguments to pass to the view, one per line.
27
          You may use token replacement patterns.'),
28
        'optional' => TRUE,
29
      ),
30
      'minimum' => array(
31
        'type' => 'integer',
32
        'label' => t('Minimum number of results'),
33
        'description' => t('This condition returns TRUE if the view has at
34
          least the given number of results.'),
35
      ),
36
    ),
37
    'group' => t('Views Bulk Operations'),
38
  );
39

    
40
  return $conditions;
41
}
42

    
43
/**
44
 * Implements hook_rules_action_info().
45
 */
46
function views_bulk_operations_rules_action_info() {
47
  $actions = array();
48
  $actions['views_bulk_operations_action_load_list'] = array(
49
    'label' => t('Load a list of entity objects from a VBO View.'),
50
    'parameter' => array(
51
      'view' => array(
52
        'type' => 'text',
53
        'label' => t('View and display'),
54
        'options list' => 'views_bulk_operations_views_list',
55
        'description' => t('Select the view and display you want to use to
56
          create a list.'),
57
        'restriction' => 'input',
58
      ),
59
      'args' => array(
60
        'type' => 'text',
61
        'label' => t('Arguments'),
62
        'description' => t('Any arguments to pass to the view, one per line.
63
          You may use token replacement patterns.'),
64
        'optional' => TRUE,
65
      ),
66
    ),
67
    'provides' => array(
68
      'entity_list' => array(
69
        'type' => 'list<entity>',
70
        'label' => t('A list of entities'),
71
      ),
72
    ),
73
    'group' => t('Views Bulk Operations'),
74
  );
75
  $actions['views_bulk_operations_action_load_id_list'] = array(
76
    'label' => t('Load a list of entity ids from a VBO View.'),
77
    'parameter' => array(
78
      'view' => array(
79
        'type' => 'text',
80
        'label' => t('View and display'),
81
        'options list' => 'views_bulk_operations_views_list',
82
        'description' => t('Select the view and display you want to use to
83
          create a list.'),
84
        'restriction' => 'input',
85
      ),
86
      'args' => array(
87
        'type' => 'text',
88
        'label' => t('Arguments'),
89
        'description' => t('Any arguments to pass to the view, one per line.
90
          You may use token replacement patterns.'),
91
        'optional' => TRUE,
92
      ),
93
    ),
94
    'provides' => array(
95
      'entity_id_list' => array(
96
        'type' => 'list<integer>',
97
        'label' => t('A list of entity ids'),
98
      ),
99
    ),
100
    'group' => t('Views Bulk Operations'),
101
  );
102

    
103
  return $actions;
104
}
105

    
106
/**
107
 * Lists all available VBO Views and their displays.
108
 * Naturally, only the displays that contain a VBO field are listed.
109
 *
110
 * @return array
111
 *   An array of all views and their displays on the form 'view|display',
112
 *   formatted to be used as an select list.
113
 */
114
function views_bulk_operations_views_list() {
115
  $selectable_displays = array();
116
  foreach (views_get_enabled_views() as $name => $base_view) {
117
    $view = $base_view->clone_view();
118
    foreach ($base_view->display as $display_name => $display) {
119
      if (!$view->set_display($display_name)) {
120
        continue;
121
      }
122

    
123
      // Initialize the style plugin and only continue to initialize handlers
124
      // if the style uses fields.
125
      if (!$view->init_style() || !$view->style_plugin->uses_fields()) {
126
        continue;
127
      }
128

    
129
      $view->init_handlers($display_name);
130
      if (_views_bulk_operations_get_field($view)) {
131
        $selectable_displays[$view->name . '|' . $display_name] = check_plain($view->human_name . ' | ' . $display->display_title);
132
      }
133
    }
134
  }
135

    
136
  return $selectable_displays;
137
}
138

    
139
/**
140
 * The 'views_bulk_operations_condition_result_count' condition.
141
 *
142
 * @param $view
143
 *   A string in the format "$view_name|$display_name".
144
 * @param $args
145
 *   Arguments that should be passed to the View.
146
 * @param $minimum
147
 *   An integer representing the minimum number of results that satisfies the
148
 *   condition.
149
 *
150
 * @return
151
 *   TRUE if the view has more than $minimum results, FALSE otherwise.
152
 */
153
function views_bulk_operations_condition_result_count($view, $args, $minimum) {
154
  $vbo = _views_bulk_operations_rules_get_field($view, $args);
155
  return (count($vbo->view->result) >= $minimum);
156
}
157

    
158
/**
159
 * The 'views_bulk_operations_action_views_load_list' action.
160
 *
161
 * @param $view
162
 *   A string in the format "$view_name|$display_name".
163
 * @param $args
164
 *   Arguments that should be passed to the View.
165
 * @return array
166
 *   Array containing the entity_list, an array of entity objects.
167
 *   - array('entity_list' => array(...))
168
 */
169
function views_bulk_operations_action_load_list($view, $args) {
170
  $vbo = _views_bulk_operations_rules_get_field($view, $args);
171

    
172
  // Get all entities, pass ids to the wrapper for lazy loading.
173
  $entity_type = $vbo->get_entity_type();
174
  $entities = entity_metadata_wrapper("list<$entity_type>", array());
175
  foreach ($vbo->view->result as $row_index => $result) {
176
    $entities[] = entity_metadata_wrapper($entity_type, $vbo->get_value($result));
177
  }
178

    
179
  return array('entity_list' => $entities);
180
}
181

    
182
/**
183
 * The 'views_bulk_operations_action_views_load_id_list' action.
184
 *
185
 * @param $view
186
 *   A string in the format "$view_name|$display_name".
187
 * @param $args
188
 *   Arguments that should be passed to the View.
189
 * @return array
190
 *   Array containing the entity_id_list, an Array of entity ids as integer
191
 *   values.
192
 *   - array('entity_list' => array(...))
193
 */
194
function views_bulk_operations_action_load_id_list($view, $args) {
195
  $vbo = _views_bulk_operations_rules_get_field($view, $args);
196

    
197
  // Get all entity ids.
198
  $ids = array();
199
  foreach ($vbo->view->result as $row_index => $result) {
200
    $ids[] = $vbo->get_value($result);
201
  }
202

    
203
  return array('entity_id_list' => $ids);
204
}
205

    
206
/**
207
 * Info alteration callback for the 'views_bulk_operations_action_views_load_list' action.
208
 *
209
 * The info hook specifies that the action returns a generic list of entities
210
 * (list<entity>). All actions that require entities of specific type can't
211
 * use such entities, so this alter hook specifies the exact entity type
212
 * after the action has been configured, allowing the view to be loaded
213
 * and its entity type extracted.
214
 */
215
function views_bulk_operations_action_load_list_info_alter(&$element_info, RulesAbstractPlugin $element) {
216
  // The action hasn't been configured yet, hence no view. Abort.
217
  if (empty($element->settings['view'])) {
218
    return;
219
  }
220

    
221
  $entity_type = _views_bulk_operations_rules_get_entity_type($element->settings['view']);
222
  if ($entity_type) {
223
    $element_info['provides']['entity_list']['type'] = "list<$entity_type>";
224
  }
225
}
226

    
227
/**
228
 * Helper function that loads and builds (but doesn't execute) the specified view,
229
 * then determines the entity type on which the VBO field operates.
230
 *
231
 * @param $view_target
232
 *   A string in the format "$view_name|$display_name".
233
 *
234
 * @return
235
 *   The entity type on which the VBO field operates.
236
 */
237
function _views_bulk_operations_rules_get_entity_type($view_target) {
238
  $entity_types = &drupal_static(__FUNCTION__);
239

    
240
  if (!isset($entity_types[$view_target])) {
241
    $views_settings = explode('|', $view_target);
242
    if ($view = views_get_view($views_settings[0])) {
243
      $view->set_display($views_settings[1]);
244
      $view->build();
245

    
246
      $vbo = _views_bulk_operations_get_field($view);
247
    }
248
    $entity_type = !empty($vbo) ? $vbo->get_entity_type() : '';
249
    $entity_types[$view_target] = $entity_type;
250
  }
251

    
252
  return $entity_types[$view_target];
253
}
254

    
255
/**
256
 * Helper function that loads, builds and executes the specified view,
257
 * then returns its VBO field.
258
 *
259
 * @param $view_target
260
 *   A string in the format "$view_name|$display_name".
261
 * @param $args
262
 *   Arguments that should be passed to the View.
263
 *
264
 * @return
265
 *   The VBO field. Contains a reference to the View.
266
 */
267
function _views_bulk_operations_rules_get_field($view_target, $args) {
268
  $views = &drupal_static(__FUNCTION__);
269

    
270
  $views_settings = explode('|', $view_target);
271
  $view_name = $views_settings[0];
272
  $display_name = $views_settings[1];
273
  // Create an array of arguments.
274
  $view_arguments = explode("\n", $args);
275
  $view_arguments = array_map('trim', $view_arguments);
276
  $view_arguments = array_filter($view_arguments, 'strlen');
277
  // Append the filtered list of arguments to $views_target, so that the correct
278
  // View is fetched from cache.
279
  if (!empty($view_arguments)) {
280
    $view_target .= '|' . implode('&', $view_arguments);
281
  }
282

    
283
  // Don't execute the requested View more than once in a single page request.
284
  if (isset($views[$view_target])) {
285
    $vbo = _views_bulk_operations_get_field($views[$view_target]);
286
    return $vbo;
287
  }
288

    
289
  // Load the view and set the properties.
290
  $view = views_get_view($view_name);
291
  $view->set_display($display_name);
292
  $view->set_arguments($view_arguments);
293
  $view->build();
294
  $vbo = _views_bulk_operations_get_field($view);
295
  // Unset every field except the VBO one (which holds the entity id).
296
  // That way the performance hit becomes much smaller, because there is no
297
  // chance of views_handler_field_field::post_execute() firing entity_load().
298
  foreach ($view->field as $field_name => $field) {
299
    if ($field_name != $vbo->options['id']) {
300
      unset($view->field[$field_name]);
301
    }
302
  }
303
  $view->execute($view->current_display);
304
  // Save the view in the static cache.
305
  $views[$view_target] = $view;
306

    
307
  return $vbo;
308
}