tracker.pages.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for tracker.module.
  5. */
  6. /**
  7. * Page callback: prints a listing of active nodes on 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. function tracker_page($account = NULL, $set_title = FALSE) {
  13. if ($account) {
  14. $query = db_select('tracker_user', 't')->extend('PagerDefault');
  15. $query->condition('t.uid', $account->uid);
  16. if ($set_title) {
  17. // When viewed from user/%user/track, display the name of the user
  18. // as page title -- the tab title remains Track so this needs to be done
  19. // here and not in the menu definition.
  20. drupal_set_title(format_username($account));
  21. }
  22. }
  23. else {
  24. $query = db_select('tracker_node', 't', array('target' => 'slave'))->extend('PagerDefault');
  25. }
  26. // This array acts as a placeholder for the data selected later
  27. // while keeping the correct order.
  28. $nodes = $query
  29. ->addTag('node_access')
  30. ->fields('t', array('nid', 'changed'))
  31. ->condition('t.published', 1)
  32. ->orderBy('t.changed', 'DESC')
  33. ->limit(25)
  34. ->execute()
  35. ->fetchAllAssoc('nid');
  36. $rows = array();
  37. if (!empty($nodes)) {
  38. // Now, get the data and put into the placeholder array.
  39. $result = db_query('SELECT n.nid, n.title, n.type, n.changed, n.uid, u.name, l.comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {users} u ON n.uid = u.uid WHERE n.nid IN (:nids)', array(':nids' => array_keys($nodes)), array('target' => 'slave'));
  40. foreach ($result as $node) {
  41. $node->last_activity = $nodes[$node->nid]->changed;
  42. $nodes[$node->nid] = $node;
  43. }
  44. // Display the data.
  45. foreach ($nodes as $node) {
  46. // Determine the number of comments.
  47. $comments = 0;
  48. if ($node->comment_count) {
  49. $comments = $node->comment_count;
  50. if ($new = comment_num_new($node->nid)) {
  51. $comments .= '<br />';
  52. $comments .= l(format_plural($new, '1 new', '@count new'), 'node/' . $node->nid, array('fragment' => 'new'));
  53. }
  54. }
  55. $row = array(
  56. 'type' => check_plain(node_type_get_name($node->type)),
  57. 'title' => array('data' => l($node->title, 'node/' . $node->nid) . ' ' . theme('mark', array('type' => node_mark($node->nid, $node->changed)))),
  58. 'author' => array('data' => theme('username', array('account' => $node))),
  59. 'replies' => array('class' => array('replies'), 'data' => $comments),
  60. 'last updated' => array('data' => t('!time ago', array('!time' => format_interval(REQUEST_TIME - $node->last_activity)))),
  61. );
  62. // Adds extra RDFa markup to the $row array if the RDF module is enabled.
  63. if (function_exists('rdf_mapping_load')) {
  64. // Each node is not loaded for performance reasons, as a result we need
  65. // to retrieve the RDF mapping for each node type.
  66. $mapping = rdf_mapping_load('node', $node->type);
  67. // Adds RDFa markup to the title of the node. Because the RDFa markup is
  68. // added to the td tag which might contain HTML code, we specify an
  69. // empty datatype to ensure the value of the title read by the RDFa
  70. // parsers is a plain literal.
  71. $row['title'] += rdf_rdfa_attributes($mapping['title']) + array('datatype' => '');
  72. // Annotates the td tag containing the author of the node.
  73. $row['author'] += rdf_rdfa_attributes($mapping['uid']);
  74. // Annotates the td tag containing the number of replies. We add the
  75. // content attribute to ensure that only the comment count is used as
  76. // the value for 'num_replies'. Otherwise, other text such as a link
  77. // to the number of new comments could be included in the 'num_replies'
  78. // value.
  79. $row['replies'] += rdf_rdfa_attributes($mapping['comment_count']);
  80. $row['replies'] += array('content' => $node->comment_count);
  81. // If the node has no comments, we assume the node itself was modified
  82. // and apply 'changed' in addition to 'last_activity'. If there are
  83. // comments present, we cannot infer whether the node itself was
  84. // modified or a comment was posted, so we use only 'last_activity'.
  85. $mapping_last_activity = rdf_rdfa_attributes($mapping['last_activity'], $node->last_activity);
  86. if ($node->comment_count == 0) {
  87. $mapping_changed = rdf_rdfa_attributes($mapping['changed'], $node->last_activity);
  88. $mapping_last_activity['property'] = array_merge($mapping_last_activity['property'], $mapping_changed['property']);
  89. }
  90. $row['last updated'] += $mapping_last_activity;
  91. // We need to add the about attribute on the tr tag to specify which
  92. // node the RDFa annotations above apply to. We move the content of
  93. // $row to a 'data' sub array so we can specify attributes for the row.
  94. $row = array('data' => $row);
  95. $row['about'] = url('node/' . $node->nid);
  96. }
  97. $rows[] = $row;
  98. }
  99. }
  100. $page['tracker'] = array(
  101. '#rows' => $rows,
  102. '#header' => array(t('Type'), t('Title'), t('Author'), t('Replies'), t('Last updated')),
  103. '#theme' => 'table',
  104. '#empty' => t('No content available.'),
  105. '#attached' => array(
  106. 'css' => array(drupal_get_path('module', 'tracker') . '/tracker.css' => array()),
  107. ),
  108. );
  109. $page['pager'] = array(
  110. '#theme' => 'pager',
  111. '#weight' => 10,
  112. );
  113. $page['#sorted'] = TRUE;
  114. return $page;
  115. }