You are here

public function Matrix::putElement in Business Rules 2.x

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

Put an element inside the matrix.

Parameters

\Drupal\business_rules\Util\Flowchart\Element $element: The element.

\Drupal\business_rules\Util\Flowchart\Element $parent: The parent element.

string $direction: The direction: bottom|right|left|bottom-right|bottom-left.

int $off_x: The X off set.

int $off_y: The Y off set.

Throws

\Exception

File

src/Util/Flowchart/Matrix.php, line 183

Class

Matrix
The matrix to handle the Business Rule flowchart.

Namespace

Drupal\business_rules\Util\Flowchart

Code

public function putElement(Element $element, Element $parent, $direction, $off_x = 0, $off_y = 0) {
  if ($parent
    ->getItem() === $this
    ->getRootElement()
    ->getItem()) {
    $parent_cell = $this
      ->getCellByElementUuid($this
      ->getRootElement()
      ->getUuid());
  }
  else {
    $parent_cell = $this
      ->getCellByElementUuid($parent
      ->getUuid());
  }
  if (is_null($parent_cell)) {
    throw new \Exception('Could not load parent');
  }
  $parent_x = $parent_cell['index_x'];
  $parent_y = $parent_cell['index_y'];

  // Only root's children can be at same X position as root.
  // After prepare all root elements, other items can be at same root's X
  // position on X,Y+1 cell.
  $root = $this
    ->getRootElement();
  $root_cel = $this
    ->getCellByElementUuid($root
    ->getUuid());
  $root_x = $root_cel['index_x'];
  switch ($direction) {
    case 'bottom':
      if ($this->numberOfRootItems >= $parent_y && $root_x === $parent_x && $element
        ->getParent() !== $root) {
        $off_x++;
      }
      if (is_null($this->matrix[$parent_x + $off_x][$parent_y + 1 + $off_y]['element'])) {
        $this
          ->putElementInPosition($element, $parent_x + $off_x, $parent_y + 1 + $off_y);
      }
      else {
        $this
          ->putElement($element, $parent, $direction, $off_x, $off_y + 1);
      }
      break;
    case 'right':
      if ($this->numberOfRootItems >= $parent_y && $root_x === $parent_x + 1 + $off_x) {
        $off_x++;
      }
      if (is_null($this->matrix[$parent_x + 1 + $off_x][$parent_y + $off_y]['element'])) {
        $this
          ->putElementInPosition($element, $parent_x + 1 + $off_x, $parent_y + $off_y);
      }
      else {
        $this
          ->putElement($element, $parent, $direction, $off_x, $off_y + 1);
      }
      break;
    case 'left':
      if ($this->numberOfRootItems >= $parent_y && $root_x === $parent_x - 1 + $off_x) {
        $off_x--;
      }
      if ($parent_x - 1 + $off_x < 0 || is_null($this->matrix[$parent_x - 1 + $off_x][$parent_y + $off_y]['element'])) {
        $this
          ->putElementInPosition($element, $parent_x - 1 + $off_x, $parent_y + $off_y);
      }
      else {
        $this
          ->putElement($element, $parent, $direction, $off_x, $off_y + 1);
      }
      break;
    case 'bottom-right':
      if ($this->numberOfRootItems >= $parent_y && $root_x === $parent_x + 1 + $off_x) {
        $off_x++;
      }
      if (is_null($this->matrix[$parent_x + 1 + $off_x][$parent_y + 1 + $off_y]['element'])) {
        $this
          ->putElementInPosition($element, $parent_x + 1 + $off_x, $parent_y + 1 + $off_y);
      }
      else {
        $this
          ->putElement($element, $parent, $direction, $off_x, $off_y + 1);
      }
      break;
    case 'bottom-left':
      if ($this->numberOfRootItems >= $parent_y && $root_x === $parent_x - 1 + $off_x) {
        $off_x--;
      }
      if ($parent_x - 1 + $off_x < 0 || is_null($this->matrix[$parent_x - 1 + $off_x][$parent_y + 1 + $off_y]['element'])) {
        $this
          ->putElementInPosition($element, $parent_x - 1 + $off_x, $parent_y + 1 + $off_y);
      }
      else {
        $this
          ->putElement($element, $parent, $direction, $off_x, $off_y + 1);
      }
      break;
  }
}