summaryrefslogtreecommitdiff
path: root/modules/tracker/tracker.pages.inc
blob: 1466848e5cdf83ff666d7a7fa781053ece1ff437 (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
90
91
<?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) {
  if ($account) {
    $query = db_select('tracker_user', 't')->extend('PagerDefault');
    $query->condition('t.uid', $account->uid);

    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);
    }
  }
  else {
    $query = db_select('tracker_node', 't')->extend('PagerDefault');
  }

  // This array acts as a placeholder for the data selected later
  // while keeping the correct order.
  $nodes = $query
    ->addTag('node_access')
    ->fields('t', array('nid', 'changed'))
    ->condition('t.published', 1)
    ->orderBy('t.changed', 'DESC')
    ->limit(25)
    ->execute()
    ->fetchAllAssoc('nid');

  if (!empty($nodes)) {
    // Now, get the data and put into the placeholder array
    $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)));
    foreach ($result as $node) {
      $node->last_activity = $nodes[$node->nid]->changed;
      $nodes[$node->nid] = $node;
    }

    // Finally display the data
    $rows = array();
    foreach ($nodes 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('fragment' => 'new'));
        }
      }

      $rows[] = array(
        check_plain(node_type_get_name($node->type)),
        l($node->title, 'node/' . $node->nid) . ' ' . theme('mark', node_mark($node->nid, $node->changed)),
        theme('username', $node),
        array('class' => array('replies'), 'data' => $comments),
        t('!time ago', array('!time' => format_interval(REQUEST_TIME - $node->last_activity)))
      );
    }
  }
  else {
    $rows[] = array(array('data' => t('No posts available.'), 'colspan' => '5'));
  }

  $page['tracker'] = array(
    '#rows' => $rows,
    '#header' => array(t('Type'), t('Post'), t('Author'), t('Replies'), t('Last updated')),
    '#theme' => 'table',
    '#attached' => array(
      'css' => array(drupal_get_path('module', 'tracker') . '/tracker.css' => array('preprocess' => FALSE)),
    ),
  );
  $page['pager'] = array(
    '#theme' => 'pager',
    '#quantity' => 25,
    '#weight' => 10,
  );
  $page['#sorted'] = TRUE;

  return $page;
}