views_handler_field_node_new_comments.inc

  1. 3.x modules/comment/views_handler_field_node_new_comments.inc
  2. 2.x modules/comment/views_handler_field_node_new_comments.inc

Definition of views_handler_field_node_new_comments.

File

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