/[drupal]/contributions/modules/nodecomment/nodecomment.views.inc
ViewVC logotype

Contents of /contributions/modules/nodecomment/nodecomment.views.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Thu Sep 25 17:58:03 2008 UTC (14 months ago) by sirkitree
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +35 -67 lines
File MIME type: text/x-php
setting HEAD to the same as DRUPAL-6--1-2-RC1

#310619: applying patch provided by wmostry
1 <?php
2 // $Id: nodecomment.views.inc,v 1.1.2.1 2008/07/22 20:38:33 sirkitree Exp $
3 /**
4 * @file
5 * Provide views data and handlers for nodecomment.module
6 */
7
8 /**
9 * @defgroup views_nodecomment_module nodecomment.module handlers
10 *
11 * Includes the tables 'node', 'node_comments' and 'node_comment_statistics'.
12 * @{
13 */
14
15 /**
16 * Implementation of hook_views_data()
17 */
18 function nodecomment_views_data() {
19 // since comments are nodes we keep with the Node group
20 $data['node_comments']['table']['group'] = t('Node');
21
22 // ----------------------------------------------------------------
23 // node table -- basic table information.
24
25 $data['node_comments']['table']['join'] = array(
26 // node_comments links to node
27 'node' => array(
28 'left_field' => 'nid',
29 'field' => 'cid',
30 ),
31 );
32
33 // ----------------------------------------------------------------
34 // node_comments table -- fields
35
36 $data['node_comments']['cid'] = array(
37 'title' => t('Nid'),
38 'help' => t('The node ID of the comment.'),
39 'argument' => array(
40 'handler' => 'views_handler_argument_node_nid',
41 'name field' => 'title',
42 'numeric' => TRUE,
43 'validate type' => 'nid',
44 ),
45 'filter' => array(
46 'handler' => 'views_handler_filter_numeric',
47 ),
48 'sort' => array(
49 'handler' => 'views_handler_sort',
50 ),
51 );
52
53 $data['node_comments']['nid'] = array(
54 'title' => t('Original Parent Nid'),
55 'help' => t('The original node the comment is a reply to.'),
56 'relationship' => array(
57 'base' => 'node',
58 'field' => 'nid',
59 'handler' => 'views_handler_relationship',
60 'label' => t('Node'),
61 ),
62 );
63
64 $data['node_comments']['pid'] = array(
65 'title' => t('Parent Nid'),
66 'help' => t('The node of the parent comment. Could be another comment.'),
67 'field' => array(
68 'handler' => 'views_handler_field',
69 ),
70 'relationship' => array(
71 'title' => t('Parent comment'),
72 'help' => t('The parent comment.'),
73 'base' => 'comments',
74 'field' => 'cid',
75 'handler' => 'views_handler_relationship',
76 'label' => t('Parent comment'),
77 ),
78 );
79
80 $data['node_comments']['name'] = array(
81 'title' => t('Author'),
82 'help' => t('The name of the poster.'),
83 'field' => array(
84 'handler' => 'views_handler_field_username_comment',
85 'click sortable' => TRUE,
86 ),
87 'filter' => array(
88 'handler' => 'views_handler_filter_string',
89 ),
90 'sort' => array(
91 'handler' => 'views_handler_sort',
92 ),
93 'argument' => array(
94 'handler' => 'views_handler_argument_string',
95 ),
96 );
97
98 $data['node_comments']['homepage'] = array(
99 'title' => t("Author's website"),
100 'help' => t("The website address of the comment's author. Can be a link. The homepage can also be linked with the Name field. Will be empty if posted by a registered user."),
101 'field' => array(
102 'handler' => 'views_handler_field_url',
103 'click sortable' => TRUE,
104 ),
105 'filter' => array(
106 'handler' => 'views_handler_filter_string',
107 ),
108 'sort' => array(
109 'handler' => 'views_handler_sort',
110 ),
111 'argument' => array(
112 'handler' => 'views_handler_argument_string',
113 ),
114 );
115
116 $data['node_comments']['thread'] = array(
117 'field' => array(
118 'title' => t('Depth'),
119 'help' => t('Display the depth of the comment if it is threaded.'),
120 'handler' => 'views_handler_field_comment_depth',
121 ),
122 'sort' => array(
123 'title' => t('Thread'),
124 'help' => t('Sort by the threaded order. This will keep child comments together with their parents.'),
125 'handler' => 'views_handler_sort_comment_thread',
126 ),
127 );
128
129 // link to reply to comment
130 $data['node_comments']['replyto_comment'] = array(
131 'field' => array(
132 'title' => t('Reply-to link'),
133 'help' => t('Provide a simple link to reply to the comment.'),
134 'handler' => 'views_handler_field_comment_link_reply',
135 ),
136 );
137
138 return $data;
139 }
140
141 /**
142 * Field handler to allow linking to a user account or homepage
143 */
144 class views_handler_field_username_comment extends views_handler_field {
145 /**
146 * Override init function to add uid and homepage fields.
147 */
148 function init(&$view, &$data) {
149 parent::init($view, $data);
150 $this->additional_fields['uid'] = 'uid';
151 $this->additional_fields['homepage'] = 'homepage';
152 }
153
154 function option_definition() {
155 $options = parent::option_definition();
156 $options['link_to_user'] = array('default' => TRUE);
157 return $options;
158 }
159
160 function options_form(&$form, &$form_state) {
161 parent::options_form($form, $form_state);
162 $form['link_to_user'] = array(
163 '#title' => t("Link this field to its user or an author's homepage"),
164 '#type' => 'checkbox',
165 '#default_value' => $this->options['link_to_user'],
166 );
167 }
168
169 function render_link($data, $values) {
170 $account->uid = $values->{$this->aliases['uid']};
171 $account->name = $values->{$this->field_alias};
172 $account->homepage = $values->{$this->aliases['homepage']};
173
174 if (!empty($this->options['link_to_user'])) {
175 return theme('username', $account);
176 }
177 else {
178 return $data;
179 }
180 }
181
182 function render($values) {
183 return $this->render_link(check_plain($values->{$this->field_alias}), $values);
184 }
185
186 }
187
188 /**
189 * Field handler to display the depth of a comment
190 */
191 class views_handler_field_comment_depth extends views_handler_field {
192 /**
193 * Work out the depth of this comment
194 */
195 function render($values) {
196 return count(explode('.', $values->comments_thread)) - 1;
197 }
198 }
199
200 /**
201 * Sort handler for ordering by thread
202 */
203 class views_handler_sort_comment_thread extends views_handler_sort {
204 function query() {
205 $this->ensure_my_table();
206
207 //Read comment_render() in comment.module for an explanation of the
208 //thinking behind this sort.
209 if ($this->options['order'] == 'DESC') {
210 $this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
211 }
212 else {
213 $alias = $this->table_alias . '_' . $this->real_field . 'asc';
214 //@todo is this secure?
215 $this->query->add_orderby(NULL, "SUBSTRING({$this->table_alias}.{$this->real_field}, 1, (LENGTH({$this->table_alias}.{$this->real_field}) - 1))", $this->options['order'], $alias);
216 }
217 }
218 }
219
220 /**
221 * Base field handler to present a link.
222 */
223 class views_handler_field_comment_link extends views_handler_field {
224 function construct() {
225 parent::construct();
226 $this->additional_fields['cid'] = 'cid';
227 $this->additional_fields['nid'] = 'nid';
228 }
229
230 function option_definition() {
231 $options = parent::option_definition();
232 $options['text'] = array('default' => '', 'translatable' => TRUE);
233 return $options;
234 }
235
236 function options_form(&$form, &$form_state) {
237 parent::options_form($form, $form_state);
238 $form['text'] = array(
239 '#type' => 'textfield',
240 '#title' => t('Text to display'),
241 '#default_value' => $this->options['text'],
242 );
243 }
244
245 function query() {
246 $this->ensure_my_table();
247 $this->add_additional_fields();
248 }
249
250 function render($values) {
251 $text = !empty($this->options['text']) ? $this->options['text'] : t('view');
252 return l($text, "node/" . $values->{$this->aliases['nid']}, array('html' => TRUE, 'fragment' => "comment-" . $values->{$this->aliases['cid']}));
253 }
254 }
255
256 /**
257 * Field handler to present a link to delete a node.
258 */
259 class views_handler_field_comment_link_reply extends views_handler_field_comment_link {
260 function render($values) {
261 //check for permission to reply to comments
262 if (!user_access('post comments')) {
263 return;
264 }
265 $text = !empty($this->options['text']) ? $this->options['text'] : t('reply');
266 return l($text, "node/add/comment/" . $values->{$this->aliases['nid']} . '/' . $values->{$this->aliases['cid']});
267 }
268 }
269
270 /**
271 * Implementation of hook_views_plugins
272 */
273 function nodecomment_views_plugins() {
274 return array(
275 'style' => array(
276 'nodecomment_threaded' => array(
277 'title' => t('Comments threaded'),
278 'help' => t('Display the comment with a threaded comment view.'),
279 'handler' => 'nodecomment_plugin_style_threaded',
280 'theme' => 'nodecomment_threaded',
281 'uses row plugin' => TRUE,
282 'type' => 'normal',
283 ),
284 ),
285 );
286 }
287
288 class nodecomment_plugin_style_threaded extends views_plugin_style {
289 function render() {
290 $divs = 0;
291 $last_depth = 0;
292 drupal_add_css(drupal_get_path('module', 'comment') .'/comment.css');
293
294 foreach ($this->view->result as $n) {
295 $node = node_load($n->nid);
296 $node->depth = count(explode('.', $node->thread)) - 1;
297
298 if ($node->depth > $last_depth) {
299 $divs++;
300 $output .= '<div class="indented">';
301 $last_depth++;
302 }
303 else {
304 while ($node->depth < $last_depth) {
305 $divs--;
306 $output .= '</div>';
307 $last_depth--;
308 }
309 }
310 $output .= node_view($node);
311 }
312
313 for ($i = 0; $i < $divs; $i++) {
314 $output .= '</div>';
315 }
316
317 return $output;
318 }
319 }
320

  ViewVC Help
Powered by ViewVC 1.1.2