You are here

class rules_variable in Rules 6

Handles loading and saving a variable

Hierarchy

Expanded class hierarchy of rules_variable

File

rules/rules.variables.inc, line 268
Provides functions and classes for handling variables

View source
class rules_variable {
  var $name;
  var $info;
  var $data;
  var $_state;
  var $_changed = FALSE;

  /**
   * Constructor
   *
   * @param $state The current evaluation state
   * @param $name The name of the given variable
   * @param $data If available, the actual data, else NULL.
   * @param $info If given, the info for the variable. If not given it will be retrieved
   *   from the current's set info.
   *
   * @return If the variable name isn't valid, FALSE.
   */
  function construct(&$state, $name, &$data, $info = NULL) {
    $this->_state =& $state;
    $info = isset($info) ? $info : $state['set_info']['arguments'][$name];
    if (isset($info)) {
      $this->info = $info + array(
        'saved' => FALSE,
        'handler' => '',
      );
      $this->name = $name;
      $this
        ->_set_data($data);
      $state['variables'][$name] =& $this;
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Gets the actual data. Be sure to keep the reference intact.
   *
   * @param $load Use the variable handler to load the variable, if necessary.
   *
   * @return The data or NULL.
   */
  function &get($load = TRUE) {
    $data = NULL;
    if ($load && !isset($this->data) && function_exists($this->info['handler'])) {

      // Call the handler to get the runtime data.
      $args = rules_get_variables(array_keys($this->_state['variables']), $this->_state, FALSE);
      $data = call_user_func_array($this->info['handler'], $args);
      $this
        ->_set_data($data);
      $this->info['handler'] = '';

      // Do not invoke it twice, if it fails
      rules_log(t('Loaded variable "@arg"', array(
        '@arg' => $this->info['label'],
      )));
    }
    else {
      if (isset($this->data)) {
        $data =& $this->data
          ->get();
      }
    }
    return $data;
  }
  function _set_data(&$data) {
    if (isset($data)) {
      $this->data = rules_get_data_object($this->info);

      // Set the data in the data object and make sure to keep a reference intact
      $this->data
        ->init($data);
    }
  }

  /**
   * Marks the variable to be saved.
   */
  function save() {
    $this->_changed = TRUE;
  }

  /**
   * Updates the actual variable
   */
  function update(&$data) {
    $this->data
      ->update($data);
  }

  /**
   * Saves the variable to db, if necessary
   */
  function save_changes() {

    //if the variable is not saved automatically, save it
    if ($this->_changed && !$this->info['saved'] && $this->data
      ->is_savable()) {
      rules_log(t('Saved variable @name of type @type.', array(
        '@name' => $this->info['label'],
        '@type' => $this->info['type'],
      )));
      $return = $this->data
        ->save();
      $this->_changed = FALSE;
      if (!$return) {
        rules_log(t('Failed saving variable @name of type @type.', array(
          '@name' => $this->info['label'],
          '@type' => $this->info['type'],
        )), TRUE);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
rules_variable::$data property
rules_variable::$info property
rules_variable::$name property
rules_variable::$_changed property
rules_variable::$_state property
rules_variable::construct function Constructor
rules_variable::get function Gets the actual data. Be sure to keep the reference intact.
rules_variable::save function Marks the variable to be saved.
rules_variable::save_changes function Saves the variable to db, if necessary
rules_variable::update function Updates the actual variable
rules_variable::_set_data function