class RulesReactionRule

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

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

Title Sort descending Modifiers Object type Summary Overriden Title
RulesReactionRule::$events protected property
RulesReactionRule::$eventSettings protected property
RulesReactionRule::$itemName protected property
RulesReactionRule::access public function
RulesReactionRule::availableVariables public function
RulesReactionRule::dependencies public function
RulesReactionRule::evaluate public function Reaction rules can't add variables to the parent scope, so clone $state.
RulesReactionRule::event public function Implements RulesTriggerableInterface::event(). Overrides RulesTriggerableInterface::event
RulesReactionRule::events public function Implements RulesTriggerableInterface::events(). Overrides RulesTriggerableInterface::events
RulesReactionRule::exportChildren protected function
RulesReactionRule::getEventSettings public function Implements RulesTriggerableInterface::getEventSettings(). Overrides RulesTriggerableInterface::getEventSettings
RulesReactionRule::importChildren protected function
RulesReactionRule::integrityCheck public function
RulesReactionRule::optimize public function Overrides optimize().
RulesReactionRule::parameterInfo public function
RulesReactionRule::providesVariables public function
RulesReactionRule::removeEvent public function Implements RulesTriggerableInterface::removeEvent(). Overrides RulesTriggerableInterface::removeEvent
RulesReactionRule::__sleep public function