1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* part-db version 0.1 |
5
|
|
|
* Copyright (C) 2005 Christoph Lechner |
6
|
|
|
* http://www.cl-projects.de/ |
7
|
|
|
* |
8
|
|
|
* part-db version 0.2+ |
9
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
10
|
|
|
* http://code.google.com/p/part-db/ |
11
|
|
|
* |
12
|
|
|
* Part-DB Version 0.4+ |
13
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
14
|
|
|
* https://github.com/jbtronics |
15
|
|
|
* |
16
|
|
|
* This program is free software; you can redistribute it and/or |
17
|
|
|
* modify it under the terms of the GNU General Public License |
18
|
|
|
* as published by the Free Software Foundation; either version 2 |
19
|
|
|
* of the License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU General Public License |
27
|
|
|
* along with this program; if not, write to the Free Software |
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace App\Services; |
33
|
|
|
|
34
|
|
|
use App\Entity\DBElement; |
35
|
|
|
use App\Entity\NamedDBElement; |
36
|
|
|
use App\Entity\StructuralDBElement; |
37
|
|
|
use App\Helpers\TreeViewNode; |
38
|
|
|
use App\Repository\StructuralDBElementRepository; |
39
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
40
|
|
|
use Symfony\Bundle\MakerBundle\Str; |
41
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* This service gives you multiple possibilities to generate trees. |
45
|
|
|
* |
46
|
|
|
* @package App\Controller |
47
|
|
|
*/ |
48
|
|
|
class TreeBuilder |
49
|
|
|
{ |
50
|
|
|
protected $url_generator; |
51
|
|
|
protected $em; |
52
|
|
|
protected $translator; |
53
|
|
|
|
54
|
|
|
public function __construct(EntityURLGenerator $URLGenerator, EntityManagerInterface $em, TranslatorInterface $translator) |
55
|
|
|
{ |
56
|
|
|
$this->url_generator = $URLGenerator; |
57
|
|
|
$this->em = $em; |
58
|
|
|
$this->translator = $translator; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Generates a tree for the given Element. The given element is the top node, all children are child nodes. |
63
|
|
|
* @param StructuralDBElement $element The element for which the tree should be generated. |
64
|
|
|
* @param string $href_type The type of the links that should be used for the links. Set to null, to disable links. |
65
|
|
|
* See EntityURLGenerator::getURL for possible types. |
66
|
|
|
* @param DBElement|null $selectedElement When a element is given here, its tree node will be marked as selected in |
67
|
|
|
* the resulting tree. When $selectedElement is not existing in the tree, then nothing happens. |
68
|
|
|
* @return TreeViewNode The Node for the given Element. |
69
|
|
|
* @throws \App\Exceptions\EntityNotSupported |
70
|
|
|
*/ |
71
|
|
|
public function elementToTreeNode(NamedDBElement $element, ?string $href_type = 'list_parts', DBElement $selectedElement = null) : TreeViewNode |
72
|
|
|
{ |
73
|
|
|
$children_nodes = null; |
74
|
|
|
|
75
|
|
|
if ($element instanceof StructuralDBElement) { |
76
|
|
|
$children = $element->getSubelements(); |
77
|
|
|
foreach ($children as $child) { |
78
|
|
|
$children_nodes[] = $this->elementToTreeNode($child, $href_type, $selectedElement); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
//Check if we need to generate a href type |
83
|
|
|
$href = null; |
84
|
|
|
|
85
|
|
|
if (!empty($href_type)) { |
86
|
|
|
$href = $this->url_generator->getURL($element, $href_type); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$tree_node = new TreeViewNode($element->__toString(), $href, $children_nodes); |
90
|
|
|
|
91
|
|
|
if($children_nodes != null) { |
92
|
|
|
$tree_node->addTag((string) count($children_nodes)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
//Check if we need to select the current part |
96
|
|
|
if ($selectedElement !== null && $element->getID() === $selectedElement->getID()) { |
97
|
|
|
$tree_node->setSelected(true); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $tree_node; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Generates a tree for all elements of the given type |
105
|
|
|
* @param StructuralDBElement $class_name The class name of the StructuralDBElement class for which the tree should |
106
|
|
|
* be generated. |
107
|
|
|
* @param string $href_type The type of the links that should be used for the links. Set to null, to disable links. |
108
|
|
|
* See EntityURLGenerator::getURL for possible types. |
109
|
|
|
* @param DBElement|null $selectedElement When a element is given here, its tree node will be marked as selected in |
110
|
|
|
* the resulting tree. When $selectedElement is not existing in the tree, then nothing happens. |
111
|
|
|
* @return TreeViewNode[] Returns an array, containing all nodes. It is empty if the given class has no elements. |
112
|
|
|
* @throws \App\Exceptions\EntityNotSupported |
113
|
|
|
*/ |
114
|
|
|
public function typeToTree(string $class_name, ?string $href_type = 'list_parts', DBElement $selectedElement = null) : array |
115
|
|
|
{ |
116
|
|
|
/** |
117
|
|
|
* @var $repo StructuralDBElementRepository |
118
|
|
|
*/ |
119
|
|
|
$repo = $this->em->getRepository($class_name); |
120
|
|
|
|
121
|
|
|
if (new $class_name() instanceof StructuralDBElement) { |
122
|
|
|
$root_nodes = $repo->findRootNodes(); |
123
|
|
|
} else { |
124
|
|
|
$root_nodes = $repo->findAll(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$array = array(); |
128
|
|
|
|
129
|
|
|
//When we use the newEdit type, add the New Element node. |
130
|
|
|
if ($href_type === 'newEdit') { |
131
|
|
|
//Generate the url for the new node |
132
|
|
|
$href = $this->url_generator->createURL(new $class_name()); |
133
|
|
|
$new_node = new TreeViewNode($this->translator->trans('entity.tree.new'), $href); |
134
|
|
|
//When the id of the selected element is null, then we have a new element, and we need to select "new" node |
135
|
|
|
if ($selectedElement != null && $selectedElement->getID() == null) { |
136
|
|
|
$new_node->setSelected(true); |
137
|
|
|
} |
138
|
|
|
$array[] = $new_node; |
139
|
|
|
//Add spacing |
140
|
|
|
$array[] = (new TreeViewNode(''))->setDisabled(true); |
141
|
|
|
|
142
|
|
|
//Every other treeNode will be used for edit |
143
|
|
|
$href_type = "edit"; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
foreach ($root_nodes as $node) { |
147
|
|
|
$array[] = $this->elementToTreeNode($node, $href_type, $selectedElement); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $array; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|