You are here

function views_handler_field_node_new_comments::pre_render in Views (for Drupal 7) 6.2

Same name and namespace in other branches
  1. 6.3 modules/comment/views_handler_field_node_new_comments.inc \views_handler_field_node_new_comments::pre_render()
  2. 7.3 modules/comment/views_handler_field_node_new_comments.inc \views_handler_field_node_new_comments::pre_render()

Run before any fields are rendered.

This gives the handlers some time to set up before any handler has been rendered.

Parameters

$values: An array of all objects returned from the query.

Overrides views_handler_field::pre_render

File

modules/comment/views_handler_field_node_new_comments.inc, line 48

Class

views_handler_field_node_new_comments
Field handler to display the number of new comments

Code

function pre_render(&$values) {
  global $user;
  if (!$user->uid || empty($values)) {
    return;
  }
  $nids = array();
  $ids = array();
  foreach ($values as $id => $result) {
    $nids[] = $result->{$this->aliases['nid']};
    $values[$id]->{$this->field_alias} = 0;

    // Create a reference so we can find this record in the values again.
    if (empty($ids[$result->{$this->aliases['nid']}])) {
      $ids[$result->{$this->aliases['nid']}] = array();
    }
    $ids[$result->{$this->aliases['nid']}][] = $id;
  }
  if ($nids) {
    $result = db_query("SELECT n.nid, COUNT(c.cid) as num_comments FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid LEFT JOIN {history} h ON h.nid = n.nid AND h.uid = %d WHERE n.nid IN (" . implode(', ', $nids) . ") AND c.timestamp > GREATEST(COALESCE(h.timestamp, %d), %d) AND c.status = %d GROUP BY n.nid  ", $user->uid, NODE_NEW_LIMIT, NODE_NEW_LIMIT, COMMENT_PUBLISHED);
    while ($node = db_fetch_object($result)) {
      foreach ($ids[$node->nid] as $id) {
        $values[$id]->{$this->field_alias} = $node->num_comments;
      }
    }
  }
}