2020-01-02 18:45:41 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-01-02 18:45:41 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Trees;
|
|
|
|
|
2020-02-01 19:48:07 +01:00
|
|
|
use App\Entity\Base\AbstractDBElement;
|
|
|
|
use App\Entity\Base\AbstractNamedDBElement;
|
|
|
|
use App\Entity\Base\AbstractStructuralDBElement;
|
2020-01-02 22:55:28 +01:00
|
|
|
use App\Helpers\Trees\TreeViewNode;
|
2020-01-04 20:24:09 +01:00
|
|
|
use App\Helpers\Trees\TreeViewNodeIterator;
|
2020-10-03 13:56:30 +02:00
|
|
|
use App\Helpers\Trees\TreeViewNodeState;
|
2020-01-02 18:45:41 +01:00
|
|
|
use App\Repository\StructuralDBElementRepository;
|
|
|
|
use App\Services\EntityURLGenerator;
|
|
|
|
use App\Services\UserCacheKeyGenerator;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Symfony\Contracts\Cache\ItemInterface;
|
|
|
|
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
2020-01-02 22:55:28 +01:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2020-01-02 18:45:41 +01:00
|
|
|
|
|
|
|
class TreeViewGenerator
|
|
|
|
{
|
|
|
|
protected $urlGenerator;
|
|
|
|
protected $em;
|
|
|
|
protected $cache;
|
|
|
|
protected $keyGenerator;
|
2020-01-02 22:55:28 +01:00
|
|
|
protected $translator;
|
2020-01-02 18:45:41 +01:00
|
|
|
|
|
|
|
public function __construct(EntityURLGenerator $URLGenerator, EntityManagerInterface $em,
|
2020-01-02 22:55:28 +01:00
|
|
|
TagAwareCacheInterface $treeCache, UserCacheKeyGenerator $keyGenerator, TranslatorInterface $translator)
|
2020-01-02 18:45:41 +01:00
|
|
|
{
|
|
|
|
$this->urlGenerator = $URLGenerator;
|
|
|
|
$this->em = $em;
|
|
|
|
$this->cache = $treeCache;
|
|
|
|
$this->keyGenerator = $keyGenerator;
|
2020-01-02 22:55:28 +01:00
|
|
|
$this->translator = $translator;
|
2020-01-02 18:45:41 +01:00
|
|
|
}
|
|
|
|
|
2020-01-02 22:55:28 +01:00
|
|
|
/**
|
|
|
|
* Gets a TreeView list for the entities of the given class.
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2020-03-15 13:56:31 +01:00
|
|
|
* @param string $class The class for which the treeView should be generated
|
2020-02-01 19:48:07 +01:00
|
|
|
* @param AbstractStructuralDBElement|null $parent The root nodes in the tree should have this element as parent (use null, if you want to get all entities)
|
2020-10-03 14:21:45 +02:00
|
|
|
* @param string $mode The link type that will be generated for the hyperlink section of each node (see EntityURLGenerator for possible values).
|
2020-03-15 13:56:31 +01:00
|
|
|
* Set to empty string, to disable href field.
|
2020-02-01 19:48:07 +01:00
|
|
|
* @param AbstractDBElement|null $selectedElement The element that should be selected. If set to null, no element will be selected.
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2020-08-21 21:36:22 +02:00
|
|
|
* @return TreeViewNode[] an array of TreeViewNode[] elements of the root elements
|
2020-01-02 22:55:28 +01:00
|
|
|
*/
|
2020-10-03 13:56:30 +02:00
|
|
|
public function getTreeView(string $class, ?AbstractStructuralDBElement $parent = null, string $mode = 'list_parts', ?AbstractDBElement $selectedElement = null): array
|
2020-01-02 18:45:41 +01:00
|
|
|
{
|
2020-01-02 22:55:28 +01:00
|
|
|
$head = [];
|
|
|
|
|
2020-10-03 13:56:30 +02:00
|
|
|
$href_type = $mode;
|
|
|
|
|
2020-01-02 22:55:28 +01:00
|
|
|
//When we use the newEdit type, add the New Element node.
|
2020-10-03 13:56:30 +02:00
|
|
|
if ('newEdit' === $mode) {
|
2020-01-02 22:55:28 +01:00
|
|
|
//Generate the url for the new node
|
|
|
|
$href = $this->urlGenerator->createURL(new $class());
|
|
|
|
$new_node = new TreeViewNode($this->translator->trans('entity.tree.new'), $href);
|
|
|
|
//When the id of the selected element is null, then we have a new element, and we need to select "new" node
|
2020-01-05 15:46:58 +01:00
|
|
|
if (null === $selectedElement || null === $selectedElement->getID()) {
|
2020-01-02 22:55:28 +01:00
|
|
|
$new_node->setSelected(true);
|
|
|
|
}
|
|
|
|
$head[] = $new_node;
|
|
|
|
//Add spacing
|
|
|
|
$head[] = (new TreeViewNode(''))->setDisabled(true);
|
|
|
|
|
|
|
|
//Every other treeNode will be used for edit
|
|
|
|
$href_type = 'edit';
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:56:30 +02:00
|
|
|
if ($mode === 'list_parts_root') {
|
|
|
|
$href_type = 'list_parts';
|
|
|
|
}
|
|
|
|
|
2020-10-03 14:04:43 +02:00
|
|
|
if ($mode === 'devices') {
|
|
|
|
$href_type = '';
|
|
|
|
}
|
|
|
|
|
2020-01-02 18:45:41 +01:00
|
|
|
$generic = $this->getGenericTree($class, $parent);
|
|
|
|
$treeIterator = new TreeViewNodeIterator($generic);
|
2020-01-02 22:55:28 +01:00
|
|
|
$recursiveIterator = new \RecursiveIteratorIterator($treeIterator, \RecursiveIteratorIterator::SELF_FIRST);
|
2020-01-02 18:45:41 +01:00
|
|
|
foreach ($recursiveIterator as $item) {
|
2020-01-05 15:46:58 +01:00
|
|
|
/** @var TreeViewNode $item */
|
2020-01-04 20:24:09 +01:00
|
|
|
if (null !== $selectedElement && $item->getId() === $selectedElement->getID()) {
|
|
|
|
$item->setSelected(true);
|
2020-01-02 18:45:41 +01:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!empty($item->getNodes())) {
|
2020-01-02 18:45:41 +01:00
|
|
|
$item->addTag((string) \count($item->getNodes()));
|
|
|
|
}
|
|
|
|
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!empty($href_type) && null !== $item->getId()) {
|
2020-01-02 18:45:41 +01:00
|
|
|
$entity = $this->em->getPartialReference($class, $item->getId());
|
|
|
|
$item->setHref($this->urlGenerator->getURL($entity, $href_type));
|
|
|
|
}
|
2020-04-29 22:59:14 +02:00
|
|
|
|
|
|
|
//Translate text if text starts with $$
|
2020-08-21 22:44:38 +02:00
|
|
|
if (0 === strpos($item->getText(), '$$')) {
|
2020-04-29 22:59:14 +02:00
|
|
|
$item->setText($this->translator->trans(substr($item->getText(), 2)));
|
|
|
|
}
|
2020-01-02 18:45:41 +01:00
|
|
|
}
|
|
|
|
|
2020-10-03 14:04:43 +02:00
|
|
|
if ($mode === 'list_parts_root' ||$mode === 'devices') {
|
2020-10-03 13:56:30 +02:00
|
|
|
$root_node = new TreeViewNode($this->translator->trans('tree.root_node.text'), null, $generic);
|
|
|
|
$root_node->setExpanded(true);
|
|
|
|
$generic = [$root_node];
|
|
|
|
}
|
|
|
|
|
2020-01-02 22:55:28 +01:00
|
|
|
return array_merge($head, $generic);
|
2020-01-02 18:45:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* /**
|
|
|
|
* Gets a tree of TreeViewNode elements. The root elements has $parent as parent.
|
|
|
|
* The treeview is generic, that means the href are null and ID values are set.
|
|
|
|
*
|
2020-03-15 13:56:31 +01:00
|
|
|
* @param string $class The class for which the tree should be generated
|
2020-08-21 21:36:22 +02:00
|
|
|
* @param AbstractStructuralDBElement|null $parent the parent the root elements should have
|
2020-01-04 20:24:09 +01:00
|
|
|
*
|
2020-01-02 18:45:41 +01:00
|
|
|
* @return TreeViewNode[]
|
|
|
|
*/
|
2020-02-01 19:48:07 +01:00
|
|
|
public function getGenericTree(string $class, ?AbstractStructuralDBElement $parent = null): array
|
2020-01-02 18:45:41 +01:00
|
|
|
{
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!is_a($class, AbstractNamedDBElement::class, true)) {
|
2020-01-02 22:55:28 +01:00
|
|
|
throw new \InvalidArgumentException('$class must be a class string that implements StructuralDBElement or NamedDBElement!');
|
2020-01-02 18:45:41 +01:00
|
|
|
}
|
2020-08-21 21:36:22 +02:00
|
|
|
if (null !== $parent && !is_a($parent, $class)) {
|
2020-01-02 22:55:28 +01:00
|
|
|
throw new \InvalidArgumentException('$parent must be of the type $class!');
|
2020-01-02 18:45:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @var StructuralDBElementRepository $repo */
|
|
|
|
$repo = $this->em->getRepository($class);
|
|
|
|
|
|
|
|
//If we just want a part of a tree, dont cache it
|
2020-01-04 20:24:09 +01:00
|
|
|
if (null !== $parent) {
|
|
|
|
return $repo->getGenericNodeTree($parent);
|
2020-01-02 18:45:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$secure_class_name = str_replace('\\', '_', $class);
|
|
|
|
$key = 'treeview_'.$this->keyGenerator->generateKey().'_'.$secure_class_name;
|
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
return $this->cache->get($key, function (ItemInterface $item) use ($repo, $parent, $secure_class_name) {
|
2020-01-02 18:45:41 +01:00
|
|
|
// Invalidate when groups, a element with the class or the user changes
|
|
|
|
$item->tag(['groups', 'tree_treeview', $this->keyGenerator->generateKey(), $secure_class_name]);
|
2020-01-04 20:24:09 +01:00
|
|
|
|
2020-01-02 18:45:41 +01:00
|
|
|
return $repo->getGenericNodeTree($parent);
|
|
|
|
});
|
|
|
|
}
|
2020-01-04 20:24:09 +01:00
|
|
|
}
|