Use new cached treeview nodes for twig inline generator (admin pages)

This commit is contained in:
Jan Böhmer 2020-01-02 22:55:28 +01:00
parent ad69c32832
commit d9b15ddbb9
20 changed files with 277 additions and 214 deletions

View file

@ -0,0 +1,81 @@
<?php
/**
* 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;
use App\Entity\Base\DBElement;
use App\Entity\Base\NamedDBElement;
use App\Entity\Base\StructuralDBElement;
use App\Helpers\Trees\TreeViewNode;
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;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* This service gives you a flat list containing all structured entities in the order of the structure.
*/
class NodesListBuilder
{
protected $em;
protected $cache;
protected $keyGenerator;
public function __construct( EntityManagerInterface $em, TagAwareCacheInterface $treeCache, UserCacheKeyGenerator $keyGenerator)
{
$this->em = $em;
$this->keyGenerator = $keyGenerator;
$this->cache = $treeCache;
}
/**
* Gets a flattened hierachical tree. Useful for generating option lists.
* In difference to the Repository Function, the results here are cached.
*
* @param string $class_name The class name of the entity you want to retrieve.
* @param StructuralDBElement|null $parent This entity will be used as root element. Set to null, to use global root
*
* @return StructuralDBElement[] A flattened list containing the tree elements.
*/
public function typeToNodesList(string $class_name, ?StructuralDBElement $parent = null): array
{
$parent_id = null != $parent ? $parent->getID() : '0';
// Backslashes are not allowed in cache keys
$secure_class_name = str_replace('\\', '_', $class_name);
$key = 'list_'.$this->keyGenerator->generateKey().'_'.$secure_class_name.$parent_id;
$ret = $this->cache->get($key, function (ItemInterface $item) use ($class_name, $parent, $secure_class_name) {
// Invalidate when groups, a element with the class or the user changes
$item->tag(['groups', 'tree_list', $this->keyGenerator->generateKey(), $secure_class_name]);
/**
* @var StructuralDBElementRepository
*/
$repo = $this->em->getRepository($class_name);
return $repo->toNodesList($parent);
});
return $ret;
}
}

View file

@ -0,0 +1,214 @@
<?php
/**
* 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;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Attachments\PartAttachment;
use App\Entity\Devices\Device;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\MeasurementUnit;
use App\Entity\Parts\Part;
use App\Entity\Parts\Storelocation;
use App\Entity\Parts\Supplier;
use App\Entity\PriceInformations\Currency;
use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\User;
use App\Helpers\Trees\TreeViewNode;
use App\Services\UserCacheKeyGenerator;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* This Service generates the tree structure for the tools.
* Whenever you change something here, you has to clear the cache, because the results are cached for performance reasons.
*/
class ToolsTreeBuilder
{
protected $translator;
protected $urlGenerator;
protected $keyGenerator;
protected $cache;
protected $security;
public function __construct(TranslatorInterface $translator, UrlGeneratorInterface $urlGenerator,
TagAwareCacheInterface $treeCache, UserCacheKeyGenerator $keyGenerator,
Security $security)
{
$this->translator = $translator;
$this->urlGenerator = $urlGenerator;
$this->cache = $treeCache;
$this->keyGenerator = $keyGenerator;
$this->security = $security;
}
/**
* Generates the tree for the tools menu.
* The result is cached.
*
* @return TreeViewNode[] The array containing all Nodes for the tools menu.
*/
public function getTree(): array
{
$key = 'tree_tools_'.$this->keyGenerator->generateKey();
return $this->cache->get($key, function (ItemInterface $item) {
//Invalidate tree, whenever group or the user changes
$item->tag(['tree_tools', 'groups', $this->keyGenerator->generateKey()]);
$tree = [];
$tree[] = new TreeViewNode($this->translator->trans('tree.tools.edit'), null, $this->getEditNodes());
$tree[] = new TreeViewNode($this->translator->trans('tree.tools.show'), null, $this->getShowNodes());
$tree[] = new TreeViewNode($this->translator->trans('tree.tools.system'), null, $this->getSystemNodes());
return $tree;
});
}
/**
* This functions creates a tree entries for the "edit" node of the tool's tree.
*
* @return TreeViewNode[]
*/
protected function getEditNodes(): array
{
$nodes = [];
if ($this->security->isGranted('read', new AttachmentType())) {
$nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.edit.attachment_types'),
$this->urlGenerator->generate('attachment_type_new')
);
}
if ($this->security->isGranted('read', new Category())) {
$nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.edit.categories'),
$this->urlGenerator->generate('category_new')
);
}
if ($this->security->isGranted('read', new Device())) {
$nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.edit.devices'),
$this->urlGenerator->generate('device_new')
);
}
if ($this->security->isGranted('read', new Supplier())) {
$nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.edit.suppliers'),
$this->urlGenerator->generate('supplier_new')
);
}
if ($this->security->isGranted('read', new Manufacturer())) {
$nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.edit.manufacturer'),
$this->urlGenerator->generate('manufacturer_new')
);
}
if ($this->security->isGranted('read', new Storelocation())) {
$nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.edit.storelocation'),
$this->urlGenerator->generate('store_location_new')
);
}
if ($this->security->isGranted('read', new Footprint())) {
$nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.edit.footprint'),
$this->urlGenerator->generate('footprint_new')
);
}
if ($this->security->isGranted('read', new Currency())) {
$nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.edit.currency'),
$this->urlGenerator->generate('currency_new')
);
}
if ($this->security->isGranted('read', new MeasurementUnit())) {
$nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.edit.measurement_unit'),
$this->urlGenerator->generate('measurement_unit_new')
);
}
if ($this->security->isGranted('create', new Part())) {
$nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.edit.part'),
$this->urlGenerator->generate('part_new')
);
}
return $nodes;
}
/**
* This function creates the tree entries for the "show" node of the tools tree.
*
* @return TreeViewNode[]
*/
protected function getShowNodes(): array
{
$show_nodes = [];
$show_nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.show.all_parts'),
$this->urlGenerator->generate('parts_show_all')
);
if ($this->security->isGranted('read', new PartAttachment())) {
$show_nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.show.all_attachments'),
$this->urlGenerator->generate('attachment_list')
);
}
return $show_nodes;
}
/**
* This function creates the tree entries for the "system" node of the tools tree.
*
* @return array
*/
protected function getSystemNodes(): array
{
$nodes = [];
if ($this->security->isGranted('read', new User())) {
$nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.system.users'),
$this->urlGenerator->generate('user_new')
);
}
if ($this->security->isGranted('read', new Group())) {
$nodes[] = new TreeViewNode(
$this->translator->trans('tree.tools.system.groups'),
$this->urlGenerator->generate('group_new')
);
}
return $nodes;
}
}

