-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathviews_bulk_operations.module
2149 lines (1972 loc) · 78.2 KB
/
views_bulk_operations.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Allows operations to be performed on items selected in a view.
*
* Table of contents:
* - API functions
* - Drupal hooks and callbacks
* - Helper functions
*/
/**
* API function to programmatically invoke a VBO.
*/
function views_bulk_operations_execute($vid, $operation_callback, $operation_arguments = array(), $view_exposed_input = array(), $view_arguments = array(), $respect_limit = FALSE) {
$view = views_get_view($vid);
if (!is_object($view)) {
watchdog('vbo', 'Could not find view %vid.', array('%vid' => $vid), WATCHDOG_ERROR);
return;
}
$vd = new views_bulk_operations_destructor($view); // this will take care of calling $view->destroy() on exit.
// Find the view display that has the VBO style.
$found = FALSE;
foreach (array_keys($view->display) as $display) {
$display_options = &$view->display[$display]->display_options;
if (isset($display_options['style_plugin']) && $display_options['style_plugin'] == 'bulk') {
$view->set_display($display);
$found = TRUE;
break;
}
}
if (!$found) {
watchdog('vbo', 'Could not find a VBO display in view %vid.', array('%vid' => $vid), WATCHDOG_ERROR);
return;
}
// Execute the view.
$view->set_exposed_input($view_exposed_input);
$view->set_arguments($view_arguments);
$view->set_items_per_page($respect_limit ? $display_options['items_per_page'] : 0);
$view->execute();
if (empty($view->result)) {
watchdog('vbo', 'No results for view %vid.', array('%vid' => $vid), WATCHDOG_WARNING);
return;
}
// Find the selected operation.
$plugin = $view->style_plugin;
$operations = $plugin->get_selected_operations();
if (!isset($operations[$operation_callback])) {
watchdog('vbo', 'Could not find operation %operation in view %vid.', array('%operation' => $operation_callback, '%vid' => $vid), WATCHDOG_ERROR);
return;
}
$operation = $plugin->get_operation_info($operation_callback);
// Execute the operation on the view results.
$execution_type = $plugin->options['execution_type'];
if ($execution_type == VBO_EXECUTION_BATCH) {
$execution_type = VBO_EXECUTION_DIRECT; // we don't yet support Batch API here
}
_views_bulk_operations_execute(
$view,
$view->result,
$operation,
$operation_arguments,
array(
'execution_type' => $execution_type,
'display_result' => $plugin->options['display_result'],
'max_performance' => $plugin->options['max_performance'],
'settings' => $operation['options']['settings'],
)
);
}
/**
* API function to add actions to a VBO.
*/
function views_bulk_operations_add_actions($vid, $actions) {
$view = views_get_view($vid);
if (!is_object($view)) {
watchdog('vbo', 'Could not find view %vid.', array('%vid' => $vid), WATCHDOG_ERROR);
return;
}
// Find the view display that has the VBO style.
$found = FALSE;
foreach (array_keys($view->display) as $display) {
$display_options = &$view->display[$display]->display_options;
if (isset($display_options['style_plugin']) && $display_options['style_plugin'] == 'bulk') {
$found = TRUE;
break;
}
}
if (!$found) {
watchdog('vbo', 'Could not find a VBO display in view %vid.', array('%vid' => $vid), WATCHDOG_ERROR);
return;
}
// Iterate on the desired actions.
$operations = $display_options['style_options']['operations'];
$ignored = $added = array();
if (!empty($actions)) foreach ($actions as $action) {
$modified = FALSE;
if (is_numeric($action)) { // aid
$action_object = db_fetch_object(db_query("SELECT * FROM {actions} WHERE aid = %d", $action));
if (is_object($action_object)) {
$parameters = unserialize($action_object->parameters);
$key = $action_object->callback . (empty($parameters) ? '' : '-'. md5($action_object->parameters));
if (isset($operations[$key])) { // available for this view
$display_options['style_options']['operations'][$key]['selected'] = TRUE;
$modified = TRUE;
}
}
}
else { // callback or title
if (isset($operations[$action])) { // callback and available for this view
$display_options['style_options']['operations'][$action]['selected'] = TRUE;
$modified = TRUE;
}
else { // try the title
$action_object = db_fetch_object(db_query("SELECT * FROM {actions} WHERE description LIKE '%s'", db_escape_string($action)));
if (is_object($action_object)) {
$parameters = unserialize($action_object->parameters);
$key = $action_object->callback . (empty($parameters) ? '' : '-'. md5($action_object->parameters));
if (isset($operations[$key])) { // available for this view
$display_options['style_options']['operations'][$key]['selected'] = TRUE;
$modified = TRUE;
}
}
}
}
if ($modified) {
$added[] = $action;
} else {
$ignored[] = $action;
}
}
// Save the view if anything was changed.
if (!empty($added)) {
$view->save();
views_object_cache_clear('view', $vid);
if (empty($ignored)) {
watchdog('vbo', 'View %vid was successfully modified. The following actions were added: %added.', array(
'%vid' => $vid,
'%added' => implode(', ', $added)
), WATCHDOG_INFO);
} else {
watchdog('vbo', 'View %vid was modified. The following actions were added: %added. The following actions were ignored: %ignored.', array(
'%vid' => $vid,
'%added' => implode(', ', $added),
'%ignored' => implode(', ', $ignored)
), WATCHDOG_WARNING);
}
}
else {
watchdog('vbo', 'View %vid was NOT modified. The following actions were ignored: %ignored.', array(
'%vid' => $vid,
'%ignored' => implode(', ', $ignored)
), WATCHDOG_ERROR);
}
}
// Define the steps in the multistep form that executes operations.
define('VBO_STEP_VIEW', 1);
define('VBO_STEP_CONFIG', 2);
define('VBO_STEP_CONFIRM', 3);
define('VBO_STEP_SINGLE', 4);
// Types of bulk execution.
define('VBO_EXECUTION_DIRECT', 1);
define('VBO_EXECUTION_BATCH', 2);
define('VBO_EXECUTION_QUEUE', 3);
// Types of aggregate actions.
define('VBO_AGGREGATE_FORCED', 1);
define('VBO_AGGREGATE_FORBIDDEN', 0);
define('VBO_AGGREGATE_OPTIONAL', 2);
// Access operations.
define('VBO_ACCESS_OP_VIEW', 0x01);
define('VBO_ACCESS_OP_UPDATE', 0x02);
define('VBO_ACCESS_OP_CREATE', 0x04);
define('VBO_ACCESS_OP_DELETE', 0x08);
/**
* Implementation of hook_cron_queue_info().
*/
function views_bulk_operations_cron_queue_info() {
return array(
'views_bulk_operations' => array(
'worker callback' => '_views_bulk_operations_execute_queue',
'time' => 30,
),
);
}
/**
* Implementation of hook_views_api().
*/
function views_bulk_operations_views_api() {
return array(
'api' => 2.0,
);
}
/**
* Implementation of hook_elements().
*/
function views_bulk_operations_elements() {
$type['views_node_selector'] = array(
'#input' => TRUE,
'#view' => NULL,
'#process' => array('views_node_selector_process'),
);
return $type;
}
/**
* Process function for views_node_selector element.
*
* @see views_bulk_operations_elements()
*/
function views_node_selector_process($element, $edit) {
$view = $element['#view'];
$view_id = _views_bulk_operations_view_id($view);
$view_name = $view->name;
// Gather options.
$result = $view->style_plugin->options['preserve_selection'] ? $_SESSION['vbo_values'][$view_name][$view_id]['result'] : $view->style_plugin->result;
$options = array();
foreach ($result as $k => $v) {
$options[$k] = '';
}
// Fix default value.
$element['#default_value'] += array('selection' => array(), 'selectall' => FALSE);
$element['#default_value']['selection'] =
$element['#default_value']['selectall'] ?
array_diff_key($options, array_filter($element['#default_value']['selection'], '_views_bulk_operations_filter_invert')) :
array_intersect_key($options, array_filter($element['#default_value']['selection']));
// Create selection FAPI elements.
$element['#tree'] = TRUE;
$element['selection'] = array(
'#options' => $options,
'#value' => $element['#default_value']['selection'],
'#attributes' => array('class' => 'select'),
);
$element['selection'] = expand_checkboxes($element['selection']);
$element['selectall'] = array(
'#type' => 'hidden',
'#default_value' => $element['#default_value']['selectall']
);
return $element;
}
/**
* Implementation of hook_theme().
*/
function views_bulk_operations_theme() {
$themes = array(
'views_node_selector' => array(
'arguments' => array('element' => NULL),
),
'views_bulk_operations_confirmation' => array(
'arguments' => array('objects' => NULL, 'invert' => FALSE, 'view' => NULL),
),
'views_bulk_operations_select_all' => array(
'arguments' => array('colspan' => 0, 'selection' => 0, 'view' => NULL),
),
'views_bulk_operations_table' => array(
'arguments' => array('header' => array(), 'rows' => array(), 'attributes' => array(), 'title' => NULL, 'view' => NULL),
'pattern' => 'views_bulk_operations_table__',
),
);
// Load action theme files.
foreach (_views_bulk_operations_load_actions() as $file) {
$action_theme_fn = 'views_bulk_operations_'. $file .'_action_theme';
if (function_exists($action_theme_fn)) {
$themes += call_user_func($action_theme_fn);
}
}
return $themes;
}
/**
* Theme function for 'views_bulk_operations_table'.
*/
function theme_views_bulk_operations_table($header, $rows, $attributes, $title, $view) {
return theme('table', $header, $rows, $attributes, $title);
}
/**
* Template preprocessor for theme function 'views_bulk_operations_table'.
*/
function template_preprocess_views_bulk_operations_table(&$vars, $hook) {
$view = $vars['view'];
$options = $view->style_plugin->options;
$handler = $view->style_plugin;
$fields = &$view->field;
$columns = $handler->sanitize_columns($options['columns'], $fields);
$active = !empty($handler->active) ? $handler->active : '';
foreach ($columns as $field => $column) {
$vars['fields'][$field] = views_css_safe($field);
if ($active == $field) {
$vars['fields'][$field] .= ' active';
}
}
$count = 0;
foreach ($vars['rows'] as $r => &$row) {
$vars['row_classes'][$r][] = ($count++ % 2 == 0) ? 'odd' : 'even';
$cells = $row;
if (isset($row['class'])) {
$vars['row_classes'][$r][] = $row['class'];
}
if (isset($row['data'])) {
$cells = $row['data'];
}
foreach ($cells as $c => &$cell) {
if (is_array($cell) && isset($cell['data'])) {
$cell = $cell['data'];
}
}
$row = $cells;
}
$vars['row_classes'][0][] = 'views-row-first';
$vars['row_classes'][count($vars['row_classes']) - 1][] = 'views-row-last';
$vars['class'] = 'views-bulk-operations-table';
if ($view->style_plugin->options['sticky']) {
drupal_add_js('misc/tableheader.js');
$vars['class'] .= ' sticky-enabled';
}
$vars['class'] .= ' cols-'. count($vars['rows']);
$vars['class'] .= ' views-table';
}
/**
* Theme function for 'views_node_selector'.
*/
function theme_views_node_selector($element) {
module_load_include('inc', 'views', 'theme/theme');
$output = '';
$view = $element['#view'];
$sets = $element['#sets'];
$vars = array(
'view' => $view,
);
// Give each group its own headers row.
foreach ($sets as $title => $records) {
$headers = array();
// template_preprocess_views_view_table() expects the raw data in 'rows'.
$vars['rows'] = $records;
// Render the view as table. Use the hook from from views/theme/theme.inc
// and allow overrides using the same algorithm as the theme system will
// do calling the theme() function.
$hook = 'views_view_table';
$hooks = theme_get_registry();
if (!isset($hooks[$hook])) {
return '';
}
$args = array(&$vars, $hook);
foreach ($hooks[$hook]['preprocess functions'] as $func) {
if (function_exists($func)) {
call_user_func_array($func, $args);
}
}
// Add checkboxes to the header and the rows.
$rows = array();
if (empty($view->style_plugin->options['hide_selector'])) {
$headers[] = array('class' => 'vbo-select-all');
// Add extra status row if needed.
$items_per_page = method_exists($view, 'get_items_per_page') ? $view->get_items_per_page() : (isset($view->pager) ? $view->pager['items_per_page'] : 0);
if ($items_per_page && $view->total_rows > $items_per_page) {
$row = theme('views_bulk_operations_select_all', count($vars['header']) + 1, _views_bulk_operations_get_selection_count($view->style_plugin, $element['#default_value']), $view);
$rows[] = $row;
}
}
else {
$headers[] = array('class' => 'no-select-all');
}
foreach ($vars['header'] as $field => $label) {
$headers[] = array('data' => $label, 'class' => "views-field views-field-{$vars['fields'][$field]}");
}
foreach ($records as $num => $object) {
$vars['row_classes'][$num][] = 'rowclick';
$row = array('class' => implode(' ', $vars['row_classes'][$num]), 'data' => array());
$row['data'][] = theme('checkbox', $element['selection'][_views_bulk_operations_hash_object($object, $view)]);
foreach ($vars['rows'][$num] as $field => $content) {
// Support field classes in Views 3, but provide a fallback for Views 2.
if (views_api_version() == 2) {
$row['data'][] = array('data' => $content, 'class' => 'views-field views-field-' . $vars['fields'][$field]);
}
else {
$row['data'][] = array('data' => $content, 'class' => $vars['field_classes'][$field][$num]);
}
}
$rows[] = $row;
}
$theme_functions = views_theme_functions('views_bulk_operations_table', $view, $view->display[$view->current_display]);
$output .= theme($theme_functions, $headers, $rows, array('class' => $vars['class']), $title, $view);
$output .= theme('hidden', $element['selectall']);
}
return theme('form_element', $element, $output);
}
/**
* Theme function for 'views_bulk_operations_select_all'.
*/
function theme_views_bulk_operations_select_all($colspan, $selection, $view) {
$clear_selection = t('Clear selection');
$select_label = t('Select all items:');
$this_page = t('on this page only');
$all_pages = t('across all pages');
$this_page_checked = $selection['selectall'] ? '' : ' checked="checked"';
$all_pages_checked = $selection['selectall'] ? ' checked="checked"' : '';
$selection_count = t('<span class="selected">@selected</span> items selected.', array('@selected' => $selection['selected']));
$output = <<<EOF
$selection_count
<span class="select">
$select_label
<input type="radio" name="select-all" id="select-this-page" value="0"$this_page_checked /><label for="select-this-page">$this_page</label>
<input type="radio" name="select-all" id="select-all-pages" value="1"$all_pages_checked /><label for="select-all-pages">$all_pages</label>
</span>
<input type="button" id="clear-selection" value="$clear_selection" />
EOF;
return array(array('data' => $output, 'class' => 'views-field views-field-select-all', 'colspan' => $colspan));
}
/**
* Form implementation for main VBO multistep form.
*/
function views_bulk_operations_form($form_state, $form_id, $plugin) {
// Erase the form parameters from $_REQUEST for a clean pager.
if (!empty($form_state['post'])) {
$_REQUEST = array_diff($_REQUEST, $form_state['post']);
}
// Force browser to reload the page if Back is hit.
if (!empty($_SERVER['HTTP_USER_AGENT']) && preg_match('/msie/i', $_SERVER['HTTP_USER_AGENT'])) {
drupal_set_header("Cache-Control: no-cache"); // works for IE6+
}
else {
drupal_set_header("Cache-Control: no-store"); // works for Firefox and other browsers
}
// Which step is this?
if (empty($form_state['storage']['step'])) {
// If empty view, render the empty text.
if (empty($plugin->view->result)) {
$form['empty'] = array('#value' => $plugin->view->display_handler->render_empty());
return $form;
}
// If there's a session variable on this view, pre-load the old values.
$view_id = _views_bulk_operations_view_id($plugin->view);
$view_name = $plugin->view->name;
if (isset($_SESSION['vbo_values'][$view_name][$view_id]) && $plugin->options['preserve_selection']) {
// Avoid PHP warnings.
$_SESSION['vbo_values'][$view_name][$view_id] += array(
'selection' => array(),
'selectall' => FALSE,
'operation' => NULL,
);
$default_objects = array(
'selection' => $_SESSION['vbo_values'][$view_name][$view_id]['selection'],
'selectall' => $_SESSION['vbo_values'][$view_name][$view_id]['selectall'],
);
$default_operation = $_SESSION['vbo_values'][$view_name][$view_id]['operation'];
}
else {
$default_objects = array('selection' => array(), 'selectall' => FALSE);
$default_operation = NULL;
}
if (count($plugin->get_selected_operations()) == 1 && $plugin->options['merge_single_action']) {
$step = VBO_STEP_SINGLE;
}
else {
$step = VBO_STEP_VIEW;
}
}
else {
_views_bulk_operations_strip_view($plugin->view);
switch ($form_state['storage']['step']) {
case VBO_STEP_VIEW:
$operation = $form_state['storage']['operation'];
if ($operation['configurable']) {
$step = VBO_STEP_CONFIG;
}
else {
$step = VBO_STEP_CONFIRM;
}
break;
case VBO_STEP_SINGLE:
case VBO_STEP_CONFIG:
$step = VBO_STEP_CONFIRM;
break;
}
}
$form['step'] = array(
'#type' => 'value',
'#value' => $step
);
$form['#plugin'] = $plugin;
switch ($step) {
case VBO_STEP_VIEW:
$form['select'] = array(
'#type' => 'fieldset',
'#title' => t('Bulk operations'),
'#prefix' => '<div id="views-bulk-operations-select">',
'#suffix' => '</div>',
);
$form['objects'] = array(
'#type' => 'views_node_selector',
'#view' => $plugin->view,
'#sets' => $plugin->sets,
'#default_value' => $default_objects,
'#prefix' => '<div class="views-node-selector">',
'#suffix' => '</div>',
);
if ($plugin->options['display_type'] == 0) {
// Create dropdown and submit button.
$form['select']['operation'] = array(
'#type' => 'select',
'#options' => array(0 => t('- Choose an operation -')) + $plugin->get_selected_operations(),
'#default_value' => $default_operation,
'#prefix' => '<div id="views-bulk-operations-dropdown">',
'#suffix' => '</div>',
);
$form['select']['submit'] = array(
'#type' => 'submit',
'#value' => t('Execute'),
'#prefix' => '<div id="views-bulk-operations-submit">',
'#suffix' => '</div>',
);
}
else {
// Create buttons for actions.
foreach ($plugin->get_selected_operations() as $md5 => $description) {
$form['select'][$md5] = array(
'#type' => 'submit',
'#value' => $description,
'#hash' => $md5,
);
}
}
break;
case VBO_STEP_SINGLE:
$operation_keys = array_keys($plugin->get_selected_operations());
$operation = $plugin->get_operation_info($operation_keys[0]);
$form['operation'] = array('#type' => 'value', '#value' => $operation_keys[0]);
if ($operation['configurable']) {
$form += _views_bulk_operations_action_form(
$operation,
$plugin->view,
$plugin->result,
$operation['options']['settings']
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => $operation['label'],
'#prefix' => '<div id="views-bulk-operations-submit">',
'#suffix' => '</div>',
);
$form['objects'] = array(
'#type' => 'views_node_selector',
'#view' => $plugin->view,
'#sets' => $plugin->sets,
'#default_value' => $default_objects,
'#prefix' => '<div class="views-node-selector">',
'#suffix' => '</div>',
);
break;
case VBO_STEP_CONFIG:
$operation = $form_state['storage']['operation'];
$form += _views_bulk_operations_action_form(
$operation,
$plugin->view,
_views_bulk_operations_get_selection_full($plugin, $form_state),
$operation['options']['settings']
);
$form['execute'] = array(
'#type' => 'submit',
'#value' => t('Next'),
'#weight' => 98,
);
$query = drupal_query_string_encode($_GET, array('q'));
$form['cancel'] = array(
'#type' => 'markup',
'#value' => l('Cancel', $_GET['q'], array('query' => $query)),
'#weight' => 99,
);
drupal_set_title(t('Set parameters for %operation', array('%operation' => $operation['label'])));
break;
case VBO_STEP_CONFIRM:
$operation = $form_state['storage']['operation'];
$query = drupal_query_string_encode($_GET, array('q'));
$form = confirm_form($form,
t('Are you sure you want to perform %operation on the selected items?', array('%operation' => $operation['label'])),
array('path' => $_GET['q'], 'query' => $query),
theme('views_bulk_operations_confirmation', $form_state['storage']['selection'], $form_state['storage']['selectall'], $plugin->view)
);
break;
}
// Use views_bulk_operations_form_submit() for form submit, regardless of form_id.
$form['#submit'][] = 'views_bulk_operations_form_submit';
$form['#validate'][] = 'views_bulk_operations_form_validate';
$form['#attributes']['class'] = 'views-bulk-operations-form views-bulk-operations-form-step-' . $step;
// A view with ajax enabled, and a space in the url, has to be decoded to work fine,
// @see http://drupal.org/node/1325632
$form['#action'] = urldecode(request_uri());
return $form;
}
/**
* Implementation of hook_form_alter().
*/
function views_bulk_operations_form_alter(&$form, &$form_state) {
// Get the form ID here to add the JS settings.
if (!empty($form['form_id']) && strpos($form['form_id']['#value'], 'views_bulk_operations_form') === 0 && !empty($form['#plugin'])) {
_views_bulk_operations_add_js($form['#plugin'], $form['#id'], $form['form_id']['#value']);
}
}
/**
* Form validate function for views_bulk_operations_form().
*/
function views_bulk_operations_form_validate($form, &$form_state) {
$form_id = $form_state['values']['form_id'];
$plugin = $form['#plugin'];
$view_id = _views_bulk_operations_view_id($plugin->view);
$view_name = $plugin->view->name;
switch ($form_state['values']['step']) {
case VBO_STEP_VIEW:
if (!array_filter($form_state['values']['objects']['selection']) && (empty($_SESSION['vbo_values'][$view_name][$view_id]) || !array_filter($_SESSION['vbo_values'][$view_name][$view_id]['selection']))) {
form_set_error('objects', t('No item selected. Please select one or more items.'));
}
if (!empty($form_state['clicked_button']['#hash'])) {
$form_state['values']['operation'] = $form_state['clicked_button']['#hash'];
}
if (!$form_state['values']['operation']) { // No action selected
form_set_error('operation', t('No operation selected. Please select an operation to perform.'));
}
if (form_get_errors()) {
_views_bulk_operations_add_js($plugin, $form['#id'], $form_id);
}
break;
case VBO_STEP_SINGLE:
if (!array_filter($form_state['values']['objects']['selection']) && (empty($_SESSION['vbo_values'][$view_name][$view_id]) || !array_filter($_SESSION['vbo_values'][$view_name][$view_id]['selection']))) {
form_set_error('objects', t('No item selected. Please select one or more items.'));
}
$operation = $plugin->get_operation_info($form_state['values']['operation']);
if ($operation['configurable']) {
_views_bulk_operations_action_validate($operation, $form, $form_state);
}
if (form_get_errors()) {
_views_bulk_operations_add_js($plugin, $form['#id'], $form_id);
}
break;
case VBO_STEP_CONFIG:
$operation = $form_state['storage']['operation'];
_views_bulk_operations_action_validate($operation, $form, $form_state);
// If the action validation fails, Form API will bring us back to this step.
// We need to strip the view here because the form function will not be called.
// Also, the $plugin variable above was carried over from last submission, so it
// does not represent the current instance of the plugin.
// That's why we had to store instances of the plugin in this global array.
if (form_get_errors()) {
global $vbo_plugins;
if (isset($vbo_plugins[$form_id])) {
_views_bulk_operations_strip_view($vbo_plugins[$form_id]->view);
}
}
break;
}
}
/**
* Form submit function for views_bulk_operations_form().
*/
function views_bulk_operations_form_submit($form, &$form_state) {
$form_id = $form_state['values']['form_id'];
$plugin = $form['#plugin'];
$view = $plugin->view;
$view_id = _views_bulk_operations_view_id($view);
$view_name = $view->name;
$form_state['storage']['step'] = $step = $form_state['values']['step'];
switch ($step) {
case VBO_STEP_VIEW:
$form_state['storage']['selection'] = _views_bulk_operations_get_selection($plugin, $form_state, $form_id);
$form_state['storage']['selectall'] = $form_state['values']['objects']['selectall'];
$form_state['storage']['operation'] = $operation = $plugin->get_operation_info($form_state['values']['operation']);
$_SESSION['vbo_values'][$view_name][$view_id]['operation'] = $operation['key'];
if (!$operation['configurable'] && !empty($operation['options']['skip_confirmation'])) {
break; // Go directly to execution
}
return;
case VBO_STEP_SINGLE:
$form_state['storage']['selection'] = _views_bulk_operations_get_selection($plugin, $form_state, $form_id);
$form_state['storage']['selectall'] = $form_state['values']['objects']['selectall'];
$form_state['storage']['operation'] = $operation = $plugin->get_operation_info($form_state['values']['operation']);
$_SESSION['vbo_values'][$view_name][$view_id]['operation'] = $operation['key'];
if ($operation['configurable']) {
$form_state['storage']['operation_arguments'] = _views_bulk_operations_action_submit($operation, $form, $form_state);
}
if (!empty($operation['options']['skip_confirmation'])) {
break; // Go directly to execution
}
return;
case VBO_STEP_CONFIG:
$operation = $form_state['storage']['operation'];
$form_state['storage']['operation_arguments'] = _views_bulk_operations_action_submit($operation, $form, $form_state);
if (!empty($operation['options']['skip_confirmation'])) {
break; // Go directly to execution
}
return;
case VBO_STEP_CONFIRM:
break;
}
// Clean up unneeded SESSION variables.
unset($_SESSION['vbo_values'][$view->name]);
// Execute the VBO.
$objects = _views_bulk_operations_get_selection_full($plugin, $form_state);
$operation = $form_state['storage']['operation'];
$operation_arguments = array();
if ($operation['configurable']) {
$operation_arguments = $form_state['storage']['operation_arguments'];
}
_views_bulk_operations_execute(
$view,
$objects,
$operation,
$operation_arguments,
array(
'execution_type' => $plugin->options['execution_type'],
'display_result' => $plugin->options['display_result'],
'max_performance' => $plugin->options['max_performance'],
'settings' => $operation['options']['settings'],
)
);
// Clean up the form.
$query = drupal_query_string_encode($_GET, array('q'));
$form_state['redirect'] = array('path' => $view->get_url(), 'query' => $query);
unset($form_state['storage']);
}
/**
* Compute the selection based on the settings.
*/
function _views_bulk_operations_get_selection($plugin, $form_state, $form_id) {
$result = $plugin->result;
$selection = $form_state['values']['objects']['selection'];
if ($plugin->options['preserve_selection']) {
$view_id = _views_bulk_operations_view_id($plugin->view);
$view_name = $plugin->view->name;
$result = $_SESSION['vbo_values'][$view_name][$view_id]['result'];
$selection = $_SESSION['vbo_values'][$view_name][$view_id]['selection'];
}
$selection = $form_state['values']['objects']['selectall'] ?
array_intersect_key($result, array_filter($selection, '_views_bulk_operations_filter_invert')) :
array_intersect_key($result, array_filter($selection));
return $selection;
}
/**
* Compute the actual selected objects based on the settings.
*/
function _views_bulk_operations_get_selection_full($plugin, $form_state) {
// Get the objects from the view if selectall was chosen.
$view = $plugin->view;
if ($form_state['storage']['selectall']) {
$view_copy = views_get_view($view->name);
$view_copy->set_exposed_input($view->exposed_input);
$view_copy->set_arguments($view->args);
$view_copy->set_items_per_page(0);
$view_copy->skip_render = TRUE; // signal our plugin to skip the rendering
$view_copy->render($view->current_display);
$objects = array();
foreach ($view_copy->result as $row) {
$objects[_views_bulk_operations_hash_object($row, $view_copy)] = $row;
}
$view_copy->destroy();
$objects = array_diff_key($objects, $form_state['storage']['selection']);
}
else {
$objects = $form_state['storage']['selection'];
}
return $objects;
}
/**
* Compute the actual number of selected items.
*/
function _views_bulk_operations_get_selection_count($plugin, $selection) {
if ($plugin->options['preserve_selection']) {
$view_id = _views_bulk_operations_view_id($plugin->view);
$view_name = $plugin->view->name;
$selection = $_SESSION['vbo_values'][$view_name][$view_id];
}
return array(
'selectall' => $selection['selectall'],
'selected' => $selection['selectall'] ?
$plugin->view->total_rows - count(array_filter($selection['selection'], '_views_bulk_operations_filter_invert')) :
count(array_filter($selection['selection']))
);
}
/**
* Theme function to show the confirmation page before executing the action.
*/
function theme_views_bulk_operations_confirmation($objects, $invert, $view) {
$selectall = $invert ? (count($objects) == 0) : (count($objects) == $view->total_rows);
if ($selectall) {
$output = format_plural(
$view->total_rows,
'You selected the only item in this view.',
'You selected all <strong>@count</strong> items in this view.'
);
}
else {
$object_info = _views_bulk_operations_object_info_for_view($view);
$items = array();
foreach ($objects as $row) {
$oid = $row->{$view->base_field};
if ($object = call_user_func($object_info['load'], $oid)) {
$items[] = check_plain((string)$object->{$object_info['title']});
}
}
$output = theme('item_list', $items,
$invert ?
format_plural(
count($objects),
'You selected all ' . $view->total_rows . ' but the following item:',
'You selected all ' . $view->total_rows . ' but the following <strong>@count</strong> items:'
) :
format_plural(
count($objects),
'You selected the following item:',
'You selected the following <strong>@count</strong> items:'
)
);
}
return $output;
}
/**
* Implementation of hook_forms().
*
* Force each instance of function to use the same callback.
*/
function views_bulk_operations_forms($form_id, $args) {
// Ensure we map a callback for our form and not something else.
$forms = array();
if (strpos($form_id, 'views_bulk_operations_form_') === 0) {
// Let the forms API know where to get the form data corresponding
// to this form id.
$forms[$form_id] = array(
'callback' => 'views_bulk_operations_form',
'callback arguments' => array($form_id),
);
}
return $forms;
}
/**
* Implementation of hook_views_bulk_operations_object_info()
*
* Hook used by VBO to be able to handle different objects as does Views 2 and the Drupal core action system.
*
* The array returned for each object type contains:
* 'type' (required) => the object type name, should be the same as 'type' field in hook_action_info().
* 'context' (optional) => the context name that should receive the object, defaults to the value of 'type' above.
* 'base_table' (required) => the Views 2 table name corresponding to that object type, should be the same as the $view->base_table attribute.
* 'oid' (currently unused) => an attribute on the object that returns the unique object identifier (should be the same as $view->base_field).
* 'load' (required) => a function($oid) that returns the corresponding object.
* 'title' (required) => an attribute on the object that returns a human-friendly identifier of the object.
* 'access' (optional) => a function($op, $node, $account = NULL) that behaves like node_access().
*
* The following attributes allow VBO to show actions on view types different than the action's type:
* 'hook' (optional) => the name of the hook supported by this object type, as defined in the 'hooks' attribute of hook_action_info().
* 'normalize' (optional) => a function($type, $object) that takes an object type and the object instance, returning additional context information for cross-type
*
* e.g., an action declaring hooks => array('user') while of type 'system' will be shown on user views, and VBO will call the user's 'normalize' function to
* prepare the action to fit the user context.
*/
function views_bulk_operations_views_bulk_operations_object_info() {
$object_info = array(
'node' => array(
'type' => 'node',
'base_table' => 'node',
'load' => '_views_bulk_operations_node_load',
'oid' => 'nid',
'title' => 'title',
'access' => 'node_access',
'hook' => 'nodeapi',
'normalize' => '_views_bulk_operations_normalize_node_context',
),
'user' => array(
'type' => 'user',
'base_table' => 'users',
'load' => 'user_load',
'oid' => 'uid',
'title' => 'name',
'context' => 'account',
'access' => '_views_bulk_operations_user_access',
'hook' => 'user',
'normalize' => '_views_bulk_operations_normalize_user_context',
),
'comment' => array(
'type' => 'comment',
'base_table' => 'comments',
'load' => '_comment_load',
'oid' => 'cid',
'title' => 'subject',
'access' => '_views_bulk_operations_comment_access',
'hook' => 'comment',
'normalize' => '_views_bulk_operations_normalize_comment_context',
),
'term' => array(
'type' => 'term',
'base_table' => 'term_data',
'load' => 'taxonomy_get_term',
'oid' => 'tid',
'title' => 'name',
'hook' => 'taxonomy',
),
'node_revision' => array(
'type' => 'node_revision',
'base_table' => 'node_revisions',
'load' => '_views_bulk_operations_node_revision_load',
'title' => 'name',
),
'file' => array(
'type' => 'file',
'base_table' => 'files',
'load' => '_views_bulk_operations_file_load',
'oid' => 'fid',
'title' => 'filename',
),
);
return $object_info;
}
/**
* Access function for objects of type 'user'.
*/
function _views_bulk_operations_user_access($op, $user, $account = NULL) {
return user_access('access user profiles', $account);
}
/**
* Access function for objects of type 'comments'.
*/
function _views_bulk_operations_comment_access($op, $comment, $account = NULL) {
return user_access('access comments', $account);
}
/**
* Load function for objects of type 'node'.
*/