You are here

abstract class RulesConditionalElement in Conditional Rules 8

Same name and namespace in other branches
  1. 7 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
RulesConditionalElement::$parent protected property The parent conditional.
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. 1
RulesConditionalElement::dependencies public function 1
RulesConditionalElement::exportChildren protected function 3
RulesConditionalElement::exportSettings protected function
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 3
RulesConditionalElement::integrityCheck public function 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 1
RulesConditionalElement::providesVariables public function 1
RulesConditionalElement::setParent public function @todo Remove once http://drupal.org/node/1671344 is resolved.