comparison modules/node/node.admin.inc @ 1:c1f4ac30525a 6.0

Drupal 6.0
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:28:28 +0100
parents
children 2427550111ae
comparison
equal deleted inserted replaced
0:5a113a1c4740 1:c1f4ac30525a
1 <?php
2 // $Id: node.admin.inc,v 1.19 2008/02/03 19:39:52 goba Exp $
3
4 /**
5 * @file
6 * Content administration and module settings UI.
7 */
8
9 /**
10 * Menu callback; presents general node configuration options.
11 */
12 function node_configure() {
13 // Only show rebuild button if there is 0 or more than 2 rows in node_access table,
14 // or if there are modules that implement node_grant.
15 if (db_result(db_query('SELECT COUNT(*) FROM {node_access}')) != 1 || count(module_implements('node_grants')) > 0) {
16 $status = '<p>'. t('If the site is experiencing problems with permissions to content, you may have to rebuild the permissions cache. Possible causes for permission problems are disabling modules or configuration changes to permissions. Rebuilding will remove all privileges to posts, and replace them with permissions based on the current modules and settings.') .'</p>';
17 $status .= '<p>'. t('Rebuilding may take some time if there is a lot of content or complex permission settings. After rebuilding has completed posts will automatically use the new permissions.') .'</p>';
18
19 $form['access'] = array(
20 '#type' => 'fieldset',
21 '#title' => t('Node access status'),
22 );
23 $form['access']['status'] = array('#value' => $status);
24 $form['access']['rebuild'] = array(
25 '#type' => 'submit',
26 '#value' => t('Rebuild permissions'),
27 );
28 }
29
30 $form['default_nodes_main'] = array(
31 '#type' => 'select', '#title' => t('Number of posts on main page'), '#default_value' => variable_get('default_nodes_main', 10),
32 '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
33 '#description' => t('The default maximum number of posts to display per page on overview pages such as the main page.')
34 );
35 $form['teaser_length'] = array(
36 '#type' => 'select', '#title' => t('Length of trimmed posts'), '#default_value' => variable_get('teaser_length', 600),
37 '#options' => array(
38 0 => t('Unlimited'),
39 200 => t('200 characters'),
40 400 => t('400 characters'),
41 600 => t('600 characters'),
42 800 => t('800 characters'),
43 1000 => t('1000 characters'),
44 1200 => t('1200 characters'),
45 1400 => t('1400 characters'),
46 1600 => t('1600 characters'),
47 1800 => t('1800 characters'),
48 2000 => t('2000 characters'),
49 ),
50 '#description' => t("The maximum number of characters used in the trimmed version of a post. Drupal will use this setting to determine at which offset long posts should be trimmed. The trimmed version of a post is typically used as a teaser when displaying the post on the main page, in XML feeds, etc. To disable teasers, set to 'Unlimited'. Note that this setting will only affect new or updated content and will not affect existing teasers.")
51 );
52
53 $form['node_preview'] = array(
54 '#type' => 'radios',
55 '#title' => t('Preview post'),
56 '#default_value' => variable_get('node_preview', 0),
57 '#options' => array(t('Optional'), t('Required')),
58 '#description' => t('Must users preview posts before submitting?'),
59 );
60
61 $form['#validate'] = array('node_configure_validate');
62
63 return system_settings_form($form);
64 }
65
66 /**
67 * Form validate callback.
68 */
69 function node_configure_validate($form, &$form_state) {
70 if ($form_state['values']['op'] == t('Rebuild permissions')) {
71 drupal_goto('admin/content/node-settings/rebuild');
72 }
73 }
74
75 /**
76 * Menu callback: confirm rebuilding of permissions.
77 */
78 function node_configure_rebuild_confirm() {
79 return confirm_form(array(), t('Are you sure you want to rebuild the permissions on site content?'),
80 'admin/content/node-settings', t('This action rebuilds all permissions on site content, and may be a lengthy process. This action cannot be undone.'), t('Rebuild permissions'), t('Cancel'));
81 }
82
83 /**
84 * Handler for wipe confirmation
85 */
86 function node_configure_rebuild_confirm_submit($form, &$form_state) {
87 node_access_rebuild(TRUE);
88 $form_state['redirect'] = 'admin/content/node-settings';
89 return;
90 }
91
92 /**
93 * Implementation of hook_node_operations().
94 */
95 function node_node_operations() {
96 $operations = array(
97 'publish' => array(
98 'label' => t('Publish'),
99 'callback' => 'node_mass_update',
100 'callback arguments' => array('updates' => array('status' => 1)),
101 ),
102 'unpublish' => array(
103 'label' => t('Unpublish'),
104 'callback' => 'node_mass_update',
105 'callback arguments' => array('updates' => array('status' => 0)),
106 ),
107 'promote' => array(
108 'label' => t('Promote to front page'),
109 'callback' => 'node_mass_update',
110 'callback arguments' => array('updates' => array('status' => 1, 'promote' => 1)),
111 ),
112 'demote' => array(
113 'label' => t('Demote from front page'),
114 'callback' => 'node_mass_update',
115 'callback arguments' => array('updates' => array('promote' => 0)),
116 ),
117 'sticky' => array(
118 'label' => t('Make sticky'),
119 'callback' => 'node_mass_update',
120 'callback arguments' => array('updates' => array('status' => 1, 'sticky' => 1)),
121 ),
122 'unsticky' => array(
123 'label' => t('Remove stickiness'),
124 'callback' => 'node_mass_update',
125 'callback arguments' => array('updates' => array('sticky' => 0)),
126 ),
127 'delete' => array(
128 'label' => t('Delete'),
129 'callback' => NULL,
130 ),
131 );
132 return $operations;
133 }
134
135 /**
136 * List node administration filters that can be applied.
137 */
138 function node_filters() {
139 // Regular filters
140 $filters['status'] = array(
141 'title' => t('status'),
142 'options' => array(
143 'status-1' => t('published'),
144 'status-0' => t('not published'),
145 'promote-1' => t('promoted'),
146 'promote-0' => t('not promoted'),
147 'sticky-1' => t('sticky'),
148 'sticky-0' => t('not sticky'),
149 ),
150 );
151 // Include translation states if we have this module enabled
152 if (module_exists('translation')) {
153 $filters['status']['options'] += array(
154 'translate-0' => t('Up to date translation'),
155 'translate-1' => t('Outdated translation'),
156 );
157 }
158
159 $filters['type'] = array('title' => t('type'), 'options' => node_get_types('names'));
160
161 // The taxonomy filter
162 if ($taxonomy = module_invoke('taxonomy', 'form_all', 1)) {
163 $filters['category'] = array('title' => t('category'), 'options' => $taxonomy);
164 }
165 // Language filter if there is a list of languages
166 if ($languages = module_invoke('locale', 'language_list')) {
167 $languages = array('' => t('Language neutral')) + $languages;
168 $filters['language'] = array('title' => t('language'), 'options' => $languages);
169 }
170 return $filters;
171 }
172
173 /**
174 * Build query for node administration filters based on session.
175 */
176 function node_build_filter_query() {
177 $filters = node_filters();
178
179 // Build query
180 $where = $args = array();
181 $join = '';
182 foreach ($_SESSION['node_overview_filter'] as $index => $filter) {
183 list($key, $value) = $filter;
184 switch ($key) {
185 case 'status':
186 // Note: no exploitable hole as $key/$value have already been checked when submitted
187 list($key, $value) = explode('-', $value, 2);
188 $where[] = 'n.'. $key .' = %d';
189 break;
190 case 'category':
191 $table = "tn$index";
192 $where[] = "$table.tid = %d";
193 $join .= "INNER JOIN {term_node} $table ON n.nid = $table.nid ";
194 break;
195 case 'type':
196 $where[] = "n.type = '%s'";
197 break;
198 case 'language':
199 $where[] = "n.language = '%s'";
200 break;
201 }
202 $args[] = $value;
203 }
204 $where = count($where) ? 'WHERE '. implode(' AND ', $where) : '';
205
206 return array('where' => $where, 'join' => $join, 'args' => $args);
207 }
208
209 /**
210 * Return form for node administration filters.
211 */
212 function node_filter_form() {
213 $session = &$_SESSION['node_overview_filter'];
214 $session = is_array($session) ? $session : array();
215 $filters = node_filters();
216
217 $i = 0;
218 $form['filters'] = array(
219 '#type' => 'fieldset',
220 '#title' => t('Show only items where'),
221 '#theme' => 'node_filters',
222 );
223 $form['#submit'][] = 'node_filter_form_submit';
224 foreach ($session as $filter) {
225 list($type, $value) = $filter;
226 if ($type == 'category') {
227 // Load term name from DB rather than search and parse options array.
228 $value = module_invoke('taxonomy', 'get_term', $value);
229 $value = $value->name;
230 }
231 else if ($type == 'language') {
232 $value = empty($value) ? t('Language neutral') : module_invoke('locale', 'language_name', $value);
233 }
234 else {
235 $value = $filters[$type]['options'][$value];
236 }
237 if ($i++) {
238 $form['filters']['current'][] = array('#value' => t('<em>and</em> where <strong>%a</strong> is <strong>%b</strong>', array('%a' => $filters[$type]['title'], '%b' => $value)));
239 }
240 else {
241 $form['filters']['current'][] = array('#value' => t('<strong>%a</strong> is <strong>%b</strong>', array('%a' => $filters[$type]['title'], '%b' => $value)));
242 }
243 if (in_array($type, array('type', 'language'))) {
244 // Remove the option if it is already being filtered on.
245 unset($filters[$type]);
246 }
247 }
248
249 foreach ($filters as $key => $filter) {
250 $names[$key] = $filter['title'];
251 $form['filters']['status'][$key] = array('#type' => 'select', '#options' => $filter['options']);
252 }
253
254 $form['filters']['filter'] = array('#type' => 'radios', '#options' => $names, '#default_value' => 'status');
255 $form['filters']['buttons']['submit'] = array('#type' => 'submit', '#value' => (count($session) ? t('Refine') : t('Filter')));
256 if (count($session)) {
257 $form['filters']['buttons']['undo'] = array('#type' => 'submit', '#value' => t('Undo'));
258 $form['filters']['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset'));
259 }
260
261 drupal_add_js('misc/form.js', 'core');
262
263 return $form;
264 }
265
266 /**
267 * Theme node administration filter form.
268 *
269 * @ingroup themeable
270 */
271 function theme_node_filter_form($form) {
272 $output = '';
273 $output .= '<div id="node-admin-filter">';
274 $output .= drupal_render($form['filters']);
275 $output .= '</div>';
276 $output .= drupal_render($form);
277 return $output;
278 }
279
280 /**
281 * Theme node administration filter selector.
282 *
283 * @ingroup themeable
284 */
285 function theme_node_filters($form) {
286 $output = '';
287 $output .= '<ul class="clear-block">';
288 if (!empty($form['current'])) {
289 foreach (element_children($form['current']) as $key) {
290 $output .= '<li>'. drupal_render($form['current'][$key]) .'</li>';
291 }
292 }
293
294 $output .= '<li><dl class="multiselect">'. (!empty($form['current']) ? '<dt><em>'. t('and') .'</em> '. t('where') .'</dt>' : '') .'<dd class="a">';
295 foreach (element_children($form['filter']) as $key) {
296 $output .= drupal_render($form['filter'][$key]);
297 }
298 $output .= '</dd>';
299
300 $output .= '<dt>'. t('is') .'</dt><dd class="b">';
301
302 foreach (element_children($form['status']) as $key) {
303 $output .= drupal_render($form['status'][$key]);
304 }
305 $output .= '</dd>';
306
307 $output .= '</dl>';
308 $output .= '<div class="container-inline" id="node-admin-buttons">'. drupal_render($form['buttons']) .'</div>';
309 $output .= '</li></ul>';
310
311 return $output;
312 }
313
314 /**
315 * Process result from node administration filter form.
316 */
317 function node_filter_form_submit($form, &$form_state) {
318 $filters = node_filters();
319 switch ($form_state['values']['op']) {
320 case t('Filter'):
321 case t('Refine'):
322 if (isset($form_state['values']['filter'])) {
323 $filter = $form_state['values']['filter'];
324
325 // Flatten the options array to accommodate hierarchical/nested options.
326 $flat_options = form_options_flatten($filters[$filter]['options']);
327
328 if (isset($flat_options[$form_state['values'][$filter]])) {
329 $_SESSION['node_overview_filter'][] = array($filter, $form_state['values'][$filter]);
330 }
331 }
332 break;
333 case t('Undo'):
334 array_pop($_SESSION['node_overview_filter']);
335 break;
336 case t('Reset'):
337 $_SESSION['node_overview_filter'] = array();
338 break;
339 }
340 }
341
342 /**
343 * Make mass update of nodes, changing all nodes in the $nodes array
344 * to update them with the field values in $updates.
345 *
346 * IMPORTANT NOTE: This function is intended to work when called
347 * from a form submit handler. Calling it outside of the form submission
348 * process may not work correctly.
349 *
350 * @param array $nodes
351 * Array of node nids to update.
352 * @param array $updates
353 * Array of key/value pairs with node field names and the
354 * value to update that field to.
355 */
356 function node_mass_update($nodes, $updates) {
357 // We use batch processing to prevent timeout when updating a large number
358 // of nodes.
359 if (count($nodes) > 10) {
360 $batch = array(
361 'operations' => array(
362 array('_node_mass_update_batch_process', array($nodes, $updates))
363 ),
364 'finished' => '_node_mass_update_batch_finished',
365 'title' => t('Processing'),
366 // We use a single multi-pass operation, so the default
367 // 'Remaining x of y operations' message will be confusing here.
368 'progress_message' => '',
369 'error_message' => t('The update has encountered an error.'),
370 // The operations do not live in the .module file, so we need to
371 // tell the batch engine which file to load before calling them.
372 'file' => drupal_get_path('module', 'node') .'/node.admin.inc',
373 );
374 batch_set($batch);
375 }
376 else {
377 foreach ($nodes as $nid) {
378 _node_mass_update_helper($nid, $updates);
379 }
380 drupal_set_message(t('The update has been performed.'));
381 }
382 }
383
384 /**
385 * Node Mass Update - helper function.
386 */
387 function _node_mass_update_helper($nid, $updates) {
388 $node = node_load($nid, NULL, TRUE);
389 foreach ($updates as $name => $value) {
390 $node->$name = $value;
391 }
392 node_save($node);
393 return $node;
394 }
395
396 /**
397 * Node Mass Update Batch operation
398 */
399 function _node_mass_update_batch_process($nodes, $updates, &$context) {
400 if (!isset($context['sandbox']['progress'])) {
401 $context['sandbox']['progress'] = 0;
402 $context['sandbox']['max'] = count($nodes);
403 $context['sandbox']['nodes'] = $nodes;
404 }
405
406 // Process nodes by groups of 5.
407 $count = min(5, count($context['sandbox']['nodes']));
408 for ($i = 1; $i <= $count; $i++) {
409 // For each nid, load the node, reset the values, and save it.
410 $nid = array_shift($context['sandbox']['nodes']);
411 $node = _node_mass_update_helper($nid, $updates);
412
413 // Store result for post-processing in the finished callback.
414 $context['results'][] = l($node->title, 'node/'. $node->nid);
415
416 // Update our progress information.
417 $context['sandbox']['progress']++;
418 }
419
420 // Inform the batch engine that we are not finished,
421 // and provide an estimation of the completion level we reached.
422 if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
423 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
424 }
425 }
426
427 /**
428 * Node Mass Update Batch 'finished' callback.
429 */
430 function _node_mass_update_batch_finished($success, $results, $operations) {
431 if ($success) {
432 drupal_set_message(t('The update has been performed.'));
433 }
434 else {
435 drupal_set_message(t('An error occurred and processing did not complete.'), 'error');
436 $message = format_plural(count($results), '1 item successfully processed:', '@count items successfully processed:');
437 $message .= theme('item_list', $results);
438 drupal_set_message($message);
439 }
440 }
441
442 /**
443 * Menu callback: content administration.
444 */
445 function node_admin_content($form_state) {
446 if (isset($form_state['values']['operation']) && $form_state['values']['operation'] == 'delete') {
447 return node_multiple_delete_confirm($form_state, array_filter($form_state['values']['nodes']));
448 }
449 $form = node_filter_form();
450
451 $form['#theme'] = 'node_filter_form';
452 $form['admin'] = node_admin_nodes();
453
454 return $form;
455 }
456
457 /**
458 * Form builder: Builds the node administration overview.
459 */
460 function node_admin_nodes() {
461
462 $filter = node_build_filter_query();
463
464 $result = pager_query(db_rewrite_sql('SELECT n.*, u.name FROM {node} n '. $filter['join'] .' INNER JOIN {users} u ON n.uid = u.uid '. $filter['where'] .' ORDER BY n.changed DESC'), 50, 0, NULL, $filter['args']);
465
466 // Enable language column if locale is enabled or if we have any node with language
467 $count = db_result(db_query("SELECT COUNT(*) FROM {node} n WHERE language != ''"));
468 $multilanguage = (module_exists('locale') || $count);
469
470 $form['options'] = array(
471 '#type' => 'fieldset',
472 '#title' => t('Update options'),
473 '#prefix' => '<div class="container-inline">',
474 '#suffix' => '</div>',
475 );
476 $options = array();
477 foreach (module_invoke_all('node_operations') as $operation => $array) {
478 $options[$operation] = $array['label'];
479 }
480 $form['options']['operation'] = array(
481 '#type' => 'select',
482 '#options' => $options,
483 '#default_value' => 'approve',
484 );
485 $form['options']['submit'] = array(
486 '#type' => 'submit',
487 '#value' => t('Update'),
488 '#submit' => array('node_admin_nodes_submit'),
489 );
490
491 $languages = language_list();
492 $destination = drupal_get_destination();
493 $nodes = array();
494 while ($node = db_fetch_object($result)) {
495 $nodes[$node->nid] = '';
496 $options = empty($node->language) ? array() : array('language' => $languages[$node->language]);
497 $form['title'][$node->nid] = array('#value' => l($node->title, 'node/'. $node->nid, $options) .' '. theme('mark', node_mark($node->nid, $node->changed)));
498 $form['name'][$node->nid] = array('#value' => check_plain(node_get_types('name', $node)));
499 $form['username'][$node->nid] = array('#value' => theme('username', $node));
500 $form['status'][$node->nid] = array('#value' => ($node->status ? t('published') : t('not published')));
501 if ($multilanguage) {
502 $form['language'][$node->nid] = array('#value' => empty($node->language) ? t('Language neutral') : t($languages[$node->language]->name));
503 }
504 $form['operations'][$node->nid] = array('#value' => l(t('edit'), 'node/'. $node->nid .'/edit', array('query' => $destination)));
505 }
506 $form['nodes'] = array('#type' => 'checkboxes', '#options' => $nodes);
507 $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
508 $form['#theme'] = 'node_admin_nodes';
509 return $form;
510 }
511
512 /**
513 * Validate node_admin_nodes form submissions.
514 *
515 * Check if any nodes have been selected to perform the chosen
516 * 'Update option' on.
517 */
518 function node_admin_nodes_validate($form, &$form_state) {
519 $nodes = array_filter($form_state['values']['nodes']);
520 if (count($nodes) == 0) {
521 form_set_error('', t('No items selected.'));
522 }
523 }
524
525 /**
526 * Process node_admin_nodes form submissions.
527 *
528 * Execute the chosen 'Update option' on the selected nodes.
529 */
530 function node_admin_nodes_submit($form, &$form_state) {
531 $operations = module_invoke_all('node_operations');
532 $operation = $operations[$form_state['values']['operation']];
533 // Filter out unchecked nodes
534 $nodes = array_filter($form_state['values']['nodes']);
535 if ($function = $operation['callback']) {
536 // Add in callback arguments if present.
537 if (isset($operation['callback arguments'])) {
538 $args = array_merge(array($nodes), $operation['callback arguments']);
539 }
540 else {
541 $args = array($nodes);
542 }
543 call_user_func_array($function, $args);
544
545 cache_clear_all();
546 }
547 else {
548 // We need to rebuild the form to go to a second step. For example, to
549 // show the confirmation form for the deletion of nodes.
550 $form_state['rebuild'] = TRUE;
551 }
552 }
553
554
555 /**
556 * Theme node administration overview.
557 *
558 * @ingroup themeable
559 */
560 function theme_node_admin_nodes($form) {
561 // If there are rows in this form, then $form['title'] contains a list of
562 // the title form elements.
563 $has_posts = isset($form['title']) && is_array($form['title']);
564 $select_header = $has_posts ? theme('table_select_header_cell') : '';
565 $header = array($select_header, t('Title'), t('Type'), t('Author'), t('Status'));
566 if (isset($form['language'])) {
567 $header[] = t('Language');
568 }
569 $header[] = t('Operations');
570 $output = '';
571
572 $output .= drupal_render($form['options']);
573 if ($has_posts) {
574 foreach (element_children($form['title']) as $key) {
575 $row = array();
576 $row[] = drupal_render($form['nodes'][$key]);
577 $row[] = drupal_render($form['title'][$key]);
578 $row[] = drupal_render($form['name'][$key]);
579 $row[] = drupal_render($form['username'][$key]);
580 $row[] = drupal_render($form['status'][$key]);
581 if (isset($form['language'])) {
582 $row[] = drupal_render($form['language'][$key]);
583 }
584 $row[] = drupal_render($form['operations'][$key]);
585 $rows[] = $row;
586 }
587
588 }
589 else {
590 $rows[] = array(array('data' => t('No posts available.'), 'colspan' => '6'));
591 }
592
593 $output .= theme('table', $header, $rows);
594 if ($form['pager']['#value']) {
595 $output .= drupal_render($form['pager']);
596 }
597
598 $output .= drupal_render($form);
599
600 return $output;
601 }
602
603 function node_multiple_delete_confirm(&$form_state, $nodes) {
604
605 $form['nodes'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
606 // array_filter returns only elements with TRUE values
607 foreach ($nodes as $nid => $value) {
608 $title = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $nid));
609 $form['nodes'][$nid] = array(
610 '#type' => 'hidden',
611 '#value' => $nid,
612 '#prefix' => '<li>',
613 '#suffix' => check_plain($title) ."</li>\n",
614 );
615 }
616 $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
617 $form['#submit'][] = 'node_multiple_delete_confirm_submit';
618 return confirm_form($form,
619 t('Are you sure you want to delete these items?'),
620 'admin/content/node', t('This action cannot be undone.'),
621 t('Delete all'), t('Cancel'));
622 }
623
624 function node_multiple_delete_confirm_submit($form, &$form_state) {
625 if ($form_state['values']['confirm']) {
626 foreach ($form_state['values']['nodes'] as $nid => $value) {
627 node_delete($nid);
628 }
629 drupal_set_message(t('The items have been deleted.'));
630 }
631 $form_state['redirect'] = 'admin/content/node';
632 return;
633 }
634