[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/token/ -> token_actions.module (source)

   1  <?php
   2  // $Id: token_actions.module,v 1.4.2.9 2010/03/25 17:49:43 davereid Exp $
   3  
   4  /**
   5   * @file
   6   * The Token Actions module.
   7   *
   8   * The Token Actions module provides ways to use tokens inside of actions.
   9   * Currently it provides the ability to show users a message, send a token-ized
  10   * mail, or redirect a user to a tokenized URL.
  11   *
  12   * @ingroup token
  13   */
  14  
  15  /**
  16   * Implementation of hook_action_info().
  17   */
  18  function token_actions_action_info() {
  19    return array(
  20      'token_actions_message_action' => array(
  21        'type' => 'system',
  22        'description' => t('Display a tokenized message to the user'),
  23        'configurable' => TRUE,
  24        'hooks' => array(
  25          'nodeapi' => array('view', 'insert', 'update', 'delete'),
  26          'comment' => array('view', 'insert', 'update', 'delete'),
  27          'user' => array('view', 'insert', 'update', 'delete', 'login'),
  28          'taxonomy' => array('insert', 'update', 'delete'),
  29        ),
  30      ),
  31      'token_actions_send_email_action' => array(
  32        'description' => t('Send tokenized e-mail'),
  33        'type' => 'system',
  34        'configurable' => TRUE,
  35        'hooks' => array(
  36          'nodeapi' => array('view', 'insert', 'update', 'delete'),
  37          'comment' => array('view', 'insert', 'update', 'delete'),
  38          'user' => array('view', 'insert', 'update', 'delete', 'login'),
  39          'taxonomy' => array('insert', 'update', 'delete'),
  40        )
  41      ),
  42      'token_actions_goto_action' => array(
  43        'description' => t('Redirect to a tokenized URL'),
  44        'type' => 'system',
  45        'configurable' => TRUE,
  46        'hooks' => array(
  47          'nodeapi' => array('view', 'insert', 'update', 'delete'),
  48          'comment' => array('view', 'insert', 'update', 'delete'),
  49          'user' => array('view', 'insert', 'update', 'delete', 'login'),
  50        )
  51      )
  52    );
  53  }
  54  
  55  /**
  56   * Return a form definition so the Send email action can be configured.
  57   *
  58   * @param $context
  59   *   Default values (if we are editing an existing action instance).
  60   * @return
  61   *   Form definition.
  62   */
  63  function token_actions_send_email_action_form($context) {
  64    // Set default values for form.
  65    if (!isset($context['recipient'])) {
  66      $context['recipient'] = '';
  67    }
  68    if (!isset($context['subject'])) {
  69      $context['subject'] = '';
  70    }
  71    if (!isset($context['message'])) {
  72      $context['message'] = '';
  73    }
  74  
  75    $form['recipient'] = array(
  76      '#type' => 'textfield',
  77      '#title' => t('Recipient'),
  78      '#default_value' => $context['recipient'],
  79      '#size' => '20',
  80      '#maxlength' => '254',
  81      '#description' => t('The email address to which the message should be sent.'),
  82    );
  83    $form['subject'] = array(
  84      '#type' => 'textfield',
  85      '#title' => t('Subject'),
  86      '#default_value' => $context['subject'],
  87      '#size' => '20',
  88      '#maxlength' => '254',
  89      '#description' => t('The subject of the message.'),
  90    );
  91    $form['message'] = array(
  92      '#type' => 'textarea',
  93      '#title' => t('Message'),
  94      '#default_value' => $context['message'],
  95      '#cols' => '80',
  96      '#rows' => '20',
  97      '#description' => t('The message that should be sent.'),
  98    );
  99  
 100    $form['help'] = array(
 101      '#type' => 'fieldset',
 102      '#collapsible' => TRUE,
 103      '#collapsed' => TRUE,
 104      '#title' => t('Placeholder tokens'),
 105      '#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."),
 106    );
 107  
 108    $form['help']['tokens'] = array(
 109      '#value' => theme('token_help', 'all'),
 110    );
 111  
 112    return $form;
 113  }
 114  
 115  function token_actions_send_email_action_submit($form, &$form_state) {
 116    // Process the HTML form to store configuration. The keyed array that
 117    // we return will be serialized to the database.
 118    $params = array(
 119      'recipient' => $form_state['values']['recipient'],
 120      'subject'   => $form_state['values']['subject'],
 121      'message'   => $form_state['values']['message'],
 122    );
 123    return $params;
 124  }
 125  
 126  /**
 127   * Implementation of a configurable Drupal action.
 128   * Sends an email.
 129   */
 130  function token_actions_send_email_action($object, $context) {
 131    token_normalize_context($context);
 132    $params['from'] = variable_get('site_mail', ini_get('sendmail_from'));
 133    $recipient = token_replace_multiple($context['recipient'], $context);
 134    $params['subject'] = str_replace(array("\r", "\n"), '', token_replace_multiple($context['subject'], $context));
 135    $params['body'] = token_replace_multiple($context['message'], $context);
 136  
 137    if (drupal_mail('token_actions', 'action_send_email', $recipient, language_default(), $params)) {
 138      watchdog('action', 'Sent email to %recipient', array('%recipient' => $recipient));
 139    }
 140    else {
 141      watchdog('error', 'Unable to send email to %recipient', array('%recipient' => $recipient));
 142    }
 143  }
 144  
 145  function token_actions_mail($key, &$message, $params) {
 146    $message['subject'] = $params['subject'];
 147    $message['body'][] = $params['body'];
 148  }
 149  
 150  function token_actions_message_action_form($context) {
 151    $form['message'] = array(
 152      '#type' => 'textarea',
 153      '#title' => t('Message'),
 154      '#default_value' => isset($context['message']) ? $context['message'] : '',
 155      '#required' => TRUE,
 156      '#rows' => '8',
 157      '#description' => t('The message to be displayed to the current user.'),
 158    );
 159  
 160    $form['help'] = array(
 161      '#type' => 'fieldset',
 162      '#collapsible' => TRUE,
 163      '#collapsed' => TRUE,
 164      '#title' => t('Placeholder tokens'),
 165      '#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."),
 166    );
 167  
 168    $form['help']['tokens'] = array(
 169      '#value' => theme('token_help', 'all'),
 170    );
 171  
 172    return $form;
 173  }
 174  
 175  function token_actions_message_action_submit($form, &$form_state) {
 176    return array('message' => $form_state['values']['message']);
 177  }
 178  
 179  /**
 180   * Implementation of a configurable Drupal action.
 181   * Sends a configurable message to the current user's screen.
 182   */
 183  function token_actions_message_action(&$object, $context = array()) {
 184    token_normalize_context($context);
 185    $context['message'] = token_replace_multiple($context['message'], $context);
 186    drupal_set_message($context['message']);
 187  }
 188  
 189  /**
 190   * Implementation of a configurable Drupal action.
 191   * Redirect user to a URL.
 192   */
 193  function token_actions_goto_action_form($context) {
 194    $form['url'] = array(
 195      '#type' => 'textfield',
 196      '#title' => t('URL'),
 197      '#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.'),
 198      '#default_value' => isset($context['url']) ? $context['url'] : '',
 199      '#required' => TRUE,
 200    );
 201    $form['help'] = array(
 202      '#type' => 'fieldset',
 203      '#collapsible' => TRUE,
 204      '#collapsed' => TRUE,
 205      '#title' => t('Placeholder tokens'),
 206      '#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."),
 207    );
 208  
 209    $form['help']['tokens'] = array(
 210      '#value' => theme('token_help', 'all'),
 211    );
 212  
 213    return $form;
 214  }
 215  
 216  function token_actions_goto_action_submit($form, &$form_state) {
 217    return array(
 218      'url' => $form_state['values']['url']
 219    );
 220  }
 221  
 222  function token_actions_goto_action($object, $context) {
 223    token_normalize_context($context);
 224    drupal_goto(token_replace_multiple($context['url'], $context));
 225  }
 226  
 227  /**
 228   * Load, into the context, the common objects user and node so we can use their
 229   * tokens. Sometimes Trigger, or Actions, load them for us, but sometimes not.
 230   */
 231  function token_normalize_context(&$context) {
 232    $context['global'] = NULL;
 233    if (empty($context['user']) && !empty($context['node'])) {
 234      $context['user'] = user_load(array('uid' => $context['node']->uid));
 235    }
 236    if (empty($context['node']) && !empty($context['comment']) && !empty($context['comment']->nid)) {
 237      $context['node'] = node_load($context['comment']->nid);
 238    }
 239  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7