4.6.x node.module node_admin_nodes()
4.7.x node.module node_admin_nodes()
5.x node.module node_admin_nodes()
6.x node.admin.inc node_admin_nodes()
7.x node.admin.inc node_admin_nodes()

Generate the content administration overview.

File

modules/node.module, line 739
The core that allows content to be submitted to the site.

Code

function node_admin_nodes() {

  /*
   ** Operations
   */
  $operations = array(
    'approve' => array(
      t('Approve the selected posts'),
      'UPDATE {node} SET status = 1, moderate = 0 WHERE nid = %d',
    ),
    'promote' => array(
      t('Promote the selected posts'),
      'UPDATE {node} SET status = 1, promote = 1 WHERE nid = %d',
    ),
    'sticky' => array(
      t('Make the selected posts sticky'),
      'UPDATE {node} SET status = 1, sticky = 1 WHERE nid = %d',
    ),
    'demote' => array(
      t('Demote the selected posts'),
      'UPDATE {node} SET promote = 0 WHERE nid = %d',
    ),
    'unpublish' => array(
      t('Unpublish the selected posts'),
      'UPDATE {node} SET status = 0 WHERE nid = %d',
    ),
    'delete' => array(
      t('Delete the selected posts'),
      '',
    ),
  );

  // Handle operations
  $op = $_POST['op'];
  $edit = $_POST['edit'];
  if (($op == t('Update') || $op == t('Delete all')) && isset($edit['operation']) && isset($edit['nodes'])) {
    $edit['nodes'] = array_diff($edit['nodes'], array(
      0,
    ));
    if (count($edit['nodes']) == 0) {
      form_set_error('', t('Please select some items to perform the update on.'));
    }
    else {
      if ($operations[$edit['operation']][1]) {

        // Flag changes
        $operation = $operations[$edit['operation']][1];
        foreach ($edit['nodes'] as $nid => $value) {
          if ($value) {
            db_query($operation, $nid);
          }
        }
        drupal_set_message(t('The update has been performed.'));
      }
      else {
        if ($edit['operation'] == 'delete') {

          // Mass delete
          if ($edit['confirm']) {
            foreach ($edit['nodes'] as $nid => $value) {
              node_delete(array(
                'nid' => $nid,
                'confirm' => 1,
              ));
            }
            drupal_set_message(t('The items have been deleted.'));
          }
          else {
            $extra = '<ul>';
            foreach ($edit['nodes'] as $nid => $value) {
              if ($value) {
                $title = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $nid));
                $extra .= '<li>' . form_hidden('nodes][' . $nid, 1) . check_plain($title) . '</li>';
              }
            }
            $extra .= '</ul>';
            $extra .= form_hidden('operation', 'delete');
            $output = theme('confirm', t('Are you sure you want to delete these items?'), 'admin/node', t('This action cannot be undone.'), t('Delete all'), t('Cancel'), $extra);
            return $output;
          }
        }
      }
    }
  }

  /*
   ** Filters
   */
  $node_types = drupal_map_assoc(node_list());
  foreach ($node_types as $k => $v) {
    $node_types[$k] = node_invoke($v, 'node_name');
  }

  // Regular filters
  $filters = array(
    'status' => array(
      'title' => t('status'),
      'options' => array(
        'status-1' => t('published'),
        'status-0' => t('not published'),
        'moderate-1' => t('in moderation'),
        'moderate-0' => t('not in moderation'),
        'promote-1' => t('promoted'),
        'promote-0' => t('not promoted'),
        'sticky-1' => t('sticky'),
        'sticky-0' => t('not sticky'),
      ),
    ),
    'type' => array(
      'title' => t('type'),
      'where' => "n.type = '%s'",
      'options' => $node_types,
    ),
  );

  // Merge all vocabularies into one for retrieving $value below
  if ($taxonomy = module_invoke('taxonomy', 'form_all')) {
    $terms = array();
    foreach ($taxonomy as $key => $value) {
      $terms = $terms + $value;
    }
    $filters['category'] = array(
      'title' => t('category'),
      'where' => 'tn.tid = %d',
      'options' => $terms,
      'join' => 'INNER JOIN {term_node} tn ON n.nid = tn.nid',
    );
  }

  // Initialize/reset filters
  if (!isset($_SESSION['node_overview_filter']) || !is_array($_SESSION['node_overview_filter']) || $op == t('Reset')) {
    $_SESSION['node_overview_filter'] = array();
  }
  $session =& $_SESSION['node_overview_filter'];
  $filter = $edit['filter'];
  if (($op == t('Filter') || $op == t('Refine')) && isset($filter)) {
    if (isset($filters[$filter]['options'][$edit[$filter]])) {
      $session[] = array(
        $filter,
        $edit[$filter],
      );
    }
  }
  if ($op == t('Undo')) {
    array_pop($session);
  }
  if ($op != '') {
    drupal_goto('admin/node');
  }

  /*
   ** Form
   */
  $output .= '<div id="node-admin-filter">';

  // Existing filters
  $form = '<ul>';
  $i = 0;
  foreach ($session as $filter) {
    list($type, $value) = $filter;
    $params = array(
      '%a' => '<strong>' . $filters[$type]['title'] . '</strong>',
      '%b' => '<strong>' . $filters[$type]['options'][$value] . '</strong>',
    );
    $form .= '<li>' . ($i++ ? t('<em>and</em> where <strong>%a</strong> is <strong>%b</strong>', $params) : t('<strong>%a</strong> is <strong>%b</strong>', $params)) . '</li>';
  }

  // New filter form
  if (isset($filters['category'])) {
    $filters['category']['options'] = $taxonomy;
  }
  $values = '';
  $options = array();
  foreach ($filters as $key => $value) {
    $options[$key] = $value['title'];
    $b .= form_select('', $key, 1, $filters[$key]['options']);
  }
  $buttons = '';
  if (count($options)) {
    $form .= '<li><dl class="multiselect">';
    $a = '';
    foreach ($options as $key => $value) {
      $a .= form_radio($value, 'filter', $key);
    }
    if (!$i) {
      $form .= t('<dd class="a">%a</dd> <dt>is</dt> <dd class="b">%b</dd>', array(
        '%a' => $a,
        '%b' => $b,
      ));
    }
    else {
      $form .= t('<dt><em>and</em> where</dt> <dd class="a">%a</dd> <dt>is</dt> <dd class="b">%b</dd>', array(
        '%a' => $a,
        '%b' => $b,
      ));
    }
    $form .= '</dl>';
    $buttons = form_submit(count($session) ? t('Refine') : t('Filter'));
  }
  if (count($session)) {
    $buttons .= form_submit(t('Undo')) . form_submit(t('Reset'));
  }
  $form .= '<div class="container-inline" id="node-admin-buttons">' . $buttons . '</div>';
  $form .= '</li></ul><br class="clear" />';
  $output .= form_group(t('Show only items where'), $form);

  // Build query
  $where = $args = array();
  $join = '';
  foreach ($session as $filter) {
    list($key, $value) = $filter;
    if ($key == 'status') {

      // Note: no exploit hole as $value has already been checked
      list($key, $value) = explode('-', $value, 2);
      $where[] = 'n.' . $key . ' = %d';
    }
    else {
      $where[] = $filters[$key]['where'];
    }
    $args[] = $value;
    $join .= $filters[$key]['join'];
  }
  $where = count($where) ? 'WHERE ' . implode(' AND ', $where) : '';
  $result = pager_query('SELECT n.*, u.name, u.uid FROM {node} n ' . $join . ' INNER JOIN {users} u ON n.uid = u.uid ' . $where . ' ORDER BY n.changed DESC', 50, 0, NULL, $args);

  // Make sure the update controls are disabled if we don't have any rows to select from.
  $disabled = !db_num_rows($result);
  $options = array();
  foreach ($operations as $key => $value) {
    $options[$key] = $value[0];
  }
  $form = form_select(NULL, 'operation', 'approve', $options, NULL, $disabled ? 'disabled="disabled"' : '');
  $form .= form_submit(t('Update'), 'op', $disabled ? array(
    'disabled' => 'disabled',
  ) : array());
  $output .= form_group(t('Update options'), "<div class=\"container-inline\">{$form}</div>");
  $output .= '</div>';

  // Overview table:
  $header = array(
    NULL,
    t('Title'),
    t('Type'),
    t('Author'),
    t('Status'),
    t('Operations'),
  );
  $destination = drupal_get_destination();
  while ($node = db_fetch_object($result)) {
    $rows[] = array(
      form_checkbox(NULL, 'nodes][' . $node->nid, 1, 0),
      l($node->title, 'node/' . $node->nid) . ' ' . theme('mark', node_mark($node->nid, $node->changed)),
      node_invoke($node, 'node_name'),
      format_name($node),
      $node->status ? t('published') : t('not published'),
      l(t('edit'), 'node/' . $node->nid . '/edit', array(), $destination),
    );
  }
  if ($pager = theme('pager', NULL, 50, 0)) {
    $rows[] = array(
      array(
        'data' => $pager,
        'colspan' => '7',
      ),
    );
  }
  if (!$rows) {
    $rows[] = array(
      array(
        'data' => t('No posts available.'),
        'colspan' => '6',
      ),
    );
  }
  $output .= theme('table', $header, $rows);
  return form($output, 'post', url('admin/node/action'));
}