views_handler_field_privatemsg_link.inc

  1. 1 views/views_handler_field_privatemsg_link.inc
  2. 7-1 views/views_handler_field_privatemsg_link.inc
  3. 7-2 views/views_handler_field_privatemsg_link.inc
  4. 6-2 views/views_handler_field_privatemsg_link.inc

Contains the privatemsg link views field handler.

File

views/views_handler_field_privatemsg_link.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains the privatemsg link views field handler.
  5. */
  6. /**
  7. * Provides a configurable link to the new message form for a specific user.
  8. */
  9. class views_handler_field_privatemsg_link extends views_handler_field {
  10. /**
  11. * Add uid as a additional field.
  12. */
  13. function construct() {
  14. parent::construct();
  15. $this->additional_fields['uid'] = 'uid';
  16. }
  17. /**
  18. * Define our additional configuration setting.
  19. */
  20. function option_definition() {
  21. $options = parent::option_definition();
  22. $options['text'] = array('default' => t('Send message'), 'translatable' => TRUE);
  23. $options['subject'] = array('default' => '', 'translatable' => TRUE);
  24. $options['return'] = array('default' => TRUE, 'translatable' => FALSE);
  25. $options['custom_destination'] = array('default' => '', 'translatable' => FALSE);
  26. return $options;
  27. }
  28. /**
  29. * Define the configuration form for our textfield.
  30. */
  31. function options_form(&$form, &$form_state) {
  32. $form['label'] = array(
  33. '#type' => 'textfield',
  34. '#title' => t('Label'),
  35. '#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
  36. '#description' => t('The label for this field that will be displayed to end users if the style requires it.'),
  37. );
  38. $form['text'] = array(
  39. '#type' => 'textfield',
  40. '#title' => t('Text to display'),
  41. '#default_value' => isset($this->options['text']) ? $this->options['text'] : '',
  42. '#description' => t('Define the text to use for the link title. You can use replacement tokens to insert any existing field output.'),
  43. );
  44. $form['subject'] = array(
  45. '#type' => 'textfield',
  46. '#title' => t('Pre-filled subject'),
  47. '#default_value' => isset($this->options['subject']) ? $this->options['subject'] : '',
  48. '#description' => t('Define the subject that will be pre-filled in the send message form. You can use replacement tokens to insert any existing field output.'),
  49. );
  50. $form['return'] = array(
  51. '#type' => 'checkbox',
  52. '#title' => t('Return to view after message was sent.'),
  53. '#default_value' => $this->options['return'],
  54. '#description' => t('Should the user be redirected back to the current view when the message was sent.'),
  55. );
  56. $form['custom_destination'] = array(
  57. '#type' => 'textfield',
  58. '#title' => t('Custom destination'),
  59. '#default_value' => $this->options['custom_destination'],
  60. '#description' => t('If non-empty, users will be forwarded to the given url. You can use replacement tokens to insert any existing field output.'),
  61. '#states' => array(
  62. 'visible' => array(
  63. "input[name='options[return]']" => array('checked' => TRUE),
  64. ),
  65. ),
  66. );
  67. // Get a list of the available fields and arguments for token replacement.
  68. $options = array();
  69. foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
  70. $options[t('Fields')]["[$field]"] = $handler->ui_name();
  71. // We only use fields up to (and including) this one.
  72. if ($field == $this->options['id']) {
  73. break;
  74. }
  75. }
  76. $count = 0; // This lets us prepare the key as we want it printed.
  77. foreach ($this->view->display_handler->get_handlers('argument') as $handler) {
  78. $options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->ui_name()));
  79. $options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->ui_name()));
  80. }
  81. // Add documentation about the new message token. Note that this is not
  82. // a real token that will be replaced but it is handled in
  83. // privatemsg_send_submit().
  84. $options[t('Privatemsg')]['[new-message]'] = t('This will redirect to the newly sent message.');
  85. $this->document_self_tokens($options[t('Fields')]);
  86. // Default text.
  87. $output = t('<p>You must add some additional fields to this display before using this field. These fields may be marked as <em>Exclude from display</em> if you prefer. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');
  88. // We have some options, so make a list.
  89. if (!empty($options)) {
  90. $output = t('<p>The following substitution patterns are available for this display. Use the pattern shown on the left to display the value indicated on the right. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');
  91. foreach (array_keys($options) as $type) {
  92. if (!empty($options[$type])) {
  93. $items = array();
  94. foreach ($options[$type] as $key => $value) {
  95. $items[] = $key . ' == ' . $value;
  96. }
  97. $output .= theme('item_list',
  98. array(
  99. 'items' => $items,
  100. 'type' => $type
  101. ));
  102. }
  103. }
  104. }
  105. $form['help'] = array(
  106. '#id' => 'views-tokens-help',
  107. '#markup' => '<div><fieldset id="views-tokens-help"><legend>' . t('Replacement patterns') . '</legend>' . $output . '</fieldset></div>',
  108. );
  109. }
  110. /**
  111. * Renders our field, displays a link if the user is allowed to.
  112. */
  113. function render($values) {
  114. if (isset($values->{$this->aliases['uid']})) {
  115. $uid = $values->{$this->aliases['uid']};
  116. }
  117. else {
  118. return '';
  119. }
  120. $text = t('Send message');
  121. if (!empty($this->options['text'])) {
  122. $tokens = $this->get_render_tokens($this);
  123. $text = strip_tags(strtr($this->options['text'], $tokens));
  124. }
  125. $subject = NULL;
  126. if (!empty($this->options['subject'])) {
  127. $tokens = $this->get_render_tokens($this);
  128. $subject = strip_tags(strtr($this->options['subject'], $tokens));
  129. }
  130. $options = array();
  131. if ($this->options['return']) {
  132. if (!empty($this->options['custom_destination'])) {
  133. $tokens = $this->get_render_tokens($this);
  134. $destination = strip_tags(strtr($this->options['custom_destination'], $tokens));
  135. $options['query'] = array('destination' => $destination);
  136. }
  137. else {
  138. $options['query'] = drupal_get_destination();
  139. }
  140. }
  141. $data = '';
  142. if (($recipient = user_load($uid)) && ($url = privatemsg_get_link(array($recipient), NULL, $subject))) {
  143. $data = l($text, $url, $options);
  144. }
  145. return $data;
  146. }
  147. /**
  148. * Only display the column for users with the appropriate permission.
  149. */
  150. function access() {
  151. return privatemsg_user_access('write privatemsg');
  152. }
  153. /**
  154. * Just do some basic checks, don't add "privatemsg_link" as field.
  155. */
  156. function query() {
  157. $this->ensure_my_table();
  158. $this->add_additional_fields();
  159. }
  160. }

Classes

Namesort descending Description
views_handler_field_privatemsg_link Provides a configurable link to the new message form for a specific user.