views_handler_field_ncs_last_comment_name.inc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_ncs_last_comment_name.
  5. */
  6. /**
  7. * Field handler to present the name of the last comment poster.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_field_ncs_last_comment_name extends views_handler_field {
  12. function query() {
  13. // last_comment_name only contains data if the user is anonymous. So we
  14. // have to join in a specially related user table.
  15. $this->ensure_my_table();
  16. // join 'users' to this table via vid
  17. $join = new views_join();
  18. $join->construct('users', $this->table_alias, 'last_comment_uid', 'uid');
  19. $join->extra = array(array('field' => 'uid', 'operator' => '!=', 'value' => '0'));
  20. // ncs_user alias so this can work with the sort handler, below.
  21. // $this->user_table = $this->query->add_relationship(NULL, $join, 'users', $this->relationship);
  22. $this->user_table = $this->query->ensure_table('ncs_users', $this->relationship, $join);
  23. $this->field_alias = $this->query->add_field(NULL, "COALESCE($this->user_table.name, $this->table_alias.$this->field)", $this->table_alias . '_' . $this->field);
  24. $this->user_field = $this->query->add_field($this->user_table, 'name');
  25. $this->uid = $this->query->add_field($this->table_alias, 'last_comment_uid');
  26. }
  27. function option_definition() {
  28. $options = parent::option_definition();
  29. $options['link_to_user'] = array('default' => TRUE, 'bool' => TRUE);
  30. return $options;
  31. }
  32. function render($values) {
  33. if (!empty($this->options['link_to_user'])) {
  34. $account = new stdClass();
  35. $account->name = $this->get_value($values);
  36. $account->uid = $values->{$this->uid};
  37. return theme('username', array(
  38. 'account' => $account
  39. ));
  40. }
  41. else {
  42. return $this->sanitize_value($this->get_value($values));
  43. }
  44. }
  45. }