Part-DB.Part-DB-server/src/Services/Trees/ToolsTreeBuilder.php

307 lines
12 KiB
PHP
Raw Normal View History

2019-03-24 15:25:40 +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);
2019-03-24 15:25:40 +01:00
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
2019-03-24 15:25:40 +01:00
*
2019-11-01 13:40:30 +01:00
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
2019-03-24 15:25:40 +01:00
*
* 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;
2019-03-24 15:25:40 +01:00
use App\Entity\Attachments\AttachmentType;
use App\Entity\Attachments\PartAttachment;
use App\Entity\Devices\Device;
use App\Entity\LabelSystem\LabelProfile;
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;
2019-03-26 15:57:51 +01:00
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2019-08-20 12:52:12 +02:00
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
2019-03-26 15:57:51 +01:00
use Symfony\Contracts\Translation\TranslatorInterface;
2019-03-24 15:25:40 +01:00
/**
* 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.
2019-03-24 15:25:40 +01:00
*/
class ToolsTreeBuilder
{
2022-09-18 22:59:31 +02:00
protected TranslatorInterface $translator;
protected UrlGeneratorInterface $urlGenerator;
protected UserCacheKeyGenerator $keyGenerator;
protected TagAwareCacheInterface $cache;
protected Security $security;
2019-03-26 15:57:51 +01:00
2019-08-20 12:52:12 +02:00
public function __construct(TranslatorInterface $translator, UrlGeneratorInterface $urlGenerator,
TagAwareCacheInterface $treeCache, UserCacheKeyGenerator $keyGenerator,
Security $security)
2019-03-26 15:57:51 +01:00
{
$this->translator = $translator;
$this->urlGenerator = $urlGenerator;
2019-08-20 12:52:12 +02:00
$this->cache = $treeCache;
$this->keyGenerator = $keyGenerator;
$this->security = $security;
2019-08-20 12:52:12 +02:00
}
2019-03-26 15:57:51 +01:00
2019-03-24 15:25:40 +01:00
/**
* Generates the tree for the tools menu.
2019-08-20 12:52:12 +02:00
* The result is cached.
*
2020-08-21 21:36:22 +02:00
* @return TreeViewNode[] the array containing all Nodes for the tools menu
2019-03-24 15:25:40 +01:00
*/
public function getTree(): array
2019-03-24 15:25:40 +01:00
{
$key = 'tree_tools_'.$this->keyGenerator->generateKey();
2019-08-20 12:52:12 +02:00
return $this->cache->get($key, function (ItemInterface $item) {
2019-08-20 12:52:12 +02:00
//Invalidate tree, whenever group or the user changes
$item->tag(['tree_tools', 'groups', $this->keyGenerator->generateKey()]);
2019-08-20 12:52:12 +02:00
$tree = [];
2020-06-03 22:28:38 +02:00
if (!empty($this->getToolsNode())) {
2022-08-13 02:40:43 +02:00
$tree[] = (new TreeViewNode($this->translator->trans('tree.tools.tools'), null, $this->getToolsNode()))
->setIcon('fa-fw fa-treeview fa-solid fa-toolbox');
2020-06-03 22:28:38 +02:00
}
if (!empty($this->getEditNodes())) {
2022-08-13 02:40:43 +02:00
$tree[] = (new TreeViewNode($this->translator->trans('tree.tools.edit'), null, $this->getEditNodes()))
->setIcon('fa-fw fa-treeview fa-solid fa-pen-to-square');
2020-06-03 22:28:38 +02:00
}
if (!empty($this->getShowNodes())) {
2022-08-13 02:40:43 +02:00
$tree[] = (new TreeViewNode($this->translator->trans('tree.tools.show'), null, $this->getShowNodes()))
->setIcon('fa-fw fa-treeview fa-solid fa-eye');
2020-06-03 22:28:38 +02:00
}
if (!empty($this->getSystemNodes())) {
2022-08-13 02:40:43 +02:00
$tree[] = (new TreeViewNode($this->translator->trans('tree.tools.system'), null, $this->getSystemNodes()))
->setIcon('fa-fw fa-treeview fa-solid fa-server');
2020-06-03 22:28:38 +02:00
}
2019-08-20 12:52:12 +02:00
return $tree;
});
}
2019-03-24 15:25:40 +01:00
2020-04-16 19:56:30 +02:00
protected function getToolsNode(): array
{
$nodes = [];
if ($this->security->isGranted('@labels.create_labels')) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
2020-05-04 23:21:58 +02:00
$this->translator->trans('tree.tools.tools.label_dialog'),
$this->urlGenerator->generate('label_dialog')
2022-08-13 02:40:43 +02:00
))->setIcon("fa-treeview fa-fw fa-solid fa-qrcode");
2020-05-04 23:21:58 +02:00
}
2020-04-16 19:56:30 +02:00
if ($this->security->isGranted('@tools.label_scanner')) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.tools.label_scanner'),
$this->urlGenerator->generate('scan_dialog')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-treeview fa-fw fa-solid fa-camera-retro');
}
if ($this->security->isGranted('@tools.reel_calculator')) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.tools.reel_calculator'),
$this->urlGenerator->generate('tools_reel_calculator')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-treeview fa-fw fa-solid fa-ruler');
}
2020-04-16 19:56:30 +02:00
return $nodes;
}
2019-08-20 12:52:12 +02:00
/**
* This functions creates a tree entries for the "edit" node of the tool's tree.
*
2019-08-20 12:52:12 +02:00
* @return TreeViewNode[]
*/
protected function getEditNodes(): array
2019-08-20 12:52:12 +02:00
{
$nodes = [];
2019-08-12 23:45:21 +02:00
if ($this->security->isGranted('read', new AttachmentType())) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.attachment_types'),
$this->urlGenerator->generate('attachment_type_new')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-file-alt');
}
if ($this->security->isGranted('read', new Category())) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.categories'),
$this->urlGenerator->generate('category_new')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-tags');
}
if ($this->security->isGranted('read', new Device())) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.devices'),
$this->urlGenerator->generate('device_new')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-archive');
}
if ($this->security->isGranted('read', new Supplier())) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.suppliers'),
$this->urlGenerator->generate('supplier_new')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-truck');
}
if ($this->security->isGranted('read', new Manufacturer())) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.manufacturer'),
$this->urlGenerator->generate('manufacturer_new')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-industry');
}
if ($this->security->isGranted('read', new Storelocation())) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.storelocation'),
$this->urlGenerator->generate('store_location_new')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-cube');
}
if ($this->security->isGranted('read', new Footprint())) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.footprint'),
$this->urlGenerator->generate('footprint_new')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-microchip');
}
if ($this->security->isGranted('read', new Currency())) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.currency'),
$this->urlGenerator->generate('currency_new')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-coins');
}
if ($this->security->isGranted('read', new MeasurementUnit())) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.measurement_unit'),
$this->urlGenerator->generate('measurement_unit_new')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-balance-scale');
}
2020-05-04 23:21:58 +02:00
if ($this->security->isGranted('read', new LabelProfile())) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.label_profile'),
$this->urlGenerator->generate('label_profile_new')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-qrcode');
}
if ($this->security->isGranted('create', new Part())) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.edit.part'),
$this->urlGenerator->generate('part_new')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-plus-square');
}
2019-03-24 15:25:40 +01:00
2019-08-20 12:52:12 +02:00
return $nodes;
}
2019-03-24 15:25:40 +01:00
2019-08-20 12:52:12 +02:00
/**
* This function creates the tree entries for the "show" node of the tools tree.
*
2019-08-20 12:52:12 +02:00
* @return TreeViewNode[]
*/
protected function getShowNodes(): array
2019-08-20 12:52:12 +02:00
{
$show_nodes = [];
if ($this->security->isGranted('@parts.read')) {
$show_nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.show.all_parts'),
$this->urlGenerator->generate('parts_show_all')
))->setIcon('fa-fw fa-treeview fa-solid fa-globe');
}
2019-03-26 15:57:51 +01:00
if ($this->security->isGranted('@attachments.list_attachments')) {
2022-08-13 02:40:43 +02:00
$show_nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.show.all_attachments'),
$this->urlGenerator->generate('attachment_list')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-paperclip');
}
if ($this->security->isGranted('@tools.statistics')) {
2022-08-13 02:40:43 +02:00
$show_nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.show.statistics'),
$this->urlGenerator->generate('statistics_view')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-chart-bar');
}
2019-08-20 12:52:12 +02:00
return $show_nodes;
}
/**
* This function creates the tree entries for the "system" node of the tools tree.
*/
protected function getSystemNodes(): array
2019-08-20 12:52:12 +02:00
{
$nodes = [];
2019-08-20 12:52:12 +02:00
if ($this->security->isGranted('read', new User())) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.system.users'),
$this->urlGenerator->generate('user_new')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-user');
}
if ($this->security->isGranted('read', new Group())) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.system.groups'),
$this->urlGenerator->generate('group_new')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-users');
}
if ($this->security->isGranted('@system.show_logs')) {
2022-08-13 02:40:43 +02:00
$nodes[] = (new TreeViewNode(
$this->translator->trans('tree.tools.system.event_log'),
$this->urlGenerator->generate('log_view')
2022-08-13 02:40:43 +02:00
))->setIcon('fa-fw fa-treeview fa-solid fa-binoculars');
}
if ($this->security->isGranted('@system.server_infos')) {
$nodes[] = (new TreeViewNode(
$this->translator->trans('tools.server_infos.title'),
$this->urlGenerator->generate('tools_server_infos')
))->setIcon('fa-fw fa-treeview fa-solid fa-database');
}
return $nodes;
2019-03-24 15:25:40 +01:00
}
}