[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/token/ -> token.rules.inc (source)

   1  <?php
   2  /**
   3   * @file
   4   * Rules integration for the token module.
   5   *
   6   * This provides a token input evaluator, so that token replacements can be used
   7   * in every rules action.
   8   */
   9  
  10  /**
  11   * Implementation of hook_rules_evaluator().
  12   */
  13  function token_rules_evaluator() {
  14    return array(
  15      'token_rules_input_evaluator' => array(
  16        'label' => t('Token replacement patterns'),
  17        'weight' => -5,
  18      ),
  19    );
  20  }
  21  
  22  /**
  23   * Prepares the evalution.
  24   *
  25   * @param $string
  26   *   The string to evaluate later.
  27   * @param $variables
  28   *   An array of available variables.
  29   * @return
  30   *   Arbitrary data, which is passed to the evaluator on evaluation.
  31   *   If NULL is returned the input evaluator will be skipped later.
  32   */
  33  function token_rules_input_evaluator_prepare($string, $variables) {
  34    $used_vars = array();
  35    foreach ($variables as $name => $info) {
  36      if (strpos($string, TOKEN_PREFIX. $name .':') !== FALSE) {
  37        $used_vars[] = $name;
  38      }
  39    }
  40    // Using ':global' instead of 'global' to avoid potential namespace conflicts
  41    // See http://drupal.org/node/932460#comment-3884866
  42    $used_vars[] = ':global';
  43    return $used_vars ? $used_vars : NULL;
  44  }
  45  
  46  /**
  47   * Apply the input evaluator.
  48   *
  49   * @param $text
  50   *   The string for which tokens should be replaced.
  51   * @param $used_vars
  52   *   The used variables as returned from preparation.
  53   * @param $state
  54   *   The current evaluation state of rules.
  55   */
  56  function token_rules_input_evaluator_apply($text, $used_vars, &$state) {
  57    static $token_cache = array();
  58  
  59    if ($used_vars) {
  60      $vars = rules_get_variables(drupal_map_assoc(array_diff($used_vars, array(':global'))), $state);
  61      if ($vars === FALSE) {
  62        //there not all needed variables available!
  63        return FALSE;
  64      }
  65      $vars[':global'] = ':global';
  66  
  67      foreach ($used_vars as $name) {
  68        $type = ($name == ':global') ? 'global' : _token_rules_map_type($state['variables'][$name]->info['type']);
  69        if ($type) {
  70          $token_id = _token_get_id($type, $vars[$name]);
  71          if (isset($token_cache[$token_id]) && $token_cache[$token_id] != $name) {
  72            // this is the same variable in another state
  73            // so we need to flush the token cache to get the fresh values
  74            token_get_values('global', NULL, TRUE);
  75          }
  76  
  77          $text = token_replace($text, $type, $vars[$name], TOKEN_PREFIX. $name .':', TOKEN_SUFFIX);
  78  
  79          // remember that this variable has been used and got cached
  80          $token_cache[$token_id] = $name;
  81        }
  82      }
  83    }
  84  
  85    return $text;
  86  }
  87  
  88  /**
  89   * Map rules types to corresponding token types
  90   */
  91  function _token_rules_map_type($type) {
  92    if (($data_type = rules_get_data_types($type)) && isset($data_type['token type'])) {
  93      return $data_type['token type'];
  94    }
  95    return $type;
  96  }
  97  
  98  /**
  99   * Some token replacement help for the condition/action edit form.
 100   */
 101  function token_rules_input_evaluator_help($variables) {
 102  
 103    $variables[':global'] = array('type' => 'global', 'label' => t('global token'),);
 104  
 105    foreach ($variables as $name => $info) {
 106      $type = _token_rules_map_type($info['type']);
 107      if ($type) {
 108        $form[$name] = array(
 109          '#type' => 'fieldset',
 110          '#title' => t('Replacement patterns for @name', array('@name' => $info['label'])),
 111          '#collapsible' => TRUE,
 112          '#collapsed' => TRUE,
 113        );
 114        $form[$name]['content'] = array(
 115          '#value' => theme('token_help', $type, TOKEN_PREFIX. $name . ':', TOKEN_SUFFIX),
 116        );
 117      }
 118    }
 119    return $form;
 120  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7