node.admin.inc

  1. drupal
    1. 6 modules/node/node.admin.inc
    2. 7 modules/node/node.admin.inc
    3. 8 core/modules/node/node.admin.inc

Content administration and module settings UI.

Functions & methods

NameDescription
node_admin_contentMenu callback: content administration.
node_admin_nodesForm builder: Builds the node administration overview.
node_admin_nodes_submitProcess node_admin_nodes form submissions.
node_admin_nodes_validateValidate node_admin_nodes form submissions.
node_build_filter_queryApply filters for node administration filters based on session.
node_configure_rebuild_confirmMenu callback: confirm rebuilding of permissions.
node_configure_rebuild_confirm_submitHandler for wipe confirmation
node_filtersList node administration filters that can be applied.
node_filter_formReturn form for node administration filters.
node_filter_form_submitProcess result from node administration filter form.
node_mass_updateMake mass update of nodes, changing all nodes in the $nodes array to update them with the field values in $updates.
node_multiple_delete_confirm
node_multiple_delete_confirm_submit
node_node_operationsImplements hook_node_operations().
_node_mass_update_batch_finishedNode Mass Update Batch 'finished' callback.
_node_mass_update_batch_processNode Mass Update Batch operation
_node_mass_update_helperNode Mass Update - helper function.

File

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