Projet

Général

Profil

Paste
Télécharger (19,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / comment.views.inc @ 7547bb19

1
<?php
2

    
3
/**
4
 * @file
5
 * Provide views data and handlers for comment.module.
6
 *
7
 * @ingroup views_module_handlers
8
 */
9

    
10
/**
11
 * Implements hook_views_data().
12
 */
13

    
14
function comment_views_data() {
15
  $data['comments']['moved to'] = 'comment';
16
  $data['comment']['comment']['moved to'] = array('field_data_comment_body', 'comment_body');
17
  $data['comment']['comment']['field']['moved to'] = array('field_data_comment_body', 'comment_body');
18
  // Define the base group of this table. Fields that don't
19
  // have a group defined will go into this field by default.
20
  $data['comment']['table']['group']  = t('Comment');
21

    
22
  $data['comment']['table']['base'] = array(
23
    'field' => 'cid',
24
    'title' => t('Comment'),
25
    'help' => t("Comments are responses to node content."),
26
    'access query tag' => 'comment_access',
27
  );
28
  $data['comment']['table']['entity type'] = 'comment';
29

    
30
  // Provide a "default relationship" to keep older views from choking.
31
  $data['comment']['table']['default_relationship'] = array(
32
    'node' => array(
33
      'table' => 'node',
34
      'field' => 'cid',
35
    ),
36
  );
37

    
38
  // ----------------------------------------------------------------
39
  // Fields
40

    
41
  // subject
42
  $data['comment']['subject'] = array(
43
    'title' => t('Title'),
44
    'help' => t('The title of the comment.'),
45
    'field' => array(
46
      'handler' => 'views_handler_field_comment',
47
      'click sortable' => TRUE,
48
    ),
49
    'filter' => array(
50
      'handler' => 'views_handler_filter_string',
51
    ),
52
    'sort' => array(
53
      'handler' => 'views_handler_sort',
54
    ),
55
    'argument' => array(
56
      'handler' => 'views_handler_argument_string',
57
    ),
58
  );
59

    
60
  // cid
61
  $data['comment']['cid'] = array(
62
    'title' => t('ID'),
63
    'help' => t('The comment ID of the field'),
64
    'field' => array(
65
      'handler' => 'views_handler_field_comment',
66
      'click sortable' => TRUE,
67
    ),
68
    'filter' => array(
69
      'handler' => 'views_handler_filter_numeric',
70
    ),
71
    'sort' => array(
72
      'handler' => 'views_handler_sort',
73
    ),
74
    'argument' => array(
75
      'handler' => 'views_handler_argument_numeric',
76
    ),
77
  );
78

    
79
  // name (of comment author)
80
  $data['comment']['name'] = array(
81
    'title' => t('Author'),
82
    'help' => t("The name of the comment's author. Can be rendered as a link to the author's homepage."),
83
    'field' => array(
84
      'handler' => 'views_handler_field_comment_username',
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
  // homepage
99
  $data['comment']['homepage'] = array(
100
    'title' => t("Author's website"),
101
    'help' => t("The website address of the comment's author. Can be rendered as a link. Will be empty if the author is a registered user."),
102
    'field' => array(
103
      'handler' => 'views_handler_field_url',
104
      'click sortable' => TRUE,
105
    ),
106
    'filter' => array(
107
      'handler' => 'views_handler_filter_string',
108
    ),
109
    'sort' => array(
110
      'handler' => 'views_handler_sort',
111
    ),
112
    'argument' => array(
113
      'handler' => 'views_handler_argument_string',
114
    ),
115
  );
116

    
117
  // hostname
118
  $data['comment']['hostname'] = array(
119
    'title' => t('Hostname'),
120
    'help' => t('Hostname of user that posted the comment.'),
121
    'field' => array(
122
      'handler' => 'views_handler_field',
123
      'click sortable' => TRUE,
124
     ),
125
    'filter' => array(
126
      'handler' => 'views_handler_filter_string',
127
    ),
128
    'sort' => array(
129
      'handler' => 'views_handler_sort',
130
    ),
131
    'argument' => array(
132
      'handler' => 'views_handler_argument_string',
133
    ),
134
  );
135

    
136
  // mail
137
  $data['comment']['mail'] = array(
138
    'title' => t('Mail'),
139
    'help' => t('Email of user that posted the comment. Will be empty if the author is a registered user.'),
140
    'field' => array(
141
      'handler' => 'views_handler_field',
142
      'click sortable' => TRUE,
143
     ),
144
    'filter' => array(
145
      'handler' => 'views_handler_filter_string',
146
    ),
147
    'sort' => array(
148
      'handler' => 'views_handler_sort',
149
    ),
150
    'argument' => array(
151
      'handler' => 'views_handler_argument_string',
152
    ),
153
  );
154

    
155
  // created (when comment was posted)
156
  $data['comment']['created'] = array(
157
    'title' => t('Post date'),
158
    'help' => t('Date and time of when the comment was created.'),
159
    'field' => array(
160
      'handler' => 'views_handler_field_date',
161
      'click sortable' => TRUE,
162
    ),
163
    'sort' => array(
164
      'handler' => 'views_handler_sort_date',
165
    ),
166
    'filter' => array(
167
      'handler' => 'views_handler_filter_date',
168
    ),
169
  );
170

    
171

    
172
  // Language field
173
  if (module_exists('locale')) {
174
    $data['comment']['language'] = array(
175
      'title' => t('Language'),
176
      'help' => t('The language the comment is in.'),
177
      'field' => array(
178
        'handler' => 'views_handler_field_locale_language',
179
        'click sortable' => TRUE,
180
      ),
181
      'filter' => array(
182
        'handler' => 'views_handler_filter_locale_language',
183
      ),
184
      'argument' => array(
185
        'handler' => 'views_handler_argument_locale_language',
186
      ),
187
      'sort' => array(
188
        'handler' => 'views_handler_sort',
189
      ),
190
    );
191
  }
192

    
193
  $data['comments']['timestamp']['moved to'] = array('comment', 'changed');
194
  // changed (when comment was last updated)
195
  $data['comment']['changed'] = array(
196
    'title' => t('Updated date'),
197
    'help' => t('Date and time of when the comment was last updated.'),
198
    'field' => array(
199
      'handler' => 'views_handler_field_date',
200
      'click sortable' => TRUE,
201
    ),
202
    'sort' => array(
203
      'handler' => 'views_handler_sort_date',
204
    ),
205
    'filter' => array(
206
      'handler' => 'views_handler_filter_date',
207
    ),
208
  );
209

    
210
  $data['comments']['timestamp_fulldate']['moved to'] = array('comment', 'changed_fulldata');
211
  $data['comment']['changed_fulldata'] = array(
212
    'title' => t('Created date'),
213
    'help' => t('Date in the form of CCYYMMDD.'),
214
    'argument' => array(
215
      'field' => 'changed',
216
      'handler' => 'views_handler_argument_node_created_fulldate',
217
    ),
218
  );
219

    
220
  $data['comments']['timestamp_year_month']['moved to'] = array('comment', 'changed_year_month');
221
  $data['comment']['changed_year_month'] = array(
222
    'title' => t('Created year + month'),
223
    'help' => t('Date in the form of YYYYMM.'),
224
    'argument' => array(
225
      'field' => 'changed',
226
      'handler' => 'views_handler_argument_node_created_year_month',
227
    ),
228
  );
229

    
230
  $data['comments']['timestamp_year']['moved to'] = array('comment', 'changed_year');
231
  $data['comment']['changed_year'] = array(
232
    'title' => t('Created year'),
233
    'help' => t('Date in the form of YYYY.'),
234
    'argument' => array(
235
      'field' => 'changed',
236
      'handler' => 'views_handler_argument_node_created_year',
237
    ),
238
  );
239

    
240
  $data['comments']['timestamp_month']['moved to'] = array('comment', 'changed_month');
241
  $data['comment']['changed_month'] = array(
242
    'title' => t('Created month'),
243
    'help' => t('Date in the form of MM (01 - 12).'),
244
    'argument' => array(
245
      'field' => 'changed',
246
      'handler' => 'views_handler_argument_node_created_month',
247
    ),
248
  );
249

    
250
  $data['comments']['timestamp_day']['moved to'] = array('comment', 'changed_day');
251
  $data['comment']['changed_day'] = array(
252
    'title' => t('Created day'),
253
    'help' => t('Date in the form of DD (01 - 31).'),
254
    'argument' => array(
255
      'field' => 'changed',
256
      'handler' => 'views_handler_argument_node_created_day',
257
    ),
258
  );
259

    
260
  $data['comments']['timestamp_week']['moved to'] = array('comment', 'changed_week');
261
  $data['comment']['changed_week'] = array(
262
    'title' => t('Created week'),
263
    'help' => t('Date in the form of WW (01 - 53).'),
264
    'argument' => array(
265
      'field' => 'changed',
266
      'handler' => 'views_handler_argument_node_created_week',
267
    ),
268
  );
269

    
270
  // status (approved or not)
271
  $data['comment']['status'] = array(
272
    'title' => t('Approved'),
273
    'help' => t('Whether the comment is approved (or still in the moderation queue).'),
274
    'field' => array(
275
      'handler' => 'views_handler_field_boolean',
276
      'click sortable' => TRUE,
277
      'output formats' => array(
278
        'approved-not-approved' => array(t('Approved'), t('Not Approved')),
279
      ),
280
    ),
281
    'filter' => array(
282
      'handler' => 'views_handler_filter_boolean_operator',
283
      'label' => t('Approved comment'),
284
      'type' => 'yes-no',
285
    ),
286
    'sort' => array(
287
      'handler' => 'views_handler_sort',
288
    ),
289
  );
290

    
291
  // link to view comment
292
  $data['comment']['view_comment'] = array(
293
    'field' => array(
294
      'title' => t('View link'),
295
      'help' => t('Provide a simple link to view the comment.'),
296
      'handler' => 'views_handler_field_comment_link',
297
    ),
298
  );
299

    
300
  // link to edit comment
301
  $data['comment']['edit_comment'] = array(
302
    'field' => array(
303
      'title' => t('Edit link'),
304
      'help' => t('Provide a simple link to edit the comment.'),
305
      'handler' => 'views_handler_field_comment_link_edit',
306
    ),
307
  );
308

    
309
  // link to delete comment
310
  $data['comment']['delete_comment'] = array(
311
    'field' => array(
312
      'title' => t('Delete link'),
313
      'help' => t('Provide a simple link to delete the comment.'),
314
      'handler' => 'views_handler_field_comment_link_delete',
315
    ),
316
  );
317

    
318

    
319
  // link to approve comment
320
  $data['comment']['approve_comment'] = array(
321
    'field' => array(
322
      'title' => t('Approve link'),
323
      'help' => t('Provide a simple link to approve the comment.'),
324
      'handler' => 'views_handler_field_comment_link_approve',
325
    ),
326
  );
327

    
328
  // link to reply to comment
329
  $data['comment']['replyto_comment'] = array(
330
    'field' => array(
331
      'title' => t('Reply-to link'),
332
      'help' => t('Provide a simple link to reply to the comment.'),
333
      'handler' => 'views_handler_field_comment_link_reply',
334
    ),
335
  );
336

    
337
  $data['comment']['thread'] = array(
338
    'field' => array(
339
      'title' => t('Depth'),
340
      'help' => t('Display the depth of the comment if it is threaded.'),
341
      'handler' => 'views_handler_field_comment_depth',
342
    ),
343
    'sort' => array(
344
      'title' => t('Thread'),
345
      'help' => t('Sort by the threaded order. This will keep child comments together with their parents.'),
346
      'handler' => 'views_handler_sort_comment_thread',
347
    ),
348
  );
349

    
350
  $data['comment']['nid'] = array(
351
    'title' => t('Nid'),
352
    'help' => t('The node ID to which the comment is a reply to.'),
353
    'relationship' => array(
354
      'title' => t('Content'),
355
      'help' => t('The content to which the comment is a reply to.'),
356
      'base' => 'node',
357
      'base field' => 'nid',
358
      'handler' => 'views_handler_relationship',
359
      'label' => t('Content'),
360
    ),
361
    'filter' => array(
362
      'handler' => 'views_handler_filter_numeric',
363
    ),
364
    'argument' => array(
365
      'handler' => 'views_handler_argument_numeric',
366
    ),
367
    'field' => array(
368
      'handler' => 'views_handler_field_numeric',
369
    ),
370
  );
371

    
372
  $data['comment']['uid'] = array(
373
    'title' => t('Author uid'),
374
    'help' => t('If you need more fields than the uid add the comment: author relationship'),
375
    'relationship' => array(
376
      'title' => t('Author'),
377
      'help' => t("The User ID of the comment's author."),
378
      'base' => 'users',
379
      'base field' => 'uid',
380
      'handler' => 'views_handler_relationship',
381
      'label' => t('author'),
382
    ),
383
    'filter' => array(
384
      'handler' => 'views_handler_filter_numeric',
385
    ),
386
    'argument' => array(
387
      'handler' => 'views_handler_argument_numeric',
388
    ),
389
    'field' => array(
390
      'handler' => 'views_handler_field_user',
391
    ),
392
  );
393

    
394
  $data['comment']['pid'] = array(
395
    'title' => t('Parent CID'),
396
    'help' => t('The Comment ID of the parent comment.'),
397
    'field' => array(
398
      'handler' => 'views_handler_field',
399
    ),
400
    'relationship' => array(
401
      'title' => t('Parent comment'),
402
      'help' => t('The parent comment.'),
403
      'base' => 'comment',
404
      'base field' => 'cid',
405
      'handler' => 'views_handler_relationship',
406
      'label' => t('Parent comment'),
407
    ),
408
  );
409

    
410
  // ----------------------------------------------------------------------
411
  // node_comment_statistics table
412

    
413
  // define the group
414
  $data['node_comment_statistics']['table']['group']  = t('Content');
415

    
416
  // joins
417
  $data['node_comment_statistics']['table']['join'] = array(
418
    //...to the node table
419
    'node' => array(
420
      'type' => 'INNER',
421
      'left_field' => 'nid',
422
      'field' => 'nid',
423
     ),
424
  );
425

    
426
  // last_comment_timestamp
427
  $data['node_comment_statistics']['last_comment_timestamp'] = array(
428
    'title' => t('Last comment time'),
429
    'help' => t('Date and time of when the last comment was posted.'),
430
    'field' => array(
431
      'handler' => 'views_handler_field_last_comment_timestamp',
432
      'click sortable' => TRUE,
433
    ),
434
    'sort' => array(
435
      'handler' => 'views_handler_sort_date',
436
    ),
437
    'filter' => array(
438
      'handler' => 'views_handler_filter_date',
439
    ),
440
  );
441

    
442
  // last_comment_name (author's name)
443
  $data['node_comment_statistics']['last_comment_name'] = array(
444
    'title' => t("Last comment author"),
445
    'help' => t('The name of the author of the last posted comment.'),
446
    'field' => array(
447
      'handler' => 'views_handler_field_ncs_last_comment_name',
448
      'click sortable' => TRUE,
449
      'no group by' => TRUE,
450
    ),
451
    'sort' => array(
452
      'handler' => 'views_handler_sort_ncs_last_comment_name',
453
      'no group by' => TRUE,
454
    ),
455
  );
456

    
457
  // comment_count
458
  $data['node_comment_statistics']['comment_count'] = array(
459
    'title' => t('Comment count'),
460
    'help' => t('The number of comments a node has.'),
461
    'field' => array(
462
      'handler' => 'views_handler_field_numeric',
463
      'click sortable' => TRUE,
464
    ),
465
    'filter' => array(
466
      'handler' => 'views_handler_filter_numeric',
467
    ),
468
    'sort' => array(
469
      'handler' => 'views_handler_sort',
470
    ),
471
    'argument' => array(
472
      'handler' => 'views_handler_argument',
473
    ),
474
  );
475

    
476
  // last_comment_timestamp
477
  $data['node_comment_statistics']['last_updated'] = array(
478
    'title' => t('Updated/commented date'),
479
    'help' => t('The most recent of last comment posted or node updated time.'),
480
    'field' => array(
481
      'handler' => 'views_handler_field_ncs_last_updated',
482
      'click sortable' => TRUE,
483
      'no group by' => TRUE,
484
    ),
485
    'sort' => array(
486
      'handler' => 'views_handler_sort_ncs_last_updated',
487
      'no group by' => TRUE,
488
    ),
489
    'filter' => array(
490
      'handler' => 'views_handler_filter_ncs_last_updated',
491
    ),
492
  );
493

    
494
  $data['node_comment_statistics']['cid'] = array(
495
    'title' => t('Last comment CID'),
496
    'help' => t('Display the last comment of a node'),
497
    'relationship' => array(
498
      'title' => t('Last Comment'),
499
      'help' => t('The last comment of a node.'),
500
      'group' => t('Comment'),
501
      'base' => 'comment',
502
      'base field' => 'cid',
503
      'handler' => 'views_handler_relationship',
504
      'label' => t('Last Comment'),
505
    ),
506
  );
507

    
508
  // last_comment_uid
509
  $data['node_comment_statistics']['last_comment_uid'] = array(
510
    'title' => t('Last comment uid'),
511
    'help' => t('The User ID of the author of the last comment of a node.'),
512
    'relationship' => array(
513
      'title' => t('Last comment author'),
514
      'base' => 'users',
515
      'base field' => 'uid',
516
      'handler' => 'views_handler_relationship',
517
      'label' => t('Last comment author'),
518
    ),
519
    'filter' => array(
520
      'handler' => 'views_handler_filter_numeric',
521
    ),
522
    'argument' => array(
523
      'handler' => 'views_handler_argument_numeric',
524
    ),
525
    'field' => array(
526
      'handler' => 'views_handler_field_numeric',
527
    ),
528
  );
529

    
530
  return $data;
531
}
532

    
533
/**
534
 * Use views_data_alter to add items to the node table that are
535
 * relevant to comments.
536
 */
537
function comment_views_data_alter(&$data) {
538
  // new comments
539
  $data['node']['new_comments'] = array(
540
    'title' => t('New comments'),
541
    'help' => t('The number of new comments on the node.'),
542
    'field' => array(
543
      'handler' => 'views_handler_field_node_new_comments',
544
      'no group by' => TRUE,
545
    ),
546
  );
547

    
548
  $data['node']['comments_link'] = array(
549
    'field' => array(
550
      'title' => t('Add comment link'),
551
      'help' => t('Display the standard add comment link used on regular nodes, which will only display if the viewing user has access to add a comment.'),
552
      'handler' => 'views_handler_field_comment_node_link',
553
    ),
554
  );
555

    
556
  // Comment status of the node
557
  $data['node']['comment'] = array(
558
    'title' => t('Comment status'),
559
    'help' => t('Whether comments are enabled or disabled on the node.'),
560
    'field' => array(
561
      'handler' => 'views_handler_field_node_comment',
562
      'click sortable' => TRUE,
563
    ),
564
    'sort' => array(
565
      'handler' => 'views_handler_sort',
566
    ),
567
    'filter' => array(
568
      'handler' => 'views_handler_filter_node_comment',
569
    ),
570
  );
571

    
572
  $data['node']['uid_touch'] = array(
573
    'title' => t('User posted or commented'),
574
    'help' => t('Display nodes only if a user posted the node or commented on the node.'),
575
    'argument' => array(
576
      'field' => 'uid',
577
      'name table' => 'users',
578
      'name field' => 'name',
579
      'handler' => 'views_handler_argument_comment_user_uid',
580
      'no group by' => TRUE,
581
    ),
582
    'filter' => array(
583
      'field' => 'uid',
584
      'name table' => 'users',
585
      'name field' => 'name',
586
      'handler' => 'views_handler_filter_comment_user_uid'
587
    ),
588
  );
589

    
590
  $data['node']['cid'] = array(
591
    'title' => t('Comments of the node'),
592
    'help' => t('Relate all comments on the node. This will create 1 duplicate record for every comment. Usually if you need this it is better to create a comment view.'),
593
    'relationship' => array(
594
      'group' => t('Comment'),
595
      'label' => t('Comments'),
596
      'base' => 'comment',
597
      'base field' => 'nid',
598
      'relationship field' => 'nid',
599
      'handler' => 'views_handler_relationship',
600
    ),
601
  );
602

    
603
}
604

    
605
/**
606
 * Implements hook_views_plugins().
607
 */
608
function comment_views_plugins() {
609
  return array(
610
    'module' => 'views',
611
    'row' => array(
612
      'comment' => array(
613
        'title' => t('Comment'),
614
        'help' => t('Display the comment with standard comment view.'),
615
        'handler' => 'views_plugin_row_comment_view',
616
        'theme' => 'views_view_row_comment',
617
        'path' => drupal_get_path('module', 'views') . '/modules/comment', // not necessary for most modules
618
        'base' => array('comment'), // only works with 'comment' as base.
619
        'uses options' => TRUE,
620
        'type' => 'normal',
621
        'help topic' => 'style-comment',
622
      ),
623
      'comment_rss' => array(
624
        'title' => t('Comment'),
625
        'help' => t('Display the comment as RSS.'),
626
        'handler' => 'views_plugin_row_comment_rss',
627
        'theme' => 'views_view_row_rss',
628
        'path' => drupal_get_path('module', 'views') . '/modules/comment', // not necessary for most modules
629
        'base' => array('comment'), // only works with 'comment' as base.
630
        'uses options' => TRUE,
631
        'type' => 'feed',
632
        'help topic' => 'style-comment-rss',
633
      ),
634
    ),
635
  );
636
}
637

    
638
/**
639
 * Template helper for theme_views_view_row_comment
640
 */
641
function template_preprocess_views_view_row_comment(&$vars) {
642
  $options = $vars['options'];
643
  $view = &$vars['view'];
644
  $plugin = &$view->style_plugin->row_plugin;
645
  $comment = $plugin->comments[$vars['row']->{$vars['field_alias']}];
646
  $node = $plugin->nodes[$comment->nid];
647
  // Put the view on the node so we can retrieve it in the preprocess.
648
  $node->view = &$view;
649

    
650
  $build = comment_view_multiple(array($comment->cid => $comment), $node, $plugin->options['view_mode']);
651
  // If we're displaying the comments without links, remove them from the
652
  // renderable array. There is no way to avoid building them in the first
653
  // place (see comment_build_content()).
654
  if (empty($options['links'])) {
655
    foreach ($build as $cid => &$comment_build) {
656
      if (isset($comment_build['links'])) {
657
        unset($comment_build['links']);
658
      }
659
    }
660
  }
661
  $vars['comment'] = drupal_render($build);
662
}