1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Configuration; |
4
|
|
|
|
5
|
|
|
use JK\Configuration\Configuration; |
6
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
7
|
|
|
use Symfony\Component\OptionsResolver\Options; |
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
9
|
|
|
|
10
|
|
|
class MenuItemConfiguration extends Configuration |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $position; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $name; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* MenuItemConfiguration constructor. |
24
|
|
|
* |
25
|
|
|
* @param string $name |
26
|
|
|
* @param string $position |
27
|
|
|
*/ |
28
|
|
|
public function __construct(string $name, string $position) |
29
|
|
|
{ |
30
|
|
|
$this->position = $position; |
31
|
|
|
$this->name = $name; |
32
|
|
|
|
33
|
|
|
parent::__construct(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Define allowed parameters and values for this configuration, using optionsResolver component. |
38
|
|
|
* |
39
|
|
|
* @param OptionsResolver $resolver |
40
|
|
|
*/ |
41
|
|
|
public function configureOptions(OptionsResolver $resolver) |
42
|
|
|
{ |
43
|
|
|
// user can defined an admin name |
44
|
|
|
$resolver |
45
|
|
|
->setDefault('admin', null) |
46
|
|
|
->setDefault('action', null) |
47
|
|
|
->setDefault('route', null) |
48
|
|
|
->setDefault('url', null) |
49
|
|
|
->setDefault('parameters', []) |
50
|
|
|
->setDefault('text', '') |
51
|
|
|
->setDefault('attr', []) |
52
|
|
|
->setDefault('items', []) |
53
|
|
|
->setDefault('icon', null) |
54
|
|
|
->setDefault('link_css_class', 'nav-link') |
55
|
|
|
->setNormalizer('admin', function(Options $options, $adminName) { |
56
|
|
|
|
57
|
|
|
// user has to defined either an admin name and an action name, or a route name with optional |
58
|
|
|
// parameters, or an url |
59
|
|
|
if ($adminName === null |
60
|
|
|
&& $options->offsetGet('route') === null |
61
|
|
|
&& $options->offsetGet('url') === null |
62
|
|
|
) { |
63
|
|
|
|
64
|
|
|
throw new InvalidOptionsException( |
65
|
|
|
'You should either defined an admin name, or route name or an uri' |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $adminName; |
70
|
|
|
}) |
71
|
|
|
// if an admin name is set, an action name can provided. This action will be the menu link |
72
|
|
|
->setNormalizer('action', function(Options $options, $action) { |
73
|
|
|
|
74
|
|
|
// if an action name is provided, an admin name should be defined too |
75
|
|
|
if ($action !== null && $options->offsetGet('admin') === null) { |
76
|
|
|
throw new InvalidOptionsException( |
77
|
|
|
'You should provide an admin name for this action '.$action |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// default to list action |
82
|
|
|
if ($options->offsetGet('admin') !== null && $action === null) { |
83
|
|
|
$action = 'list'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $action; |
87
|
|
|
}) |
88
|
|
|
->setAllowedTypes('parameters', 'array') |
89
|
|
|
->setNormalizer('attr', function(Options $options, $attr) { |
90
|
|
|
|
91
|
|
|
if (!is_array($attr)) { |
92
|
|
|
$attr = []; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (empty($attr['id'])) { |
96
|
|
|
$attr['id'] = uniqid('admin-menu-'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ('horizontal' === $this->position && !key_exists('class', $attr)) { |
100
|
|
|
$attr['class'] = 'nav-item'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $attr; |
104
|
|
|
}) |
105
|
|
|
->setNormalizer('items', function(Options $options, $items) { |
106
|
|
|
if (!is_array($items)) { |
107
|
|
|
$items = []; |
108
|
|
|
} |
109
|
|
|
$resolver = new OptionsResolver(); |
110
|
|
|
$resolvedItems = []; |
111
|
|
|
|
112
|
|
|
foreach ($items as $name => $item) { |
113
|
|
|
$itemConfiguration = new MenuItemConfiguration($name, $this->position); |
114
|
|
|
$itemConfiguration->configureOptions($resolver); |
115
|
|
|
$itemConfiguration->setParameters($resolver->resolve($item)); |
116
|
|
|
|
117
|
|
|
$resolvedItems[$name] = $itemConfiguration; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $resolvedItems; |
121
|
|
|
}) |
122
|
|
|
->setNormalizer('text', function (Options $options, $text) { |
123
|
|
|
if (!$text) { |
124
|
|
|
// TODO use translation pattern key instead |
125
|
|
|
$text = ucfirst($this->name); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $text; |
129
|
|
|
}) |
130
|
|
|
; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|