summaryrefslogtreecommitdiff
path: root/modules/tracker/tracker.pages.inc
blob: e99965927bfe2a3a8fce4413c092ee8b43996d5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
// $Id$

/**
 * @file
 * User page callbacks for the tracker module.
 */


/**
 * Menu callback. Prints a listing of active nodes on the site.
 */
function tracker_page($account = NULL, $set_title = FALSE) {
  // Add CSS
  drupal_add_css(drupal_get_path('module', 'tracker') . '/tracker.css', array('preprocess' => FALSE));

  $header = array(
    t('Type'),
    t('Post'),
    t('Author'),
    t('Replies'),
    t('Last updated')
  );

  // TODO: These queries are very expensive, see http://drupal.org/node/105639
  $query = db_select('node', 'n', array('target' => 'slave'))
    ->fields('n', array('nid', 'title', 'type', 'changed', 'uid'));
  $query->join('users', 'u', 'n.uid = u.uid');
  $query->addField('u', 'name');
  $query->join('node_comment_statistics', 'l', 'n.nid = l.nid');
  $query->addExpression('GREATEST(n.changed, l.last_comment_timestamp)', 'last_updated');
  $query->addField('l', 'comment_count');
  $query->orderBy('last_updated', 'DESC');

  if ($account) {
    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
      // here and not in the menu definition.
      drupal_set_title($account->name);
    }
    $query->leftJoin('comment', 'c', 'n.nid = c.nid AND (c.status = :status OR c.status IS NULL)', array(':status' => COMMENT_PUBLISHED));
    $query->condition(db_or()
      ->condition('n.uid', $account->uid)
      ->condition('c.uid', $account->uid)
    );
  }

  $query->condition('n.status', 1);
  $query->distinct();
  $query->extend('PagerDefault')->extend('TableSort')
    ->limit(25, 0)
    ->setHeader($header);

  $result = $query->execute();

  $rows = array();
  foreach ($result as $node) {
    // Determine the number of comments:
    $comments = 0;
    if ($node->comment_count) {
      $comments = $node->comment_count;

      if ($new = comment_num_new($node->nid)) {
        $comments .= '<br />';
        $comments .= l(format_plural($new, '1 new', '@count new'), "node/$node->nid", array('query' => comment_new_page_count($node->comment_count, $new, $node), 'fragment' => 'new'));
      }
    }

    $rows[] = array(
      check_plain(node_get_types('name', $node->type)),
      l($node->title, "node/$node->nid") . ' ' . theme('mark', node_mark($node->nid, $node->changed)),
      theme('username', $node),
      array('class' => 'replies', 'data' => $comments),
      t('!time ago', array('!time' => format_interval(REQUEST_TIME - $node->last_updated)))
    );
  }

  if (!$rows) {
    $rows[] = array(array('data' => t('No posts available.'), 'colspan' => '5'));
  }

  $output = '<div id="tracker">';
  $output .= theme('table', $header, $rows);
  $output .= theme('pager', NULL);
  $output .= '</div>';

  return $output;
}