source: sipes/modules_contrib/token/token_actions.module @ c43ea01

stableversion-3.0
Last change on this file since c43ea01 was 177a560, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio de modulos contribuidos de drupal

  • Propiedad mode establecida a 100755
File size: 9.6 KB
Línea 
1<?php
2
3/**
4 * @file
5 * The Token Actions module.
6 *
7 * The Token Actions module provides ways to use tokens inside of actions.
8 * Currently it provides the ability to show users a message, send a token-ized
9 * mail, or redirect a user to a tokenized URL.
10 *
11 * @ingroup token
12 */
13
14/**
15 * Implements hook_action_info().
16 */
17function token_actions_action_info() {
18  return array(
19    'token_actions_message_action' => array(
20      'type' => 'system',
21      'description' => t('Display a tokenized message to the user'),
22      'configurable' => TRUE,
23      'hooks' => array(
24        'nodeapi' => array('view', 'insert', 'update', 'delete'),
25        'comment' => array('view', 'insert', 'update', 'delete'),
26        'user' => array('view', 'insert', 'update', 'delete', 'login'),
27        'taxonomy' => array('insert', 'update', 'delete'),
28      ),
29    ),
30    'token_actions_send_email_action' => array(
31      'description' => t('Send tokenized e-mail'),
32      'type' => 'system',
33      'configurable' => TRUE,
34      'hooks' => array(
35        'nodeapi' => array('view', 'insert', 'update', 'delete'),
36        'comment' => array('view', 'insert', 'update', 'delete'),
37        'user' => array('view', 'insert', 'update', 'delete', 'login'),
38        'taxonomy' => array('insert', 'update', 'delete'),
39      )
40    ),
41    'token_actions_goto_action' => array(
42      'description' => t('Redirect to a tokenized URL'),
43      'type' => 'system',
44      'configurable' => TRUE,
45      'hooks' => array(
46        'nodeapi' => array('view', 'insert', 'update', 'delete'),
47        'comment' => array('view', 'insert', 'update', 'delete'),
48        'user' => array('view', 'insert', 'update', 'delete', 'login'),
49      )
50    )
51  );
52}
53
54/**
55 * Implements hook_mail().
56 */
57function token_actions_mail($key, &$message, $params) {
58  $message['subject'] = $params['subject'];
59  $message['body'][] = $params['body'];
60}
61
62/**
63 * Action callback to send a tokenized e-mail.
64 *
65 * @see token_actions_send_email_action_form()
66 * @see token_actions_send_email_action_submit()
67 */
68function token_actions_send_email_action($object, $context) {
69  token_normalize_context($context);
70  $params['from'] = variable_get('site_mail', ini_get('sendmail_from'));
71  $recipient = token_replace_multiple($context['recipient'], $context);
72  $params['subject'] = str_replace(array("\r", "\n"), '', token_replace_multiple($context['subject'], $context));
73  $params['body'] = token_replace_multiple($context['message'], $context);
74
75  if (drupal_mail('token_actions', 'action_send_email', $recipient, language_default(), $params)) {
76    watchdog('action', 'Sent email to %recipient', array('%recipient' => $recipient));
77  }
78  else {
79    watchdog('error', 'Unable to send email to %recipient', array('%recipient' => $recipient));
80  }
81}
82
83/**
84 * Form builder; Prepare a form for a tokenized e-mail action.
85 *
86 * @see token_actions_send_email_action()
87 * @see token_actions_send_email_action_validate()
88 * @see token_actions_send_email_action_submit()
89 */
90function token_actions_send_email_action_form($context) {
91  // Set default values for form.
92  $context += array(
93    'recipient' => '',
94    'subject' => '',
95    'message' => '',
96  );
97
98  $form['recipient'] = array(
99    '#type' => 'textfield',
100    '#title' => t('Recipient'),
101    '#default_value' => $context['recipient'],
102    '#required' => TRUE,
103    '#size' => '20',
104    '#maxlength' => '254',
105    '#description' => t('The email address to which the message should be sent.'),
106    '#element_validate' => array('token_element_validate'),
107    '#token_types' => array('all'),
108  );
109  $form['subject'] = array(
110    '#type' => 'textfield',
111    '#title' => t('Subject'),
112    '#default_value' => $context['subject'],
113    '#size' => '20',
114    '#maxlength' => '254',
115    '#description' => t('The subject of the message.'),
116    '#element_validate' => array('token_element_validate'),
117    '#token_types' => array('all'),
118  );
119  $form['message'] = array(
120    '#type' => 'textarea',
121    '#title' => t('Message'),
122    '#default_value' => $context['message'],
123    '#required' => TRUE,
124    '#cols' => '80',
125    '#rows' => '20',
126    '#description' => t('The message that should be sent.'),
127    '#element_validate' => array('token_element_validate'),
128    '#token_types' => array('all'),
129  );
130
131  $form['help'] = array(
132    '#type' => 'fieldset',
133    '#collapsible' => TRUE,
134    '#collapsed' => TRUE,
135    '#title' => t('Placeholder tokens'),
136    '#description' => t("The following placeholder tokens can be used in to generate the URL path. Some tokens may not be available, depending on the context in which the action is triggered."),
137  );
138  $form['help']['tokens'] = array(
139    '#value' => theme('token_tree', 'all'),
140  );
141
142  return $form;
143}
144
145/**
146 * Validate token_actions_send_email_action form submissions.
147 *
148 * @see token_actions_send_email_action()
149 * @see token_actions_send_email_action_form()
150 * @see token_actions_send_email_action_submit()
151 */
152function token_actions_send_email_action_validate($form, $form_state) {
153  $form_values = $form_state['values'];
154  if (!valid_email_address($form_values['recipient']) && strpos($form_values['recipient'], 'mail') === FALSE) {
155    // We want the literal %mail placeholder to be emphasized in the error message.
156    form_set_error('recipient', t('Enter a valid email address or use a token e-mail address such as %mail.', array('%mail' => '[mail]')));
157  }
158}
159
160/**
161 * Process token_actions_send_email_action form submissions.
162 *
163 * @see token_actions_send_email_action()
164 * @see token_actions_send_email_action_form()
165 * @see token_actions_send_email_action_validate()
166 */
167function token_actions_send_email_action_submit($form, $form_state) {
168  return array(
169    'recipient' => $form_state['values']['recipient'],
170    'subject'   => $form_state['values']['subject'],
171    'message'   => $form_state['values']['message'],
172  );
173}
174
175/**
176 * Action callback to send a message to the current user's screen.
177 *
178 * @see token_actions_message_action_form()
179 * @see token_actions_message_action_submit()
180 */
181function token_actions_message_action(&$object, $context = array()) {
182  token_normalize_context($context);
183  $context['message'] = token_replace_multiple($context['message'], $context);
184  drupal_set_message($context['message']);
185}
186
187/**
188 * Form builder; Prepare a form for a tokenized message action.
189 *
190 * @see token_actions_message_action()
191 * @see token_actions_message_action_submit()
192 */
193function token_actions_message_action_form($context) {
194  $context += array('message' => '');
195
196  $form['message'] = array(
197    '#type' => 'textarea',
198    '#title' => t('Message'),
199    '#default_value' => $context['message'],
200    '#required' => TRUE,
201    '#rows' => '8',
202    '#description' => t('The message to be displayed to the current user.'),
203    '#element_validate' => array('token_element_validate'),
204    '#token_types' => array('all'),
205  );
206
207  $form['help'] = array(
208    '#type' => 'fieldset',
209    '#collapsible' => TRUE,
210    '#collapsed' => TRUE,
211    '#title' => t('Placeholder tokens'),
212    '#description' => t("The following placeholder tokens can be used in the custom message text. Some tokens may not be available, depending on the context in which the action is triggered."),
213  );
214  $form['help']['tokens'] = array(
215    '#value' => theme('token_tree', 'all'),
216  );
217
218  return $form;
219}
220
221/**
222 * Process token_actions_message_action form submissions.
223 *
224 * @see token_actions_message_action()
225 * @see token_actions_message_action_form()
226 */
227function token_actions_message_action_submit($form, $form_state) {
228  return array('message' => $form_state['values']['message']);
229}
230
231/**
232 * Action callback to redirect the user to a tokenized URL.
233 *
234 * @see token_actions_goto_action_form()
235 * @see token_actions_goto_action_submit()
236 */
237function token_actions_goto_action($object, $context) {
238  token_normalize_context($context);
239  drupal_goto(token_replace_multiple($context['url'], $context));
240}
241
242/**
243 * Form builder; Prepare a form for a tokenized redirect action.
244 *
245 * @see token_actions_goto_action()
246 * @see token_actions_goto_action_submit()
247 */
248function token_actions_goto_action_form($context) {
249  $context += array('url' => '');
250
251  $form['url'] = array(
252    '#type' => 'textfield',
253    '#title' => t('URL'),
254    '#description' => t('The URL to which the user should be redirected. This can be an internal URL like node/1234 or an external URL like http://drupal.org.'),
255    '#default_value' => $context['url'],
256    '#required' => TRUE,
257    '#element_validate' => array('token_element_validate'),
258    '#token_types' => array('all'),
259  );
260
261  $form['help'] = array(
262    '#type' => 'fieldset',
263    '#collapsible' => TRUE,
264    '#collapsed' => TRUE,
265    '#title' => t('Placeholder tokens'),
266    '#description' => t("The following placeholder tokens can be used in the URL path. Some tokens may not be available, depending on the context in which the action is triggered."),
267  );
268  $form['help']['tokens'] = array(
269    '#value' => theme('token_tree', 'all'),
270  );
271
272  return $form;
273}
274
275/**
276 * Process token_actions_goto_action form submissions.
277 *
278 * @see token_actions_goto_action()
279 * @see token_actions_goto_action_form()
280 */
281function token_actions_goto_action_submit($form, $form_state) {
282  return array('url' => $form_state['values']['url']);
283}
284
285/**
286 * Normalize an action context for use with token_replace_multiple().
287 */
288function token_normalize_context(&$context) {
289  $context['global'] = NULL;
290
291  if (empty($context['user']) && !empty($context['node'])) {
292    $context['user'] = user_load(array('uid' => $context['node']->uid));
293  }
294  if (empty($context['node']) && !empty($context['comment']) && !empty($context['comment']->nid)) {
295    $context['node'] = node_load($context['comment']->nid);
296  }
297  if (empty($context['user']) && !empty($context['account'])) {
298    $context['user'] = $context['account'];
299  }
300}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.