node.admin.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. $langcode = entity_language('node', $node);
  404. $l_options = $langcode != LANGUAGE_NONE && isset($languages[$langcode]) ? array('language' => $languages[$langcode]) : array();
  405. $options[$node->nid] = array(
  406. 'title' => array(
  407. 'data' => array(
  408. '#type' => 'link',
  409. '#title' => $node->title,
  410. '#href' => 'node/' . $node->nid,
  411. '#options' => $l_options,
  412. '#suffix' => ' ' . theme('mark', array('type' => node_mark($node->nid, $node->changed))),
  413. ),
  414. ),
  415. 'type' => check_plain(node_type_get_name($node)),
  416. 'author' => theme('username', array('account' => $node)),
  417. 'status' => $node->status ? t('published') : t('not published'),
  418. 'changed' => format_date($node->changed, 'short'),
  419. );
  420. if ($multilanguage) {
  421. if ($langcode == LANGUAGE_NONE || isset($languages[$langcode])) {
  422. $options[$node->nid]['language'] = $langcode == LANGUAGE_NONE ? t('Language neutral') : t($languages[$langcode]->name);
  423. }
  424. else {
  425. $options[$node->nid]['language'] = t('Undefined language (@langcode)', array('@langcode' => $langcode));
  426. }
  427. }
  428. // Build a list of all the accessible operations for the current node.
  429. $operations = array();
  430. if (node_access('update', $node)) {
  431. $operations['edit'] = array(
  432. 'title' => t('edit'),
  433. 'href' => 'node/' . $node->nid . '/edit',
  434. 'query' => $destination,
  435. );
  436. }
  437. if (node_access('delete', $node)) {
  438. $operations['delete'] = array(
  439. 'title' => t('delete'),
  440. 'href' => 'node/' . $node->nid . '/delete',
  441. 'query' => $destination,
  442. );
  443. }
  444. $options[$node->nid]['operations'] = array();
  445. if (count($operations) > 1) {
  446. // Render an unordered list of operations links.
  447. $options[$node->nid]['operations'] = array(
  448. 'data' => array(
  449. '#theme' => 'links__node_operations',
  450. '#links' => $operations,
  451. '#attributes' => array('class' => array('links', 'inline')),
  452. ),
  453. );
  454. }
  455. elseif (!empty($operations)) {
  456. // Render the first and only operation as a link.
  457. $link = reset($operations);
  458. $options[$node->nid]['operations'] = array(
  459. 'data' => array(
  460. '#type' => 'link',
  461. '#title' => $link['title'],
  462. '#href' => $link['href'],
  463. '#options' => array('query' => $link['query']),
  464. ),
  465. );
  466. }
  467. }
  468. // Only use a tableselect when the current user is able to perform any
  469. // operations.
  470. if ($admin_access) {
  471. $form['nodes'] = array(
  472. '#type' => 'tableselect',
  473. '#header' => $header,
  474. '#options' => $options,
  475. '#empty' => t('No content available.'),
  476. );
  477. }
  478. // Otherwise, use a simple table.
  479. else {
  480. $form['nodes'] = array(
  481. '#theme' => 'table',
  482. '#header' => $header,
  483. '#rows' => $options,
  484. '#empty' => t('No content available.'),
  485. );
  486. }
  487. $form['pager'] = array('#markup' => theme('pager'));
  488. return $form;
  489. }
  490. /**
  491. * Validate node_admin_nodes form submissions.
  492. *
  493. * Check if any nodes have been selected to perform the chosen
  494. * 'Update option' on.
  495. */
  496. function node_admin_nodes_validate($form, &$form_state) {
  497. // Error if there are no items to select.
  498. if (!is_array($form_state['values']['nodes']) || !count(array_filter($form_state['values']['nodes']))) {
  499. form_set_error('', t('No items selected.'));
  500. }
  501. }
  502. /**
  503. * Process node_admin_nodes form submissions.
  504. *
  505. * Execute the chosen 'Update option' on the selected nodes.
  506. */
  507. function node_admin_nodes_submit($form, &$form_state) {
  508. $operations = module_invoke_all('node_operations');
  509. $operation = $operations[$form_state['values']['operation']];
  510. // Filter out unchecked nodes
  511. $nodes = array_filter($form_state['values']['nodes']);
  512. if ($function = $operation['callback']) {
  513. // Add in callback arguments if present.
  514. if (isset($operation['callback arguments'])) {
  515. $args = array_merge(array($nodes), $operation['callback arguments']);
  516. }
  517. else {
  518. $args = array($nodes);
  519. }
  520. call_user_func_array($function, $args);
  521. cache_clear_all();
  522. }
  523. else {
  524. // We need to rebuild the form to go to a second step. For example, to
  525. // show the confirmation form for the deletion of nodes.
  526. $form_state['rebuild'] = TRUE;
  527. }
  528. }
  529. function node_multiple_delete_confirm($form, &$form_state, $nodes) {
  530. $form['nodes'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
  531. // array_filter returns only elements with TRUE values
  532. foreach ($nodes as $nid => $value) {
  533. $title = db_query('SELECT title FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
  534. $form['nodes'][$nid] = array(
  535. '#type' => 'hidden',
  536. '#value' => $nid,
  537. '#prefix' => '<li>',
  538. '#suffix' => check_plain($title) . "</li>\n",
  539. );
  540. }
  541. $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
  542. $form['#submit'][] = 'node_multiple_delete_confirm_submit';
  543. $confirm_question = format_plural(count($nodes),
  544. 'Are you sure you want to delete this item?',
  545. 'Are you sure you want to delete these items?');
  546. return confirm_form($form,
  547. $confirm_question,
  548. 'admin/content', t('This action cannot be undone.'),
  549. t('Delete'), t('Cancel'));
  550. }
  551. function node_multiple_delete_confirm_submit($form, &$form_state) {
  552. if ($form_state['values']['confirm']) {
  553. node_delete_multiple(array_keys($form_state['values']['nodes']));
  554. $count = count($form_state['values']['nodes']);
  555. watchdog('content', 'Deleted @count posts.', array('@count' => $count));
  556. drupal_set_message(format_plural($count, 'Deleted 1 post.', 'Deleted @count posts.'));
  557. }
  558. $form_state['redirect'] = 'admin/content';
  559. }