1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Implementation of hook_drush_help(). |
---|
5 | */ |
---|
6 | function views_bulk_operations_drush_help($section) { |
---|
7 | switch ($section) { |
---|
8 | case 'drush:vbo-list': |
---|
9 | return dt('List all Views Bulk Operations (VBO) views, along with the operations associated with each.'); |
---|
10 | case 'drush:vbo-execute': |
---|
11 | return dt('Execute a bulk operation based on a Views Bulk Operations (VBO) view.'); |
---|
12 | } |
---|
13 | } |
---|
14 | |
---|
15 | /** |
---|
16 | * Implementation of hook_drush_command(). |
---|
17 | */ |
---|
18 | function views_bulk_operations_drush_command() { |
---|
19 | $items['vbo-list'] = array( |
---|
20 | 'callback' => 'drush_views_bulk_operations_list', |
---|
21 | 'description' => 'List all Views Bulk Operations (VBO) views, along with the operations associated with each.', |
---|
22 | ); |
---|
23 | $items['vbo-execute'] = array( |
---|
24 | 'callback' => 'drush_views_bulk_operations_execute', |
---|
25 | 'description' => 'Execute a bulk operation based on a Views Bulk Operations (VBO) view.', |
---|
26 | 'arguments' => array( |
---|
27 | 'vid' => 'ID or name of the view to be executed.', |
---|
28 | 'operation' => 'Callback name of the operation to be applied on the view results.', |
---|
29 | 'input:name=value ...' => 'Parameter to be passed as view input filter.', |
---|
30 | 'operation:name=value ...' => 'Parameter to be passed as operation argument.', |
---|
31 | 'argument:value ...' => 'Parameter to be passed as view argument.', |
---|
32 | ), |
---|
33 | 'examples' => array( |
---|
34 | '$ drush vbo-execute admin_content node_publish_action' => |
---|
35 | 'Publish nodes returned by view admin_content.', |
---|
36 | '$ drush vbo-execute 44 node_assign_owner_action operation:owner_uid=3' => |
---|
37 | 'Change node ownership on nodes returned by view #44, passing argument owner_uid=3 to the action.', |
---|
38 | '$ drush vbo-execute admin_content node_unpublish_action input:type=expense argument:3' => |
---|
39 | 'Unpublish nodes returned by view admin_content, filtering results of type expense and passing value 3 as first view argument.', |
---|
40 | ), |
---|
41 | ); |
---|
42 | return $items; |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Implementation of 'vbo-list' command. |
---|
47 | */ |
---|
48 | function drush_views_bulk_operations_list() { |
---|
49 | // Impersonate admin. |
---|
50 | global $user; |
---|
51 | $user = user_load(array('uid' => '1')); |
---|
52 | session_save_session(FALSE); |
---|
53 | |
---|
54 | // Find all VBO views and their associated operations. |
---|
55 | $rows = array(array(sprintf('%5s', dt('VID')), dt('NAME (DISPLAY)'), dt('DESCRIPTION'), dt('OPERATIONS'))); |
---|
56 | foreach (views_get_all_views() as $name => $view) { |
---|
57 | foreach (array_keys($view->display) as $display) { |
---|
58 | $display_options = $view->display[$display]->display_options; |
---|
59 | if (isset($display_options['style_plugin']) && $display_options['style_plugin'] == 'bulk') { |
---|
60 | $view->build($display); |
---|
61 | $operations = array(); |
---|
62 | foreach ($view->style_plugin->get_selected_operations() as $operation => $label) { |
---|
63 | $operations[] = $label .' ('. $operation .')'; |
---|
64 | } |
---|
65 | $operations[] = ''; |
---|
66 | $rows[] = array( |
---|
67 | empty($view->vid) ? sprintf('%5s', '---') : sprintf('%5d', $view->vid), |
---|
68 | sprintf('%s (%s)', $view->name, $display), |
---|
69 | $view->description, |
---|
70 | implode("\n", $operations), |
---|
71 | ); |
---|
72 | $view->destroy(); |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | drush_print_table($rows, TRUE); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Implementation of 'vbo-execute' command. |
---|
81 | */ |
---|
82 | function drush_views_bulk_operations_execute($vid = NULL, $operation = NULL) { |
---|
83 | // Parse arguments. |
---|
84 | if (is_null($vid)) { |
---|
85 | drush_set_error('VIEWS_BULK_OPERATIONS_MISSING_VID', dt('Please specify a view ID to execute.')); |
---|
86 | return; |
---|
87 | } |
---|
88 | if (is_null($operation)) { |
---|
89 | drush_set_error('VIEWS_BULK_OPERATIONS_MISSING_OPERATION', dt('Please specify an operation to execute.')); |
---|
90 | return; |
---|
91 | } |
---|
92 | $args = func_get_args(); |
---|
93 | $view_exposed_input = array(); |
---|
94 | $operation_arguments = array(); |
---|
95 | $view_arguments = array(); |
---|
96 | if (count($args) > 2) for ($i=2; $i<count($args); $i++) { |
---|
97 | $parts = array(); |
---|
98 | if (FALSE === preg_match('/^(?<type>\w+):(?:(?<name>\w+)=)?(?<value>(.*?))$/', $args[$i], $parts)) { |
---|
99 | drush_set_error('VIEWS_BULK_OPERATIONS_INVALID_PARAMETER', dt('The parameter %arg should be of the form type:[name=]value where type in {input, argument, operation}.', array('%arg' => $args[$i]))); |
---|
100 | return; |
---|
101 | } |
---|
102 | switch ($parts['type']) { |
---|
103 | case 'input': |
---|
104 | $view_exposed_input[$parts['name']] = $parts['value']; |
---|
105 | break; |
---|
106 | case 'operation': |
---|
107 | $operation_arguments[$parts['name']] = $parts['value']; |
---|
108 | break; |
---|
109 | case 'argument': |
---|
110 | $view_arguments[] = $parts['value']; |
---|
111 | break; |
---|
112 | default: |
---|
113 | drush_set_error('VIEWS_BULK_OPERATIONS_UNKNOWN_PARAMETER', dt('The parameter type %type is unknown. Please specify either input, argument or operation.', array('%type' => $parts['type']))); |
---|
114 | return; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | // Impersonate admin. |
---|
119 | global $user; |
---|
120 | $user = user_load(array('uid' => '1')); |
---|
121 | session_save_session(FALSE); |
---|
122 | |
---|
123 | // Execute the VBO. |
---|
124 | views_bulk_operations_execute($vid, $operation, $operation_arguments, $view_exposed_input, $view_arguments); |
---|
125 | } |
---|