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 | declare(strict_types=1); |
||||
22 | |||||
23 | namespace App\DataTables\Column; |
||||
24 | |||||
25 | use App\Entity\Attachments\Attachment; |
||||
26 | use App\Entity\Base\AbstractDBElement; |
||||
27 | use App\Entity\Contracts\NamedElementInterface; |
||||
28 | use App\Entity\LogSystem\AbstractLogEntry; |
||||
29 | use App\Entity\LogSystem\UserNotAllowedLogEntry; |
||||
30 | use App\Entity\Parameters\AbstractParameter; |
||||
31 | use App\Entity\Parts\PartLot; |
||||
32 | use App\Entity\PriceInformations\Orderdetail; |
||||
33 | use App\Entity\PriceInformations\Pricedetail; |
||||
34 | use App\Entity\ProjectSystem\ProjectBOMEntry; |
||||
35 | use App\Exceptions\EntityNotSupportedException; |
||||
36 | use App\Repository\LogEntryRepository; |
||||
37 | use App\Services\ElementTypeNameGenerator; |
||||
38 | use App\Services\EntityURLGenerator; |
||||
39 | use Doctrine\ORM\EntityManagerInterface; |
||||
40 | use Omines\DataTablesBundle\Column\AbstractColumn; |
||||
41 | use Symfony\Component\OptionsResolver\OptionsResolver; |
||||
42 | use Symfony\Contracts\Translation\TranslatorInterface; |
||||
43 | |||||
44 | class LogEntryTargetColumn extends AbstractColumn |
||||
45 | { |
||||
46 | protected EntityManagerInterface $em; |
||||
47 | protected LogEntryRepository $entryRepository; |
||||
48 | protected EntityURLGenerator $entityURLGenerator; |
||||
49 | protected ElementTypeNameGenerator $elementTypeNameGenerator; |
||||
50 | protected TranslatorInterface $translator; |
||||
51 | |||||
52 | public function __construct(EntityManagerInterface $entityManager, EntityURLGenerator $entityURLGenerator, |
||||
53 | ElementTypeNameGenerator $elementTypeNameGenerator, TranslatorInterface $translator) |
||||
54 | { |
||||
55 | $this->em = $entityManager; |
||||
56 | $this->entryRepository = $entityManager->getRepository(AbstractLogEntry::class); |
||||
57 | |||||
58 | $this->entityURLGenerator = $entityURLGenerator; |
||||
59 | $this->elementTypeNameGenerator = $elementTypeNameGenerator; |
||||
60 | $this->translator = $translator; |
||||
61 | } |
||||
62 | |||||
63 | /** |
||||
64 | * @param $value |
||||
65 | * @return mixed |
||||
66 | */ |
||||
67 | public function normalize($value) |
||||
68 | { |
||||
69 | return $value; |
||||
70 | } |
||||
71 | |||||
72 | public function configureOptions(OptionsResolver $resolver): self |
||||
73 | { |
||||
74 | parent::configureOptions($resolver); |
||||
75 | $resolver->setDefault('show_associated', true); |
||||
76 | $resolver->setDefault('showAccessDeniedPath', true); |
||||
77 | |||||
78 | return $this; |
||||
79 | } |
||||
80 | |||||
81 | public function render($value, $context): string |
||||
82 | { |
||||
83 | if ($context instanceof UserNotAllowedLogEntry && $this->options['showAccessDeniedPath']) { |
||||
84 | return htmlspecialchars($context->getPath()); |
||||
85 | } |
||||
86 | |||||
87 | /** @var AbstractLogEntry $context */ |
||||
88 | $target = $this->entryRepository->getTargetElement($context); |
||||
89 | |||||
90 | $tmp = ''; |
||||
91 | |||||
92 | //The element is existing |
||||
93 | if ($target instanceof NamedElementInterface && !empty($target->getName())) { |
||||
94 | try { |
||||
95 | $tmp = sprintf( |
||||
96 | '<a href="%s">%s</a>', |
||||
97 | $this->entityURLGenerator->infoURL($target), |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
98 | $this->elementTypeNameGenerator->getTypeNameCombination($target, true) |
||||
99 | ); |
||||
100 | } catch (EntityNotSupportedException $exception) { |
||||
101 | $tmp = $this->elementTypeNameGenerator->getTypeNameCombination($target, true); |
||||
102 | } |
||||
103 | } elseif ($target instanceof AbstractDBElement) { //Target does not have a name |
||||
104 | $tmp = sprintf( |
||||
105 | '<i>%s</i>: %s', |
||||
106 | $this->elementTypeNameGenerator->getLocalizedTypeLabel($target), |
||||
107 | $target->getID() |
||||
108 | ); |
||||
109 | } elseif (null === $target && $context->hasTarget()) { //Element was deleted |
||||
110 | $tmp = sprintf( |
||||
111 | '<i>%s</i>: %s [%s]', |
||||
112 | $this->elementTypeNameGenerator->getLocalizedTypeLabel($context->getTargetClass()), |
||||
113 | $context->getTargetID(), |
||||
114 | $this->translator->trans('log.target_deleted') |
||||
115 | ); |
||||
116 | } |
||||
117 | |||||
118 | //Add a hint to the associated element if possible |
||||
119 | if (null !== $target && $this->options['show_associated']) { |
||||
120 | if ($target instanceof Attachment && null !== $target->getElement()) { |
||||
121 | $on = $target->getElement(); |
||||
122 | } elseif ($target instanceof AbstractParameter && null !== $target->getElement()) { |
||||
123 | $on = $target->getElement(); |
||||
124 | } elseif ($target instanceof PartLot && null !== $target->getPart()) { |
||||
125 | $on = $target->getPart(); |
||||
126 | } elseif ($target instanceof Orderdetail && null !== $target->getPart()) { |
||||
127 | $on = $target->getPart(); |
||||
128 | } elseif ($target instanceof Pricedetail && null !== $target->getOrderdetail() && null !== $target->getOrderdetail()->getPart()) { |
||||
129 | $on = $target->getOrderdetail()->getPart(); |
||||
130 | } elseif ($target instanceof ProjectBOMEntry && null !== $target->getProject()) { |
||||
131 | $on = $target->getProject(); |
||||
132 | } |
||||
133 | |||||
134 | if (isset($on) && is_object($on)) { |
||||
135 | try { |
||||
136 | $tmp .= sprintf( |
||||
137 | ' (<a href="%s">%s</a>)', |
||||
138 | $this->entityURLGenerator->infoURL($on), |
||||
139 | $this->elementTypeNameGenerator->getTypeNameCombination($on, true) |
||||
140 | ); |
||||
141 | } catch (EntityNotSupportedException $exception) { |
||||
142 | $tmp .= ' ('.$this->elementTypeNameGenerator->getTypeNameCombination($target, true).')'; |
||||
0 ignored issues
–
show
It seems like
$target can also be of type App\Entity\Base\AbstractDBElement and App\Entity\PriceInformations\Pricedetail and App\Entity\ProjectSystem\ProjectBOMEntry ; however, parameter $entity of App\Services\ElementType...etTypeNameCombination() does only seem to accept App\Entity\Contracts\NamedElementInterface , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
143 | } |
||||
144 | } |
||||
145 | } |
||||
146 | |||||
147 | //Log is not associated with an element |
||||
148 | return $tmp; |
||||
149 | } |
||||
150 | } |
||||
151 |