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\Form\Type; |
24
|
|
|
|
25
|
|
|
use App\Entity\Attachments\AttachmentType; |
26
|
|
|
use App\Entity\Base\AbstractStructuralDBElement; |
27
|
|
|
use App\Entity\PriceInformations\Currency; |
28
|
|
|
use App\Services\Attachments\AttachmentURLGenerator; |
29
|
|
|
use App\Services\Trees\NodesListBuilder; |
30
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
31
|
|
|
use RuntimeException; |
32
|
|
|
use Symfony\Component\Intl\Currencies; |
33
|
|
|
use Symfony\Component\OptionsResolver\Options; |
34
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* An entity to select a currency shortly |
38
|
|
|
*/ |
39
|
|
|
class CurrencyEntityType extends StructuralEntityType |
40
|
|
|
{ |
41
|
|
|
protected ?string $base_currency; |
42
|
|
|
|
43
|
|
|
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, AttachmentURLGenerator $attachmentURLGenerator, ?string $base_currency) |
44
|
|
|
{ |
45
|
|
|
parent::__construct($em, $builder, $attachmentURLGenerator); |
46
|
|
|
$this->base_currency = $base_currency; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function configureOptions(OptionsResolver $resolver): void |
50
|
|
|
{ |
51
|
|
|
//Important to call the parent resolver! |
52
|
|
|
parent::configureOptions($resolver); |
53
|
|
|
|
54
|
|
|
$resolver->setDefault('class', Currency::class); |
55
|
|
|
$resolver->setDefault('disable_not_selectable', true); |
56
|
|
|
$resolver->setDefault('choice_translation_domain', false); |
57
|
|
|
|
58
|
|
|
// This options allows you to override the currency shown for the null value |
59
|
|
|
$resolver->setDefault('base_currency', null); |
60
|
|
|
|
61
|
|
|
$resolver->setDefault('empty_message', function (Options $options) { |
62
|
|
|
//By default we use the global base currency: |
63
|
|
|
$iso_code = $this->base_currency; |
64
|
|
|
|
65
|
|
|
if ($options['base_currency']) { //Allow to override it |
66
|
|
|
$iso_code = $options['base_currency']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return Currencies::getSymbol($iso_code); |
|
|
|
|
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
$resolver->setDefault('used_to_select_parent', false); |
73
|
|
|
|
74
|
|
|
//If short is set to true, then the name of the entity will only shown in the dropdown list not in the selected value. |
75
|
|
|
$resolver->setDefault('short', false); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function generateChoiceAttr(AbstractStructuralDBElement $choice, $key, $value, $options): array |
79
|
|
|
{ |
80
|
|
|
$tmp = parent::generateChoiceAttr($choice, $key, $value, $options); |
81
|
|
|
|
82
|
|
|
if(!empty($choice->getIsoCode())) { |
|
|
|
|
83
|
|
|
$symbol = Currencies::getSymbol($choice->getIsoCode()); |
84
|
|
|
} else { |
85
|
|
|
$symbol = null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($options['short']) { |
89
|
|
|
$tmp['data-short'] = $symbol; |
90
|
|
|
} else { |
91
|
|
|
$tmp['data-short'] = $choice->getName(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$tmp += [ |
95
|
|
|
'data-symbol' => $symbol, |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
return $tmp; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function getChoiceContent(AbstractStructuralDBElement $choice, $key, $value, $options): string |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
if(!$choice instanceof Currency) { |
104
|
|
|
throw new RuntimeException('$choice must be an instance of Currency!'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
//Generate the level spacing |
108
|
|
|
/** @var AbstractStructuralDBElement|null $parent */ |
109
|
|
|
$parent = $options['subentities_of']; |
110
|
|
|
/*** @var AbstractStructuralDBElement $choice */ |
111
|
|
|
$level = $choice->getLevel(); |
112
|
|
|
//If our base entity is not the root level, we need to change the level, to get zero position |
113
|
|
|
if (null !== $options['subentities_of']) { |
114
|
|
|
$level -= $parent->getLevel() - 1; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$tmp = str_repeat('<span class="picker-level"></span>', $level); |
118
|
|
|
|
119
|
|
|
//Show currency symbol or ISO code and the name of the currency |
120
|
|
|
if(!empty($choice->getIsoCode())) { |
121
|
|
|
$tmp .= Currencies::getSymbol($choice->getIsoCode()); |
122
|
|
|
//Add currency name as badge |
123
|
|
|
$tmp .= sprintf('<span class="badge bg-primary ms-2 %s">%s</span>', $options['short'] ? 'picker-hs' : '' , htmlspecialchars($choice->getName())); |
124
|
|
|
} else { |
125
|
|
|
$tmp .= htmlspecialchars($choice->getName()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $tmp; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|