[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/views/modules/comment/ -> views_handler_field_node_new_comments.inc (source)

   1  <?php
   2  
   3  /**
   4   * Field handler to display the number of new comments
   5   */
   6  class views_handler_field_node_new_comments extends views_handler_field_numeric {
   7    function init(&$view, $options) {
   8      parent::init($view, $options);
   9      
  10      // translate an older setting:
  11      if (!empty($options['no_empty'])) {
  12        $this->options['hide_empty'] = TRUE;
  13        unset($this->options['no_empty']);
  14      }
  15    }
  16  
  17    function construct() {
  18      parent::construct();
  19      $this->additional_fields['nid'] = 'nid';
  20      $this->additional_fields['type'] = 'type';
  21      $this->additional_fields['comment_count'] = array('table' => 'node_comment_statistics', 'field' => 'comment_count');
  22    }
  23  
  24    function option_definition() {
  25      $options = parent::option_definition();
  26  
  27      $options['link_to_comment'] = array('default' => TRUE);
  28  
  29      return $options;
  30    }
  31  
  32    function options_form(&$form, &$form_state) {
  33      parent::options_form($form, $form_state);
  34      $form['link_to_comment'] = array(
  35        '#title' => t('Link this field to new comments'),
  36        '#description' => t('This will override any other link you have set.'),
  37        '#type' => 'checkbox',
  38        '#default_value' => $this->options['link_to_comment'],
  39      );
  40    }
  41  
  42    function query() {
  43      $this->ensure_my_table();
  44      $this->add_additional_fields();
  45      $this->field_alias = $this->table . '_' . $this->field;
  46    }
  47  
  48    function pre_render(&$values) {
  49      global $user;
  50      if (!$user->uid || empty($values)) {
  51        return;
  52      }
  53  
  54      $nids = array();
  55      $ids = array();
  56      foreach ($values as $id => $result) {
  57        $nids[] = $result->{$this->aliases['nid']};
  58        $values[$id]->{$this->field_alias} = 0;
  59        // Create a reference so we can find this record in the values again.
  60        if (empty($ids[$result->{$this->aliases['nid']}])) {
  61          $ids[$result->{$this->aliases['nid']}] = array();
  62        }
  63        $ids[$result->{$this->aliases['nid']}][] = $id;
  64      }
  65  
  66      if ($nids) {
  67        $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);
  68  
  69        while ($node = db_fetch_object($result)) {
  70          foreach ($ids[$node->nid] as $id) {
  71            $values[$id]->{$this->field_alias} = $node->num_comments;
  72          }
  73        }
  74  
  75      }
  76    }
  77  
  78    function render_link($data, $values) {
  79      if (!empty($this->options['link_to_comment']) && $data !== NULL && $data !== '') {
  80        $node = new stdClass();
  81        $node->nid = $values->{$this->aliases['nid']};
  82        $node->type = $values->{$this->aliases['type']};
  83        $this->options['alter']['make_link'] = TRUE;
  84        $this->options['alter']['path'] = 'node/' . $node->nid;
  85        $this->options['alter']['query'] = comment_new_page_count($values->{$this->aliases['comment_count']}, $values->{$this->field_alias}, $node);
  86        $this->options['alter']['fragment'] = 'new';
  87      }
  88  
  89      return $data;
  90    }
  91  
  92    function render($values) {
  93      if (!empty($values->{$this->field_alias})) {
  94        return $this->render_link(parent::render($values), $values);
  95      }
  96      else {
  97        $this->options['alter']['make_link'] = FALSE;
  98      }
  99    }
 100  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7