You are here

class RulesReactionRule in Rules 7.2

Represents rules getting triggered by events.

Hierarchy

Expanded class hierarchy of RulesReactionRule

1 string reference to 'RulesReactionRule'
rules_rules_plugin_info in ./rules.module
Implements hook_rules_plugin_info().

File

includes/rules.plugins.inc, line 380
Contains plugin info and implementations not needed for rule evaluation.

View source
class RulesReactionRule extends Rule implements RulesTriggerableInterface {

  /**
   * @var string
   */
  protected $itemName = 'reaction rule';

  /**
   * @var array
   */
  protected $events = array();

  /**
   * @var array
   */
  protected $eventSettings = array();

  /**
   * Implements RulesTriggerableInterface::events().
   */
  public function events() {
    return $this->events;
  }

  /**
   * Implements RulesTriggerableInterface::removeEvent().
   */
  public function removeEvent($event) {
    if (($id = array_search($event, $this->events)) !== FALSE) {
      unset($this->events[$id]);
    }
    return $this;
  }

  /**
   * Implements RulesTriggerableInterface::event().
   */
  public function event($event_name, array $settings = NULL) {

    // Process any settings and determine the configured event's name.
    if ($settings) {
      $handler = rules_get_event_handler($event_name, $settings);
      if ($suffix = $handler
        ->getEventNameSuffix()) {
        $event_name .= '--' . $suffix;
        $this->eventSettings[$event_name] = $settings;
      }
      else {

        // Do not store settings if there is no suffix.
        unset($this->eventSettings[$event_name]);
      }
    }
    if (array_search($event_name, $this->events) === FALSE) {
      $this->events[] = $event_name;
    }
    return $this;
  }

  /**
   * Implements RulesTriggerableInterface::getEventSettings().
   */
  public function getEventSettings($event_name) {
    if (isset($this->eventSettings[$event_name])) {
      return $this->eventSettings[$event_name];
    }
  }
  public function integrityCheck() {
    parent::integrityCheck();

    // Check integrity of the configured events.
    foreach ($this->events as $event_name) {
      $handler = rules_get_event_handler($event_name, $this
        ->getEventSettings($event_name));
      $handler
        ->validate();
    }
    return $this;
  }

  /**
   * Reaction rules can't add variables to the parent scope, so clone $state.
   */
  public function evaluate(RulesState $state) {

    // Implement recursion prevention for reaction rules.
    if ($state
      ->isBlocked($this)) {
      return rules_log('Not evaluating @plugin %label to prevent recursion.', array(
        '%label' => $this
          ->label(),
        '@plugin' => $this
          ->plugin(),
      ), RulesLog::INFO, $this);
    }
    $state
      ->block($this);
    $copy = clone $state;
    parent::evaluate($copy);
    $state
      ->unblock($this);
  }
  public function access() {
    foreach ($this->events as $event_name) {
      $event_info = rules_get_event_info($event_name);
      if (!empty($event_info['access callback']) && !call_user_func($event_info['access callback'], 'event', $event_info['name'])) {
        return FALSE;
      }
    }
    return parent::access();
  }
  public function dependencies() {
    $modules = array_flip(parent::dependencies());
    foreach ($this->events as $event_name) {
      $event_info = rules_get_event_info($event_name);
      if (isset($event_info['module'])) {
        $modules[$event_info['module']] = TRUE;
      }
    }
    return array_keys($modules);
  }
  public function providesVariables() {
    return array();
  }
  public function parameterInfo($optional = FALSE) {

    // If executed directly, all variables as defined by the event need to
    // be passed.
    return rules_filter_array($this
      ->availableVariables(), 'handler', FALSE);
  }
  public function availableVariables() {
    if (!isset($this->availableVariables)) {
      if (isset($this->parent)) {

        // Return the event variables provided by the event set, once cached.
        $this->availableVariables = $this->parent
          ->stateVariables();
      }
      else {

        // The intersection of the variables provided by the events are
        // available.
        foreach ($this->events as $event_name) {
          $handler = rules_get_event_handler($event_name, $this
            ->getEventSettings($event_name));
          if (isset($this->availableVariables)) {
            $event_vars = $handler
              ->availableVariables();

            // Merge variable info by intersecting the variable-info keys also,
            // so we have only metadata available that is valid for all of the
            // provided variables.
            foreach (array_intersect_key($this->availableVariables, $event_vars) as $name => $variable_info) {
              $this->availableVariables[$name] = array_intersect_key($variable_info, $event_vars[$name]);
            }
          }
          else {
            $this->availableVariables = $handler
              ->availableVariables();
          }
        }
        $this->availableVariables = isset($this->availableVariables) ? RulesState::defaultVariables() + $this->availableVariables : RulesState::defaultVariables();
      }
    }
    return $this->availableVariables;
  }
  public function __sleep() {
    return parent::__sleep() + drupal_map_assoc(array(
      'events',
      'eventSettings',
    ));
  }
  protected function exportChildren($key = 'ON') {
    $export = array();
    foreach ($this->events as $event_name) {
      $export[$key][$event_name] = (array) $this
        ->getEventSettings($event_name);
    }
    return $export + parent::exportChildren();
  }
  protected function importChildren($export, $key = 'ON') {

    // Detect and support old-style exports: a numerically indexed array of
    // event names.
    if (isset($export[$key])) {
      if (is_string(reset($export[$key])) && is_numeric(key($export[$key]))) {
        $this->events = $export[$key];
      }
      else {
        $this->events = array_keys($export[$key]);
        $this->eventSettings = array_filter($export[$key]);
      }
    }
    parent::importChildren($export);
  }

  /**
   * Overrides optimize().
   */
  public function optimize() {
    parent::optimize();

    // No need to keep event settings for evaluation.
    $this->eventSettings = array();
  }

}

Members