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