1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Definition of views_handler_field_node_new_comments.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Field handler to display the number of new comments.
|
10
|
*
|
11
|
* @ingroup views_field_handlers
|
12
|
*/
|
13
|
class views_handler_field_node_new_comments extends views_handler_field_numeric {
|
14
|
|
15
|
/**
|
16
|
* {@inheritdoc}
|
17
|
*/
|
18
|
public function init(&$view, &$options) {
|
19
|
parent::init($view, $options);
|
20
|
|
21
|
// Translate an older setting.
|
22
|
if (!empty($options['no_empty'])) {
|
23
|
$this->options['hide_empty'] = TRUE;
|
24
|
unset($this->options['no_empty']);
|
25
|
}
|
26
|
}
|
27
|
|
28
|
/**
|
29
|
* {@inheritdoc}
|
30
|
*/
|
31
|
public function construct() {
|
32
|
parent::construct();
|
33
|
$this->additional_fields['nid'] = 'nid';
|
34
|
$this->additional_fields['type'] = 'type';
|
35
|
$this->additional_fields['comment_count'] = array('table' => 'node_comment_statistics', 'field' => 'comment_count');
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* {@inheritdoc}
|
40
|
*/
|
41
|
public function option_definition() {
|
42
|
$options = parent::option_definition();
|
43
|
|
44
|
$options['link_to_comment'] = array('default' => TRUE, 'bool' => TRUE);
|
45
|
|
46
|
return $options;
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* {@inheritdoc}
|
51
|
*/
|
52
|
public function options_form(&$form, &$form_state) {
|
53
|
$form['link_to_comment'] = array(
|
54
|
'#title' => t('Link this field to new comments'),
|
55
|
'#description' => t("Enable to override this field's links."),
|
56
|
'#type' => 'checkbox',
|
57
|
'#default_value' => $this->options['link_to_comment'],
|
58
|
);
|
59
|
|
60
|
parent::options_form($form, $form_state);
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* {@inheritdoc}
|
65
|
*/
|
66
|
public function query() {
|
67
|
$this->ensure_my_table();
|
68
|
$this->add_additional_fields();
|
69
|
$this->field_alias = $this->table . '_' . $this->field;
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* {@inheritdoc}
|
74
|
*/
|
75
|
public function pre_render(&$values) {
|
76
|
global $user;
|
77
|
if (!$user->uid || empty($values)) {
|
78
|
return;
|
79
|
}
|
80
|
|
81
|
$nids = array();
|
82
|
$ids = array();
|
83
|
foreach ($values as $id => $result) {
|
84
|
$nids[] = $result->{$this->aliases['nid']};
|
85
|
$values[$id]->{$this->field_alias} = 0;
|
86
|
// Create a reference so we can find this record in the values again.
|
87
|
if (empty($ids[$result->{$this->aliases['nid']}])) {
|
88
|
$ids[$result->{$this->aliases['nid']}] = array();
|
89
|
}
|
90
|
$ids[$result->{$this->aliases['nid']}][] = $id;
|
91
|
}
|
92
|
|
93
|
if ($nids) {
|
94
|
$result = db_query("SELECT n.nid, COUNT(c.cid) as num_comments FROM {node} n INNER JOIN {comment} c ON n.nid = c.nid
|
95
|
LEFT JOIN {history} h ON h.nid = n.nid AND h.uid = :h_uid WHERE n.nid IN (:nids)
|
96
|
AND c.changed > GREATEST(COALESCE(h.timestamp, :timestamp), :timestamp) AND c.status = :status GROUP BY n.nid ", array(
|
97
|
':status' => COMMENT_PUBLISHED,
|
98
|
':h_uid' => $user->uid,
|
99
|
':nids' => $nids,
|
100
|
':timestamp' => NODE_NEW_LIMIT,
|
101
|
));
|
102
|
|
103
|
foreach ($result as $node) {
|
104
|
foreach ($ids[$node->nid] as $id) {
|
105
|
$values[$id]->{$this->field_alias} = $node->num_comments;
|
106
|
}
|
107
|
}
|
108
|
}
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* {@inheritdoc}
|
113
|
*/
|
114
|
public function render_link($data, $values) {
|
115
|
if (!empty($this->options['link_to_comment']) && $data !== NULL && $data !== '') {
|
116
|
$node = new stdClass();
|
117
|
$node->nid = $this->get_value($values, 'nid');
|
118
|
$node->type = $this->get_value($values, 'type');
|
119
|
$this->options['alter']['make_link'] = TRUE;
|
120
|
$this->options['alter']['path'] = 'node/' . $node->nid;
|
121
|
$this->options['alter']['query'] = comment_new_page_count($this->get_value($values, 'comment_count'), $this->get_value($values), $node);
|
122
|
$this->options['alter']['fragment'] = 'new';
|
123
|
}
|
124
|
|
125
|
return $data;
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* {@inheritdoc}
|
130
|
*/
|
131
|
public function render($values) {
|
132
|
$value = $this->get_value($values);
|
133
|
if (!empty($value)) {
|
134
|
return $this->render_link(parent::render($values), $values);
|
135
|
}
|
136
|
else {
|
137
|
$this->options['alter']['make_link'] = FALSE;
|
138
|
}
|
139
|
}
|
140
|
|
141
|
}
|