1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (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 Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace App\Twig; |
22
|
|
|
|
23
|
|
|
use App\Entity\Attachments\Attachment; |
24
|
|
|
use App\Entity\Base\AbstractDBElement; |
25
|
|
|
use App\Entity\ProjectSystem\Project; |
26
|
|
|
use App\Entity\LabelSystem\LabelProfile; |
27
|
|
|
use App\Entity\Parts\Category; |
28
|
|
|
use App\Entity\Parts\Footprint; |
29
|
|
|
use App\Entity\Parts\Manufacturer; |
30
|
|
|
use App\Entity\Parts\MeasurementUnit; |
31
|
|
|
use App\Entity\Parts\Part; |
32
|
|
|
use App\Entity\Parts\Storelocation; |
33
|
|
|
use App\Entity\Parts\Supplier; |
34
|
|
|
use App\Entity\PriceInformations\Currency; |
35
|
|
|
use App\Entity\UserSystem\Group; |
36
|
|
|
use App\Entity\UserSystem\User; |
37
|
|
|
use App\Services\ElementTypeNameGenerator; |
38
|
|
|
use App\Services\EntityURLGenerator; |
39
|
|
|
use App\Services\Trees\TreeViewGenerator; |
40
|
|
|
use Twig\Extension\AbstractExtension; |
41
|
|
|
use Twig\TwigFunction; |
42
|
|
|
use Twig\TwigTest; |
43
|
|
|
|
44
|
|
|
final class EntityExtension extends AbstractExtension |
45
|
|
|
{ |
46
|
|
|
protected EntityURLGenerator $entityURLGenerator; |
47
|
|
|
protected TreeViewGenerator $treeBuilder; |
48
|
|
|
private ElementTypeNameGenerator $nameGenerator; |
49
|
|
|
|
50
|
|
|
public function __construct(EntityURLGenerator $entityURLGenerator, TreeViewGenerator $treeBuilder, ElementTypeNameGenerator $elementTypeNameGenerator) |
51
|
|
|
{ |
52
|
|
|
$this->entityURLGenerator = $entityURLGenerator; |
53
|
|
|
$this->treeBuilder = $treeBuilder; |
54
|
|
|
$this->nameGenerator = $elementTypeNameGenerator; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getTests(): array |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
|
|
/* Checks if the given variable is an entitity (instance of AbstractDBElement) */ |
61
|
|
|
new TwigTest('entity', static function ($var) { |
62
|
|
|
return $var instanceof AbstractDBElement; |
63
|
|
|
}), |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getFunctions(): array |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
/* Returns a string representation of the given entity */ |
71
|
|
|
new TwigFunction('entity_type', [$this, 'getEntityType']), |
72
|
|
|
/* Returns the URL to the given entity */ |
73
|
|
|
new TwigFunction('entity_url', [$this, 'generateEntityURL']), |
74
|
|
|
/* Generates a JSON array of the given tree */ |
75
|
|
|
new TwigFunction('tree_data', [$this, 'treeData']), |
76
|
|
|
|
77
|
|
|
/* Gets a human readable label for the type of the given entity */ |
78
|
|
|
new TwigFunction('entity_type_label', [$this->nameGenerator, 'getLocalizedTypeLabel']), |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function treeData(AbstractDBElement $element, string $type = 'newEdit'): string |
83
|
|
|
{ |
84
|
|
|
$tree = $this->treeBuilder->getTreeView(get_class($element), null, $type, $element); |
85
|
|
|
|
86
|
|
|
return json_encode($tree, JSON_THROW_ON_ERROR); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function generateEntityURL(AbstractDBElement $entity, string $method = 'info'): string |
90
|
|
|
{ |
91
|
|
|
return $this->entityURLGenerator->getURL($entity, $method); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getEntityType(object $entity): ?string |
95
|
|
|
{ |
96
|
|
|
$map = [ |
97
|
|
|
Part::class => 'part', |
98
|
|
|
Footprint::class => 'footprint', |
99
|
|
|
Storelocation::class => 'storelocation', |
100
|
|
|
Manufacturer::class => 'manufacturer', |
101
|
|
|
Category::class => 'category', |
102
|
|
|
Project::class => 'device', |
103
|
|
|
Attachment::class => 'attachment', |
104
|
|
|
Supplier::class => 'supplier', |
105
|
|
|
User::class => 'user', |
106
|
|
|
Group::class => 'group', |
107
|
|
|
Currency::class => 'currency', |
108
|
|
|
MeasurementUnit::class => 'measurement_unit', |
109
|
|
|
LabelProfile::class => 'label_profile', |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
foreach ($map as $class => $type) { |
113
|
|
|
if ($entity instanceof $class) { |
114
|
|
|
return $type; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return false; |
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |