uc_cart_handler_field_cart_user.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @file
  4. * Views field handler.
  5. */
  6. /**
  7. * Field handler: allows linking to a user from a cart id.
  8. *
  9. * TODO: Should we subclass views_handler_field_user rather than
  10. * views_handler_field?
  11. */
  12. class uc_cart_handler_field_cart_user extends views_handler_field {
  13. /**
  14. * Overrides init function to provide generic option to link to user.
  15. */
  16. function init(&$view, &$data) {
  17. parent::init($view, $data);
  18. if (!empty($this->options['link_to_user'])) {
  19. $this->additional_fields['cart_id'] = 'cart_id';
  20. }
  21. }
  22. /**
  23. * Overrides views_handler::option_definition().
  24. */
  25. function option_definition() {
  26. $options = parent::option_definition();
  27. $options['link_to_user'] = array('default' => TRUE);
  28. $options['overwrite_anonymous'] = array('default' => TRUE);
  29. $options['anonymous_text'] = array('default' => 'Anonymous', 'translatable' => TRUE);
  30. return $options;
  31. }
  32. /**
  33. * Overrides views_handler::options_form().
  34. *
  35. * Provides link to node option.
  36. */
  37. function options_form(&$form, &$form_state) {
  38. parent::options_form($form, $form_state);
  39. $form['link_to_user'] = array(
  40. '#title' => t('Link this field to its user'),
  41. '#description' => t('This will override any other link you have set.'),
  42. '#type' => 'checkbox',
  43. '#default_value' => $this->options['link_to_user'],
  44. );
  45. $form['overwrite_anonymous'] = array(
  46. '#title' => t('Overwrite the value to display for anonymous users'),
  47. '#type' => 'checkbox',
  48. '#default_value' => !empty($this->options['overwrite_anonymous']),
  49. '#description' => t('If selected, you will see a field to enter the text to use for anonymous users.'),
  50. );
  51. $form['anonymous_text'] = array(
  52. '#title' => t('Text to display for anonymous users'),
  53. '#type' => 'textfield',
  54. '#default_value' => $this->options['anonymous_text'],
  55. '#dependency' => array(
  56. 'edit-options-overwrite-anonymous' => array(1),
  57. ),
  58. );
  59. }
  60. /**
  61. *
  62. */
  63. function render_link($data, $values) {
  64. if (!empty($this->options['link_to_user']) &&
  65. user_access('access user profiles') &&
  66. $values->{$this->aliases['cart_id']} &&
  67. $data !== NULL &&
  68. $data !== '') {
  69. $this->options['alter']['make_link'] = TRUE;
  70. $this->options['alter']['path'] = "user/" . $values->{$this->aliases['cart_id']};
  71. }
  72. return $data;
  73. }
  74. /**
  75. * Overrides views_handler_field::render().
  76. */
  77. function render($values) {
  78. $result = db_query('SELECT name FROM {users} u WHERE u.uid = :uid', array(':uid' => $values->{$this->field_alias}))->fetchField();
  79. if ($result) {
  80. return $this->render_link($result, $values);
  81. }
  82. else {
  83. // If the cart belongs to an unauthenticated user
  84. if (!empty($this->options['overwrite_anonymous'])) {
  85. return check_plain($this->options['anonymous_text']);
  86. }
  87. else {
  88. return $values->{$this->field_alias};
  89. }
  90. }
  91. }
  92. }