Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views_bulk_operations / views_bulk_operations.rules.inc @ 9a28ac3f

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
    foreach ($base_view->display as $display_name => $display) {
118
      $view = $base_view->clone_view();
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
    // Grab the entire entity if it's already loaded or fall back to the
177
    // entity identifier.
178
    $entity = $vbo->get_value($result);
179
    $data = $entity ? $entity : $result->{$vbo->real_field};
180
    $entities[] = entity_metadata_wrapper($entity_type, $data);
181
  }
182

    
183
  return array('entity_list' => $entities);
184
}
185

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

    
201
  // Get all entity ids.
202
  $ids = array();
203
  foreach ($vbo->view->result as $row_index => $result) {
204
    $ids[] = $vbo->get_value($result);
205
  }
206

    
207
  return array('entity_id_list' => $ids);
208
}
209

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

    
225
  $entity_type = _views_bulk_operations_rules_get_entity_type($element->settings['view']);
226
  if ($entity_type) {
227
    $element_info['provides']['entity_list']['type'] = "list<$entity_type>";
228
  }
229
}
230

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

    
244
  if (!isset($entity_types[$view_target])) {
245
    $views_settings = explode('|', $view_target);
246
    if ($view = views_get_view($views_settings[0])) {
247
      $view->set_display($views_settings[1]);
248
      $view->build();
249

    
250
      $vbo = _views_bulk_operations_get_field($view);
251
    }
252
    $entity_type = !empty($vbo) ? $vbo->get_entity_type() : '';
253
    $entity_types[$view_target] = $entity_type;
254
  }
255

    
256
  return $entity_types[$view_target];
257
}
258

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

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

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

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

    
311
  return $vbo;
312
}