node.admin.inc 23 KB

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