Passed
Push — master ( b8da4c...47584d )
by Jan
07:00
created

generateChoiceValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 18
rs 10
c 1
b 0
f 0
1
<?php
2
/*
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 *  Copyright (C) 2019 - 2023 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\Form\Type\Helper;
22
23
use App\Entity\Attachments\AttachmentType;
24
use App\Entity\Base\AbstractStructuralDBElement;
25
use App\Entity\PriceInformations\Currency;
26
use App\Services\Attachments\AttachmentURLGenerator;
27
use RuntimeException;
28
use Symfony\Component\Intl\Currencies;
29
use Symfony\Component\OptionsResolver\Options;
30
use Symfony\Contracts\Translation\TranslatorInterface;
31
32
class StructuralEntityChoiceHelper
33
{
34
35
    private AttachmentURLGenerator $attachmentURLGenerator;
36
    private TranslatorInterface $translator;
37
38
    public function __construct(AttachmentURLGenerator $attachmentURLGenerator, TranslatorInterface $translator)
39
    {
40
        $this->attachmentURLGenerator = $attachmentURLGenerator;
41
        $this->translator = $translator;
42
    }
43
44
    /**
45
     * Generates the choice attributes for the given AbstractStructuralDBElement.
46
     * @param  AbstractStructuralDBElement  $choice
47
     * @param Options|array $options
48
     * @return array|string[]
49
     */
50
    public function generateChoiceAttr(AbstractStructuralDBElement $choice, $options): array
51
    {
52
        $tmp = [];
53
54
        //Disable attribute if the choice is marked as not selectable
55
        if (($options['disable_not_selectable'] ?? false) && $choice->isNotSelectable()) {
56
            $tmp += ['disabled' => 'disabled'];
57
        }
58
59
        if ($choice instanceof AttachmentType) {
60
            $tmp += ['data-filetype_filter' => $choice->getFiletypeFilter()];
61
        }
62
63
        $level = $choice->getLevel();
64
        /** @var AbstractStructuralDBElement|null $parent */
65
        $parent = $options['subentities_of'] ?? null;
66
        if (null !== $parent) {
67
            $level -= $parent->getLevel() - 1;
68
        }
69
70
        $tmp += [
71
            'data-level' => $level,
72
            'data-parent' => $choice->getParent() ? $choice->getParent()->getFullPath() : null,
73
            'data-path' => $choice->getFullPath('->'),
74
            'data-image' => $choice->getMasterPictureAttachment() ? $this->attachmentURLGenerator->getThumbnailURL($choice->getMasterPictureAttachment(), 'thumbnail_xs') : null,
75
        ];
76
77
        if ($choice instanceof AttachmentType && !empty($choice->getFiletypeFilter())) {
78
            $tmp += ['data-filetype_filter' => $choice->getFiletypeFilter()];
79
        }
80
81
        return $tmp;
82
    }
83
84
    /**
85
     * Generates the choice attributes for the given AbstractStructuralDBElement.
86
     * @param  Currency $choice
87
     * @param Options|array $options
88
     * @return array|string[]
89
     */
90
    public function generateChoiceAttrCurrency(Currency $choice, $options): array
91
    {
92
        $tmp = $this->generateChoiceAttr($choice, $options);
93
94
        if(!empty($choice->getIsoCode())) {
95
            $symbol = Currencies::getSymbol($choice->getIsoCode());
96
        } else {
97
            $symbol = null;
98
        }
99
100
        if ($options['short']) {
101
            $tmp['data-short'] = $symbol;
102
        } else {
103
            $tmp['data-short'] = $choice->getName();
104
        }
105
106
        $tmp += [
107
            'data-symbol' => $symbol,
108
        ];
109
110
        return $tmp;
111
    }
112
113
    /**
114
     * Returns the choice label for the given AbstractStructuralDBElement.
115
     * @param  AbstractStructuralDBElement  $choice
116
     * @return string
117
     */
118
    public function generateChoiceLabel(AbstractStructuralDBElement $choice): string
119
    {
120
        return $choice->getName();
121
    }
122
123
    /**
124
     * Returns the choice value for the given AbstractStructuralDBElement.
125
     * @param  AbstractStructuralDBElement|null  $element
126
     * @return string|int|null
127
     */
128
    public function generateChoiceValue(?AbstractStructuralDBElement $element)
129
    {
130
        if ($element === null) {
131
            return null;
132
        }
133
134
        /**
135
         * Do not change the structure below, even when inspection says it can be replaced with a null coalescing operator.
136
         * It is important that the value returned here for a existing element is an int, and for a new element a string.
137
         * I dont really understand why, but it seems to be important for the choice_loader to work correctly.
138
         * So please do not change this!
139
         */
140
        if ($element->getID() === null) {
141
            //Must be the same as the separator in the choice_loader, otherwise this will not work!
142
            return $element->getFullPath('->');
143
        }
144
145
        return $element->getID();
146
    }
147
148
    /**
149
     * @param  AbstractStructuralDBElement|null  $element
150
     * @return string|null
151
     */
152
    public function generateGroupBy(AbstractStructuralDBElement $element): ?string
153
    {
154
        //Show entities that are not added to DB yet separately from other entities
155
        if ($element->getID() === null) {
156
            return $this->translator->trans('entity.select.group.new_not_added_to_DB');
157
        }
158
159
        return null;
160
    }
161
}