You are here

abstract class RulesConditionalElement in Conditional Rules 7

Same name and namespace in other branches
  1. 8 includes/rules_conditional.core.inc \RulesConditionalElement

Base conditional element plugin implementation.

Hierarchy

Expanded class hierarchy of RulesConditionalElement

File

includes/rules_conditional.core.inc, line 193
Conditional Rules framework implementation.

View source
abstract class RulesConditionalElement extends RulesActionContainer implements RulesActionInterface {

  /**
   * The parent conditional.
   * @var RulesConditionalContainer
   */
  protected $parent;
  public function label() {
    $info = $this
      ->pluginInfo();
    $label = isset($info['label']) ? $info['label'] : t('unlabeled');
    return $label;
  }

  /**
   * @todo Remove once http://drupal.org/node/1671344 is resolved.
   */
  public function setParent(RulesContainerPlugin $parent) {
    if ($this->parent === $parent) {
      return;
    }

    // Check parent class against the compatible class.
    $pluginInfo = $this
      ->pluginInfo();
    if (empty($pluginInfo['embeddable'])) {
      throw new RulesEvaluationException('This element cannot be embedded.', array(), $this, RulesLog::ERROR);
    }
    elseif (!$parent instanceof $pluginInfo['embeddable']) {
      throw new RulesEvaluationException('The given container is incompatible with this element.', array(), $this, RulesLog::ERROR);
    }
    if (isset($this->parent) && ($key = array_search($this, $this->parent->children, TRUE)) !== FALSE) {

      // Remove element from any previous parent.
      unset($this->parent->children[$key]);
      $this->parent
        ->resetInternalCache();
    }

    // Update parent.
    $this->parent = $parent;
    $parent->children[] = $this;
    $this->parent
      ->resetInternalCache();
  }
  public function providesVariables() {
    $provides = parent::providesVariables();
    if (!$this
      ->isRoot()) {
      foreach ($this->children as $action) {
        $provides += $action
          ->providesVariables();
      }
    }
    return $provides;
  }

  /**
   * Determines whether this branch is default, i.e. covers the remainder of
   * conditions outside of all non-default branches inside the conditional.
   */
  public function isDefault() {
    return FALSE;
  }

  /**
   * Determines whether this branch can be evaluated.
   *
   * Non-default plugins should override this method.
   */
  public function canEvaluate(RulesState $state) {
    return $this
      ->isDefault();
  }

  /**
   * Gets the previous sibling element.
   *
   * @return RulesPlugin
   */
  public function getPreviousSibling() {
    if (isset($this->parent) && method_exists($this->parent, 'getIterator')) {
      $previous = NULL;
      foreach ($this->parent
        ->getIterator() as $element) {
        if ($element === $this) {
          return $previous;
        }
        $previous = $element;
      }
    }

    // Otherwise, return nothing if no previous sibling is applicable.
    return NULL;
  }

  /**
   * Gets sibling elements.
   *
   * @return array of RulesPlugin objects.
   */
  public function getAllSibling() {
    if (isset($this->parent)) {
      $siblings = NULL;
      foreach ($this->parent
        ->getIterator() as $element) {
        if (!($element === $this)) {
          $siblings[] = $element;
        }
      }
      return $siblings;
    }

    // Otherwise, return nothing if no sibling.
    return NULL;
  }

  /**
   * Gets the next sibling element.
   *
   * @return RulesPlugin
   */
  public function getNextSibling() {
    if (isset($this->parent)) {
      $previous = NULL;
      foreach ($this->parent
        ->getIterator() as $element) {
        if ($previous === $this) {
          return $element;
        }
        $previous = $element;
      }
    }

    // Otherwise, return nothing if no next sibling is applicable.
    return NULL;
  }
  public function integrityCheck() {
    parent::integrityCheck();
    $this
      ->checkSiblings();
    return $this;
  }

  /**
   * Checks basic conditional element integrity.
   */
  protected function checkSiblings() {

    // Check a default element is the last.
    if ($this
      ->isDefault() && $this
      ->getNextSibling()) {
      throw new RulesIntegrityException(t('The "%plugin" cannot precede another element.', array(
        '%plugin' => $this
          ->plugin(),
      )), $this);
    }
    $pluginInfo = $this
      ->pluginInfo();

    // Check dependent element.
    if (!empty($pluginInfo['conditional depends'])) {
      if (!($previous = $this
        ->getPreviousSibling()) || !in_array($previous
        ->plugin(), $pluginInfo['conditional depends'])) {
        $depends = $pluginInfo['conditional depends'];
        $list = t('"%plugin"', array(
          '%plugin' => array_shift($depends),
        ));
        foreach ($depends as $depend) {
          $list = t('!preceding, "%plugin"', array(
            '!preceding' => $list,
            '%plugin' => $depend,
          ));
        }
        throw new RulesIntegrityException(t('The "%plugin" must be preceded by one of: !list.', array(
          '%plugin' => $this
            ->plugin(),
          '!list' => $list,
        )), $this);
      }
    }

    // Check single element in a conditional container.
    if (!$this
      ->isRoot() && $this
      ->parentElement() instanceof RulesConditionalContainer && !empty($pluginInfo['conditional single'])) {
      $plugin = $this
        ->plugin();
      $previous = $this
        ->getPreviousSibling();
      $next = $this
        ->getNextSibling();
      do {
        if ($previous && $previous
          ->plugin() == $plugin || $next && $next
          ->plugin() == $plugin) {
          throw new RulesIntegrityException(t('The "%plugin" cannot occur more than once within the enclosing container.', array(
            '%plugin' => $this
              ->plugin(),
          )), $this);
        }
      } while ($previous && ($previous = $previous
        ->getPreviousSibling()) || $next && ($next = $next
        ->getNextSibling()));
    }
  }

