views_handler_field_node_new_comments.inc 3.8 KB

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