You are here

private function Flowchart::mountMatrix in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Util/Flowchart/Flowchart.php \Drupal\business_rules\Util\Flowchart\Flowchart::mountMatrix()

Mount the graph matrix.

Parameters

\Drupal\Core\Entity\EntityInterface $item: The Business Rule item.

null|\Drupal\business_rules\Util\Flowchart\Element $parent: The parent Element.

string $info: Additional info for the method.

string $arrowLabel: The label for the arrow.

2 calls to Flowchart::mountMatrix()
Flowchart::getGraph in src/Util/Flowchart/Flowchart.php
Show the Business Rule workflow for one item.
Flowchart::getGraphDefinition in src/Util/Flowchart/Flowchart.php
Get the workflow graph definition.

File

src/Util/Flowchart/Flowchart.php, line 140

Class

Flowchart
Class Flowchart.

Namespace

Drupal\business_rules\Util\Flowchart

Code

private function mountMatrix(EntityInterface $item, Element $parent = NULL, $info = '', $arrowLabel = '') {
  if (empty($parent)) {
    $root = new Element($item);
    $this->matrix
      ->putRootElement($root);
  }
  else {
    $off_x = 0;
    $off_y = 0;
    $direction = 'bottom';
    if ($parent
      ->getItem() instanceof BusinessRule) {
      $direction = 'bottom';
    }
    elseif ($parent
      ->getItem() instanceof Condition) {
      if ($info == 'success') {
        $direction = 'bottom-right';
      }
      elseif ($info == 'fail') {
        $direction = 'bottom-left';
      }
    }
    elseif ($parent
      ->getItem() instanceof Action) {

      // If root's children: right.
      // If at right of the root: right.
      // If at left of the root: left.
      $root_cell = $this->matrix
        ->getCellByElementUuid($this->matrix
        ->getRootElement()
        ->getUuid());
      $parent_cell = $this->matrix
        ->getCellByElementUuid($parent
        ->getUuid());
      if (empty($parent
        ->getParent())) {
        $direction = 'bottom';
      }
      elseif ($parent_cell['index_x'] >= $root_cell['index_x'] && $this->matrix
        ->rightCellIsEmpty($parent)) {
        $direction = 'right';
      }
      else {
        $direction = 'left';
      }

      // If item is condition and grandparent is the root, then change X off.
      if ($item instanceof Condition) {
        if (!empty($parent
          ->getParent()) && $parent
          ->getParent()
          ->getItem() instanceof BusinessRule) {
          if ($direction == 'right') {
            if (count($item
              ->getFailItems())) {
              $off_x = 1;
            }
          }
          if ($direction == 'left') {
            if (count($item
              ->getSuccessItems())) {
              $off_x = -1;
            }
          }
        }
      }
    }
    $element = new Element($item, $parent, '', $arrowLabel);
    $this->matrix
      ->putElement($element, $parent, $direction, $off_x, $off_y);
  }
  if ($this
    ->itemHasChildren($item)) {

    // Check if parent is the root element.
    if ($this->matrix
      ->getRootElement()
      ->getItem() === $item) {
      $parent_element = $this->matrix
        ->getRootElement();
    }
    else {
      $parent_element = $this->matrix
        ->getElementByItem($item);
    }
    if ($item instanceof BusinessRule) {
      $children = $item
        ->getItems();

      /** @var \Drupal\business_rules\BusinessRulesItemObject $child */
      foreach ($children as $child) {
        $child = $child
          ->loadEntity();
        if (!empty($child)) {
          $this
            ->mountMatrix($child, $parent_element);
        }
      }
    }
    elseif ($item instanceof Condition) {
      $success_items = $item
        ->getSuccessItems();

      /** @var \Drupal\business_rules\BusinessRulesItemObject $success_item */
      foreach ($success_items as $success_item) {
        $success_item = $success_item
          ->loadEntity();
        $yes = $this
          ->t('Yes');
        if (!empty($success_item)) {
          $this
            ->mountMatrix($success_item, $parent_element, 'success', $yes
            ->render());
        }
      }
      $fail_items = $item
        ->getFailItems();
      foreach ($fail_items as $fail_item) {
        $fail_item = $fail_item
          ->loadEntity();
        $no = $this
          ->t('No');
        if (!empty($fail_item)) {
          $this
            ->mountMatrix($fail_item, $parent_element, 'fail', $no
            ->render());
        }
      }
    }
    elseif ($item instanceof Action) {
      $children = $item
        ->getSettings('items');
      if (count($children)) {
        $children = BusinessRulesItemObject::itemsArrayToItemsObject($children);
        foreach ($children as $child) {
          $child = $child
            ->loadEntity();
          if (!empty($child)) {
            $this
              ->mountMatrix($child, $parent_element);
          }
        }
      }
    }
  }
}