00001 <?php
00002
00008 class views_handler_field_node_new_comments extends views_handler_field_numeric {
00009 function init(&$view, &$options) {
00010 parent::init($view, $options);
00011
00012
00013 if (!empty($options['no_empty'])) {
00014 $this->options['hide_empty'] = TRUE;
00015 unset($this->options['no_empty']);
00016 }
00017 }
00018
00019 function construct() {
00020 parent::construct();
00021 $this->additional_fields['nid'] = 'nid';
00022 $this->additional_fields['type'] = 'type';
00023 $this->additional_fields['comment_count'] = array('table' => 'node_comment_statistics', 'field' => 'comment_count');
00024 }
00025
00026 function option_definition() {
00027 $options = parent::option_definition();
00028
00029 $options['link_to_comment'] = array('default' => TRUE);
00030
00031 return $options;
00032 }
00033
00034 function options_form(&$form, &$form_state) {
00035 $form['link_to_comment'] = array(
00036 '#title' => t('Link this field to new comments'),
00037 '#description' => t("Enable to override this field's links."),
00038 '#type' => 'checkbox',
00039 '#default_value' => $this->options['link_to_comment'],
00040 );
00041
00042 parent::options_form($form, $form_state);
00043 }
00044
00045 function query() {
00046 $this->ensure_my_table();
00047 $this->add_additional_fields();
00048 $this->field_alias = $this->table . '_' . $this->field;
00049 }
00050
00051 function pre_render(&$values) {
00052 global $user;
00053 if (!$user->uid || empty($values)) {
00054 return;
00055 }
00056
00057 $nids = array();
00058 $ids = array();
00059 foreach ($values as $id => $result) {
00060 $nids[] = $result->{$this->aliases['nid']};
00061 $values[$id]->{$this->field_alias} = 0;
00062
00063 if (empty($ids[$result->{$this->aliases['nid']}])) {
00064 $ids[$result->{$this->aliases['nid']}] = array();
00065 }
00066 $ids[$result->{$this->aliases['nid']}][] = $id;
00067 }
00068
00069 if ($nids) {
00070 $result = db_query("SELECT n.nid, COUNT(c.cid) as num_comments FROM {node} n INNER JOIN {comment} c ON n.nid = c.nid
00071 LEFT JOIN {history} h ON h.nid = n.nid AND h.uid = :h_uid WHERE n.nid IN (:nids)
00072 AND c.changed > GREATEST(COALESCE(h.timestamp, :timestamp), :timestamp) AND c.status = :status GROUP BY n.nid ", array(
00073 ':status' => COMMENT_PUBLISHED,
00074 ':h_uid' => $user->uid,
00075 ':nids' => $nids,
00076 ':timestamp' => NODE_NEW_LIMIT,
00077 ));
00078
00079 foreach ($result as $node) {
00080 foreach ($ids[$node->nid] as $id) {
00081 $values[$id]->{$this->field_alias} = $node->num_comments;
00082 }
00083 }
00084 }
00085 }
00086
00087 function render_link($data, $values) {
00088 if (!empty($this->options['link_to_comment']) && $data !== NULL && $data !== '') {
00089 $node = new stdClass();
00090 $node->nid = $this->get_value($values, 'nid');
00091 $node->type = $this->get_value($values, 'type');
00092 $this->options['alter']['make_link'] = TRUE;
00093 $this->options['alter']['path'] = 'node/' . $node->nid;
00094 $this->options['alter']['query'] = comment_new_page_count($this->get_value($values, 'comment_count'), $this->get_value($values), $node);
00095 $this->options['alter']['fragment'] = 'new';
00096 }
00097
00098 return $data;
00099 }
00100
00101 function render($values) {
00102 $value = $this->get_value($values);
00103 if (!empty($value)) {
00104 return $this->render_link(parent::render($values), $values);
00105 }
00106 else {
00107 $this->options['alter']['make_link'] = FALSE;
00108 }
00109 }
00110 }