  /**
   * Deletes the element and its children.
   */
  public function delete($keep_children = FALSE) {
    parent::delete($keep_children);
  }
  public function dependencies() {
    $modules = array(
      'rules_conditional' => 1,
    );
    $modules += array_flip(parent::dependencies());
    return array_keys($modules);
  }
  protected function importChildren($export, $key = NULL) {
    parent::importChildren($export, 'DO');
  }
  protected function exportChildren($key = NULL) {
    return parent::exportChildren('DO');
  }
  protected function exportSettings() {
    $export = parent::exportSettings();

    // Remove provided variables as plugin is only a container.
    if (isset($export['PROVIDE'])) {
      unset($export['PROVIDE']);
    }
    return $export;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RulesActionContainer::action public function Adds an action to the container.
RulesActionContainer::componentProvidesVariables public function Returns an array of provided variable names.
RulesActionContainer::evaluate public function Evaluate, whereas by default new vars are visible in the parent's scope. Overrides RulesPlugin::evaluate 2
RulesActionContainer::exportToArray protected function Overrides RulesContainerPlugin::exportToArray 1
RulesActionContainer::import public function Applies the given export. Overrides RulesContainerPlugin::import 1
RulesActionContainer::pluginProvidesVariables public function Returns info about variables 'provided' by the plugin. Overrides RulesPlugin::pluginProvidesVariables
RulesActionContainer::__construct public function Overrides RulesContainerPlugin::__construct 3
RulesConditionalElement::$parent protected property The parent conditional. Overrides RulesPlugin::$parent
RulesConditionalElement::canEvaluate public function Determines whether this branch can be evaluated. 4
RulesConditionalElement::checkSiblings protected function Checks basic conditional element integrity.
RulesConditionalElement::delete public function Deletes the element and its children. Overrides RulesContainerPlugin::delete 1
RulesConditionalElement::dependencies public function Calculates an array of required modules. Overrides RulesContainerPlugin::dependencies 1
RulesConditionalElement::exportChildren protected function Overrides RulesContainerPlugin::exportChildren 3
RulesConditionalElement::exportSettings protected function Overrides RulesPlugin::exportSettings
RulesConditionalElement::getAllSibling public function Gets sibling elements.
RulesConditionalElement::getNextSibling public function Gets the next sibling element.
RulesConditionalElement::getPreviousSibling public function Gets the previous sibling element.
RulesConditionalElement::importChildren protected function Overrides RulesContainerPlugin::importChildren 3
RulesConditionalElement::integrityCheck public function Makes sure the plugin is configured right. Overrides RulesContainerPlugin::integrityCheck 1
RulesConditionalElement::isDefault public function Determines whether this branch is default, i.e. covers the remainder of conditions outside of all non-default branches inside the conditional. 3
RulesConditionalElement::label public function Returns the label of the element. Overrides RulesPlugin::label 1
RulesConditionalElement::providesVariables public function Returns info about all variables provided for later evaluated elements. Overrides RulesActionContainer::providesVariables 1
RulesConditionalElement::setParent public function @todo Remove once http://drupal.org/node/1671344 is resolved. Overrides RulesPlugin::setParent
RulesContainerPlugin::$children protected property
RulesContainerPlugin::access public function Whether the currently logged in user has access to all configured elements. Overrides RulesPlugin::access 1
RulesContainerPlugin::availableVariables public function Returns info about variables available to be used as arguments for this element. Overrides RulesPlugin::availableVariables
RulesContainerPlugin::componentVariables public function Returns the specified variables, in case the plugin is used as component.
RulesContainerPlugin::destroy public function Removes circular object references so PHP garbage collector can work. Overrides RulesPlugin::destroy 1
RulesContainerPlugin::executeByArgs public function Executes container with the given arguments. Overrides RulesPlugin::executeByArgs 1
RulesContainerPlugin::exportFlat protected function Determines whether the element should be exported in flat style. 1
RulesContainerPlugin::getIterator public function Allows access to the children through the iterator. 1
RulesContainerPlugin::optimize public function Overrides optimize(). Overrides RulesPlugin::optimize
RulesContainerPlugin::parameterInfo public function Returns info about parameters needed for executing the configured plugin. Overrides RulesPlugin::parameterInfo
RulesContainerPlugin::resetInternalCache public function Resets any internal static caches. Overrides RulesPlugin::resetInternalCache 1
RulesContainerPlugin::setUpVariables protected function Returns info about all variables that have to be setup in the state. Overrides RulesPlugin::setUpVariables
RulesContainerPlugin::sortChildren public function Sorts all child elements by their weight. 1
RulesContainerPlugin::stateVariables protected function Returns available state variables for an element. 4
RulesContainerPlugin::variableInfoAssertions protected function Returns asserted additions to the available variable info. Overrides RulesPlugin::variableInfoAssertions 1
RulesContainerPlugin::__clone public function By default we do a deep clone. Overrides RulesPlugin::__clone 1
RulesContainerPlugin::__sleep public function Overrides RulesPlugin::__sleep 2
RulesExtendable::$itemInfo protected property
RulesExtendable::$itemName protected property The name of the item this class represents in the info hook. 9
RulesExtendable::facesAs public function
RulesExtendable::forceSetUp public function Forces the object to be setUp, this executes setUp() if not done yet. 1
RulesExtendable::itemFacesAs public static function Returns whether the a RuleExtendable supports the given interface.
RulesExtendable::rebuildCache public function Allows items to add something to the rules cache. 1
RulesExtendable::setUp protected function 1
RulesExtendable::__call public function Magic method: Invoke the dynamically implemented methods.
RulesPlugin::$availableVariables protected property Static cache for availableVariables(). 1
RulesPlugin::$cache protected property Overrides RulesExtendable::$cache
RulesPlugin::$elementId protected property Identifies an element inside a configuration.
RulesPlugin::$hook protected property Overrides RulesExtendable::$hook
RulesPlugin::$id public property If this is a configuration saved to the db, the id of it.
RulesPlugin::$info protected property Info about this element. Usage depends on the plugin. 2
RulesPlugin::$name public property
RulesPlugin::$settings public property An array of settings for this element.
RulesPlugin::$weight public property
RulesPlugin::applyDataSelector public function Applies the given data selector.
RulesPlugin::checkParameterSettings protected function Checks whether parameters are correctly configured.
RulesPlugin::checkVarName protected function
RulesPlugin::compare protected static function
RulesPlugin::depth public function Returns the depth of this element in the configuration.
RulesPlugin::elementId public function Returns the element id, which identifies the element inside the config.
RulesPlugin::elementMap public function Gets the element map helper object, which helps mapping elements to ids.
RulesPlugin::elements public function Iterate over all elements nested below the current element.
RulesPlugin::ensureNameExists protected function Ensure the configuration has a name. If not, generate one.
RulesPlugin::entityInfo public function
RulesPlugin::entityType public function
RulesPlugin::execute public function Execute the configuration.
RulesPlugin::export public function Exports a rule configuration.
RulesPlugin::exportParameterSetting protected function
RulesPlugin::form public function Seamlessly invokes the method implemented via faces.
RulesPlugin::form_submit public function
RulesPlugin::form_validate public function
RulesPlugin::getArgument protected function Returns the argument for the parameter $name described with $info.
RulesPlugin::getArgumentInfo public function Returns info about the configured argument.
RulesPlugin::getExecutionArguments protected function Gets the right arguments for executing the element.
RulesPlugin::getPluginName public function Gets the name of this plugin instance. 1
RulesPlugin::hasStatus public function Checks if the configuration has a certain exportable status.
RulesPlugin::identifier public function Returns the config name.
RulesPlugin::importParameterSetting protected function
RulesPlugin::importSettings protected function 1
RulesPlugin::info public function Returns the info of the plugin. 2
RulesPlugin::internalIdentifier public function
RulesPlugin::isRoot public function Returns whether the element is the root of the configuration.
RulesPlugin::parentElement public function Returns the element's parent.
RulesPlugin::plugin public function Returns the name of the element's plugin.
RulesPlugin::pluginInfo public function Returns info about the element's plugin.
RulesPlugin::pluginParameterInfo public function Returns info about parameters needed by the plugin. 2
RulesPlugin::processSettings public function Processes the settings e.g. to prepare input evaluators. 1
RulesPlugin::returnExport protected function Finalizes the configuration export.
RulesPlugin::returnVariables protected function Gets variables to return once the configuration has been executed. 2
RulesPlugin::root public function Gets the root element of the configuration.
RulesPlugin::save public function Saves the configuration to the database. 1
RulesPlugin::setUpState public function Sets up the execution state for the given arguments.
RulesPlugin::__toString public function When converted to a string, just use the export format.