[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

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

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


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