/[drupal]/contributions/modules/views_bulk_operations/views_bulk_operations.module
ViewVC logotype

Contents of /contributions/modules/views_bulk_operations/views_bulk_operations.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.42 - (show annotations) (download) (as text)
Thu Jul 24 21:11:00 2008 UTC (16 months ago) by kratib
Branch: MAIN
CVS Tags: HEAD
Changes since 1.41: +108 -1 lines
File MIME type: text/x-php
Added default view
1 <?php
2 // $Id: views_bulk_operations.module,v 1.41 2008/07/24 20:54:47 kratib Exp $
3
4 define('VIEWS_BULK_OPS_STEP_VIEW', 1);
5 define('VIEWS_BULK_OPS_STEP_CONFIG', 2);
6 define('VIEWS_BULK_OPS_STEP_CONFIRM', 3);
7 define('VIEWS_BULK_OPS_STEP_SINGLE', 4);
8
9 // maximum number of node titles that will be displayed in operation
10 // confirmation page.
11 define('VIEWS_BULK_OPS_MAX_CONFIRM_NODES', 10);
12
13 function views_bulk_operations_elements() {
14 $type['views_node_selector'] = array(
15 '#input' => TRUE,
16 '#view' => NULL,
17 '#process' => array('views_node_selector_process'),
18 );
19 return $type;
20 }
21
22 function views_bulk_operations_theme() {
23 return array(
24 'views_node_selector' => array(
25 'arguments' => array('element' => NULL),
26 ),
27 'views_bulk_operations_confirmation' => array(
28 'arguments' => array('nodes' => NULL),
29 ),
30 );
31 }
32
33 function theme_views_node_selector($element) {
34 $view = $element['#view'];
35
36 // Construct header.
37 $header = array();
38 $header[] = theme('table_select_header_cell');
39 foreach ($view->field as $id => $field) {
40 $header[] = $field->label();
41 }
42
43 // Construct rows.
44 $rows = array();
45 foreach ($view->result as $node) {
46 $row = array();
47 $row[] = array('data' => theme('checkbox', $element[$node->nid]));
48 foreach ($view->field as $id => $field) {
49 $row[] = array('data' => $field->theme($node));
50 }
51 $rows[] = $row;
52 }
53
54 $output = theme('table', $header, $rows);
55 return theme('form_element', $element, $output);
56 }
57
58 function views_node_selector_process($element, $edit) {
59 $view = $element['#view'];
60 $options = array();
61 foreach ($view->result as $node) {
62 $options[$node->nid] = '';
63 }
64 $element['#options'] = $options;
65 $element = expand_checkboxes($element);
66 return $element;
67 }
68
69 function views_bulk_operations_form($form_state, $plugin) {
70 drupal_add_css(drupal_get_path('module', 'views_bulk_operations') . '/views_bulk_operations.css', 'module');
71
72 if (!isset($form_state['storage']['step'])) {
73 $step = VIEWS_BULK_OPS_STEP_VIEW;
74 } else switch ($form_state['storage']['step']) {
75 case VIEWS_BULK_OPS_STEP_VIEW:
76 $operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['action']);
77 if ($operation['configurable']) {
78 $step = VIEWS_BULK_OPS_STEP_CONFIG;
79 }
80 else {
81 $step = VIEWS_BULK_OPS_STEP_CONFIRM;
82 }
83 break;
84 case VIEWS_BULK_OPS_STEP_CONFIG:
85 $step = VIEWS_BULK_OPS_STEP_CONFIRM;
86 }
87 $form['step'] = array('#type' => 'value', '#value' => $step);
88 $form['plugin'] = array('#type' => 'value', '#value' => $plugin);
89
90 switch ($step) {
91 case VIEWS_BULK_OPS_STEP_VIEW:
92 $form['select'] = array(
93 '#type' => 'fieldset',
94 '#title' => t('Bulk operations'),
95 '#prefix' => '<div id="views-bulk-operations-select">',
96 '#suffix' => '</div>',
97 );
98 $form['select']['action'] = array(
99 '#type' => 'select',
100 '#options' => array(0 => t('- Choose an operation -')) + $plugin->get_selected_operations(),
101 '#prefix' => '<div id="views-bulk-operations-dropdown">',
102 '#suffix' => '</div>',
103 );
104 $form['nodes'] = array(
105 '#type' => 'views_node_selector',
106 '#view' => $plugin->view,
107 '#prefix' => '<div class="views-node-selector">',
108 '#suffix' => '</div>',
109 );
110 $form['select']['submit'] = array(
111 '#type' => 'submit',
112 '#value' => t('Execute'),
113 '#prefix' => '<div id="views-bulk-operations-submit">',
114 '#suffix' => '</div>',
115 );
116 break;
117 case VIEWS_BULK_OPS_STEP_CONFIG:
118 $operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['action']);
119 $form += _views_bulk_operations_action_form($operation);
120 $form['execute'] = array(
121 '#type' => 'submit',
122 '#value' => t('Next'),
123 );
124 drupal_set_title(t('Set parameters for \'%action\'', array('%action' => $operation['label'])));
125 break;
126 case VIEWS_BULK_OPS_STEP_CONFIRM:
127 $operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['action']);
128 $query = drupal_query_string_encode($_GET, array('q'));
129 $form = confirm_form($form,
130 t('Are you sure you want to perform \'%action\' on selected nodes?', array('%action' => $operation['label'])),
131 $query ? array('path' => $_GET['q'], 'query' => $query) : array('path' => $_GET['q']),
132 theme('views_bulk_operations_confirmation', $form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['nodes']));
133 }
134 return $form;
135 }
136
137 function views_bulk_operations_form_validate($form, &$form_state) {
138 switch ($form_state['values']['step']) {
139 case VIEWS_BULK_OPS_STEP_VIEW:
140 if (!array_sum($form_state['values']['nodes'])) { // If all 0, no node selected
141 form_set_error('nodes', t('No nodes selected. Please select one or more nodes.'));
142 }
143 if (!$form_state['values']['action']) { // No action selected
144 form_set_error('action', t('No operation selected. Please select an operation to perform.'));
145 }
146 break;
147 case VIEWS_BULK_OPS_STEP_CONFIG:
148 $plugin = $form_state['values']['plugin'];
149 $operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['action']);
150 _views_bulk_operations_action_validate($operation, $form, $form_state);
151 break;
152 }
153 }
154
155 function views_bulk_operations_form_submit($form, &$form_state) {
156 switch ($form_state['values']['step']) {
157 case VIEWS_BULK_OPS_STEP_VIEW:
158 $form_state['storage']['step'] = $form_state['values']['step'];
159 $form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW] = $form_state['values'];
160 return;
161 case VIEWS_BULK_OPS_STEP_CONFIG:
162 $form_state['storage']['step'] = $form_state['values']['step'];
163 $form_state['storage'][VIEWS_BULK_OPS_STEP_CONFIG] = $form_state['values'];
164 return;
165 case VIEWS_BULK_OPS_STEP_CONFIRM:
166 break;
167 }
168
169 $plugin = $form_state['values']['plugin'];
170 $operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['action']);
171 $nodes = $form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['nodes'];
172 if ($operation['type'] == 'action') {
173 foreach ($nodes as $nid) {
174 if (is_numeric($nid) && $nid > 0) {
175 $node = node_load($nid);
176 $params = array();
177 if ($operation['configurable']) {
178 $form_state['values'] += $form_state['storage'][VIEWS_BULK_OPS_STEP_CONFIG];
179 $params = _views_bulk_operations_action_submit($operation, $form, $form_state);
180 }
181 _views_bulk_operations_action_do($operation, $node, $params);
182 drupal_set_message(t('Performed %action on node %title.', array('%action' => $operation['label'], '%title' => $node->title)));
183 }
184 }
185 }
186 else { // type == 'node'
187 $nodes = array_filter($nodes);
188 if (isset($operation['callback arguments'])) {
189 $args = array_merge(array($nodes), $operation['callback arguments']);
190 }
191 else {
192 $args = array($nodes);
193 }
194 call_user_func_array($operation['callback'], $args);
195 drupal_set_message(t('Performed %action on nodes %nid.', array('%action' => $operation['label'], '%nid' => implode(',', $nodes))));
196 }
197
198 unset($form_state['storage']);
199 $form_state['redirect'] = $_GET['q'];
200 }
201
202 function _views_bulk_operations_action_form($action) {
203 $action_form = $action['callback'].'_form';
204 return call_user_func($action_form, array());
205 }
206
207 function _views_bulk_operations_action_validate($action, $form, $form_values) {
208 $action_validate = $action['callback'].'_validate';
209 call_user_func($action_validate, $form, $form_values);
210 }
211
212 function _views_bulk_operations_action_submit($action, $form, $form_values) {
213 $action_submit = $action['callback'].'_submit';
214 return call_user_func($action_submit, $form, $form_values);
215 }
216
217 function _views_bulk_operations_action_do($action, $node, $context) {
218 $action_do = $action['callback'];
219 actions_do($action['callback'], $node, $context);
220 node_save($node);
221 }
222
223 function theme_views_bulk_operations_confirmation($nodes) {
224 $count = 0;
225 $output = t('You selected the following nodes:').'<br /><ul>';
226 foreach ($nodes as $nid) {
227 // Number of titles to display before we say "...and more"
228 if ((VIEWS_BULK_OPS_MAX_CONFIRM_NODES > 0) && ($count >= VIEWS_BULK_OPS_MAX_CONFIRM_NODES)) {
229 $output .= '<li>'.t('...and %remaining more.', array('%remaining' => count($nodes) - $count)).'</li>';
230 break;
231 }
232 if (is_numeric($nid) && $nid > 0) {
233 $node = node_load($nid);
234 $output .= '<li>'.$node->title.'</li>';
235 $count++;
236 }
237 }
238 $output .= '</ul>';
239 return $output;
240 }
241
242 function views_bulk_operations_node_operations() {
243 $operations = array(
244 'bulk_delete' => array(
245 'label' => t('Delete'),
246 'callback' => 'views_bulk_operations_delete_nodes',
247 ),
248 );
249 return $operations;
250 }
251
252 function views_bulk_operations_delete_nodes($nodes) {
253 foreach ($nodes as $nid) {
254 node_delete($nid);
255 }
256 }
257
258 function views_bulk_operations_views_default_views() {
259 $view = new view;
260 $view->name = 'admin_content';
261 $view->description = 'View, edit and delete your site\'s content.';
262 $view->tag = '';
263 $view->view_php = '';
264 $view->base_table = 'node';
265 $view->is_cacheable = FALSE;
266 $view->api_version = 2;
267 $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
268 $handler = $view->new_display('default', 'Defaults', 'default');
269 $handler->override_option('fields', array(
270 'title' => array(
271 'label' => 'Title',
272 'link_to_node' => 1,
273 'exclude' => 0,
274 'id' => 'title',
275 'table' => 'node',
276 'field' => 'title',
277 'relationship' => 'none',
278 'override' => array(
279 'button' => 'Override',
280 ),
281 ),
282 'type' => array(
283 'label' => 'Type',
284 'link_to_node' => 0,
285 'exclude' => 0,
286 'id' => 'type',
287 'table' => 'node',
288 'field' => 'type',
289 'override' => array(
290 'button' => 'Override',
291 ),
292 'relationship' => 'none',
293 ),
294 'name' => array(
295 'label' => 'Author',
296 'link_to_user' => 1,
297 'exclude' => 0,
298 'id' => 'name',
299 'table' => 'users',
300 'field' => 'name',
301 'override' => array(
302 'button' => 'Override',
303 ),
304 'relationship' => 'none',
305 ),
306 'status' => array(
307 'label' => 'Published',
308 'type' => 'yes-no',
309 'not' => 0,
310 'exclude' => 0,
311 'id' => 'status',
312 'table' => 'node',
313 'field' => 'status',
314 'override' => array(
315 'button' => 'Override',
316 ),
317 'relationship' => 'none',
318 ),
319 'edit_node' => array(
320 'id' => 'edit_node',
321 'table' => 'node',
322 'field' => 'edit_node',
323 ),
324 ));
325 $handler->override_option('access', array(
326 'type' => 'none',
327 'role' => array(),
328 'perm' => '',
329 ));
330 $handler->override_option('title', 'Content');
331 $handler->override_option('items_per_page', 0);
332 $handler->override_option('style_plugin', 'bulk');
333 $handler->override_option('style_options', array(
334 'selected_operations' => array(
335 '867e6bb83ef13a39c914e114fab4d6f2' => '867e6bb83ef13a39c914e114fab4d6f2',
336 'c6d3850086dc3c1bca4bf0625ce78c56' => 'c6d3850086dc3c1bca4bf0625ce78c56',
337 '548749a939aa6334157edff3d8f69351' => '548749a939aa6334157edff3d8f69351',
338 '6a99179e9c90e9edfb34d075fa234d78' => '6a99179e9c90e9edfb34d075fa234d78',
339 'c4ca4238a0b923820dcc509a6f75849b' => 'c4ca4238a0b923820dcc509a6f75849b',
340 '799e8527bbd3d99e4c8c25a8a674d264' => 0,
341 'fce859fcce0e81943c3e31c20ccd68ff' => 0,
342 '0341fcc505281031474b68c72e03a997' => 0,
343 '94a098afb1689f950d72ed4c44f8655d' => 0,
344 '15faf15d7f591f8ef8e80238dd7c8b8c' => 0,
345 'c6db6d4ee28924020b504a5feb2bff9f' => 0,
346 ),
347 ));
348 $handler = $view->new_display('page', 'Page', 'page');
349 $handler->override_option('path', 'admin/content/node2');
350 $handler->override_option('menu', array(
351 'type' => 'none',
352 'title' => '',
353 'weight' => 0,
354 ));
355 $handler->override_option('tab_options', array(
356 'type' => 'none',
357 'title' => '',
358 'weight' => 0,
359 ));
360
361 $views[$view->name] = $view;
362 return $views;
363 }
364

  ViewVC Help
Powered by ViewVC 1.1.2