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\Repository; |
23
|
|
|
|
24
|
|
|
use App\Entity\Base\StructuralDBElement; |
25
|
|
|
use App\Helpers\Trees\StructuralDBElementIterator; |
26
|
|
|
use App\Helpers\TreeViewNode; |
27
|
|
|
use Doctrine\ORM\EntityRepository; |
28
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
29
|
|
|
|
30
|
|
|
class StructuralDBElementRepository extends NamedDBElementRepository |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Finds all nodes without a parent node. They are our root nodes. |
34
|
|
|
* |
35
|
|
|
* @return StructuralDBElement[] |
36
|
|
|
*/ |
37
|
|
|
public function findRootNodes(): array |
38
|
|
|
{ |
39
|
|
|
return $this->findBy(['parent' => null], ['name' => 'ASC']); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Gets a tree of TreeViewNode elements. The root elements has $parent as parent. |
45
|
|
|
* The treeview is generic, that means the href are null and ID values are set. |
46
|
|
|
* @param StructuralDBElement|null $parent The parent the root elements should have. |
47
|
|
|
* @return TreeViewNode[] |
48
|
|
|
*/ |
49
|
|
|
public function getGenericNodeTree(?StructuralDBElement $parent = null) : array |
50
|
|
|
{ |
51
|
|
|
$result = []; |
52
|
|
|
|
53
|
|
|
$entities = $this->findBy(['parent' => $parent], ['name' => 'ASC']); |
54
|
|
|
foreach ($entities as $entity) { |
55
|
|
|
/** @var StructuralDBElement $entity */ |
56
|
|
|
//Make a recursive call to find all children nodes |
57
|
|
|
$children = $this->getGenericNodeTree($entity); |
58
|
|
|
$node = new TreeViewNode($entity->getName(), null, $children); |
59
|
|
|
//Set the ID of this entity to later be able to reconstruct the URL |
60
|
|
|
$node->setId($entity->getID()); |
61
|
|
|
$result[] = $node; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $result; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets a flattened hierarchical tree. Useful for generating option lists. |
69
|
|
|
* |
70
|
|
|
* @param StructuralDBElement|null $parent This entity will be used as root element. Set to null, to use global root |
71
|
|
|
* |
72
|
|
|
* @return StructuralDBElement[] A flattened list containing the tree elements. |
73
|
|
|
*/ |
74
|
|
|
public function toNodesList(?StructuralDBElement $parent = null): array |
75
|
|
|
{ |
76
|
|
|
$result = []; |
77
|
|
|
|
78
|
|
|
$entities = $this->findBy(['parent' => $parent], ['name' => 'ASC']); |
79
|
|
|
|
80
|
|
|
$elementIterator = new StructuralDBElementIterator($entities); |
81
|
|
|
$recursiveIterator = new \RecursiveIteratorIterator($elementIterator, \RecursiveIteratorIterator::SELF_FIRST); |
82
|
|
|
//$result = iterator_to_array($recursiveIterator); |
83
|
|
|
|
84
|
|
|
//We can not use iterator_to_array here or we get only the parent elements |
85
|
|
|
foreach($recursiveIterator as $item) { |
86
|
|
|
$result[] = $item; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $result; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|