[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

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

   1  <?php
   2  // $Id: rules_admin.rule_proxy.inc,v 1.1.2.3 2010/11/25 11:14:53 fago Exp $
   3  
   4  
   5  /**
   6   * @file Contains the rules proxy class
   7   */
   8  
   9  /**
  10   * This is a smally proxy for the real rule. It provides some useful operations for the admin UI.
  11   * It builds a small index for the elements of a rule, so that they can be easily identified and modified.
  12   */
  13  class rules_admin_rule_proxy {
  14  
  15    var $_rule;
  16    var $_rule_name;
  17    var $_new_name;
  18    var $_counter;      //used when generating ids
  19    var $_variables;    //variables defined by previous rules
  20    var $map = array(); //stores the id => element map
  21  
  22    /**
  23     * Constructor
  24     *
  25     * @param $rule_name The name of the rule
  26     * @param $rule The rule to create a proxy for.
  27     */
  28    function rules_admin_rule_proxy($rule_name, &$rule) {
  29      $rule += array('#conditions' => array(), '#actions' => array());
  30  
  31      //save a copy of the original rule for later
  32      $this->_rule = $rule;
  33      $this->_rule_name = $rule_name;
  34  
  35      //and the reference
  36      $this->map[0] = &$rule;
  37  
  38      $this->_counter = 0;
  39      $this->_generate_index($rule['#conditions']);
  40      $this->_generate_index($rule['#actions']);
  41    }
  42  
  43    /**
  44     * Gets the referenced rule
  45     */
  46    function &get_rule() {
  47      return $this->get_element(0);
  48    }
  49  
  50    /**
  51     * Gets the rule's name
  52     */
  53    function get_rule_name() {
  54      return $this->_rule_name;
  55    }
  56  
  57    function set_rule_name($name) {
  58      $this->_new_name = $name;
  59    }
  60  
  61    /**
  62     * Gets an element of the referenced rule by id
  63     */
  64    function &get_element($id) {
  65      if (!isset($this->map[$id])) {
  66        $this->map[$id] = FALSE;
  67      }
  68      return $this->map[$id];
  69    }
  70  
  71    /**
  72     * Gets the id of the parent element
  73     */
  74    function get_element_parent_id($id) {
  75      $element = $this->get_element($id);
  76      while ($id > 0) {
  77        $id--;
  78        //get the element and look if it's the parent
  79        $parent = $this->get_element($id);
  80        foreach (element_children($parent) as $key) {
  81          if ($parent[$key] == $element) {
  82            //parent found!
  83            return $id;
  84          }
  85        }
  86      }
  87      return FALSE;
  88    }
  89  
  90    /**
  91     * Saves the changes in the rule and clears the cache if necessary.
  92     */
  93    function save_changes() {
  94      $rule = $this->get_rule();
  95      $orig_rule = $this->_rule;
  96      if ($orig_rule != $rule || isset($this->_new_name)) {
  97        $this->save_rule();
  98        rules_clear_cache();
  99      }
 100    }
 101  
 102    /**
 103     * Creates an id for each element and stores a reference on it
 104     */
 105    function _generate_index(&$elements) {
 106      //generate ids
 107      $this->_counter++;
 108      $this->map[$this->_counter] = &$elements;
 109  
 110      //recurse
 111      foreach (element_children($elements) as $key) {
 112        $this->_generate_index($elements[$key]);
 113      }
 114    }
 115  
 116    /**
 117     * Gets the rule with set #id properties, useful for rendering.
 118     * Note: Any possible changes done, won't appear in the returned rule.
 119     */
 120    function get_indexed_rule() {
 121      $this->_counter = 0;
 122      $index_rule = $this->_rule;
 123      $this->_generate_index_rule($index_rule['#conditions']);
 124      $this->_generate_index_rule($index_rule['#actions']);
 125      return $index_rule;
 126    }
 127  
 128    function _generate_index_rule(&$elements) {
 129      //generate ids
 130      $this->_counter++;
 131      $elements['#id'] = $this->_counter;
 132  
 133      //recurse
 134      foreach (element_children($elements) as $key) {
 135        $this->_generate_index_rule($elements[$key]);
 136      }
 137    }
 138  
 139    /**
 140     * Saves a the rule in the database
 141     */
 142    function save_rule() {
 143      $rule = $this->get_rule();
 144  
 145      if (!isset($rule['#status']) || $rule['#status'] == 'default') {
 146        $rule['#status'] = 'altered';
 147      }
 148  
 149      // Sort conditions and actions.
 150      rules_sort_elements($rule['#conditions']);
 151      rules_sort_elements($rule['#actions']);
 152  
 153      rules_item_save('rules', $this->get_rule_name(), $rule);
 154      if (isset($this->_new_name)) {
 155        rules_item_change_name('rules', $this->get_rule_name(), $this->_new_name);
 156        $this->_rule_name = $this->_new_name;
 157        unset($this->_new_name);
 158      }
 159    }
 160  
 161    /**
 162     * Deletes the rule configuration from the database
 163     */
 164    function delete_rule() {
 165      $rule = $this->get_rule();
 166      rules_item_delete('rules', $this->get_rule_name(), $rule);
 167    }
 168  
 169    /**
 170     * Cleans the given rule. This means, array keys that are neither elements
 171     * nor properties are removed.
 172     */
 173    function clean_rule() {
 174      $rule = &$this->get_rule();
 175      $this->_clean_rule($rule['#conditions']);
 176      $this->_clean_rule($rule['#actions']);
 177    }
 178  
 179    function _clean_rule(&$element) {
 180      $children = array();
 181      foreach (element_children($element) as $key) {
 182        if (!isset($element[$key]['#type'])) {
 183          //this element can be removed, but care for it's children
 184          foreach (element_children($element[$key]) as $child_key) {
 185            $children[$child_key] = $element[$key][$child_key];
 186          }
 187          unset($element[$key]);
 188        }
 189        else {
 190          $this->_clean_rule($element[$key]);
 191        }
 192      }
 193      if (count($children)) {
 194        $element = array_merge($element, $children);
 195      }
 196    }
 197  
 198    /**
 199     * Gets the set of this rule, which contains only active rules.
 200     */
 201    function get_set() {
 202      $rule = $this->get_rule();
 203      $set = rules_get_rule_set($rule['#set']);
 204      return $set ? $set : array('info' => array(), 'rules' => array());
 205    }
 206  
 207    /**
 208     * Gets the info about the set of this rule
 209     */
 210    function get_set_info() {
 211      $rule = $this->get_rule();
 212      return rules_get_rule_sets($rule['#set']) + array('arguments' => array());
 213    }
 214  
 215    /**
 216     * Gets the info about all new defined variables in this rule,
 217     * before the element with the given id
 218     */
 219    function get_new_variables($id = NULL) {
 220      $vars = array();
 221      $i = 0;
 222      $element = TRUE;
 223      while (isset($id) && $i < $id || !isset($id) && $element !== FALSE) {
 224        $element = $this->get_element($i);
 225        $vars = $this->element_get_new_variables($element) + $vars;
 226        $i++;
 227      }
 228      return $vars;
 229    }
 230  
 231    /**
 232     * Gets info about all new defined variables by the given element
 233     */
 234    function element_get_new_variables($element) {
 235      if (isset($element['#type']) && $element['#type'] == 'action') {
 236        $info = rules_get_element_info($element);
 237        return $info['new variables'];
 238      }
 239      return array();
 240    }
 241  
 242    /**
 243     * Gets info about all available variables
 244     * This are the arguments of the rule's set as well as further arguments that
 245     * might be provided by actions of this or preceding rules. Variables set to be hidden
 246     * are left out.
 247     *
 248     * @param $id The id of the element of the current rule until which we should get new variables.
 249     *   If not given all new variables of this rule will be considered.
 250     */
 251    function get_available_variables($id = NULL) {
 252      $set_info = $this->get_set_info();
 253      $vars = $this->get_new_variables($id) + $this->_get_available_variables() + $set_info['arguments'];
 254      return array_filter($vars, 'rules_admin_element_filter');
 255    }
 256  
 257    /**
 258     * Gets info about all defined variables
 259     * This are all available variables as well as variables defined in and after this rule.
 260     */
 261    function get_defined_variables() {
 262      $set_info = $this->get_set_info();
 263      $vars = $this->_get_available_variables(TRUE) + $set_info['arguments'];
 264      return array_filter($vars, 'rules_admin_element_filter');
 265    }
 266  
 267    /**
 268     * Gets new variables defined by actions in rules, which are evaluated until this rule.
 269     *
 270     * @param $all
 271     *   If set to TRUE all variables defined by any rules in this rule set will returned.
 272     */
 273    function _get_available_variables($all = FALSE) {
 274      if (!isset($this->_variables[$all])) {
 275        $this->_variables[$all] = array();
 276        $set = $this->get_set();
 277  
 278        //sort the rules
 279        rules_sort_children($set['rules']);
 280  
 281        foreach (element_children($set['rules']) as $name) {
 282          if ($name == $this->get_rule_name()) {
 283            if (!$all) {
 284              return $this->_variables[$all];
 285            }
 286          }
 287          $set['rules'][$name] += array('#actions' => array());
 288          foreach ($set['rules'][$name]['#actions'] as $action) {
 289            $this->_variables[$all] += $this->element_get_new_variables($action);
 290          }
 291        }
 292      }
 293      return $this->_variables[$all];
 294    }
 295  }
 296  


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