1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or |
8
|
|
|
* modify it under the terms of the GNU General Public License |
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
10
|
|
|
* of the License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program; if not, write to the Free Software |
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace App\Services\Trees; |
23
|
|
|
|
24
|
|
|
use App\Entity\Base\DBElement; |
25
|
|
|
use App\Entity\Base\NamedDBElement; |
26
|
|
|
use App\Entity\Base\StructuralDBElement; |
27
|
|
|
use App\Helpers\Trees\TreeViewNode; |
28
|
|
|
use App\Repository\StructuralDBElementRepository; |
29
|
|
|
use App\Services\EntityURLGenerator; |
30
|
|
|
use App\Services\UserCacheKeyGenerator; |
31
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
32
|
|
|
use Symfony\Contracts\Cache\ItemInterface; |
33
|
|
|
use Symfony\Contracts\Cache\TagAwareCacheInterface; |
34
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* This service gives you a flat list containing all structured entities in the order of the structure. |
38
|
|
|
*/ |
39
|
|
|
class NodesListBuilder |
40
|
|
|
{ |
41
|
|
|
protected $em; |
42
|
|
|
protected $cache; |
43
|
|
|
protected $keyGenerator; |
44
|
|
|
|
45
|
|
|
public function __construct( EntityManagerInterface $em, TagAwareCacheInterface $treeCache, UserCacheKeyGenerator $keyGenerator) |
46
|
|
|
{ |
47
|
|
|
$this->em = $em; |
48
|
|
|
$this->keyGenerator = $keyGenerator; |
49
|
|
|
$this->cache = $treeCache; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Gets a flattened hierachical tree. Useful for generating option lists. |
54
|
|
|
* In difference to the Repository Function, the results here are cached. |
55
|
|
|
* |
56
|
|
|
* @param string $class_name The class name of the entity you want to retrieve. |
57
|
|
|
* @param StructuralDBElement|null $parent This entity will be used as root element. Set to null, to use global root |
58
|
|
|
* |
59
|
|
|
* @return StructuralDBElement[] A flattened list containing the tree elements. |
60
|
|
|
*/ |
61
|
|
|
public function typeToNodesList(string $class_name, ?StructuralDBElement $parent = null): array |
62
|
|
|
{ |
63
|
|
|
$parent_id = null != $parent ? $parent->getID() : '0'; |
64
|
|
|
// Backslashes are not allowed in cache keys |
65
|
|
|
$secure_class_name = str_replace('\\', '_', $class_name); |
66
|
|
|
$key = 'list_'.$this->keyGenerator->generateKey().'_'.$secure_class_name.$parent_id; |
67
|
|
|
|
68
|
|
|
$ret = $this->cache->get($key, function (ItemInterface $item) use ($class_name, $parent, $secure_class_name) { |
69
|
|
|
// Invalidate when groups, a element with the class or the user changes |
70
|
|
|
$item->tag(['groups', 'tree_list', $this->keyGenerator->generateKey(), $secure_class_name]); |
71
|
|
|
/** |
72
|
|
|
* @var StructuralDBElementRepository |
73
|
|
|
*/ |
74
|
|
|
$repo = $this->em->getRepository($class_name); |
75
|
|
|
|
76
|
|
|
return $repo->toNodesList($parent); |
|
|
|
|
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
return $ret; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|