node.admin.inc

  1. 7.x drupal-7.x/modules/node/node.admin.inc
  2. 6.x drupal-6.x/modules/node/node.admin.inc

Content administration and module settings UI.

File

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