tracker.pages.inc

User page callbacks for tracker.module.

File

drupal/core/modules/tracker/tracker.pages.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for tracker.module.
  5. */
  6. /**
  7. * Page callback: Generates a page of tracked nodes for the site.
  8. *
  9. * Queries the database for info, adds RDFa info if applicable, and generates
  10. * the render array that will be used to render the page.
  11. *
  12. * @return array
  13. * A renderable array.
  14. *
  15. * @see tracker_menu()
  16. */
  17. function tracker_page($account = NULL, $set_title = FALSE) {
  18. if ($account) {
  19. $query = db_select('tracker_user', 't')
  20. ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
  21. ->addMetaData('base_table', 'tracker_user')
  22. ->condition('t.uid', $account->uid);
  23. if ($set_title) {
  24. // When viewed from user/%user/track, display the name of the user
  25. // as page title -- the tab title remains Track so this needs to be done
  26. // here and not in the menu definition.
  27. drupal_set_title(user_format_name($account));
  28. }
  29. }
  30. else {
  31. $query = db_select('tracker_node', 't', array('target' => 'slave'))
  32. ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
  33. ->addMetaData('base_table', 'tracker_node');
  34. }
  35. // This array acts as a placeholder for the data selected later
  36. // while keeping the correct order.
  37. $tracker_data = $query
  38. ->addTag('node_access')
  39. ->fields('t', array('nid', 'changed'))
  40. ->condition('t.published', 1)
  41. ->orderBy('t.changed', 'DESC')
  42. ->limit(25)
  43. ->execute()
  44. ->fetchAllAssoc('nid');
  45. $rows = array();
  46. if (!empty($tracker_data)) {
  47. $nids = array_keys($tracker_data);
  48. $nodes = node_load_multiple($nids);
  49. // Now, get the data and put into the placeholder array.
  50. // @todo This should be actually filtering on the desired language and just
  51. // fall back to the default language.
  52. $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();
  53. foreach ($result as $nid => $comment_count) {
  54. $nodes[$nid]->last_activity = $tracker_data[$nid]->changed;
  55. $nodes[$nid]->comment_count = $comment_count;
  56. }
  57. // Display the data.
  58. foreach ($nodes as $node) {
  59. // Determine the number of comments.
  60. $comments = 0;
  61. if ($node->comment_count) {
  62. $comments = $node->comment_count;
  63. if ($new = comment_num_new($node->nid)) {
  64. $comments .= '<br />';
  65. $comments .= l(format_plural($new, '1 new', '@count new'), 'node/' . $node->nid, array('fragment' => 'new'));
  66. }
  67. }
  68. $mark_build = array(
  69. '#theme' => 'mark',
  70. '#mark_type' => node_mark($node->nid, $node->changed),
  71. );
  72. $row = array(
  73. 'type' => check_plain(node_get_type_label($node)),
  74. // Do not use $node->label(), because $node comes from the database.
  75. 'title' => array('data' => l($node->title, 'node/' . $node->nid) . ' ' . drupal_render($mark_build)),
  76. 'author' => array('data' => array('#theme' => 'username', '#account' => $node)),
  77. 'replies' => array('class' => array('replies'), 'data' => $comments),
  78. 'last updated' => array('data' => t('!time ago', array('!time' => format_interval(REQUEST_TIME - $node->last_activity)))),
  79. );
  80. // Adds extra RDFa markup to the $row array if the RDF module is enabled.
  81. if (function_exists('rdf_mapping_load')) {
  82. // Each node is not loaded for performance reasons, as a result we need
  83. // to retrieve the RDF mapping for each node type.
  84. $mapping = rdf_mapping_load('node', $node->type);
  85. // Adds RDFa markup to the title of the node. Because the RDFa markup is
  86. // added to the td tag which might contain HTML code, we specify an
  87. // empty datatype to ensure the value of the title read by the RDFa
  88. // parsers is a plain literal.
  89. $row['title'] += rdf_rdfa_attributes($mapping['title']) + array('datatype' => '');
  90. // Annotates the td tag containing the author of the node.
  91. $row['author'] += rdf_rdfa_attributes($mapping['uid']);
  92. // Annotates the td tag containing the number of replies. We add the
  93. // content attribute to ensure that only the comment count is used as
  94. // the value for 'num_replies'. Otherwise, other text such as a link
  95. // to the number of new comments could be included in the 'num_replies'
  96. // value.
  97. $row['replies'] += rdf_rdfa_attributes($mapping['comment_count']);
  98. $row['replies'] += array('content' => $node->comment_count);
  99. // If the node has no comments, we assume the node itself was modified
  100. // and apply 'changed' in addition to 'last_activity'. If there are
  101. // comments present, we cannot infer whether the node itself was
  102. // modified or a comment was posted, so we use only 'last_activity'.
  103. $mapping_last_activity = rdf_rdfa_attributes($mapping['last_activity'], $node->last_activity);
  104. if ($node->comment_count == 0) {
  105. $mapping_changed = rdf_rdfa_attributes($mapping['changed'], $node->last_activity);
  106. $mapping_last_activity['property'] = array_merge($mapping_last_activity['property'], $mapping_changed['property']);
  107. }
  108. $row['last updated'] += $mapping_last_activity;
  109. // We need to add the about attribute on the tr tag to specify which
  110. // node the RDFa annotations above apply to. We move the content of
  111. // $row to a 'data' sub array so we can specify attributes for the row.
  112. $row = array('data' => $row);
  113. $row['about'] = url('node/' . $node->nid);
  114. }
  115. $rows[] = $row;
  116. }
  117. }
  118. $page['tracker'] = array(
  119. '#rows' => $rows,
  120. '#header' => array(t('Type'), t('Title'), t('Author'), t('Replies'), t('Last updated')),
  121. '#theme' => 'table',
  122. '#empty' => t('No content available.'),
  123. );
  124. $page['pager'] = array(
  125. '#theme' => 'pager',
  126. '#quantity' => 25,
  127. '#weight' => 10,
  128. );
  129. $page['#sorted'] = TRUE;
  130. return $page;
  131. }

Functions

Namesort descending Description
tracker_page Page callback: Generates a page of tracked nodes for the site.