You are here

function insight_page_report_pages in Insight 7

Form builder: Builds the node administration overview.

1 call to insight_page_report_pages()
insight_page_report in ./insight.page_reports.inc
Menu callback: content administration.

File

./insight.page_reports.inc, line 20

Code

function insight_page_report_pages() {

  // TODO set admin_access correctly
  $admin_access = TRUE;

  // Build the 'Update options' form.
  $form['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Update options'),
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
    '#access' => $admin_access,
  );
  $options = array();

  //foreach (module_invoke_all('node_operations') as $operation => $array) {
  foreach (insight_node_operations() as $operation => $array) {
    $options[$operation] = $array['label'];
  }
  $form['options']['operation'] = array(
    '#type' => 'select',
    '#title' => t('Operation'),
    '#title_display' => 'invisible',
    '#options' => $options,
    '#default_value' => 'approve',
  );
  $form['options']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
    '#validate' => array(
      'node_admin_nodes_validate',
    ),
    '#submit' => array(
      'node_admin_nodes_submit',
    ),
  );

  // Enable language column if translation module is enabled or if we have any
  // node with language.
  $multilanguage = module_exists('translation') || db_query_range("SELECT 1 FROM {node} WHERE language <> :language", 0, 1, array(
    ':language' => LANGUAGE_NONE,
  ))
    ->fetchField();
  $analyzer_defs = insight_analyzer_info();

  // Build the sortable table header.
  $header = array(
    'title' => array(
      'data' => t('Title'),
      'field' => 'n.title',
    ),
    'type' => array(
      'data' => t('Type'),
      'field' => 'n.type',
    ),
  );
  if ($multilanguage) {
    $header['language'] = array(
      'data' => t('Language'),
      'field' => 'n.language',
    );
  }
  $report_defs = array();
  $report_short_titles = array();
  foreach ($analyzer_defs as $analyzer_name => $analyzer_def) {
    if (isset($analyzer_def['reports'])) {
      foreach ($analyzer_def['reports'] as $report_name => $report_def) {
        $report_defs[$report_name] = $report_def;
        $report_short_titles[$report_def['short title']] = $report_name . '.score';
        $header[$report_name] = array(
          'data' => $report_def['short title'],
          'field' => $report_name . '.status',
        );
      }
    }
  }
  $header['operations'] = array(
    'data' => t('Operations'),
  );
  $query = db_select('node', 'n')
    ->extend('PagerDefault')
    ->extend('TableSort');
  insight_page_report_build_filter_query($query);
  if (!user_access('bypass node access')) {

    // If the user is able to view their own unpublished nodes, allow them
    // to see these in addition to published nodes. Check that they actually
    // have some unpublished nodes to view before adding the condition.
    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())) {
      $query
        ->condition(db_or()
        ->condition('n.status', 1)
        ->condition('n.nid', $own_unpublished, 'IN'));
    }
    else {

      // If not, restrict the query to published nodes.
      $query
        ->condition('n.status', 1);
    }
  }
  $query
    ->fields('n')
    ->limit(50)
    ->groupBy('n.nid')
    ->orderByHeader($header);

  // if sorted by report status, add report score as second sort
  if (isset($_GET['order']) && isset($_GET['sort']) && isset($report_short_titles[$_GET['order']])) {
    $query
      ->orderBy($report_short_titles[$_GET['order']], $_GET['sort']);
  }
  foreach ($report_defs as $name => $def) {
    $alias[$name] = $query
      ->leftJoin('insight_report', $name, "n.nid = %alias.nid AND %alias.name = '{$name}'");
    $query
      ->addField($alias[$name], 'irid', $name . '_irid');
    $query
      ->addField($alias[$name], 'active', $name . '_active');
    $query
      ->addField($alias[$name], 'status', $name . '_status');
    $query
      ->addField($alias[$name], 'score', $name . '_score');
    $query
      ->addField($alias[$name], 'help', $name . '_help');
  }
  $nodes = $query
    ->execute()
    ->fetchAll();

  // Prepare the list of nodes.
  $languages = language_list();
  $destination = drupal_get_destination();
  $options = array();
  foreach ($nodes as $node) {
    $l_options = $node->language != LANGUAGE_NONE && isset($languages[$node->language]) ? array(
      'language' => $languages[$node->language],
    ) : array();
    $options[$node->nid] = array(
      'title' => array(
        'data' => array(
          '#type' => 'link',
          '#title' => $node->title,
          '#href' => 'node/' . $node->nid,
          '#options' => $l_options,
          '#suffix' => ' ' . theme('mark', array(
            'type' => node_mark($node->nid, $node->changed),
          )),
        ),
      ),
      'type' => check_plain(node_type_get_name($node)),
    );
    if ($multilanguage) {
      if ($node->language == LANGUAGE_NONE || isset($languages[$node->language])) {
        $options[$node->nid]['language'] = $node->language == LANGUAGE_NONE ? t('Language neutral') : t($languages[$node->language]->name);
      }
      else {
        $options[$node->nid]['language'] = t('Undefined language (@langcode)', array(
          '@langcode' => $node->language,
        ));
      }
    }
    foreach ($report_defs as $name => $def) {
      $v = array(
        'irid' => $node->{$name . '_irid'},
        'active' => $node->{$name . '_active'},
        'score' => $node->{$name . '_score'},
        'status' => $node->{$name . '_status'},
        'help' => $node->{$name . '_help'},
        'type' => $name,
      );
      $options[$node->nid][$name] = theme_insight_report_value(array(
        'value' => $v,
        'report_def' => $report_defs[$name],
      ));
    }

    // Build a list of all the accessible operations for the current node.
    $operations = array();
    if (TRUE && node_access('update', $node)) {
      $operations['analyze'] = array(
        'title' => t('analyze'),
        'href' => 'admin/reports/insight/autoanalyze/node/' . $node->nid,
        'query' => $destination,
      );
    }
    if (node_access('update', $node)) {
      $operations['edit'] = array(
        'title' => t('edit'),
        'href' => 'node/' . $node->nid . '/edit',
        'query' => $destination,
      );
    }
    $options[$node->nid]['operations'] = array();
    if (count($operations) > 1) {

      // Render an unordered list of operations links.
      $options[$node->nid]['operations'] = array(
        'data' => array(
          '#theme' => 'links__node_operations',
          '#links' => $operations,
          '#attributes' => array(
            'class' => array(
              'links',
              'inline',
            ),
          ),
        ),
      );
    }
    elseif (!empty($operations)) {

      // Render the first and only operation as a link.
      $link = reset($operations);
      $options[$node->nid]['operations'] = array(
        'data' => array(
          '#type' => 'link',
          '#title' => $link['title'],
          '#href' => $link['href'],
          '#options' => array(
            'query' => $link['query'],
          ),
        ),
      );
    }
  }

  // Only use a tableselect when the current user is able to perform any
  // operations.
  if ($admin_access) {
    $form['nodes'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $options,
      '#empty' => t('No content available.'),
    );
  }
  else {
    $form['nodes'] = array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $options,
      '#empty' => t('No content available.'),
    );
  }
  $form['pager'] = array(
    '#markup' => theme('pager'),
  );
  return $form;
}