-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathActionGroupDto.php
More file actions
312 lines (261 loc) · 7.88 KB
/
ActionGroupDto.php
File metadata and controls
312 lines (261 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
namespace EasyCorp\Bundle\EasyAdminBundle\Dto;
use EasyCorp\Bundle\EasyAdminBundle\Config\ActionGroup;
use EasyCorp\Bundle\EasyAdminBundle\Twig\Component\Option\ButtonStyle;
use EasyCorp\Bundle\EasyAdminBundle\Twig\Component\Option\ButtonVariant;
use Symfony\Contracts\Translation\TranslatableInterface;
/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
final class ActionGroupDto
{
private ?string $type = null;
private ?string $name = null;
private TranslatableInterface|string|false|null $label = null;
private ?string $icon = null;
private string $cssClass = '';
private string $addedCssClass = '';
/** @var array<string, string> */
private array $htmlAttributes = [];
private ?string $templatePath = null;
private ?ActionDto $mainAction = null;
/** @var array<int, ActionDto|array{type: string, content?: mixed}> */
private array $items = [];
/** @var callable|null */
private $displayCallable;
private ButtonVariant $variant = ButtonVariant::Default;
private ButtonStyle $style = ButtonStyle::Solid;
private bool $hasAnyActionWithIcon = false;
public function __clone(): void
{
if (null !== $this->mainAction) {
$this->mainAction = clone $this->mainAction;
}
foreach ($this->items as $index => $item) {
if ($item instanceof ActionDto) {
$this->items[$index] = clone $item;
}
}
}
// A utility method for Twig templates that deal with actions and action groups
public function isActionGroup(): bool
{
return true;
}
public function getType(): string
{
return $this->type;
}
public function setType(string $type): void
{
$this->type = $type;
}
public function isEntityAction(): bool
{
return ActionGroup::TYPE_ENTITY === $this->type;
}
public function isGlobalAction(): bool
{
return ActionGroup::TYPE_GLOBAL === $this->type;
}
public function isBatchAction(): bool
{
return false;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getLabel(): TranslatableInterface|string|false|null
{
return $this->label;
}
public function setLabel(TranslatableInterface|string|false|null $label): void
{
$this->label = $label;
}
public function getIcon(): ?string
{
return $this->icon;
}
public function setIcon(?string $icon): void
{
$this->icon = $icon;
}
public function getCssClass(): string
{
return trim($this->cssClass);
}
public function setCssClass(string $cssClass): void
{
$this->cssClass = $cssClass;
}
public function getAddedCssClass(): string
{
return trim($this->addedCssClass);
}
public function setAddedCssClass(string $cssClass): void
{
$this->addedCssClass .= ' '.$cssClass;
}
/**
* @return array<string, string>
*/
public function getHtmlAttributes(): array
{
return $this->htmlAttributes;
}
/**
* @param array<string, string> $htmlAttributes
*/
public function addHtmlAttributes(array $htmlAttributes): void
{
$this->htmlAttributes = array_merge($this->htmlAttributes, $htmlAttributes);
}
/**
* @param array<string, string> $htmlAttributes
*/
public function setHtmlAttributes(array $htmlAttributes): void
{
$this->htmlAttributes = $htmlAttributes;
}
public function setHtmlAttribute(string $attributeName, string $attributeValue): void
{
$this->htmlAttributes[$attributeName] = $attributeValue;
}
public function getTemplatePath(): ?string
{
return $this->templatePath;
}
public function setTemplatePath(string $templatePath): void
{
$this->templatePath = $templatePath;
}
public function getMainAction(): ?ActionDto
{
return $this->mainAction;
}
public function setMainAction(?ActionDto $mainAction): void
{
$this->mainAction = $mainAction;
}
public function hasMainAction(): bool
{
return null !== $this->mainAction;
}
/**
* Returns only the ActionDto items (excluding dividers and headers).
*
* @return ActionDto[]
*/
public function getActions(): array
{
return array_values(array_filter(
$this->items,
static fn ($item) => $item instanceof ActionDto
));
}
/**
* Returns all items including actions, dividers, and headers.
*
* @return array<int, ActionDto|array{type: string, content?: mixed}>
*/
public function getItems(): array
{
return $this->items;
}
/**
* @param list<ActionDto|mixed> $actions
*/
public function setActions(array $actions): void
{
$this->items = [];
foreach ($actions as $action) {
if ($action instanceof ActionDto) {
$this->items[] = $action;
}
}
}
public function addAction(ActionDto $action): void
{
$this->items[] = $action;
}
public function removeAction(string $actionName): void
{
$this->items = array_filter(
$this->items,
static fn ($item) => !($item instanceof ActionDto && $item->getName() === $actionName)
);
$this->items = array_values($this->items);
}
public function addDivider(): void
{
$this->items[] = ['type' => 'divider'];
}
public function addHeader(TranslatableInterface|string $label): void
{
$this->items[] = ['type' => 'header', 'content' => $label];
}
public function isDisplayed(?EntityDto $entityDto = null): bool
{
return null === $this->displayCallable || (bool) \call_user_func($this->displayCallable, $entityDto?->getInstance());
}
public function setDisplayCallable(callable $displayCallable): void
{
$this->displayCallable = $displayCallable;
}
public function getVariant(): ButtonVariant
{
return $this->variant;
}
public function setVariant(?ButtonVariant $variant): void
{
$this->variant = $variant ?? ButtonVariant::Default;
}
public function getStyle(): ButtonStyle
{
return $this->style;
}
public function hasAnyActionWithIcon(): bool
{
return $this->hasAnyActionWithIcon;
}
public function setHasAnyActionWithIcon(bool $hasAnyActionWithIcon): void
{
$this->hasAnyActionWithIcon = $hasAnyActionWithIcon;
}
public function getAsConfigObject(): ActionGroup
{
$action = ActionGroup::new($this->name, $this->label, $this->icon);
$action->setCssClass($this->cssClass);
$action->addCssClass($this->addedCssClass);
$action->setHtmlAttributes($this->htmlAttributes);
$action->setHtmlAttributes($this->htmlAttributes);
if (null !== $this->templatePath) {
$action->setTemplatePath($this->templatePath);
}
if ($this->isGlobalAction()) {
$action->createAsGlobalActionGroup();
}
if (null !== $this->mainAction) {
$action->addMainAction($this->mainAction->getAsConfigObject());
}
foreach ($this->items as $index => $item) {
if ($item instanceof ActionDto) {
$action->addAction($item->getAsConfigObject());
} elseif ('divider' === ($item['type'] ?? null)) {
$action->addDivider();
} elseif ('header' === ($item['type'] ?? null)) {
$action->addHeader($item['content']);
}
}
if (null !== $this->displayCallable) {
$action->displayIf($this->displayCallable);
}
return $action;
}
}