forked from yokai-php/sonata-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkflowExtension.php
227 lines (203 loc) · 6.75 KB
/
WorkflowExtension.php
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
<?php
namespace Yokai\SonataWorkflow\Admin\Extension;
use Knp\Menu\ItemInterface as MenuItemInterface;
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Route\RouteCollection;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\Transition;
use Symfony\Component\Workflow\Workflow;
/**
* @author Yann Eugoné <[email protected]>
*/
class WorkflowExtension extends AbstractAdminExtension
{
/**
* @var Registry
*/
private $registry;
/**
* @var array
*/
private $options;
/**
* @param Registry $registry
* @param array $options
*/
public function __construct(Registry $registry, array $options = [])
{
$this->registry = $registry;
$this->configureOptions($resolver = new OptionsResolver());
$this->options = $resolver->resolve($options);
}
/**
* @inheritdoc
*/
public function configureRoutes(AdminInterface $admin, RouteCollection $collection)
{
$collection->add(
'workflow_apply_transition',
$admin->getRouterIdParameter() . '/workflow/transition/{transition}/apply'
);
}
/**
* @inheritdoc
*/
public function alterNewInstance(AdminInterface $admin, $object)
{
try {
$workflow = $this->getWorkflow($object, $this->options['workflow_name']);
} catch (InvalidArgumentException $exception) {
return;
}
$workflow->getMarking($object);
}
/**
* @inheritdoc
*/
public function configureSideMenu(
AdminInterface $admin,
MenuItemInterface $menu,
$action,
AdminInterface $childAdmin = null
) {
if (!in_array($action, $this->options['render_actions'], true)) {
return;
}
$subject = $admin->getSubject();
if (null === $subject) {
return;
}
try {
$workflow = $this->getWorkflow($subject, $this->options['workflow_name']);
} catch (InvalidArgumentException $exception) {
return;
}
$transitions = $workflow->getEnabledTransitions($subject);
if (count($transitions) === 0) {
$this->noTransitions($menu);
} else {
$this->transitionsDropdown($menu, $admin, $transitions, $subject);
}
}
/**
* @param object $subject
* @param string|null $workflowName
*
* @return Workflow
* @throws InvalidArgumentException
*/
protected function getWorkflow($subject, $workflowName = null)
{
return $this->registry->get($subject, $workflowName);
}
/**
* @param OptionsResolver $resolver
*/
protected function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefaults([
'render_actions' => ['edit', 'show'],
'workflow_name' => null,
'no_transition_display' => false,
'no_transition_label' => 'workflow_transitions_empty',
'no_transition_icon' => 'fa fa-code-fork',
'dropdown_transitions_label' => 'workflow_transitions',
'dropdown_transitions_icon' => 'fa fa-code-fork',
'transitions_default_icon' => null,
'transitions_icons' => [],
])
->setAllowedTypes('render_actions', ['string[]'])
->setAllowedTypes('workflow_name', ['string', 'null'])
->setAllowedTypes('no_transition_display', ['bool'])
->setAllowedTypes('no_transition_label', ['string'])
->setAllowedTypes('no_transition_icon', ['string'])
->setAllowedTypes('dropdown_transitions_label', ['string'])
->setAllowedTypes('dropdown_transitions_icon', ['string', 'null'])
->setAllowedTypes('transitions_default_icon', ['string', 'null'])
->setAllowedTypes('transitions_icons', ['array'])
;
}
/**
* @param MenuItemInterface $menu
*/
protected function noTransitions(MenuItemInterface $menu)
{
if ($this->options['no_transition_display']) {
$menu->addChild($this->options['no_transition_label'], [
'uri' => '#',
'attributes' => [
'icon' => $this->options['no_transition_icon'],
],
]);
}
}
/**
* @param MenuItemInterface $menu
* @param AdminInterface $admin
* @param Transition[] $transitions
* @param object $subject
*/
protected function transitionsDropdown(MenuItemInterface $menu, AdminInterface $admin, $transitions, $subject)
{
$workflowMenu = $menu->addChild($this->options['dropdown_transitions_label'], [
'attributes' => [
'dropdown' => true,
'icon' => $this->options['dropdown_transitions_icon'],
],
]);
foreach ($transitions as $transition) {
$this->transitionsItem($workflowMenu, $admin, $transition, $subject);
}
}
/**
* @param MenuItemInterface $menu
* @param AdminInterface $admin
* @param Transition $transition
* @param object $subject
*/
protected function transitionsItem(MenuItemInterface $menu, AdminInterface $admin, Transition $transition, $subject)
{
$options = [
'uri' => $this->generateTransitionUri($admin, $transition, $subject),
'attributes' => [],
];
if ($icon = $this->getTransitionIcon($transition)) {
$options['attributes']['icon'] = $icon;
}
$menu->addChild(
$admin->getLabelTranslatorStrategy()->getLabel($transition->getName(), 'workflow'),
$options
);
}
/**
* @param Transition $transition
*
* @return string|null
*/
protected function getTransitionIcon(Transition $transition)
{
if (isset($this->options['transitions_icons'][$transition->getName()])) {
return $this->options['transitions_icons'][$transition->getName()];
}
return $this->options['transitions_default_icon'];
}
/**
* @param AdminInterface $admin
* @param Transition $transition
* @param object $subject
*
* @return string
*/
protected function generateTransitionUri(AdminInterface $admin, Transition $transition, $subject)
{
return $admin->generateObjectUrl(
'workflow_apply_transition',
$subject,
['transition' => $transition->getName()]
);
}
}