tracker.pages.inc 6.14 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * User page callbacks for tracker.module.
6 7 8 9
 */


/**
10 11 12 13 14 15 16 17 18
 * Page callback: Generates a page of tracked nodes for the site.
 *
 * Queries the database for info, adds RDFa info if applicable, and generates
 * the render array that will be used to render the page.
 *
 * @return array
 *   A renderable array.
 *
 * @see tracker_menu()
19
 */
20 21
function tracker_page($account = NULL, $set_title = FALSE) {
  if ($account) {
22
    $query = db_select('tracker_user', 't')
23 24
      ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
      ->addMetaData('base_table', 'tracker_user')
25
      ->condition('t.uid', $account->id());
26

27 28 29
    if ($set_title) {
      // When viewed from user/%user/track, display the name of the user
      // as page title -- the tab title remains Track so this needs to be done
30
      // here and not in the menu definition.
31
      drupal_set_title(user_format_name($account));
32
    }
33
  }
34
  else {
35
    $query = db_select('tracker_node', 't', array('target' => 'slave'))
36 37
      ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
      ->addMetaData('base_table', 'tracker_node');
38 39 40 41
  }

  // This array acts as a placeholder for the data selected later
  // while keeping the correct order.
42
  $tracker_data = $query
43 44 45 46 47 48 49
    ->addTag('node_access')
    ->fields('t', array('nid', 'changed'))
    ->condition('t.published', 1)
    ->orderBy('t.changed', 'DESC')
    ->limit(25)
    ->execute()
    ->fetchAllAssoc('nid');
50

51
  $rows = array();
52 53
  if (!empty($tracker_data)) {
    $nids = array_keys($tracker_data);
54
    $nodes = node_load_multiple($nids);
55
    // Now, get the data and put into the placeholder array.
56 57 58
    // @todo This should be actually filtering on the desired language and just
    //   fall back to the default language.
    $result = db_query('SELECT n.nid, l.comment_count FROM {node_field_data} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.nid IN (:nids) AND n.default_langcode = 1 ORDER BY n.changed DESC', array(':nids' => $nids), array('target' => 'slave'))->fetchAllKeyed();
59 60 61
    foreach ($result as $nid => $comment_count) {
      $nodes[$nid]->last_activity = $tracker_data[$nid]->changed;
      $nodes[$nid]->comment_count = $comment_count;
62
    }
63

64
    // Display the data.
65
    foreach ($nodes as $node) {
66
      // Determine the number of comments.
67 68 69
      $comments = 0;
      if ($node->comment_count) {
        $comments = $node->comment_count;
70

71
        if ($new = comment_num_new($node->id())) {
72
          $comments .= '<br />';
73
          $comments .= l(format_plural($new, '1 new', '@count new'), 'node/' . $node->id(), array('fragment' => 'new'));
74
        }
75 76
      }

77 78
      $mark_build = array(
        '#theme' => 'mark',
79
        '#status' => node_mark($node->id(), $node->getChangedTime()),
80 81
      );

82
      $row = array(
83
        'type' => check_plain(node_get_type_label($node)),
84 85
        'title' => array('data' => l($node->getTitle(), 'node/' . $node->id()) . ' ' . drupal_render($mark_build)),
        'author' => array('data' => array('#theme' => 'username', '#account' => $node->getAuthor())),
86 87
        'replies' => array('class' => array('replies'), 'data' => $comments),
        'last updated' => array('data' => t('!time ago', array('!time' => format_interval(REQUEST_TIME - $node->last_activity)))),
88
      );
89 90

      // Adds extra RDFa markup to the $row array if the RDF module is enabled.
91
      if (module_exists('rdf')) {
92
        $mapping = rdf_get_mapping('node', $node->getType());
93 94 95 96
        // Adds RDFa markup to the title of the node. Because the RDFa markup is
        // added to the td tag which might contain HTML code, we specify an
        // empty datatype to ensure the value of the title read by the RDFa
        // parsers is a plain literal.
97 98
        $title_mapping = $mapping->getPreparedFieldMapping('title');
        $row['title'] += rdf_rdfa_attributes($title_mapping) + array('datatype' => '');
99
        // Annotates the td tag containing the author of the node.
100 101
        $uid_mapping = $mapping->getPreparedFieldMapping('uid');
        $row['author'] += rdf_rdfa_attributes($uid_mapping);
102 103 104 105 106
        // Annotates the td tag containing the number of replies. We add the
        // content attribute to ensure that only the comment count is used as
        // the value for 'num_replies'. Otherwise, other text such as a link
        // to the number of new comments could be included in the 'num_replies'
        // value.
107 108
        $comment_count_mapping = $mapping->getPreparedFieldMapping('comment_count');
        $row['replies'] += rdf_rdfa_attributes($comment_count_mapping);
109 110 111 112 113
        $row['replies'] += array('content' => $node->comment_count);
        // If the node has no comments, we assume the node itself was modified
        // and apply 'changed' in addition to 'last_activity'.  If there are
        // comments present, we cannot infer whether the node itself was
        // modified or a comment was posted, so we use only 'last_activity'.
114 115
        $last_activity_mapping = $mapping->getPreparedFieldMapping('last_activity');
        $last_activity_attributes = rdf_rdfa_attributes($last_activity_mapping, $node->last_activity);
116
        if ($node->comment_count == 0) {
117 118 119
          $changed_mapping = $mapping->getPreparedFieldMapping('changed');
          $changed_attributes = rdf_rdfa_attributes($changed_mapping, $node->last_activity);
          $last_activity_attributes['property'] = array_merge($last_activity_attributes['property'], $changed_attributes['property']);
120
        }
121
        $row['last updated'] += $last_activity_attributes;
122 123

        // We need to add the about attribute on the tr tag to specify which
124
        // node the RDFa annotations above apply to. We move the content of
125 126
        // $row to a 'data' sub array so we can specify attributes for the row.
        $row = array('data' => $row);
127
        $row['about'] = url('node/' . $node->id());
128 129
      }
      $rows[] = $row;
130
    }
131 132
  }

133 134
  $page['tracker'] = array(
    '#rows' => $rows,
135
    '#header' => array(t('Type'), t('Title'), t('Author'), t('Replies'), t('Last updated')),
136
    '#theme' => 'table',
137
    '#empty' => t('No content available.'),
138 139 140 141 142 143 144
  );
  $page['pager'] = array(
    '#theme' => 'pager',
    '#quantity' => 25,
    '#weight' => 10,
  );
  $page['#sorted'] = TRUE;
145

146
  return $page;
147
}