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

File

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