These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Blast Project package. |
||
5 | * |
||
6 | * Copyright (C) 2015-2017 Libre Informatique |
||
7 | * |
||
8 | * This file is licenced under the GNU LGPL v3. |
||
9 | * For the full copyright and license information, please view the LICENSE.md |
||
10 | * file that was distributed with this source code. |
||
11 | */ |
||
12 | |||
13 | namespace Blast\CoreBundle\Admin\Traits; |
||
14 | |||
15 | use Sonata\AdminBundle\Datagrid\ListMapper; |
||
16 | use Sonata\AdminBundle\Form\FormMapper; |
||
17 | use Sonata\AdminBundle\Mapper\BaseGroupedMapper; |
||
18 | use Sonata\AdminBundle\Mapper\BaseMapper; |
||
19 | use Sonata\AdminBundle\Show\ShowMapper; |
||
20 | use Symfony\Component\Validator\Constraints\NotBlank; |
||
21 | use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader; |
||
22 | use Doctrine\ORM\QueryBuilder; |
||
23 | |||
24 | trait Mapper |
||
25 | { |
||
26 | /** |
||
27 | * Force tabulations on Show views. |
||
28 | * |
||
29 | * @var bool |
||
30 | */ |
||
31 | protected $forceTabs = false; |
||
32 | |||
33 | /** |
||
34 | * Links in the view navbar. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $helperLinks = []; |
||
39 | |||
40 | /** |
||
41 | * Admin titles (for list, show, edit and create). |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | public $titles = []; |
||
46 | |||
47 | /** |
||
48 | * Admin title templates (for list, show, edit and create). |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | public $titleTemplates = []; |
||
53 | |||
54 | protected function configureMapper(BaseMapper $mapper) |
||
55 | { |
||
56 | $classes = $this->getCurrentComposition(); |
||
57 | $blast = $this |
||
58 | ->getConfigurationPool() |
||
59 | ->getContainer() |
||
60 | ->getParameter('blast') |
||
61 | ; |
||
62 | $this |
||
63 | ->getConfigurationPool() |
||
64 | ->getContainer() |
||
65 | ->get('logger') |
||
66 | ->debug(sprintf( |
||
67 | '[BlastCoreBundle] Processing the configuration in this order: %s', |
||
68 | implode(', ', $classes) |
||
69 | )) |
||
70 | ; |
||
71 | |||
72 | $fcts = [ |
||
73 | 'tabs' => $mapper instanceof ShowMapper ? |
||
74 | ['getter' => 'getShowTabs', 'setter' => 'setShowTabs'] : |
||
75 | ['getter' => 'getFormTabs', 'setter' => 'setFormTabs'], |
||
76 | 'groups' => $mapper instanceof ShowMapper ? |
||
77 | ['getter' => 'getShowGroups', 'setter' => 'setShowGroups'] : |
||
78 | ['getter' => 'getFormGroups', 'setter' => 'setFormGroups'], |
||
79 | ]; |
||
80 | |||
81 | // Figure out if we have to display tabs on the Show view |
||
82 | $this->forceTabs = false; |
||
83 | if ($mapper instanceof ShowMapper) { |
||
84 | foreach ($classes as $class) { |
||
85 | if (isset($blast[$class])) { |
||
86 | foreach (array_reverse($list = array_merge([get_class($mapper)], array_values(class_parents($mapper)))) as $mapper_class) { |
||
87 | if (!empty($blast[$class][$mapper_class]['forceTabs'])) { |
||
88 | $this->forceTabs = true; |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | |||
95 | // builds the configuration, based on the Mapper class |
||
96 | foreach ($classes as $class) { |
||
97 | if (!isset($blast[$class])) { |
||
98 | continue; |
||
99 | } |
||
100 | |||
101 | // copy stuff from elsewhere |
||
102 | foreach (array_reverse($list = array_merge([get_class($mapper)], array_values(class_parents($mapper)))) as $mapper_class) { |
||
103 | if (isset($blast[$class][$mapper_class]) && !empty($blast[$class][$mapper_class]['_copy'])) { |
||
104 | if (!is_array($blast[$class][$mapper_class]['_copy'])) { |
||
105 | $blast[$class][$mapper_class]['_copy'] = [$blast[$class][$mapper_class]['_copy']]; |
||
106 | } |
||
107 | foreach ($blast[$class][$mapper_class]['_copy'] as $copy) { |
||
108 | $list = array_merge( |
||
109 | $list, array_merge([$copy], array_values(class_parents($copy))) |
||
110 | ); |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | |||
115 | $specialKeys = ['_actions', '_list_actions', '_batch_actions', '_export_formats', '_extra_templates', '_helper_links']; |
||
116 | |||
117 | // process data... |
||
118 | foreach (array_reverse($list) as $mapper_class) { |
||
119 | if (!isset($blast[$class][$mapper_class])) { |
||
120 | continue; |
||
121 | } |
||
122 | |||
123 | // remove fields |
||
124 | if (isset($blast[$class][$mapper_class]['remove'])) { |
||
125 | View Code Duplication | if (isset($blast['all'][$mapper_class]['remove'])) { |
|
126 | $blast[$class][$mapper_class]['remove'] = array_merge_recursive( |
||
127 | $blast[$class][$mapper_class]['remove'], |
||
128 | $blast['all'][$mapper_class]['remove'] |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | foreach ($blast[$class][$mapper_class]['remove'] as $key => $field) { |
||
133 | if (in_array($key, $specialKeys)) { |
||
134 | continue; |
||
135 | } |
||
136 | |||
137 | if ($mapper->has($key)) { |
||
138 | $mapper->remove($key); |
||
139 | |||
140 | // compensating the partial removal in Sonata Admin, that does not touch the groups when removing a field |
||
141 | View Code Duplication | if ($mapper instanceof BaseGroupedMapper) { |
|
142 | foreach ($groups = $this->{$fcts['groups']['getter']}() as $groupkey => $group) { |
||
143 | if (isset($group['fields'][$key])) { |
||
144 | unset($groups[$groupkey]['fields'][$key]); |
||
145 | if (!$groups[$groupkey]['fields']) { |
||
146 | unset($groups[$groupkey]); |
||
147 | } |
||
148 | $this->{$fcts['groups']['setter']}($groups); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | |||
156 | // add fields & more |
||
157 | if (isset($blast[$class][$mapper_class]['add'])) { |
||
158 | View Code Duplication | if (isset($blast['all'][$mapper_class]['add'])) { |
|
159 | $blast[$class][$mapper_class]['add'] = array_merge( |
||
160 | $blast[$class][$mapper_class]['add'], |
||
161 | $blast['all'][$mapper_class]['add'] |
||
162 | ); |
||
163 | } |
||
164 | |||
165 | // do not parse _batch_actions & co |
||
166 | foreach ($specialKeys as $sk) { |
||
167 | View Code Duplication | if (isset($blast[$class][$mapper_class]['add'][$sk])) { |
|
168 | unset($blast[$class][$mapper_class]['add'][$sk]); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | $this->addContent($mapper, $blast[$class][$mapper_class]['add']); |
||
173 | } |
||
174 | |||
175 | // set Admin titles |
||
176 | $titleTemplate = isset($blast[$class][$mapper_class]['titleTemplate']) ? $blast[$class][$mapper_class]['titleTemplate'] : null; |
||
177 | $title = isset($blast[$class][$mapper_class]['title']) ? $blast[$class][$mapper_class]['title'] : null; |
||
178 | $this->setTitles($mapper, $titleTemplate, $title); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | if ($mapper instanceof BaseGroupedMapper) { // ShowMapper and FormMapper |
||
183 | // removing empty groups |
||
184 | $groups = $this->{$fcts['groups']['getter']}(); |
||
185 | if (is_array($groups)) { |
||
186 | foreach ($groups as $groupkey => $group) { |
||
187 | if (!$group['fields']) { |
||
188 | unset($groups[$groupkey]); |
||
189 | } |
||
190 | } |
||
191 | $this->{$fcts['groups']['setter']}($groups); |
||
192 | } |
||
193 | |||
194 | // removing empty tabs |
||
195 | $tabs = $this->{$fcts['tabs']['getter']}(); |
||
196 | View Code Duplication | if (is_array($tabs)) { |
|
197 | foreach ($tabs as $tabkey => $tab) { |
||
198 | foreach ($tab['groups'] as $groupkey => $group) { |
||
199 | if (!isset($this->{$fcts['groups']['getter']}()[$group])) { |
||
200 | unset($tabs[$tabkey]['groups'][$groupkey]); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | if (!$tabs[$tabkey]['groups']) { |
||
205 | unset($tabs[$tabkey]); |
||
206 | } |
||
207 | } |
||
208 | $this->{$fcts['tabs']['setter']}($tabs); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | $this->fixTemplates($mapper); |
||
213 | |||
214 | if (!$mapper instanceof FormMapper) { |
||
215 | $this->fixShowRoutes($mapper); |
||
216 | } |
||
217 | |||
218 | // Debug profiler |
||
219 | $this->getConfigurationPool()->getContainer()->get('blast_core.profiler.collector') |
||
220 | ->collect('Mapper', $mapper) |
||
221 | ->collect('Classes', $classes); |
||
222 | |||
223 | return $this; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @param BaseMapper $mapper |
||
228 | * @param array $group |
||
229 | * |
||
230 | * @return BaseMapper |
||
231 | */ |
||
232 | protected function addContent(BaseMapper $mapper, $group) |
||
233 | { |
||
234 | // helper links |
||
235 | $this->parseHelperLinks(); |
||
236 | |||
237 | // flat organization (DatagridMapper / ListMapper...) |
||
238 | if (!$mapper instanceof BaseGroupedMapper) { |
||
239 | //list actions |
||
240 | $this->addActions(); |
||
241 | $this->removeActions(); |
||
242 | |||
243 | // options pre-treatment |
||
244 | $options = []; |
||
245 | if (isset($group['_options'])) { |
||
246 | $options = $group['_options']; |
||
247 | unset($group['_options']); |
||
248 | } |
||
249 | |||
250 | // content |
||
251 | foreach ($group as $add => $opts) { |
||
252 | $this->addField($mapper, $add, $opts); |
||
253 | } |
||
254 | |||
255 | // options |
||
256 | if (isset($options['fieldsOrder'])) { |
||
257 | $mapper->reorder($options['fieldsOrder']); |
||
258 | } |
||
259 | |||
260 | // extra templates |
||
261 | $this->parseExtraTemplates(); |
||
262 | |||
263 | return $mapper; |
||
264 | } |
||
265 | |||
266 | $fcts = [ |
||
267 | 'tabs' => $mapper instanceof ShowMapper ? |
||
268 | ['getter' => 'getShowTabs', 'setter' => 'setShowTabs'] : |
||
269 | ['getter' => 'getFormTabs', 'setter' => 'setFormTabs'], |
||
270 | 'groups' => $mapper instanceof ShowMapper ? |
||
271 | ['getter' => 'getShowGroups', 'setter' => 'setShowGroups'] : |
||
272 | ['getter' => 'getFormGroups', 'setter' => 'setFormGroups'], |
||
273 | ]; |
||
274 | |||
275 | // if a grouped organization can be shapped |
||
276 | // options |
||
277 | $tabsOptions = null; |
||
278 | if (isset($group['_options'])) { |
||
279 | $tabsOptions = $group['_options']; |
||
280 | unset($group['_options']); |
||
281 | } |
||
282 | |||
283 | // content |
||
284 | foreach ($group as $tab => $tabcontent) { // loop on content... |
||
285 | if (self::arrayDepth($tabcontent) < 1) { |
||
286 | // direct add |
||
287 | $this->addField($mapper, $tab, $tabcontent); |
||
288 | $mapper->end()->end(); |
||
289 | } else { |
||
290 | // groups/withs order |
||
291 | $groupsOrder = null; |
||
292 | if (isset($tabcontent['_options']['groupsOrder'])) { |
||
293 | $groupsOrder = $tabcontent['_options']['groupsOrder']; |
||
294 | unset($tabcontent['_options']['groupsOrder']); |
||
295 | } |
||
296 | |||
297 | $endgroup = $endtab = false; |
||
298 | |||
299 | // tab |
||
300 | if (!empty($tabcontent['_options']['hideTitle']) || $mapper instanceof ShowMapper && !$this->forceTabs) { |
||
301 | // display tabs as groups |
||
302 | $tabs = $this->{$fcts['tabs']['getter']}(); |
||
303 | $groups = $this->{$fcts['groups']['getter']}(); |
||
304 | if (isset($tabs[$tab])) { |
||
305 | $tabs[$tab]['auto_created'] = true; |
||
306 | $this->{$fcts['tabs']['setter']}($tabs); |
||
307 | |||
308 | foreach ($groups as $groupkey => $group) { |
||
309 | if (!isset($groups[$group['name']])) { |
||
310 | $groups[$group['name']] = $group; |
||
311 | unset($groups[$groupkey]); |
||
312 | } |
||
313 | } |
||
314 | $this->{$fcts['groups']['setter']}($groups); |
||
315 | } |
||
316 | } else { |
||
317 | $mapper->tab($tab, isset($tabcontent['_options']) ? $tabcontent['_options'] : []); |
||
318 | $endtab = true; |
||
319 | } |
||
320 | |||
321 | // adding count of collections items in tab |
||
322 | if (isset($tabcontent['_options']['countChildItems']) && is_array($tabcontent['_options']['countChildItems'])) { |
||
323 | $tabs = $this->{$fcts['tabs']['getter']}(); |
||
324 | if (strpos($tabs[$tab]['class'], 'countable-tab') === false) { |
||
325 | $tabs[$tab]['class'] .= ' countable-tab'; |
||
326 | |||
327 | foreach ($tabcontent['_options']['countChildItems'] as $fieldToCount) { |
||
328 | if (strpos($tabs[$tab]['class'], 'count-' . $fieldToCount) === false) { |
||
329 | $tabs[$tab]['class'] .= ' count-' . $fieldToCount; |
||
330 | } |
||
331 | } |
||
332 | |||
333 | $this->{$fcts['tabs']['setter']}($tabs); |
||
334 | } |
||
335 | } |
||
336 | |||
337 | // clearing tabcontent options |
||
338 | if (isset($tabcontent['_options'])) { |
||
339 | unset($tabcontent['_options']); |
||
340 | } |
||
341 | |||
342 | $finalOrder = null; |
||
343 | |||
344 | // with |
||
345 | if (self::arrayDepth($tabcontent) > 0) { |
||
346 | foreach ($tabcontent as $with => $withcontent) { |
||
347 | $opt = isset($withcontent['_options']) ? $withcontent['_options'] : []; |
||
348 | $finalOrder = (isset($opt['fieldsOrder']) ? $opt['fieldsOrder'] : null); |
||
349 | |||
350 | if (empty($opt['hideTitle'])) { |
||
351 | $endtab = true; |
||
352 | $endgroup = true; |
||
353 | $mapper->with($with, $opt); |
||
354 | } |
||
355 | if (isset($withcontent['_options'])) { |
||
356 | unset($withcontent['_options']); |
||
357 | } |
||
358 | |||
359 | // final adds |
||
360 | if (self::arrayDepth($withcontent) > 0) { |
||
361 | foreach ($withcontent as $name => $options) { |
||
362 | $fieldDescriptionOptions = []; |
||
363 | if (isset($options['_options'])) { |
||
364 | $fieldDescriptionOptions = $options['_options']; |
||
365 | unset($options['_options']); |
||
366 | } |
||
367 | $this->addField($mapper, $name, $options, $fieldDescriptionOptions); |
||
368 | $endgroup = $endtab = true; |
||
369 | } |
||
370 | } |
||
371 | |||
372 | if ($finalOrder != null) { |
||
373 | $mapper->reorder($finalOrder); |
||
374 | } |
||
375 | |||
376 | if ($endgroup) { |
||
377 | $mapper->end(); |
||
378 | } |
||
379 | } |
||
380 | } |
||
381 | |||
382 | // order groups / withs (using tabs, because they are prioritary at the end) |
||
383 | if (isset($groupsOrder)) { |
||
384 | // preparing |
||
385 | $otabs = $mapper->getAdmin()->{$fcts['tabs']['getter']}(); |
||
386 | $groups = $mapper->getAdmin()->{$fcts['groups']['getter']}(); |
||
387 | |||
388 | // pre-ordering |
||
389 | $newgroups = []; |
||
390 | $buf = empty($otabs[$tab]['auto_created']) ? "$tab." : ''; |
||
391 | foreach ($groupsOrder as $groupname) { |
||
392 | if (isset($otabs[$tab]) && in_array("$buf$groupname", $otabs[$tab]['groups'])) { |
||
393 | $newgroups[] = "$buf$groupname"; |
||
394 | } |
||
395 | } |
||
396 | |||
397 | // ordering tabs |
||
398 | foreach (empty($otabs[$tab]['groups']) ? [] : $otabs[$tab]['groups'] as $groupname) { |
||
399 | if (!in_array($groupname, $newgroups)) { |
||
400 | $newgroups[] = $groupname; |
||
401 | } |
||
402 | } |
||
403 | $otabs[$tab]['groups'] = $newgroups; |
||
404 | |||
405 | // "persisting" |
||
406 | $mapper->getAdmin()->{$fcts['tabs']['setter']}($otabs); |
||
407 | } |
||
408 | |||
409 | if ($endtab) { |
||
410 | $mapper->end(); |
||
411 | } |
||
412 | } |
||
413 | } |
||
414 | |||
415 | // ordering tabs |
||
416 | if (isset($tabsOptions['tabsOrder']) && $tabs = $this->{$fcts['tabs']['getter']}()) { |
||
417 | $newtabs = []; |
||
418 | foreach ($tabsOptions['tabsOrder'] as $tabname) { |
||
419 | if (isset($tabs[$tabname])) { |
||
420 | $newtabs[$tabname] = $tabs[$tabname]; |
||
421 | } |
||
422 | } |
||
423 | foreach ($tabs as $tabname => $tab) { |
||
424 | if (!isset($newtabs[$tabname])) { |
||
425 | $newtabs[$tabname] = $tab; |
||
426 | } |
||
427 | } |
||
428 | $this->{$fcts['tabs']['setter']}($newtabs); |
||
429 | } |
||
430 | |||
431 | // ordering the ShowMapper |
||
432 | if ($mapper instanceof ShowMapper) { |
||
433 | $order = []; |
||
434 | $groups = $this->{$fcts['groups']['getter']}(); |
||
435 | foreach ($this->{$fcts['tabs']['getter']}() as $tab) { |
||
436 | foreach ($tab['groups'] as $group) { |
||
437 | if (isset($groups[$group])) { |
||
438 | $order[] = $group; |
||
439 | } |
||
440 | } |
||
441 | } |
||
442 | foreach ($groups as $name => $content) { |
||
443 | if (!in_array($name, $order)) { |
||
444 | $order[] = $name; |
||
445 | } |
||
446 | } |
||
447 | |||
448 | $newgroups = []; |
||
449 | foreach ($order as $grp) { |
||
450 | $newgroups[$grp] = $groups[$grp]; |
||
451 | } |
||
452 | $this->{$fcts['groups']['setter']}($newgroups); |
||
453 | } |
||
454 | |||
455 | return $mapper; |
||
456 | } |
||
457 | |||
458 | protected function addField(BaseMapper $mapper, $name, $options = [], $fieldDescriptionOptions = []) |
||
459 | { |
||
460 | // avoid duplicates |
||
461 | if ($mapper->has($name)) { |
||
462 | $mapper->remove($name); |
||
463 | } |
||
464 | |||
465 | if (!is_array($options)) { |
||
466 | $options = []; |
||
467 | } |
||
468 | |||
469 | View Code Duplication | if (isset($options['only_new'])) { |
|
470 | if ($options['only_new'] && $this->subject && !$this->subject->isNew()) { |
||
471 | return $mapper; |
||
472 | } |
||
473 | unset($options['only_new']); |
||
474 | } |
||
475 | |||
476 | View Code Duplication | if (isset($options['only_not_new'])) { |
|
477 | if ($options['only_not_new'] && (!$this->subject || $this->subject->isNew())) { |
||
478 | return $mapper; |
||
479 | } |
||
480 | unset($options['only_not_new']); |
||
481 | } |
||
482 | |||
483 | $type = null; |
||
484 | if (isset($options['type'])) { |
||
485 | $type = $options['type']; |
||
486 | unset($options['type']); |
||
487 | } |
||
488 | |||
489 | if (isset($options['required']) && $options['required'] === true) { |
||
490 | $options['constraints'] = [new NotBlank()]; |
||
491 | } |
||
492 | |||
493 | if (isset($options['query'])) { |
||
494 | $this->manageQueryCallback($mapper, $options); |
||
495 | } |
||
496 | |||
497 | if (isset($options['choicesCallback'])) { |
||
498 | $this->manageChoicesCallback($mapper, $options); |
||
499 | } |
||
500 | |||
501 | // save-and-remove CoreBundle-specific options |
||
502 | $extras = []; |
||
503 | foreach ([ |
||
504 | 'template' => 'setTemplate', |
||
505 | 'initializeAssociationAdmin' => null, |
||
506 | ] as $extra => $method) { |
||
507 | if (isset($fieldDescriptionOptions[$extra])) { |
||
508 | $extras[$extra] = [$method, $fieldDescriptionOptions[$extra]]; |
||
509 | unset($fieldDescriptionOptions[$extra]); |
||
510 | } |
||
511 | } |
||
512 | |||
513 | $mapper->add($name, $type, $options, $fieldDescriptionOptions); |
||
514 | |||
515 | // apply extra options |
||
516 | foreach ($extras as $extra => $call) { |
||
517 | if ($call[0]) { |
||
518 | $mapper->get($name)->{$call[0]}($call[1]); |
||
519 | } else { |
||
520 | switch ($extra) { |
||
521 | case 'initializeAssociationAdmin': |
||
522 | // only if "true" |
||
523 | if (!$call[1]) { |
||
524 | break; |
||
525 | } |
||
526 | |||
527 | // initialize the association-admin |
||
528 | $mapper->get($name)->getAssociationAdmin()->configureShowFields(new ShowMapper( |
||
529 | $mapper->get($name)->getAssociationAdmin()->getShowBuilder(), $mapper->get($name)->getAssociationAdmin()->getShow(), $mapper->get($name)->getAssociationAdmin() |
||
530 | )); |
||
531 | |||
532 | // set the efficient template |
||
533 | if (!isset($extras['template'])) { |
||
534 | $mapper->get($name)->setTemplate('BlastCoreBundle:CRUD:show_association_admin.html.twig'); |
||
535 | } |
||
536 | break; |
||
537 | } |
||
538 | } |
||
539 | } |
||
540 | |||
541 | return $mapper; |
||
542 | } |
||
543 | |||
544 | protected function configureFields($function, BaseMapper $mapper, $class = null) |
||
545 | { |
||
546 | if (!$class) { |
||
547 | $class = $this->getOriginalClass(); |
||
548 | } |
||
549 | |||
550 | return $class::$function($mapper); |
||
551 | } |
||
552 | |||
553 | /** |
||
554 | * @param array $actions |
||
555 | * */ |
||
556 | protected function handleBatchActions(array $actions = []) |
||
557 | { |
||
558 | $blast = $this->getConfigurationPool()->getContainer()->getParameter('blast'); |
||
559 | $mapperClass = ListMapper::class; |
||
560 | $actionKey = '_batch_actions'; |
||
561 | |||
562 | foreach ($this->getCurrentComposition() as $class) { |
||
563 | if (isset($blast[$class][$mapperClass])) { |
||
564 | $config = $blast[$class][$mapperClass]; |
||
565 | |||
566 | View Code Duplication | if (isset($blast['all'][$mapperClass])) { |
|
567 | $config = array_merge_recursive( |
||
568 | $config, |
||
569 | $blast['all'][$mapperClass] |
||
570 | ); |
||
571 | } |
||
572 | |||
573 | // remove / reset |
||
574 | if (isset($config['remove'][$actionKey])) { |
||
575 | $actions = parent::getBatchActions(); |
||
576 | |||
577 | foreach ($config['remove'][$actionKey] as $action) { |
||
578 | if (isset($actions[$action])) { |
||
579 | unset($actions[$action]); |
||
580 | } |
||
581 | } |
||
582 | } |
||
583 | |||
584 | // add |
||
585 | if (isset($config['add'][$actionKey])) { |
||
586 | $buf = $config['add'][$actionKey]; |
||
587 | |||
588 | foreach ($buf as $action => $props) { |
||
589 | $name = 'batch_action_' . $action; |
||
590 | |||
591 | foreach ([ |
||
592 | 'label' => $name, |
||
593 | 'params' => [], |
||
594 | 'translation_domain' => $this->getTranslationDomain(), |
||
595 | 'action' => $name, |
||
596 | 'route' => 'batch_' . $action, |
||
597 | ] as $field => $value) { |
||
598 | if (empty($props[$field])) { |
||
599 | $props[$field] = $value; |
||
600 | } |
||
601 | } |
||
602 | |||
603 | $actions[$action] = $props; |
||
604 | } |
||
605 | } |
||
606 | } |
||
607 | } |
||
608 | |||
609 | return $actions; |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * @param array $actions |
||
614 | * */ |
||
615 | protected function handleListActions(array $actions = []) |
||
0 ignored issues
–
show
|
|||
616 | { |
||
617 | $this->_listActionLoaded = true; |
||
618 | $blast = $this->getConfigurationPool()->getContainer()->getParameter('blast'); |
||
619 | |||
620 | foreach ($this->getCurrentComposition() as $class) { |
||
621 | // remove / reset |
||
622 | View Code Duplication | if (isset($blast[$class][ListMapper::class]['remove']['_list_actions'])) { |
|
623 | foreach ($blast[$class][ListMapper::class]['remove']['_list_actions'] as $action) { |
||
624 | $this->removeListAction($action); |
||
625 | } |
||
626 | } |
||
627 | |||
628 | // add |
||
629 | if (isset($blast[$class][ListMapper::class]['add']['_list_actions'])) { |
||
630 | foreach ($blast[$class][ListMapper::class]['add']['_list_actions'] as $action => $props) { |
||
631 | $props['translation_domain'] = isset($props['translation_domain']) ? $props['translation_domain'] : $this->getTranslationDomain(); |
||
632 | $this->addListAction($action, $props); |
||
633 | } |
||
634 | } |
||
635 | } |
||
636 | |||
637 | return $this->getListActions(); |
||
638 | } |
||
639 | |||
640 | /** |
||
641 | * @param array $formats |
||
642 | * */ |
||
643 | protected function addPresetExportFormats(array $formats = []) |
||
644 | { |
||
645 | $blast = $this->getConfigurationPool()->getContainer()->getParameter('blast'); |
||
646 | $this->exportFields = $formats; |
||
647 | |||
648 | foreach ($this->getCurrentComposition() as $class) { |
||
649 | // remove / reset |
||
650 | if (isset($blast[$class][ListMapper::class]['remove']['_export_format'])) { |
||
651 | $this->exportFields = []; |
||
652 | } |
||
653 | |||
654 | // add |
||
655 | if (isset($blast[$class][ListMapper::class]['add']['_export_format'])) { |
||
656 | foreach ($blast[$class][ListMapper::class]['add']['_export_format'] as $format => $fields) { |
||
657 | // if no fields are defined (not an associative array) |
||
658 | if (intval($format) . '' == '' . $format && !is_array($fields)) { |
||
659 | $format = $fields; |
||
660 | $this->exportFields[$format] = $fields = []; |
||
661 | } |
||
662 | |||
663 | // if a copy of an other format is requested |
||
664 | if (!is_array($fields) && isset($blast[$class][ListMapper::class]['add']['_export_format'][$fields])) { |
||
665 | $blast[$class][ListMapper::class]['add']['_export_format'][$format] = // the global fields array |
||
666 | $fields = // the local fields array |
||
667 | $blast[$class][ListMapper::class]['add']['_export_format'][$fields]; // the source fields array |
||
668 | } |
||
669 | |||
670 | // removes a specific format |
||
671 | if (substr($format, 0, 1) == '-') { |
||
672 | unset($this->exportFields[substr($format, 1)]); |
||
673 | continue; |
||
674 | } |
||
675 | |||
676 | // if an order is defined, use it to order the extracted fields |
||
677 | if (!$fields && isset($blast[$class][ListMapper::class]['add']['_options']['fieldsOrder'])) { |
||
678 | // get back default fields |
||
679 | $tmp = parent::getExportFields(); |
||
680 | $fields = []; |
||
681 | |||
682 | // takes the ordered fields |
||
683 | View Code Duplication | foreach ($blast[$class][ListMapper::class]['add']['_options']['fieldsOrder'] as $field) { |
|
684 | if (in_array($field, $tmp)) { |
||
685 | $fields[] = $field; |
||
686 | } |
||
687 | } |
||
688 | |||
689 | // then the forgotten fields as they come |
||
690 | View Code Duplication | foreach ($tmp as $field) { |
|
691 | if (!in_array($field, $blast[$class][ListMapper::class]['add']['_options']['fieldsOrder'])) { |
||
692 | $fields[] = $field; |
||
693 | } |
||
694 | } |
||
695 | } |
||
696 | $this->exportFields[$format] = $fields; |
||
697 | } |
||
698 | } |
||
699 | } |
||
700 | |||
701 | return $this->exportFields; |
||
702 | } |
||
703 | |||
704 | /** |
||
705 | * @todo parse ShowMapper and FormMapper |
||
706 | */ |
||
707 | protected function parseExtraTemplates() |
||
708 | { |
||
709 | $blast = $this->getConfigurationPool()->getContainer()->getParameter('blast'); |
||
710 | |||
711 | foreach ($this->getCurrentComposition() as $class) { |
||
712 | // remove / reset |
||
713 | if (isset($blast[$class][ListMapper::class]['remove']['_extra_templates'])) { |
||
714 | // TODO |
||
715 | } |
||
716 | |||
717 | // add |
||
718 | View Code Duplication | if (isset($blast[$class][ListMapper::class]['add']['_extra_templates'])) { |
|
719 | foreach ($blast[$class][ListMapper::class]['add']['_extra_templates'] as $template) { |
||
720 | $this->addExtraTemplate('list', $template); |
||
721 | } |
||
722 | } |
||
723 | } |
||
724 | } |
||
725 | |||
726 | protected function parseHelperLinks() |
||
727 | { |
||
728 | $blast = $this->getConfigurationPool()->getContainer()->getParameter('blast'); |
||
729 | $mappers = [ |
||
730 | 'list' => ListMapper::class, |
||
731 | 'show' => ShowMapper::class, |
||
732 | 'form' => FormMapper::class, |
||
733 | ]; |
||
734 | |||
735 | foreach ($this->getCurrentComposition() as $class) { |
||
736 | foreach ($mappers as $mapper => $mapper_class) { |
||
737 | // remove / reset |
||
738 | if (isset($blast[$class][$mapper_class]['remove']['_helper_links'])) { |
||
739 | // TODO |
||
740 | } |
||
741 | |||
742 | // add |
||
743 | View Code Duplication | if (isset($blast[$class][$mapper_class]['add']['_helper_links'])) { |
|
744 | foreach ($blast[$class][$mapper_class]['add']['_helper_links'] as $link) { |
||
745 | $this->addHelperLink($mapper, $link); |
||
746 | } |
||
747 | } |
||
748 | } |
||
749 | } |
||
750 | } |
||
751 | |||
752 | protected function setTitles(BaseMapper $mapper, $titleTemplate, $title) |
||
753 | { |
||
754 | $contexts = [ |
||
755 | ListMapper::class => 'list', |
||
756 | ShowMapper::class => 'show', |
||
757 | FormMapper::class => 'form', |
||
758 | ]; |
||
759 | if (!isset($contexts[get_class($mapper)])) { |
||
760 | return; |
||
761 | } |
||
762 | |||
763 | $context = $contexts[get_class($mapper)]; |
||
764 | if ($titleTemplate) { |
||
765 | $this->titleTemplates[$context] = $titleTemplate; |
||
766 | } |
||
767 | if ($title) { |
||
768 | $this->titles[$context] = $title; |
||
769 | } |
||
770 | } |
||
771 | |||
772 | protected function getFormThemeMapping() |
||
773 | { |
||
774 | $theme = []; |
||
775 | $blast = $this->getConfigurationPool()->getContainer()->getParameter('blast'); |
||
776 | |||
777 | foreach ($this->getCurrentComposition() as $class) { |
||
778 | if (isset($blast[$class])) { |
||
779 | if (isset($blast[$class]['form_theme'])) { |
||
780 | $theme = array_merge($theme, $blast[$class]['form_theme']); |
||
781 | } |
||
782 | } |
||
783 | } |
||
784 | |||
785 | return $theme; |
||
786 | } |
||
787 | |||
788 | protected function getBaseRouteMapping() |
||
789 | { |
||
790 | $baseRoute = []; |
||
791 | $blast = $this->getConfigurationPool()->getContainer()->getParameter('blast'); |
||
792 | |||
793 | foreach ($this->getCurrentComposition() as $class) { |
||
794 | if (isset($blast[$class]) && isset($blast[$class]['baseRoute'])) { |
||
795 | $reflexionClass = new \ReflectionClass($class); |
||
796 | if (!$reflexionClass->isTrait()) { |
||
797 | $baseRoute = array_merge($baseRoute, $blast[$class]['baseRoute']); |
||
798 | } |
||
799 | } |
||
800 | } |
||
801 | |||
802 | return $baseRoute; |
||
803 | } |
||
804 | |||
805 | protected function manageQueryCallback($mapper, &$options) |
||
0 ignored issues
–
show
|
|||
806 | { |
||
807 | $query = $options['query']; |
||
808 | $entityClass = $options['class'] ? $options['class'] : $this->getClass(); |
||
809 | |||
810 | if (!$query instanceof QueryBuilder) { |
||
811 | if (!is_array($query)) { |
||
812 | throw new Exception('« query » option must be an array : ["FQDN"=>"static method name"]'); |
||
813 | } |
||
814 | |||
815 | list($className, $methodName) = $query; |
||
816 | |||
817 | $queryFunction = call_user_func($className . '::' . $methodName, $this->getModelManager(), $entityClass); |
||
818 | |||
819 | $options['query'] = $queryFunction; |
||
820 | } |
||
821 | } |
||
822 | |||
823 | protected function manageChoicesCallback($mapper, &$options) |
||
0 ignored issues
–
show
|
|||
824 | { |
||
825 | $callback = $options['choicesCallback']; |
||
826 | $entityClass = $options['class'] ? $options['class'] : $this->getClass(); |
||
827 | |||
828 | if (!is_array($callback)) { |
||
829 | throw new Exception('« choicesCallback » option must be an array : ["FQDN"=>"static method name"]'); |
||
830 | } |
||
831 | |||
832 | list($className, $methodName) = $callback; |
||
833 | |||
834 | $choicesFunction = call_user_func($className . '::' . $methodName, $this->getModelManager(), $entityClass); |
||
835 | |||
836 | $options['choices'] = $choicesFunction; |
||
837 | $options['choice_loader'] = new CallbackChoiceLoader(function () use ($options) { |
||
838 | return $options['choices']; |
||
839 | }); |
||
840 | unset($options['choicesCallback']); |
||
841 | } |
||
842 | } |
||
843 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.