|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Menu\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\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
|
|
|
* Define allowed parameters and values for this configuration, using optionsResolver component. |
|
14
|
|
|
* |
|
15
|
|
|
* @param OptionsResolver $resolver |
|
16
|
|
|
*/ |
|
17
|
1 |
|
public function configureOptions(OptionsResolver $resolver) |
|
18
|
|
|
{ |
|
19
|
|
|
// user can defined an admin name |
|
20
|
|
|
$resolver |
|
21
|
1 |
|
->setDefault('admin', null) |
|
22
|
|
|
->setNormalizer('admin', function(Options $options, $adminName) { |
|
23
|
|
|
|
|
24
|
|
|
// user has to defined either an admin name and an action name, or a route name with optional |
|
25
|
|
|
// parameters, or an url |
|
26
|
|
|
if ($adminName === null |
|
27
|
1 |
|
&& $options->offsetGet('route') === null |
|
28
|
1 |
|
&& $options->offsetGet('url') === null |
|
29
|
1 |
|
) { |
|
30
|
|
|
|
|
31
|
|
|
throw new InvalidOptionsException( |
|
32
|
|
|
'You should either defined an admin name, or route name or an uri' |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
return $adminName; |
|
37
|
1 |
|
}); |
|
38
|
|
|
|
|
39
|
|
|
// if an admin name is set, an action name can provided. This action will be the menu link |
|
40
|
|
|
$resolver |
|
41
|
1 |
|
->setDefault('action', null) |
|
42
|
|
|
->setNormalizer('action', function(Options $options, $action) { |
|
43
|
|
|
|
|
44
|
|
|
// if an action name is provided, an admin name should be defined too |
|
45
|
1 |
|
if ($action !== null && $options->offsetGet('admin') === null) { |
|
46
|
|
|
throw new InvalidOptionsException( |
|
47
|
|
|
'You should provide an admin name for this action '.$action |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// default to list action |
|
52
|
1 |
|
if ($options->offsetGet('admin') !== null && $action === null) { |
|
53
|
|
|
$action = 'list'; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return $action; |
|
57
|
1 |
|
}); |
|
58
|
|
|
|
|
59
|
|
|
// a route can also be provided |
|
60
|
|
|
$resolver |
|
61
|
1 |
|
->setDefault('route', null) |
|
62
|
1 |
|
->setDefault('url', null) |
|
63
|
1 |
|
->setDefault('parameters', []) |
|
64
|
1 |
|
->setAllowedTypes('parameters', 'array'); |
|
65
|
|
|
|
|
66
|
|
|
// menu item displayed text |
|
67
|
|
|
$resolver |
|
68
|
1 |
|
->setDefault('text', ''); |
|
69
|
|
|
|
|
70
|
|
|
// menu item html attributes |
|
71
|
|
|
$resolver |
|
72
|
1 |
|
->setDefault('attr', []) |
|
73
|
|
View Code Duplication |
->setNormalizer('attr', function(Options $options, $attr) { |
|
74
|
|
|
|
|
75
|
1 |
|
if (!is_array($attr)) { |
|
76
|
|
|
$attr = []; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
if (empty($attr['id'])) { |
|
80
|
1 |
|
$attr['id'] = uniqid('admin-menu-'); |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
return $attr; |
|
84
|
1 |
|
}); |
|
85
|
|
|
|
|
86
|
|
|
// menu sub item |
|
87
|
|
|
$resolver |
|
88
|
1 |
|
->setDefault('items', []) |
|
89
|
1 |
View Code Duplication |
->setNormalizer('items', function(Options $options, $items) { |
|
90
|
|
|
|
|
91
|
1 |
|
if (!is_array($items)) { |
|
92
|
|
|
$items = []; |
|
93
|
|
|
} |
|
94
|
1 |
|
$resolver = new OptionsResolver(); |
|
95
|
1 |
|
$resolvedItems = []; |
|
96
|
|
|
|
|
97
|
1 |
|
foreach ($items as $name => $item) { |
|
98
|
1 |
|
$itemConfiguration = new MenuItemConfiguration(); |
|
99
|
1 |
|
$itemConfiguration->configureOptions($resolver); |
|
100
|
1 |
|
$itemConfiguration->setParameters($resolver->resolve($item)); |
|
101
|
|
|
|
|
102
|
1 |
|
$resolvedItems[$name] = $itemConfiguration; |
|
103
|
1 |
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
return $resolvedItems; |
|
106
|
1 |
|
}); |
|
107
|
|
|
|
|
108
|
1 |
|
$resolver->setDefault('icon', null); |
|
109
|
1 |
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|