/[drupal]/contributions/modules/action_view/actionview.module
ViewVC logotype

Contents of /contributions/modules/action_view/actionview.module

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


Revision 1.1 - (show annotations) (download) (as text)
Thu Dec 6 08:10:34 2007 UTC (23 months, 3 weeks ago) by mdekkers
Branch: MAIN
CVS Tags: DRUPAL-5--4-0, DRUPAL-5--4-1, HEAD
File MIME type: text/x-php
should fix:
#195710
#197415
#193912
1 <?php
2 // $id$
3 /**
4 * @file
5 * Action view module
6 */
7
8 // Load all our module 'on behalfs'.
9 // Whenever we decide we need this, load our module 'on behalfs'.
10 $path = drupal_get_path('module', 'actionview') . '/modules';
11 $files = drupal_system_listing('actionview_.*\.inc$', $path, 'name', 0);
12 global $actionview_permissions;
13
14 $actionview_permissions = array();
15 foreach($files as $file) {
16 // The filename format is very specific. It must be actionview_MODULENAME.inc
17 $module = str_replace('actionview_', '', $file->name);
18 if (module_exists($module)) {
19 require_once("./$file->filename");
20 }
21 }
22
23 /**
24 * Implementation of hook_perm().
25 */
26 function actionview_perm() {
27 global $actionview_permissions;
28 return $actionview_permissions;
29 }
30
31 /**
32 * Implementation of hook_views_style_plugins(). Add new views style
33 */
34 function actionview_views_style_plugins()
35 {
36 return array(
37 'action_table' => array(
38 'name' => t('Action View'),
39 'theme' => 'actionview_views_action_table',
40 'validate' => 'views_ui_plugin_validate_action_table',
41 'needs_table_header' => TRUE,
42 'needs_fields' => TRUE,
43 'even_empty' => TRUE,
44 'weight' => -10,
45 ),
46 );
47 }
48
49
50 /**
51 * Display the nodes of a view as a table with action form.
52 */
53 function theme_actionview_views_action_table($view, $nodes, $type)
54 {
55 return drupal_get_form('actionview_form', $view, $nodes, $type);
56 }
57
58 /**
59 * Return form with allowed operations and checkboxes for each node
60 *
61 * @param unknown_type $view
62 * @param array $nodes
63 * @param unknown_type $type
64 * @param array $form_values
65 * @return array
66 */
67 function actionview_form($view, $nodes, $type, $form_values = NULL)
68 {
69 // create multistep form
70 $form=array();
71 $form['#multistep'] = TRUE;
72 $step = isset($form_values) ? (int) $form_values['step'] : 1;
73 $form['step'] = array(
74 '#type' => 'hidden',
75 '#value' => $step + 1
76 );
77
78 if ($step == 1 ){
79 $form['#redirect'] = FALSE;
80 }
81 else
82 {
83 $d = str_replace('destination=', '', drupal_get_destination());
84 $_REQUEST['destination'] = $d;
85 }
86
87
88 $form['actions'] = array(
89 '#type' => 'fieldset',
90 '#title' => t('Update options'),
91 '#prefix' => '<div class="container-inline">',
92 '#suffix' => '</div>',
93 );
94 if (count($nodes) > 0)
95 {
96 $operations = module_invoke_all('views_operations', $nodes);
97 }
98 else
99 {
100 $operations = array();
101 }
102
103 if ($step == 1)
104 {
105 if (count($operations))
106 {
107 $options = array();
108
109 foreach ($operations as $operation => $array)
110 {
111 if (isset($array['label']))
112 {
113 $options[$operation] = $array['label'];
114 }
115 }
116 asort($options);
117 $form['actions']['operations'] = array(
118 '#type' => 'select',
119 '#multiple' => TRUE,
120 '#options' => $options,
121 '#attributes' => array('class' => 'actionview-operation')
122 );
123
124 $form['actions']['submit'] = array(
125 '#type' => 'submit',
126 '#value' => t('Next'),
127 );
128 }
129 }
130 else
131 {
132 $list = '';
133 $form_fields = array();
134 $i =0;
135 foreach ($form_values['operations'] as $op)
136 {
137 if (!isset($operations[$op]['label']))
138 {
139 foreach ($operations as $key => $value)
140 {
141 if (is_array($value['label']) && isset($value['label'][$op]))
142 {
143 $list .= '<li>'.$key.': '.$value['label'][$op].'</li>';
144 }
145 }
146
147 }
148 else
149 {
150 $list .= '<li>'.$operations[$op]['label'].'</li>';
151 }
152 $operation_field = explode('-', $op);
153 $field = $operation_field[1];
154 if (isset($operations['form_fields_'.$field]['form']))
155 {
156 $form_fields = array_merge($form_fields, $operations['form_fields_'.$field]['form']);
157 }
158
159 $form['actions']['operation']['operation_'.$i++] = array(
160 '#type' => 'hidden',
161 '#value' => $op
162 );
163 }
164
165 $form['actions']['operation_list'] = array(
166 '#type' => 'markup',
167 '#value' => '<ol>'.$list.'</ol>'
168 );
169 $form['edit_fields'] = array(
170 '#type' => 'fieldset',
171 '#title' => t('Edit fields for selected nodes'),
172 '#collapsible' => TRUE,
173 '#collapsed' =>FALSE
174 );
175 if (isset($form_fields))
176 {
177 $form['edit_fields'] = array_merge($form['edit_fields'], $form_fields);
178 }
179 $form['actions']['submit'] = array(
180 '#type' => 'submit',
181 '#value' => t('Update'),
182 );
183
184 }
185
186
187 $fields = _views_get_fields();
188 $nodes_ids = array();
189 foreach ($nodes as $node)
190 {
191 $nodes_ids[$node->nid] = '';//$node->nid;
192 foreach ($view->field as $field)
193 {
194 if ($fields[$field['id']]['visible'] !== FALSE)
195 {
196 $form[$field['fullname']][$node->nid] = array(
197 '#value' => views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view),
198 '#type' => 'markup'
199 );
200 }
201 }
202 }
203
204 $form['nids'] = array(
205 '#type' => 'checkboxes',
206 '#options' => $nodes_ids,
207 );
208
209 $form['view'] = array(
210 '#type' => 'value',
211 '#value' => $view);
212
213 $form['nodes'] = array(
214 '#type' => 'value',
215 '#value' => $nodes);
216
217 return $form;
218 }
219
220
221 function theme_actionview_form($form)
222 {
223 $view = $form['view']['#value'];
224 $nodes = $form['nodes']['#value'];
225
226 $headers = array();
227 if (is_array($view->table_header))
228 {
229 $headers = array_merge(array(theme('table_select_header_cell')), $view->table_header);
230 }
231 else
232 {
233 $headers = array(theme('table_select_header_cell'));
234 }
235
236 $fields = _views_get_fields();
237 $rows = array();
238
239 foreach ($nodes as $node)
240 {
241 $row = array();
242 $row[] = array(
243 'data' => drupal_render($form['nids'][$node->nid]),
244 'class' => ''
245 );
246
247 foreach ($view->field as $field)
248 {
249 if ($fields[$field['id']]['visible'] !== FALSE)
250 {
251 $cell['data'] = drupal_render($form[$field['fullname']][$node->nid]);
252 $cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
253 $row[] = $cell;
254 }
255 }
256 $rows[] = $row;
257 }
258
259 $table = theme('table', $headers, $rows);
260 $output = drupal_render($form['actions']);
261 $output .= drupal_render($form['edit_fields']);
262 $output .= $table;
263 $output .= drupal_render($form);
264
265 return $output;
266 }
267
268
269 function actionview_form_validate($form_id, &$form_values) {
270 $nodes = array_filter($form_values['nids']);
271 if (count($nodes) == 0) {
272 form_set_error('', t('No items selected.'));
273 return;
274 }
275 if ($form_values['step'] == 2 && count($form_values['operations'])<1)
276 {
277 form_set_error('', t('No operations selected.'));
278 return;
279 }
280
281 if ((int)$form_values['step'] > 2)
282 {
283 // write operations into array
284 $i = 0;
285 $form_values['edit_node_fields'] = array();
286 $form_values['operations'] = array();
287 while (isset($form_values['operation_'.$i]))
288 {
289 $operation_field = explode('-', $form_values['operation_'.$i]);
290 $operation = $operation_field[0];
291 $field = $operation_field[1];
292
293 if ($operation == 'edit_node_field')
294 {
295 $form_values['edit_node_fields'][] = $operation_field[1];
296 }
297 else
298 {
299 $form_values['operations'][] = $form_values['operation_'.$i];
300 }
301 $i++;
302 }
303 if (isset($form_values['edit_node_fields']))
304 {
305 $nids = array_filter($form_values['nids']);
306 // load full nodes (only selected) and change fields
307 foreach ($form_values['nodes'] as $key => $node)
308 {
309 if (in_array($node->nid, $nids))
310 {
311 $form_values['nodes'][$key] = node_load(array('nid' => $node->nid));
312 foreach ($form_values['edit_node_fields'] as $field)
313 {
314 $form_values['nodes'][$key]->$field = $form_values[$field];
315 }
316 }
317 else
318 {
319 unset($form_values['nodes'][$key]);
320 }
321 }
322
323 module_invoke_all('views_operations_validate', $form_id, $form_values);
324 foreach ($form_values['nodes'] as $key => $node)
325 {
326 node_validate($form_values['nodes'][$key]);
327 }
328 }
329 }
330 }
331
332 function actionview_form_submit($form_id, $form_values)
333 {
334 // if not last step
335 if ((int)$form_values['step'] < 3)
336 {
337 return;
338 }
339
340 // write operations into array
341 $i = 0;
342 while (isset($form_values['operation_'.$i]))
343 {
344 $operation_field = explode('-', $form_values['operation_'.$i]);
345 $operation = $operation_field[0];
346 $field = $operation_field[1];
347 if ($operation == 'edit_node_field')
348 {
349 $form_values['edit_node_fields'][] = $operation_field[1];
350 }
351 else
352 {
353 $form_values['operations'][] = $form_values['operation_'.$i];
354 }
355 $i++;
356 }
357
358
359
360 // Filter out unchecked nids or other ids
361 $nids = array_filter($form_values['nids']);
362
363 // changing all needed node fields in one function
364 if (isset($form_values['edit_node_fields']))
365 {
366 // load full nodes (only selected) and change fields
367 foreach ($form_values['nodes'] as $key => $node)
368 {
369 if (in_array($node->nid, $nids))
370 {
371 $form_values['nodes'][$key] = node_load(array('nid' => $node->nid));
372 foreach ($form_values['edit_node_fields'] as $field)
373 {
374 $form_values['nodes'][$key]->$field = $form_values[$field];
375 }
376 }
377 else
378 {
379 unset($form_values['nodes'][$key]);
380 }
381 }
382 // allow other modules change nodes
383 module_invoke_all('views_operations_validate', $form_id, $form_values);
384
385 // saving all nodes
386 $args = array($nids, $form_values);
387 $function = 'actionview_save_nodes';
388 call_user_func_array($function, $args);
389 }
390
391 // other operations
392 if (isset($form_values['operations']))
393 {
394 $operations = module_invoke_all('views_operations', $form_values['nodes']);
395
396
397 foreach ($form_values['operations'] as $op)
398 {
399 $operation = $operations[$op];
400 // try to get callback from module which expect one operation
401 if (!$operation)
402 {
403 $form_values['operation'] = $op;
404 $operations = module_invoke_all('views_operations', $form_values['nodes']);
405 $operation = $operations[$op];
406 }
407
408
409 if (isset($operation['id_field']))
410 {
411 // if callback function expects not nids
412 $ids = array_filter($form_values[$operation['id_field']]);
413 }
414 else
415 {
416 $ids = $nids;
417 }
418 if ($function = $operation['callback'])
419 {
420 // Add in callback arguments if present.
421 if (isset($operation['callback arguments']))
422 {
423 $args = array_merge(array($ids), $operation['callback arguments']);
424 }
425 else
426 {
427 $args = array($ids, $form_values);
428 }
429 call_user_func_array($function, $args);
430
431 }
432 }
433 }
434
435 cache_clear_all('*', 'cache_menu', TRUE);
436 drupal_set_message(t('The update has been performed.'));
437 }
438
439
440 /**
441 * Callback functions for save changes in fields of nodes
442 *
443 * @param array $nids
444 * @param array $form_values
445 */
446 function actionview_save_nodes($nids, $form_values)
447 {
448 foreach ($form_values['nodes'] as $key => $node)
449 {
450 if (in_array($node->nid, $nids))
451 {
452 node_validate($node);
453 node_save($node);
454 }
455 }
456 }
457
458
459
460
461
462 /**
463 * Checks if all filtered nodes have the same type
464 *
465 * @param array $nodes
466 * @param string $type
467 * @return bool
468 */
469 function actionview_check_node_type($nodes = array(), $type = 'node')
470 {
471 foreach ((array)$nodes as $node)
472 {
473 if (isset($node->node_type))
474 {
475 $node_type = $node->node_type;
476 }
477 else
478 {
479 // load full node if don't have information about type
480 $n = node_load(array('nid' => $node->nid));
481 $node_type = $n->type;
482 }
483 if ($node_type != $type)
484 {
485 return false;
486 }
487 }
488 return true;
489 }
490

  ViewVC Help
Powered by ViewVC 1.1.2