View file

@ -23,15 +23,17 @@ namespace App\Services\Trees;
use App\Entity\Base\DBElement;
use App\Entity\Base\NamedDBElement;
use App\Entity\Base\StructuralDBElement;
use App\Helpers\Trees\TreeViewNodeIterator;
use App\Helpers\TreeViewNode;
use App\Helpers\Trees\TreeViewNode;
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;
use Symfony\Contracts\Translation\TranslatorInterface;
class TreeViewGenerator
{
@ -40,21 +42,51 @@ class TreeViewGenerator
protected $em;
protected $cache;
protected $keyGenerator;
protected $translator;
public function __construct(EntityURLGenerator $URLGenerator, EntityManagerInterface $em,
TagAwareCacheInterface $treeCache, UserCacheKeyGenerator $keyGenerator)
TagAwareCacheInterface $treeCache, UserCacheKeyGenerator $keyGenerator, TranslatorInterface $translator)
{
$this->urlGenerator = $URLGenerator;
$this->em = $em;
$this->cache = $treeCache;
$this->keyGenerator = $keyGenerator;
$this->translator = $translator;
}
public function getTreeView(string $class, ?StructuralDBElement $parent = null, string $href_type = 'list_parts', DBElement $selectedElement = null)
/**
* Gets a TreeView list for the entities of the given class.
* @param string $class The class for which the treeView should be generated
* @param StructuralDBElement|null $parent The root nodes in the tree should have this element as parent (use null, if you want to get all entities)
* @param string $href_type The link type that will be generated for the hyperlink section of each node (see EntityURLGenerator for possible values).
* Set to empty string, to disable href field.
* @param DBElement|null $selectedElement The element that should be selected. If set to null, no element will be selected.
* @return TreeViewNode[] An array of TreeViewNode[] elements of the root elements.
*/
public function getTreeView(string $class, ?StructuralDBElement $parent = null, string $href_type = 'list_parts', DBElement $selectedElement = null) : array
{
$head = [];
//When we use the newEdit type, add the New Element node.
if ('newEdit' === $href_type) {
//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
if (null == $selectedElement || null == $selectedElement->getID()) {
$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';
}
$generic = $this->getGenericTree($class, $parent);
$treeIterator = new TreeViewNodeIterator($generic);
$recursiveIterator = new \RecursiveIteratorIterator($treeIterator);
$recursiveIterator = new \RecursiveIteratorIterator($treeIterator, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($recursiveIterator as $item) {
/** @var $item TreeViewNode */
if ($selectedElement !== null && $item->getId() === $selectedElement->getID()) {
@ -71,7 +103,7 @@ class TreeViewGenerator
}
}
return $generic;
return array_merge($head, $generic);
}
/**
@ -85,11 +117,11 @@ class TreeViewGenerator
*/
public function getGenericTree(string $class, ?StructuralDBElement $parent = null) : array
{
if(!is_a($class, StructuralDBElement::class, true)) {
throw new \InvalidArgumentException('$class must be a class string that implements StructuralDBElement!');
if(!is_a($class, NamedDBElement::class, true)) {
throw new \InvalidArgumentException('$class must be a class string that implements StructuralDBElement or NamedDBElement!');
}
if($parent !== null && !is_a($parent, $class)) {
throw new \InvalidArgumentException('$parent must be of the type class!');
throw new \InvalidArgumentException('$parent must be of the type $class!');
}
/** @var StructuralDBElementRepository $repo */