dummy org chart / horrible logic and html

This commit is contained in:
Dennis Eichhorn 2019-06-08 23:49:40 +02:00
parent 85c014fcab
commit 0fb7e7821a
2 changed files with 178 additions and 1 deletions

View File

@ -15,8 +15,11 @@ declare(strict_types=1);
namespace Modules\Organization\Controller;
use Modules\Organization\Models\DepartmentMapper;
use Modules\Organization\Models\Department;
use Modules\Organization\Models\PositionMapper;
use Modules\Organization\Models\Position;
use Modules\Organization\Models\UnitMapper;
use Modules\Organization\Models\Unit;
use phpOMS\Contract\RenderableInterface;
use phpOMS\Message\RequestAbstract;
@ -99,9 +102,53 @@ final class BackendController extends Controller
$view->setTemplate('/Modules/Organization/Theme/Backend/organigram');
$units = UnitMapper::getAll();
$unitTree = $this->createOrgTree($units);
$view->setData('unitTree', $unitTree);
$departments = DepartmentMapper::getAll();
$depTree = $this->createOrgTree($departments);
$view->setData('departmentTree', $depTree);
$positions = PositionMapper::getAll();
$posTree = $this->createOrgTree($positions);
$view->setData('positionTree', $posTree);
return $view;
}
private function createOrgTree($components) : array
{
$tree = [];
foreach ($components as $component) {
$ref = null;
if ($component instanceof Department) {
$ref = $component->getUnit()->getId();
} elseif ($component instanceof Position) {
$ref = $component->getDepartment()->getId();
}
if (!isset($tree[$ref][$component->getId()])) {
$tree[$ref][$component->getId()] = ['obj' => null, 'children' => [], 'index' => 0];
}
$tree[$ref][$component->getId()]['obj'] = $component;
$parent = $component->getParent()->getId();
if (!($component instanceof Position) // parent could be in different department then ignore
|| $component->getParent()->getDepartment()->getId() === $component->getDepartment()->getId()
) {
if (!isset($tree[$ref][$parent])) {
$tree[$ref][$parent] = ['obj' => null, 'children' => [], 'index' => 0];
}
$tree[$ref][$parent]['children'][] = &$tree[$ref][$component->getId()];
}
}
return $tree;
}
/**
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response

View File

@ -13,7 +13,137 @@
/**
* @var \phpOMS\Views\View $this
*/
$unitTree = $this->getData('unitTree');
$depTree = $this->getData('departmentTree');
$posTree = $this->getData('positionTree');
$unitRoot = $unitTree[null][0]['children'];
// units
// departments
// positions
?>
<div class="row">asdf
<div class="row">
<?php foreach ($unitRoot as $unitEle) : ?>
<div class="row" style="margin: 0 auto;">
<?php while (!empty($unitEle) && $unitEle['obj'] !== null) {
$unitTree[null][$unitEle['obj']->getParent()->getId()]['index'] = $unitTree[null][$unitEle['obj']->getParent()->getId()]['index'] + 1;
?>
<?php while (!empty($unitEle)) {
$unitId = $unitEle['obj']->getId(); ?>
<div class="col" style="margin: 0 auto; background: #00f; padding: 1rem;">
<div style="margin: 0 auto; background: #f00; padding: 1rem;"><?= $unitEle['obj']->getName(); ?></div>
<?php if (isset($depTree[$unitId]) && !empty($depTree[$unitId])) : ?>
<!-- departments -->
<div class="row">
<?php
$depRoot = $depTree[$unitId][0]['children'] ?? []; foreach ($depRoot as $depEle) : ?>
<div class="row" style="margin: 0 auto;">
<?php while (!empty($depEle) && $depEle['obj'] !== null) {
$depTree[$unitId][$depEle['obj']->getParent()->getId()]['index'] = $depTree[$unitId][$depEle['obj']->getParent()->getId()]['index'] + 1;
?>
<?php while (!empty($depEle)) { ?>
<div class="col" style="margin: 0 auto; background: #0f0; padding: 1rem;">
<div style="margin: 0 auto; background: #ff0; padding: 1rem;"><?= $depEle['obj']->getName(); ?></div>
<!-- positions -->
<ul>
<?php
$depId = $depEle['obj']->getId();
$posRoot = !isset($posTree[$depId]) ? [] : $posTree[$depId];
foreach ($posRoot as $posEle) : ?>
<li><ul>
<?php while (!empty($posEle) && $posEle['obj'] !== null) {
if (isset($posTree[$depId][$posEle['obj']->getParent()->getId()])) {
// here is a bug or somewhere else... the index is not moved correctly $c is always 0
$posTree[$depId][$posEle['obj']->getParent()->getId()]['index'] = $posTree[$depId][$posEle['obj']->getParent()->getId()]['index'] + $c + 1;
}
?>
<?php $c = 0; $toClosePos = 0; while (!empty($posEle)) { $toClosePos = 0; ?>
<li><ul>
<li><?= $posEle['obj']->getName(); ?>
<li><ul>
<?php
// find the closest parent who has un-rendered children
if (empty($posEle['children'])) {
$parentPos = $posEle['obj'];
do {
++$toClosePos;
$parentPos = $parentPos->getParent();
$parentPosId = $parentPos->getId();
} while ($parentPosId !== \array_keys($posTree[$depId])[0]
&& $parentPosId !== $depId
&& $parentPosId !== 0
&& !isset($posTree[$depId][$parentPosId]['children'][($posTree[$depId][$parentPosId]['index'] ?? 0) + $c + 1])
);
}
$posEle = [];
} // if no more children go back to parrent?>
<?= \str_repeat('</ul>', $toClosePos); ?>
<?php
if (isset($posTree[$depId][$parentPosId ?? 0])) {
$posEle = $posTree[$depId][$parentPosId]['children'][$posTree[$depId][$parentPosId]['index'] + $c + 1] ?? [];
}
} ?>
</ul>
<?php endforeach; ?>
</ul>
<div class="row">
<?php
// find the closest parent who has un-rendered children
$toCloseDep = 0;
if (empty($depEle['children'])) {
$parentDep = $depEle['obj'];
do {
++$toCloseDep;
$parentDep = $parentDep->getParent();
$parentDepId = $parentDep->getId();
} while ($parentDepId !== 0
&& !isset($depTree[$unitId][$parentDepId]['children'][($depTree[$unitId][$parentDepId]['index'] ?? 0) + 1])
);
}
$depEle = $depEle['children'][0] ?? [];
} // if no more children go back to parrent?>
<?= \str_repeat('</div>', $toCloseDep*2); ?>
<?php
$depEle = $depTree[$unitId][$parentDepId]['children'][$depTree[$unitId][$parentDepId]['index'] + 1] ?? [];
} ?>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<div class="row">
<?php
// find the closest parent who has un-rendered children
$toCloseUnit = 0;
if (empty($unitEle['children'])) {
$parentUnit = $unitEle['obj'];
do {
++$toCloseUnit;
$parentUnit = $parentUnit->getParent();
$parentUnitId = $parentUnit->getId();
} while ($parentUnitId !== 0
&& !isset($unitTree[null][$parentUnitId]['children'][($unitTree[null][$parentUnitId]['index'] ?? 0) + 1])
);
}
$unitEle = $unitEle['children'][0] ?? [];
} // if no more children go back to parrent?>
<?= \str_repeat('</div>', $toCloseUnit*2); ?>
<?php
$unitEle = $unitTree[null][$parentUnitId]['children'][$unitTree[null][$parentUnitId]['index'] + 1] ?? [];
} ?>
</div>
<?php endforeach; ?>
</div>