Complex classes like AbstractAdmin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractAdmin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
60 | abstract class AbstractAdmin implements AdminInterface, DomainObjectInterface, AdminTreeInterface |
||
61 | { |
||
62 | public const CONTEXT_MENU = 'menu'; |
||
63 | public const CONTEXT_DASHBOARD = 'dashboard'; |
||
64 | |||
65 | public const CLASS_REGEX = |
||
66 | '@ |
||
67 | (?:([A-Za-z0-9]*)\\\)? # vendor name / app name |
||
68 | (Bundle\\\)? # optional bundle directory |
||
69 | ([A-Za-z0-9]+?)(?:Bundle)?\\\ # bundle name, with optional suffix |
||
70 | ( |
||
71 | Entity|Document|Model|PHPCR|CouchDocument|Phpcr| |
||
72 | Doctrine\\\Orm|Doctrine\\\Phpcr|Doctrine\\\MongoDB|Doctrine\\\CouchDB |
||
73 | )\\\(.*)@x'; |
||
74 | |||
75 | public const MOSAIC_ICON_CLASS = 'fa fa-th-large fa-fw'; |
||
76 | |||
77 | /** |
||
78 | * The list FieldDescription constructed from the configureListField method. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $listFieldDescriptions = []; |
||
83 | |||
84 | /** |
||
85 | * The show FieldDescription constructed from the configureShowFields method. |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | protected $showFieldDescriptions = []; |
||
90 | |||
91 | /** |
||
92 | * The list FieldDescription constructed from the configureFormField method. |
||
93 | * |
||
94 | * @var array |
||
95 | */ |
||
96 | protected $formFieldDescriptions = []; |
||
97 | |||
98 | /** |
||
99 | * The filter FieldDescription constructed from the configureFilterField method. |
||
100 | * |
||
101 | * @var array |
||
102 | */ |
||
103 | protected $filterFieldDescriptions = []; |
||
104 | |||
105 | /** |
||
106 | * The number of result to display in the list. |
||
107 | * |
||
108 | * @var int |
||
109 | */ |
||
110 | protected $maxPerPage = 32; |
||
111 | |||
112 | /** |
||
113 | * The maximum number of page numbers to display in the list. |
||
114 | * |
||
115 | * @var int |
||
116 | */ |
||
117 | protected $maxPageLinks = 25; |
||
118 | |||
119 | /** |
||
120 | * The base route name used to generate the routing information. |
||
121 | * |
||
122 | * @var string |
||
123 | */ |
||
124 | protected $baseRouteName; |
||
125 | |||
126 | /** |
||
127 | * The base route pattern used to generate the routing information. |
||
128 | * |
||
129 | * @var string |
||
130 | */ |
||
131 | protected $baseRoutePattern; |
||
132 | |||
133 | /** |
||
134 | * The base name controller used to generate the routing information. |
||
135 | * |
||
136 | * @var string |
||
137 | */ |
||
138 | protected $baseControllerName; |
||
139 | |||
140 | /** |
||
141 | * The label class name (used in the title/breadcrumb ...). |
||
142 | * |
||
143 | * @var string |
||
144 | */ |
||
145 | protected $classnameLabel; |
||
146 | |||
147 | /** |
||
148 | * The translation domain to be used to translate messages. |
||
149 | * |
||
150 | * @var string |
||
151 | */ |
||
152 | protected $translationDomain = 'messages'; |
||
153 | |||
154 | /** |
||
155 | * Options to set to the form (ie, validation_groups). |
||
156 | * |
||
157 | * @var array |
||
158 | */ |
||
159 | protected $formOptions = []; |
||
160 | |||
161 | /** |
||
162 | * Default values to the datagrid. |
||
163 | * |
||
164 | * @var array |
||
165 | */ |
||
166 | protected $datagridValues = [ |
||
167 | '_page' => 1, |
||
168 | '_per_page' => 32, |
||
169 | ]; |
||
170 | |||
171 | /** |
||
172 | * Predefined per page options. |
||
173 | * |
||
174 | * @var array |
||
175 | */ |
||
176 | protected $perPageOptions = [16, 32, 64, 128, 256]; |
||
177 | |||
178 | /** |
||
179 | * Pager type. |
||
180 | * |
||
181 | * @var string |
||
182 | */ |
||
183 | protected $pagerType = Pager::TYPE_DEFAULT; |
||
184 | |||
185 | /** |
||
186 | * The code related to the admin. |
||
187 | * |
||
188 | * @var string |
||
189 | */ |
||
190 | protected $code; |
||
191 | |||
192 | /** |
||
193 | * The label. |
||
194 | * |
||
195 | * @var string |
||
196 | */ |
||
197 | protected $label; |
||
198 | |||
199 | /** |
||
200 | * Whether or not to persist the filters in the session. |
||
201 | * |
||
202 | * NEXT_MAJOR: remove this property |
||
203 | * |
||
204 | * @var bool |
||
205 | * |
||
206 | * @deprecated since sonata-project/admin-bundle 3.34, to be removed in 4.0. |
||
207 | */ |
||
208 | protected $persistFilters = false; |
||
209 | |||
210 | /** |
||
211 | * Array of routes related to this admin. |
||
212 | * |
||
213 | * @var RouteCollection |
||
214 | */ |
||
215 | protected $routes; |
||
216 | |||
217 | /** |
||
218 | * The subject only set in edit/update/create mode. |
||
219 | * |
||
220 | * @var object|null |
||
221 | */ |
||
222 | protected $subject; |
||
223 | |||
224 | /** |
||
225 | * Define a Collection of child admin, ie /admin/order/{id}/order-element/{childId}. |
||
226 | * |
||
227 | * @var array |
||
228 | */ |
||
229 | protected $children = []; |
||
230 | |||
231 | /** |
||
232 | * Reference the parent collection. |
||
233 | * |
||
234 | * @var AdminInterface|null |
||
235 | */ |
||
236 | protected $parent = null; |
||
237 | |||
238 | /** |
||
239 | * The base code route refer to the prefix used to generate the route name. |
||
240 | * |
||
241 | * NEXT_MAJOR: remove this attribute. |
||
242 | * |
||
243 | * @deprecated This attribute is deprecated since sonata-project/admin-bundle 3.24 and will be removed in 4.0 |
||
244 | * |
||
245 | * @var string |
||
246 | */ |
||
247 | protected $baseCodeRoute = ''; |
||
248 | |||
249 | /** |
||
250 | * NEXT_MAJOR: should be default array and private. |
||
251 | * |
||
252 | * @var string|array |
||
253 | */ |
||
254 | protected $parentAssociationMapping = null; |
||
255 | |||
256 | /** |
||
257 | * Reference the parent FieldDescription related to this admin |
||
258 | * only set for FieldDescription which is associated to an Sub Admin instance. |
||
259 | * |
||
260 | * @var FieldDescriptionInterface |
||
261 | */ |
||
262 | protected $parentFieldDescription; |
||
263 | |||
264 | /** |
||
265 | * If true then the current admin is part of the nested admin set (from the url). |
||
266 | * |
||
267 | * @var bool |
||
268 | */ |
||
269 | protected $currentChild = false; |
||
270 | |||
271 | /** |
||
272 | * The uniqid is used to avoid clashing with 2 admin related to the code |
||
273 | * ie: a Block linked to a Block. |
||
274 | * |
||
275 | * @var string |
||
276 | */ |
||
277 | protected $uniqid; |
||
278 | |||
279 | /** |
||
280 | * The Entity or Document manager. |
||
281 | * |
||
282 | * @var ModelManagerInterface |
||
283 | */ |
||
284 | protected $modelManager; |
||
285 | |||
286 | /** |
||
287 | * The current request object. |
||
288 | * |
||
289 | * @var Request|null |
||
290 | */ |
||
291 | protected $request; |
||
292 | |||
293 | /** |
||
294 | * The translator component. |
||
295 | * |
||
296 | * NEXT_MAJOR: remove this property |
||
297 | * |
||
298 | * @var \Symfony\Component\Translation\TranslatorInterface |
||
299 | * |
||
300 | * @deprecated since sonata-project/admin-bundle 3.9, to be removed with 4.0 |
||
301 | */ |
||
302 | protected $translator; |
||
303 | |||
304 | /** |
||
305 | * The related form contractor. |
||
306 | * |
||
307 | * @var FormContractorInterface |
||
308 | */ |
||
309 | protected $formContractor; |
||
310 | |||
311 | /** |
||
312 | * The related list builder. |
||
313 | * |
||
314 | * @var ListBuilderInterface |
||
315 | */ |
||
316 | protected $listBuilder; |
||
317 | |||
318 | /** |
||
319 | * The related view builder. |
||
320 | * |
||
321 | * @var ShowBuilderInterface |
||
322 | */ |
||
323 | protected $showBuilder; |
||
324 | |||
325 | /** |
||
326 | * The related datagrid builder. |
||
327 | * |
||
328 | * @var DatagridBuilderInterface |
||
329 | */ |
||
330 | protected $datagridBuilder; |
||
331 | |||
332 | /** |
||
333 | * @var RouteBuilderInterface |
||
334 | */ |
||
335 | protected $routeBuilder; |
||
336 | |||
337 | /** |
||
338 | * The datagrid instance. |
||
339 | * |
||
340 | * @var DatagridInterface|null |
||
341 | */ |
||
342 | protected $datagrid; |
||
343 | |||
344 | /** |
||
345 | * The router instance. |
||
346 | * |
||
347 | * @var RouteGeneratorInterface|null |
||
348 | */ |
||
349 | protected $routeGenerator; |
||
350 | |||
351 | /** |
||
352 | * The generated breadcrumbs. |
||
353 | * |
||
354 | * NEXT_MAJOR : remove this property |
||
355 | * |
||
356 | * @var array |
||
357 | */ |
||
358 | protected $breadcrumbs = []; |
||
359 | |||
360 | /** |
||
361 | * @var SecurityHandlerInterface |
||
362 | */ |
||
363 | protected $securityHandler = null; |
||
364 | |||
365 | /** |
||
366 | * @var ValidatorInterface |
||
367 | */ |
||
368 | protected $validator = null; |
||
369 | |||
370 | /** |
||
371 | * The configuration pool. |
||
372 | * |
||
373 | * @var Pool |
||
374 | */ |
||
375 | protected $configurationPool; |
||
376 | |||
377 | /** |
||
378 | * @var MenuItemInterface |
||
379 | */ |
||
380 | protected $menu; |
||
381 | |||
382 | /** |
||
383 | * @var MenuFactoryInterface |
||
384 | */ |
||
385 | protected $menuFactory; |
||
386 | |||
387 | /** |
||
388 | * @var array<string, bool> |
||
389 | */ |
||
390 | protected $loaded = [ |
||
391 | 'view_fields' => false, |
||
392 | 'view_groups' => false, |
||
393 | 'routes' => false, |
||
394 | 'tab_menu' => false, |
||
395 | ]; |
||
396 | |||
397 | /** |
||
398 | * @var string[] |
||
399 | */ |
||
400 | protected $formTheme = []; |
||
401 | |||
402 | /** |
||
403 | * @var string[] |
||
404 | */ |
||
405 | protected $filterTheme = []; |
||
406 | |||
407 | /** |
||
408 | * @var array<string, string> |
||
409 | * |
||
410 | * @deprecated since sonata-project/admin-bundle 3.34, will be dropped in 4.0. Use TemplateRegistry services instead |
||
411 | */ |
||
412 | protected $templates = []; |
||
413 | |||
414 | /** |
||
415 | * @var AdminExtensionInterface[] |
||
416 | */ |
||
417 | protected $extensions = []; |
||
418 | |||
419 | /** |
||
420 | * @var LabelTranslatorStrategyInterface |
||
421 | */ |
||
422 | protected $labelTranslatorStrategy; |
||
423 | |||
424 | /** |
||
425 | * Setting to true will enable preview mode for |
||
426 | * the entity and show a preview button in the |
||
427 | * edit/create forms. |
||
428 | * |
||
429 | * @var bool |
||
430 | */ |
||
431 | protected $supportsPreviewMode = false; |
||
432 | |||
433 | /** |
||
434 | * Roles and permissions per role. |
||
435 | * |
||
436 | * @var array 'role' => ['permission', 'permission'] |
||
437 | */ |
||
438 | protected $securityInformation = []; |
||
439 | |||
440 | protected $cacheIsGranted = []; |
||
441 | |||
442 | /** |
||
443 | * Action list for the search result. |
||
444 | * |
||
445 | * @var string[] |
||
446 | */ |
||
447 | protected $searchResultActions = ['edit', 'show']; |
||
448 | |||
449 | protected $listModes = [ |
||
450 | 'list' => [ |
||
451 | 'class' => 'fa fa-list fa-fw', |
||
452 | ], |
||
453 | 'mosaic' => [ |
||
454 | 'class' => self::MOSAIC_ICON_CLASS, |
||
455 | ], |
||
456 | ]; |
||
457 | |||
458 | /** |
||
459 | * The Access mapping. |
||
460 | * |
||
461 | * @var array [action1 => requiredRole1, action2 => [requiredRole2, requiredRole3]] |
||
462 | */ |
||
463 | protected $accessMapping = []; |
||
464 | |||
465 | /** |
||
466 | * @var MutableTemplateRegistryInterface |
||
467 | */ |
||
468 | private $templateRegistry; |
||
469 | |||
470 | /** |
||
471 | * The class name managed by the admin class. |
||
472 | * |
||
473 | * @var string |
||
474 | */ |
||
475 | private $class; |
||
476 | |||
477 | /** |
||
478 | * The subclasses supported by the admin class. |
||
479 | * |
||
480 | * @var array<string, string> |
||
481 | */ |
||
482 | private $subClasses = []; |
||
483 | |||
484 | /** |
||
485 | * The list collection. |
||
486 | * |
||
487 | * @var FieldDescriptionCollection |
||
488 | */ |
||
489 | private $list; |
||
490 | |||
491 | /** |
||
492 | * @var FieldDescriptionCollection|null |
||
493 | */ |
||
494 | private $show; |
||
495 | |||
496 | /** |
||
497 | * @var Form|null |
||
498 | */ |
||
499 | private $form; |
||
500 | |||
501 | /** |
||
502 | * @var DatagridInterface |
||
503 | */ |
||
504 | private $filter; |
||
505 | |||
506 | /** |
||
507 | * The cached base route name. |
||
508 | * |
||
509 | * @var string |
||
510 | */ |
||
511 | private $cachedBaseRouteName; |
||
512 | |||
513 | /** |
||
514 | * The cached base route pattern. |
||
515 | * |
||
516 | * @var string |
||
517 | */ |
||
518 | private $cachedBaseRoutePattern; |
||
519 | |||
520 | /** |
||
521 | * The form group disposition. |
||
522 | * |
||
523 | * @var array|bool |
||
524 | */ |
||
525 | private $formGroups = false; |
||
526 | |||
527 | /** |
||
528 | * The form tabs disposition. |
||
529 | * |
||
530 | * @var array|bool |
||
531 | */ |
||
532 | private $formTabs = false; |
||
533 | |||
534 | /** |
||
535 | * The view group disposition. |
||
536 | * |
||
537 | * @var array|bool |
||
538 | */ |
||
539 | private $showGroups = false; |
||
540 | |||
541 | /** |
||
542 | * The view tab disposition. |
||
543 | * |
||
544 | * @var array|bool |
||
545 | */ |
||
546 | private $showTabs = false; |
||
547 | |||
548 | /** |
||
549 | * The manager type to use for the admin. |
||
550 | * |
||
551 | * @var string |
||
552 | */ |
||
553 | private $managerType; |
||
554 | |||
555 | /** |
||
556 | * The breadcrumbsBuilder component. |
||
557 | * |
||
558 | * @var BreadcrumbsBuilderInterface |
||
559 | */ |
||
560 | private $breadcrumbsBuilder; |
||
561 | |||
562 | /** |
||
563 | * Component responsible for persisting filters. |
||
564 | * |
||
565 | * @var FilterPersisterInterface|null |
||
566 | */ |
||
567 | private $filterPersister; |
||
568 | |||
569 | /** |
||
570 | * @param string $code |
||
571 | * @param string $class |
||
572 | * @param string $baseControllerName |
||
573 | */ |
||
574 | public function __construct($code, $class, $baseControllerName) |
||
583 | |||
584 | /** |
||
585 | * {@inheritdoc} |
||
586 | * |
||
587 | * NEXT_MAJOR: return null to indicate no override |
||
588 | */ |
||
589 | public function getExportFormats() |
||
595 | |||
596 | /** |
||
597 | * @return array |
||
598 | */ |
||
599 | public function getExportFields() |
||
611 | |||
612 | public function getDataSourceIterator() |
||
634 | |||
635 | public function validate(ErrorElement $errorElement, $object) |
||
638 | |||
639 | /** |
||
640 | * define custom variable. |
||
641 | */ |
||
642 | public function initialize() |
||
643 | { |
||
644 | if (!$this->classnameLabel) { |
||
645 | /* NEXT_MAJOR: remove cast to string, null is not supposed to be |
||
646 | supported but was documented as such */ |
||
647 | $this->classnameLabel = substr( |
||
648 | (string) $this->getClass(), |
||
649 | strrpos((string) $this->getClass(), '\\') + 1 |
||
650 | ); |
||
651 | } |
||
652 | |||
653 | // NEXT_MAJOR: Remove this line. |
||
654 | $this->baseCodeRoute = $this->getCode(); |
||
655 | |||
656 | $this->configure(); |
||
657 | } |
||
658 | |||
659 | public function configure() |
||
660 | { |
||
661 | } |
||
662 | |||
663 | public function update($object) |
||
664 | { |
||
665 | $this->preUpdate($object); |
||
666 | foreach ($this->extensions as $extension) { |
||
667 | $extension->preUpdate($this, $object); |
||
668 | } |
||
669 | |||
670 | $result = $this->getModelManager()->update($object); |
||
671 | // BC compatibility |
||
672 | if (null !== $result) { |
||
673 | $object = $result; |
||
674 | } |
||
675 | |||
676 | $this->postUpdate($object); |
||
677 | foreach ($this->extensions as $extension) { |
||
678 | $extension->postUpdate($this, $object); |
||
679 | } |
||
680 | |||
681 | return $object; |
||
682 | } |
||
683 | |||
684 | public function create($object) |
||
685 | { |
||
686 | $this->prePersist($object); |
||
687 | foreach ($this->extensions as $extension) { |
||
688 | $extension->prePersist($this, $object); |
||
689 | } |
||
690 | |||
691 | $result = $this->getModelManager()->create($object); |
||
692 | // BC compatibility |
||
693 | if (null !== $result) { |
||
694 | $object = $result; |
||
695 | } |
||
696 | |||
697 | $this->postPersist($object); |
||
698 | foreach ($this->extensions as $extension) { |
||
699 | $extension->postPersist($this, $object); |
||
700 | } |
||
701 | |||
702 | $this->createObjectSecurity($object); |
||
703 | |||
704 | return $object; |
||
705 | } |
||
706 | |||
707 | public function delete($object) |
||
708 | { |
||
709 | $this->preRemove($object); |
||
710 | foreach ($this->extensions as $extension) { |
||
711 | $extension->preRemove($this, $object); |
||
712 | } |
||
713 | |||
714 | $this->getSecurityHandler()->deleteObjectSecurity($this, $object); |
||
715 | $this->getModelManager()->delete($object); |
||
716 | |||
717 | $this->postRemove($object); |
||
718 | foreach ($this->extensions as $extension) { |
||
719 | $extension->postRemove($this, $object); |
||
720 | } |
||
721 | } |
||
722 | |||
723 | /** |
||
724 | * @param object $object |
||
725 | */ |
||
726 | public function preValidate($object) |
||
727 | { |
||
728 | } |
||
729 | |||
730 | public function preUpdate($object) |
||
731 | { |
||
732 | } |
||
733 | |||
734 | public function postUpdate($object) |
||
735 | { |
||
736 | } |
||
737 | |||
738 | public function prePersist($object) |
||
739 | { |
||
740 | } |
||
741 | |||
742 | public function postPersist($object) |
||
743 | { |
||
744 | } |
||
745 | |||
746 | public function preRemove($object) |
||
747 | { |
||
748 | } |
||
749 | |||
750 | public function postRemove($object) |
||
751 | { |
||
752 | } |
||
753 | |||
754 | public function preBatchAction($actionName, ProxyQueryInterface $query, array &$idx, $allElements) |
||
755 | { |
||
756 | } |
||
757 | |||
758 | public function getFilterParameters() |
||
759 | { |
||
760 | $parameters = []; |
||
761 | |||
762 | // build the values array |
||
763 | if ($this->hasRequest()) { |
||
764 | $filters = $this->request->query->get('filter', []); |
||
765 | if (isset($filters['_page'])) { |
||
766 | $filters['_page'] = (int) $filters['_page']; |
||
767 | } |
||
768 | if (isset($filters['_per_page'])) { |
||
769 | $filters['_per_page'] = (int) $filters['_per_page']; |
||
770 | } |
||
771 | |||
772 | // if filter persistence is configured |
||
773 | // NEXT_MAJOR: remove `$this->persistFilters !== false` from the condition |
||
774 | if (false !== $this->persistFilters && null !== $this->filterPersister) { |
||
775 | // if reset filters is asked, remove from storage |
||
776 | if ('reset' === $this->request->query->get('filters')) { |
||
777 | $this->filterPersister->reset($this->getCode()); |
||
778 | } |
||
779 | |||
780 | // if no filters, fetch from storage |
||
781 | // otherwise save to storage |
||
782 | if (empty($filters)) { |
||
783 | $filters = $this->filterPersister->get($this->getCode()); |
||
784 | } else { |
||
785 | $this->filterPersister->set($this->getCode(), $filters); |
||
786 | } |
||
787 | } |
||
788 | |||
789 | $parameters = array_merge( |
||
790 | $this->getModelManager()->getDefaultSortValues($this->getClass()), |
||
791 | $this->datagridValues, |
||
792 | $this->getDefaultFilterValues(), |
||
793 | $filters |
||
794 | ); |
||
795 | |||
796 | if (!$this->determinedPerPageValue($parameters['_per_page'])) { |
||
797 | $parameters['_per_page'] = $this->maxPerPage; |
||
798 | } |
||
799 | |||
800 | // always force the parent value |
||
801 | if ($this->isChild() && $this->getParentAssociationMapping()) { |
||
802 | $name = str_replace('.', '__', $this->getParentAssociationMapping()); |
||
803 | $parameters[$name] = ['value' => $this->request->get($this->getParent()->getIdParameter())]; |
||
804 | } |
||
805 | } |
||
806 | |||
807 | return $parameters; |
||
808 | } |
||
809 | |||
810 | public function buildDatagrid() |
||
811 | { |
||
812 | if ($this->datagrid) { |
||
813 | return; |
||
814 | } |
||
815 | |||
816 | $filterParameters = $this->getFilterParameters(); |
||
817 | |||
818 | // transform _sort_by from a string to a FieldDescriptionInterface for the datagrid. |
||
819 | if (isset($filterParameters['_sort_by']) && \is_string($filterParameters['_sort_by'])) { |
||
820 | if ($this->hasListFieldDescription($filterParameters['_sort_by'])) { |
||
821 | $filterParameters['_sort_by'] = $this->getListFieldDescription($filterParameters['_sort_by']); |
||
822 | } else { |
||
823 | $filterParameters['_sort_by'] = $this->getModelManager()->getNewFieldDescriptionInstance( |
||
824 | $this->getClass(), |
||
825 | $filterParameters['_sort_by'], |
||
826 | [] |
||
827 | ); |
||
828 | |||
829 | $this->getListBuilder()->buildField(null, $filterParameters['_sort_by'], $this); |
||
830 | } |
||
831 | } |
||
832 | |||
833 | // initialize the datagrid |
||
834 | $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $filterParameters); |
||
835 | |||
836 | $this->datagrid->getPager()->setMaxPageLinks($this->maxPageLinks); |
||
837 | |||
838 | $mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this); |
||
839 | |||
840 | // build the datagrid filter |
||
841 | $this->configureDatagridFilters($mapper); |
||
842 | |||
843 | // ok, try to limit to add parent filter |
||
844 | if ($this->isChild() && $this->getParentAssociationMapping() && !$mapper->has($this->getParentAssociationMapping())) { |
||
845 | $mapper->add($this->getParentAssociationMapping(), null, [ |
||
846 | 'show_filter' => false, |
||
847 | 'label' => false, |
||
848 | 'field_type' => ModelHiddenType::class, |
||
849 | 'field_options' => [ |
||
850 | 'model_manager' => $this->getModelManager(), |
||
851 | ], |
||
852 | 'operator_type' => HiddenType::class, |
||
853 | ], null, null, [ |
||
854 | 'admin_code' => $this->getParent()->getCode(), |
||
855 | ]); |
||
856 | } |
||
857 | |||
858 | foreach ($this->getExtensions() as $extension) { |
||
859 | $extension->configureDatagridFilters($mapper); |
||
860 | } |
||
861 | } |
||
862 | |||
863 | /** |
||
864 | * Returns the name of the parent related field, so the field can be use to set the default |
||
865 | * value (ie the parent object) or to filter the object. |
||
866 | * |
||
867 | * @throws \InvalidArgumentException |
||
868 | * |
||
869 | * @return string|null |
||
870 | */ |
||
871 | public function getParentAssociationMapping() |
||
872 | { |
||
873 | // NEXT_MAJOR: remove array check |
||
874 | if (\is_array($this->parentAssociationMapping) && $this->getParent()) { |
||
875 | $parent = $this->getParent()->getCode(); |
||
876 | |||
877 | if (\array_key_exists($parent, $this->parentAssociationMapping)) { |
||
878 | return $this->parentAssociationMapping[$parent]; |
||
879 | } |
||
880 | |||
881 | throw new \InvalidArgumentException(sprintf( |
||
882 | "There's no association between %s and %s.", |
||
883 | $this->getCode(), |
||
884 | $this->getParent()->getCode() |
||
885 | )); |
||
886 | } |
||
887 | |||
888 | // NEXT_MAJOR: remove this line |
||
889 | return $this->parentAssociationMapping; |
||
890 | } |
||
891 | |||
892 | /** |
||
893 | * @param string $code |
||
894 | * @param string $value |
||
895 | */ |
||
896 | final public function addParentAssociationMapping($code, $value) |
||
897 | { |
||
898 | $this->parentAssociationMapping[$code] = $value; |
||
899 | } |
||
900 | |||
901 | /** |
||
902 | * Returns the baseRoutePattern used to generate the routing information. |
||
903 | * |
||
904 | * @throws \RuntimeException |
||
905 | * |
||
906 | * @return string the baseRoutePattern used to generate the routing information |
||
907 | */ |
||
908 | public function getBaseRoutePattern() |
||
909 | { |
||
910 | if (null !== $this->cachedBaseRoutePattern) { |
||
911 | return $this->cachedBaseRoutePattern; |
||
912 | } |
||
913 | |||
914 | if ($this->isChild()) { // the admin class is a child, prefix it with the parent route pattern |
||
915 | $baseRoutePattern = $this->baseRoutePattern; |
||
916 | if (!$this->baseRoutePattern) { |
||
917 | preg_match(self::CLASS_REGEX, $this->class, $matches); |
||
918 | |||
919 | if (!$matches) { |
||
920 | throw new \RuntimeException(sprintf('Please define a default `baseRoutePattern` value for the admin class `%s`', static::class)); |
||
921 | } |
||
922 | $baseRoutePattern = $this->urlize($matches[5], '-'); |
||
923 | } |
||
924 | |||
925 | $this->cachedBaseRoutePattern = sprintf( |
||
926 | '%s/%s/%s', |
||
927 | $this->getParent()->getBaseRoutePattern(), |
||
928 | $this->getParent()->getRouterIdParameter(), |
||
929 | $baseRoutePattern |
||
930 | ); |
||
931 | } elseif ($this->baseRoutePattern) { |
||
932 | $this->cachedBaseRoutePattern = $this->baseRoutePattern; |
||
933 | } else { |
||
934 | preg_match(self::CLASS_REGEX, $this->class, $matches); |
||
935 | |||
936 | if (!$matches) { |
||
937 | throw new \RuntimeException(sprintf('Please define a default `baseRoutePattern` value for the admin class `%s`', static::class)); |
||
938 | } |
||
939 | |||
940 | $this->cachedBaseRoutePattern = sprintf( |
||
941 | '/%s%s/%s', |
||
942 | empty($matches[1]) ? '' : $this->urlize($matches[1], '-').'/', |
||
943 | $this->urlize($matches[3], '-'), |
||
944 | $this->urlize($matches[5], '-') |
||
945 | ); |
||
946 | } |
||
947 | |||
948 | return $this->cachedBaseRoutePattern; |
||
949 | } |
||
950 | |||
951 | /** |
||
952 | * Returns the baseRouteName used to generate the routing information. |
||
953 | * |
||
954 | * @throws \RuntimeException |
||
955 | * |
||
956 | * @return string the baseRouteName used to generate the routing information |
||
957 | */ |
||
958 | public function getBaseRouteName() |
||
959 | { |
||
960 | if (null !== $this->cachedBaseRouteName) { |
||
961 | return $this->cachedBaseRouteName; |
||
962 | } |
||
963 | |||
964 | if ($this->isChild()) { // the admin class is a child, prefix it with the parent route name |
||
965 | $baseRouteName = $this->baseRouteName; |
||
966 | if (!$this->baseRouteName) { |
||
967 | preg_match(self::CLASS_REGEX, $this->class, $matches); |
||
968 | |||
969 | if (!$matches) { |
||
970 | throw new \RuntimeException(sprintf('Cannot automatically determine base route name, please define a default `baseRouteName` value for the admin class `%s`', static::class)); |
||
971 | } |
||
972 | $baseRouteName = $this->urlize($matches[5]); |
||
973 | } |
||
974 | |||
975 | $this->cachedBaseRouteName = sprintf( |
||
976 | '%s_%s', |
||
977 | $this->getParent()->getBaseRouteName(), |
||
978 | $baseRouteName |
||
979 | ); |
||
980 | } elseif ($this->baseRouteName) { |
||
981 | $this->cachedBaseRouteName = $this->baseRouteName; |
||
982 | } else { |
||
983 | preg_match(self::CLASS_REGEX, $this->class, $matches); |
||
984 | |||
985 | if (!$matches) { |
||
986 | throw new \RuntimeException(sprintf('Cannot automatically determine base route name, please define a default `baseRouteName` value for the admin class `%s`', static::class)); |
||
987 | } |
||
988 | |||
989 | $this->cachedBaseRouteName = sprintf( |
||
990 | 'admin_%s%s_%s', |
||
991 | empty($matches[1]) ? '' : $this->urlize($matches[1]).'_', |
||
992 | $this->urlize($matches[3]), |
||
993 | $this->urlize($matches[5]) |
||
994 | ); |
||
995 | } |
||
996 | |||
997 | return $this->cachedBaseRouteName; |
||
998 | } |
||
999 | |||
1000 | /** |
||
1001 | * urlize the given word. |
||
1002 | * |
||
1003 | * @param string $word |
||
1004 | * @param string $sep the separator |
||
1005 | * |
||
1006 | * @return string |
||
1007 | */ |
||
1008 | public function urlize($word, $sep = '_') |
||
1009 | { |
||
1010 | return strtolower(preg_replace('/[^a-z0-9_]/i', $sep.'$1', $word)); |
||
1011 | } |
||
1012 | |||
1013 | public function getClass() |
||
1014 | { |
||
1015 | if ($this->hasActiveSubClass()) { |
||
1016 | if ($this->getParentFieldDescription()) { |
||
1017 | throw new \RuntimeException('Feature not implemented: an embedded admin cannot have subclass'); |
||
1018 | } |
||
1019 | |||
1020 | $subClass = $this->getRequest()->query->get('subclass'); |
||
1021 | |||
1022 | if (!$this->hasSubClass($subClass)) { |
||
1023 | throw new \RuntimeException(sprintf('Subclass "%s" is not defined.', $subClass)); |
||
1024 | } |
||
1025 | |||
1026 | return $this->getSubClass($subClass); |
||
1027 | } |
||
1028 | |||
1029 | // see https://github.com/sonata-project/SonataCoreBundle/commit/247eeb0a7ca7211142e101754769d70bc402a5b4 |
||
1030 | if ($this->subject && \is_object($this->subject)) { |
||
1031 | return ClassUtils::getClass($this->subject); |
||
1032 | } |
||
1033 | |||
1034 | return $this->class; |
||
1035 | } |
||
1036 | |||
1037 | public function getSubClasses() |
||
1038 | { |
||
1039 | return $this->subClasses; |
||
1040 | } |
||
1041 | |||
1042 | /** |
||
1043 | * NEXT_MAJOR: remove this method. |
||
1044 | */ |
||
1045 | public function addSubClass($subClass) |
||
1046 | { |
||
1047 | @trigger_error(sprintf( |
||
1048 | 'Method "%s" is deprecated since sonata-project/admin-bundle 3.30 and will be removed in 4.0.', |
||
1049 | __METHOD__ |
||
1050 | ), E_USER_DEPRECATED); |
||
1051 | |||
1052 | if (!\in_array($subClass, $this->subClasses, true)) { |
||
1053 | $this->subClasses[] = $subClass; |
||
1054 | } |
||
1055 | } |
||
1056 | |||
1057 | public function setSubClasses(array $subClasses) |
||
1058 | { |
||
1059 | $this->subClasses = $subClasses; |
||
1060 | } |
||
1061 | |||
1062 | public function hasSubClass($name) |
||
1063 | { |
||
1064 | return isset($this->subClasses[$name]); |
||
1065 | } |
||
1066 | |||
1067 | public function hasActiveSubClass() |
||
1068 | { |
||
1069 | if (\count($this->subClasses) > 0 && $this->request) { |
||
1070 | return null !== $this->getRequest()->query->get('subclass'); |
||
1071 | } |
||
1072 | |||
1073 | return false; |
||
1074 | } |
||
1075 | |||
1076 | public function getActiveSubClass() |
||
1077 | { |
||
1078 | if (!$this->hasActiveSubClass()) { |
||
1079 | @trigger_error(sprintf( |
||
1080 | 'Calling %s() when there is no active subclass is deprecated since sonata-project/admin-bundle 3.52 and will throw an exception in 4.0. '. |
||
1081 | 'Use %s::hasActiveSubClass() to know if there is an active subclass.', |
||
1082 | __METHOD__, |
||
1083 | __CLASS__ |
||
1084 | ), E_USER_DEPRECATED); |
||
1085 | // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare string as return type |
||
1086 | // throw new \LogicException(sprintf( |
||
1087 | // 'Admin "%s" has no active subclass.', |
||
1088 | // static::class |
||
1089 | // )); |
||
1090 | |||
1091 | return null; |
||
1092 | } |
||
1093 | |||
1094 | return $this->getSubClass($this->getActiveSubclassCode()); |
||
1095 | } |
||
1096 | |||
1097 | public function getActiveSubclassCode() |
||
1098 | { |
||
1099 | if (!$this->hasActiveSubClass()) { |
||
1100 | @trigger_error(sprintf( |
||
1101 | 'Calling %s() when there is no active subclass is deprecated since sonata-project/admin-bundle 3.52 and will throw an exception in 4.0. '. |
||
1102 | 'Use %s::hasActiveSubClass() to know if there is an active subclass.', |
||
1103 | __METHOD__, |
||
1104 | __CLASS__ |
||
1105 | ), E_USER_DEPRECATED); |
||
1106 | // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare string as return type |
||
1107 | // throw new \LogicException(sprintf( |
||
1108 | // 'Admin "%s" has no active subclass.', |
||
1109 | // static::class |
||
1110 | // )); |
||
1111 | |||
1112 | return null; |
||
1113 | } |
||
1114 | |||
1115 | $subClass = $this->getRequest()->query->get('subclass'); |
||
1116 | |||
1117 | if (!$this->hasSubClass($subClass)) { |
||
1118 | @trigger_error(sprintf( |
||
1119 | 'Calling %s() when there is no active subclass is deprecated since sonata-project/admin-bundle 3.52 and will throw an exception in 4.0. '. |
||
1120 | 'Use %s::hasActiveSubClass() to know if there is an active subclass.', |
||
1121 | __METHOD__, |
||
1122 | __CLASS__ |
||
1123 | ), E_USER_DEPRECATED); |
||
1124 | // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare string as return type |
||
1125 | // throw new \LogicException(sprintf( |
||
1126 | // 'Admin "%s" has no active subclass.', |
||
1127 | // static::class |
||
1128 | // )); |
||
1129 | |||
1130 | return null; |
||
1131 | } |
||
1132 | |||
1133 | return $subClass; |
||
1134 | } |
||
1135 | |||
1136 | public function getBatchActions() |
||
1137 | { |
||
1138 | $actions = []; |
||
1139 | |||
1140 | if ($this->hasRoute('delete') && $this->hasAccess('delete')) { |
||
1141 | $actions['delete'] = [ |
||
1142 | 'label' => 'action_delete', |
||
1143 | 'translation_domain' => 'SonataAdminBundle', |
||
1144 | 'ask_confirmation' => true, // by default always true |
||
1145 | ]; |
||
1146 | } |
||
1147 | |||
1148 | $actions = $this->configureBatchActions($actions); |
||
1149 | |||
1150 | foreach ($this->getExtensions() as $extension) { |
||
1151 | // NEXT_MAJOR: remove method check |
||
1152 | if (method_exists($extension, 'configureBatchActions')) { |
||
1153 | $actions = $extension->configureBatchActions($this, $actions); |
||
1154 | } |
||
1155 | } |
||
1156 | |||
1157 | foreach ($actions as $name => &$action) { |
||
1158 | if (!\array_key_exists('label', $action)) { |
||
1159 | $action['label'] = $this->getTranslationLabel($name, 'batch', 'label'); |
||
1160 | } |
||
1161 | |||
1162 | if (!\array_key_exists('translation_domain', $action)) { |
||
1163 | $action['translation_domain'] = $this->getTranslationDomain(); |
||
1164 | } |
||
1165 | } |
||
1166 | |||
1167 | return $actions; |
||
1168 | } |
||
1169 | |||
1170 | public function getRoutes() |
||
1171 | { |
||
1172 | $this->buildRoutes(); |
||
1173 | |||
1174 | return $this->routes; |
||
1175 | } |
||
1176 | |||
1177 | public function getRouterIdParameter() |
||
1178 | { |
||
1179 | return '{'.$this->getIdParameter().'}'; |
||
1180 | } |
||
1181 | |||
1182 | public function getIdParameter() |
||
1183 | { |
||
1184 | $parameter = 'id'; |
||
1185 | |||
1186 | for ($i = 0; $i < $this->getChildDepth(); ++$i) { |
||
1187 | $parameter = 'child'.ucfirst($parameter); |
||
1188 | } |
||
1189 | |||
1190 | return $parameter; |
||
1191 | } |
||
1192 | |||
1193 | public function hasRoute($name) |
||
1194 | { |
||
1195 | if (!$this->routeGenerator) { |
||
1196 | throw new \RuntimeException('RouteGenerator cannot be null'); |
||
1197 | } |
||
1198 | |||
1199 | return $this->routeGenerator->hasAdminRoute($this, $name); |
||
1200 | } |
||
1201 | |||
1202 | /** |
||
1203 | * @param string $name |
||
1204 | * @param string|null $adminCode |
||
1205 | * |
||
1206 | * @return bool |
||
1207 | */ |
||
1208 | public function isCurrentRoute($name, $adminCode = null) |
||
1209 | { |
||
1210 | if (!$this->hasRequest()) { |
||
1211 | return false; |
||
1212 | } |
||
1213 | |||
1214 | $request = $this->getRequest(); |
||
1215 | $route = $request->get('_route'); |
||
1216 | |||
1217 | if ($adminCode) { |
||
1218 | $admin = $this->getConfigurationPool()->getAdminByAdminCode($adminCode); |
||
1219 | } else { |
||
1220 | $admin = $this; |
||
1221 | } |
||
1222 | |||
1223 | if (!$admin) { |
||
1224 | return false; |
||
1225 | } |
||
1226 | |||
1227 | return ($admin->getBaseRouteName().'_'.$name) === $route; |
||
1228 | } |
||
1229 | |||
1230 | public function generateObjectUrl($name, $object, array $parameters = [], $absolute = RoutingUrlGeneratorInterface::ABSOLUTE_PATH) |
||
1231 | { |
||
1232 | $parameters['id'] = $this->getUrlsafeIdentifier($object); |
||
1233 | |||
1234 | return $this->generateUrl($name, $parameters, $absolute); |
||
1235 | } |
||
1236 | |||
1237 | public function generateUrl($name, array $parameters = [], $absolute = RoutingUrlGeneratorInterface::ABSOLUTE_PATH) |
||
1238 | { |
||
1239 | return $this->routeGenerator->generateUrl($this, $name, $parameters, $absolute); |
||
1240 | } |
||
1241 | |||
1242 | public function generateMenuUrl($name, array $parameters = [], $absolute = RoutingUrlGeneratorInterface::ABSOLUTE_PATH) |
||
1243 | { |
||
1244 | return $this->routeGenerator->generateMenuUrl($this, $name, $parameters, $absolute); |
||
1245 | } |
||
1246 | |||
1247 | final public function setTemplateRegistry(MutableTemplateRegistryInterface $templateRegistry) |
||
1248 | { |
||
1249 | $this->templateRegistry = $templateRegistry; |
||
1250 | } |
||
1251 | |||
1252 | /** |
||
1253 | * @param array<string, string> $templates |
||
1254 | */ |
||
1255 | public function setTemplates(array $templates) |
||
1256 | { |
||
1257 | // NEXT_MAJOR: Remove this line |
||
1258 | $this->templates = $templates; |
||
1259 | |||
1260 | $this->getTemplateRegistry()->setTemplates($templates); |
||
1261 | } |
||
1262 | |||
1263 | /** |
||
1264 | * @param string $name |
||
1265 | * @param string $template |
||
1266 | */ |
||
1267 | public function setTemplate($name, $template) |
||
1268 | { |
||
1269 | // NEXT_MAJOR: Remove this line |
||
1270 | $this->templates[$name] = $template; |
||
1271 | |||
1272 | $this->getTemplateRegistry()->setTemplate($name, $template); |
||
1273 | } |
||
1274 | |||
1275 | /** |
||
1276 | * @deprecated since sonata-project/admin-bundle 3.34, will be dropped in 4.0. Use TemplateRegistry services instead |
||
1277 | * |
||
1278 | * @return array<string, string> |
||
1279 | */ |
||
1280 | public function getTemplates() |
||
1281 | { |
||
1282 | return $this->getTemplateRegistry()->getTemplates(); |
||
1283 | } |
||
1284 | |||
1285 | /** |
||
1286 | * @deprecated since sonata-project/admin-bundle 3.34, will be dropped in 4.0. Use TemplateRegistry services instead |
||
1287 | * |
||
1288 | * @param string $name |
||
1289 | * |
||
1290 | * @return string|null |
||
1291 | */ |
||
1292 | public function getTemplate($name) |
||
1293 | { |
||
1294 | return $this->getTemplateRegistry()->getTemplate($name); |
||
1295 | } |
||
1296 | |||
1297 | public function getNewInstance() |
||
1298 | { |
||
1299 | $object = $this->getModelManager()->getModelInstance($this->getClass()); |
||
1300 | foreach ($this->getExtensions() as $extension) { |
||
1301 | $extension->alterNewInstance($this, $object); |
||
1302 | } |
||
1303 | |||
1304 | return $object; |
||
1305 | } |
||
1306 | |||
1307 | public function getFormBuilder() |
||
1308 | { |
||
1309 | $this->formOptions['data_class'] = $this->getClass(); |
||
1310 | |||
1311 | $formBuilder = $this->getFormContractor()->getFormBuilder( |
||
1312 | $this->getUniqid(), |
||
1313 | $this->formOptions |
||
1314 | ); |
||
1315 | |||
1316 | $this->defineFormBuilder($formBuilder); |
||
1317 | |||
1318 | return $formBuilder; |
||
1319 | } |
||
1320 | |||
1321 | /** |
||
1322 | * This method is being called by the main admin class and the child class, |
||
1323 | * the getFormBuilder is only call by the main admin class. |
||
1324 | */ |
||
1325 | public function defineFormBuilder(FormBuilderInterface $formBuilder) |
||
1326 | { |
||
1327 | $mapper = new FormMapper($this->getFormContractor(), $formBuilder, $this); |
||
1328 | |||
1329 | $this->configureFormFields($mapper); |
||
1330 | |||
1331 | foreach ($this->getExtensions() as $extension) { |
||
1332 | $extension->configureFormFields($mapper); |
||
1333 | } |
||
1334 | |||
1335 | $this->attachInlineValidator(); |
||
1336 | } |
||
1337 | |||
1338 | public function attachAdminClass(FieldDescriptionInterface $fieldDescription) |
||
1339 | { |
||
1340 | $pool = $this->getConfigurationPool(); |
||
1341 | |||
1342 | $adminCode = $fieldDescription->getOption('admin_code'); |
||
1343 | |||
1344 | if (null !== $adminCode) { |
||
1345 | $admin = $pool->getAdminByAdminCode($adminCode); |
||
1346 | } else { |
||
1347 | $admin = $pool->getAdminByClass($fieldDescription->getTargetEntity()); |
||
1348 | } |
||
1349 | |||
1350 | if (!$admin) { |
||
1351 | return; |
||
1352 | } |
||
1353 | |||
1354 | if ($this->hasRequest()) { |
||
1355 | $admin->setRequest($this->getRequest()); |
||
1356 | } |
||
1357 | |||
1358 | $fieldDescription->setAssociationAdmin($admin); |
||
1359 | } |
||
1360 | |||
1361 | public function getObject($id) |
||
1362 | { |
||
1363 | $object = $this->getModelManager()->find($this->getClass(), $id); |
||
1364 | foreach ($this->getExtensions() as $extension) { |
||
1365 | $extension->alterObject($this, $object); |
||
1366 | } |
||
1367 | |||
1368 | return $object; |
||
1369 | } |
||
1370 | |||
1371 | public function getForm() |
||
1372 | { |
||
1373 | $this->buildForm(); |
||
1374 | |||
1375 | return $this->form; |
||
1376 | } |
||
1377 | |||
1378 | public function getList() |
||
1379 | { |
||
1380 | $this->buildList(); |
||
1381 | |||
1382 | return $this->list; |
||
1383 | } |
||
1384 | |||
1385 | /** |
||
1386 | * @final since sonata-project/admin-bundle 3.x |
||
1387 | */ |
||
1388 | public function createQuery($context = 'list') |
||
1389 | { |
||
1390 | if (\func_num_args() > 0) { |
||
1391 | @trigger_error( |
||
1392 | 'The $context argument of '.__METHOD__.' is deprecated since 3.3, to be removed in 4.0.', |
||
1393 | E_USER_DEPRECATED |
||
1394 | ); |
||
1395 | } |
||
1396 | |||
1397 | $query = $this->getModelManager()->createQuery($this->getClass()); |
||
1398 | |||
1399 | $query = $this->configureQuery($query); |
||
1400 | foreach ($this->extensions as $extension) { |
||
1401 | $extension->configureQuery($this, $query, $context); |
||
1402 | } |
||
1403 | |||
1404 | return $query; |
||
1405 | } |
||
1406 | |||
1407 | public function getDatagrid() |
||
1408 | { |
||
1409 | $this->buildDatagrid(); |
||
1410 | |||
1411 | return $this->datagrid; |
||
1412 | } |
||
1413 | |||
1414 | public function buildTabMenu($action, AdminInterface $childAdmin = null) |
||
1415 | { |
||
1416 | if ($this->loaded['tab_menu']) { |
||
1417 | return $this->menu; |
||
1418 | } |
||
1419 | |||
1420 | $this->loaded['tab_menu'] = true; |
||
1421 | |||
1422 | $menu = $this->menuFactory->createItem('root'); |
||
1423 | $menu->setChildrenAttribute('class', 'nav navbar-nav'); |
||
1424 | $menu->setExtra('translation_domain', $this->translationDomain); |
||
1425 | |||
1426 | // Prevents BC break with KnpMenuBundle v1.x |
||
1427 | if (method_exists($menu, 'setCurrentUri')) { |
||
1428 | $menu->setCurrentUri($this->getRequest()->getBaseUrl().$this->getRequest()->getPathInfo()); |
||
1429 | } |
||
1430 | |||
1431 | $this->configureTabMenu($menu, $action, $childAdmin); |
||
1432 | |||
1433 | foreach ($this->getExtensions() as $extension) { |
||
1434 | $extension->configureTabMenu($this, $menu, $action, $childAdmin); |
||
1435 | } |
||
1436 | |||
1437 | $this->menu = $menu; |
||
1438 | |||
1439 | return $this->menu; |
||
1440 | } |
||
1441 | |||
1442 | public function buildSideMenu($action, AdminInterface $childAdmin = null) |
||
1443 | { |
||
1444 | return $this->buildTabMenu($action, $childAdmin); |
||
1445 | } |
||
1446 | |||
1447 | /** |
||
1448 | * @param string $action |
||
1449 | * |
||
1450 | * @return ItemInterface |
||
1451 | */ |
||
1452 | public function getSideMenu($action, AdminInterface $childAdmin = null) |
||
1453 | { |
||
1454 | if ($this->isChild()) { |
||
1455 | return $this->getParent()->getSideMenu($action, $this); |
||
1456 | } |
||
1457 | |||
1458 | $this->buildSideMenu($action, $childAdmin); |
||
1459 | |||
1460 | return $this->menu; |
||
1461 | } |
||
1462 | |||
1463 | /** |
||
1464 | * Returns the root code. |
||
1465 | * |
||
1466 | * @return string the root code |
||
1467 | */ |
||
1468 | public function getRootCode() |
||
1469 | { |
||
1470 | return $this->getRoot()->getCode(); |
||
1471 | } |
||
1472 | |||
1473 | /** |
||
1474 | * Returns the master admin. |
||
1475 | * |
||
1476 | * @return AbstractAdmin the root admin class |
||
1477 | */ |
||
1478 | public function getRoot() |
||
1479 | { |
||
1480 | $parentFieldDescription = $this->getParentFieldDescription(); |
||
1481 | |||
1482 | if (!$parentFieldDescription) { |
||
1483 | return $this; |
||
1484 | } |
||
1485 | |||
1486 | return $parentFieldDescription->getAdmin()->getRoot(); |
||
1487 | } |
||
1488 | |||
1489 | public function setBaseControllerName($baseControllerName) |
||
1490 | { |
||
1491 | $this->baseControllerName = $baseControllerName; |
||
1492 | } |
||
1493 | |||
1494 | public function getBaseControllerName() |
||
1495 | { |
||
1496 | return $this->baseControllerName; |
||
1497 | } |
||
1498 | |||
1499 | /** |
||
1500 | * @param string $label |
||
1501 | */ |
||
1502 | public function setLabel($label) |
||
1503 | { |
||
1504 | $this->label = $label; |
||
1505 | } |
||
1506 | |||
1507 | public function getLabel() |
||
1508 | { |
||
1509 | return $this->label; |
||
1510 | } |
||
1511 | |||
1512 | /** |
||
1513 | * @param bool $persist |
||
1514 | * |
||
1515 | * NEXT_MAJOR: remove this method |
||
1516 | * |
||
1517 | * @deprecated since sonata-project/admin-bundle 3.34, to be removed in 4.0. |
||
1518 | */ |
||
1519 | public function setPersistFilters($persist) |
||
1520 | { |
||
1521 | @trigger_error( |
||
1522 | 'The '.__METHOD__.' method is deprecated since version 3.34 and will be removed in 4.0.', |
||
1523 | E_USER_DEPRECATED |
||
1524 | ); |
||
1525 | |||
1526 | $this->persistFilters = $persist; |
||
1527 | } |
||
1528 | |||
1529 | public function setFilterPersister(FilterPersisterInterface $filterPersister = null) |
||
1530 | { |
||
1531 | $this->filterPersister = $filterPersister; |
||
1532 | // NEXT_MAJOR remove the deprecated property will be removed. Needed for persisted filter condition. |
||
1533 | $this->persistFilters = true; |
||
1534 | } |
||
1535 | |||
1536 | /** |
||
1537 | * @param int $maxPerPage |
||
1538 | */ |
||
1539 | public function setMaxPerPage($maxPerPage) |
||
1540 | { |
||
1541 | $this->maxPerPage = $maxPerPage; |
||
1542 | } |
||
1543 | |||
1544 | /** |
||
1545 | * @return int |
||
1546 | */ |
||
1547 | public function getMaxPerPage() |
||
1548 | { |
||
1549 | return $this->maxPerPage; |
||
1550 | } |
||
1551 | |||
1552 | /** |
||
1553 | * @param int $maxPageLinks |
||
1554 | */ |
||
1555 | public function setMaxPageLinks($maxPageLinks) |
||
1556 | { |
||
1557 | $this->maxPageLinks = $maxPageLinks; |
||
1558 | } |
||
1559 | |||
1560 | /** |
||
1561 | * @return int |
||
1562 | */ |
||
1563 | public function getMaxPageLinks() |
||
1564 | { |
||
1565 | return $this->maxPageLinks; |
||
1566 | } |
||
1567 | |||
1568 | public function getFormGroups() |
||
1569 | { |
||
1570 | return $this->formGroups; |
||
1571 | } |
||
1572 | |||
1573 | public function setFormGroups(array $formGroups) |
||
1574 | { |
||
1575 | $this->formGroups = $formGroups; |
||
1576 | } |
||
1577 | |||
1578 | public function removeFieldFromFormGroup($key) |
||
1579 | { |
||
1580 | foreach ($this->formGroups as $name => $formGroup) { |
||
1581 | unset($this->formGroups[$name]['fields'][$key]); |
||
1582 | |||
1583 | if (empty($this->formGroups[$name]['fields'])) { |
||
1584 | unset($this->formGroups[$name]); |
||
1585 | } |
||
1586 | } |
||
1587 | } |
||
1588 | |||
1589 | /** |
||
1590 | * @param array $group |
||
1591 | */ |
||
1592 | public function reorderFormGroup($group, array $keys) |
||
1593 | { |
||
1594 | $formGroups = $this->getFormGroups(); |
||
1595 | $formGroups[$group]['fields'] = array_merge(array_flip($keys), $formGroups[$group]['fields']); |
||
1596 | $this->setFormGroups($formGroups); |
||
1597 | } |
||
1598 | |||
1599 | public function getFormTabs() |
||
1600 | { |
||
1601 | return $this->formTabs; |
||
1602 | } |
||
1603 | |||
1604 | public function setFormTabs(array $formTabs) |
||
1605 | { |
||
1606 | $this->formTabs = $formTabs; |
||
1607 | } |
||
1608 | |||
1609 | public function getShowTabs() |
||
1610 | { |
||
1611 | return $this->showTabs; |
||
1612 | } |
||
1613 | |||
1614 | public function setShowTabs(array $showTabs) |
||
1615 | { |
||
1616 | $this->showTabs = $showTabs; |
||
1617 | } |
||
1618 | |||
1619 | public function getShowGroups() |
||
1620 | { |
||
1621 | return $this->showGroups; |
||
1622 | } |
||
1623 | |||
1624 | public function setShowGroups(array $showGroups) |
||
1625 | { |
||
1626 | $this->showGroups = $showGroups; |
||
1627 | } |
||
1628 | |||
1629 | public function reorderShowGroup($group, array $keys) |
||
1630 | { |
||
1631 | $showGroups = $this->getShowGroups(); |
||
1632 | $showGroups[$group]['fields'] = array_merge(array_flip($keys), $showGroups[$group]['fields']); |
||
1633 | $this->setShowGroups($showGroups); |
||
1634 | } |
||
1635 | |||
1636 | public function setParentFieldDescription(FieldDescriptionInterface $parentFieldDescription) |
||
1637 | { |
||
1638 | $this->parentFieldDescription = $parentFieldDescription; |
||
1639 | } |
||
1640 | |||
1641 | public function getParentFieldDescription() |
||
1642 | { |
||
1643 | return $this->parentFieldDescription; |
||
1644 | } |
||
1645 | |||
1646 | public function hasParentFieldDescription() |
||
1647 | { |
||
1648 | return $this->parentFieldDescription instanceof FieldDescriptionInterface; |
||
1649 | } |
||
1650 | |||
1651 | public function setSubject($subject) |
||
1652 | { |
||
1653 | if (\is_object($subject) && !is_a($subject, $this->getClass(), true)) { |
||
1654 | $message = <<<'EOT' |
||
1655 | You are trying to set entity an instance of "%s", |
||
1656 | which is not the one registered with this admin class ("%s"). |
||
1657 | This is deprecated since 3.5 and will no longer be supported in 4.0. |
||
1658 | EOT; |
||
1659 | |||
1660 | @trigger_error( |
||
1661 | sprintf($message, \get_class($subject), $this->getClass()), |
||
1662 | E_USER_DEPRECATED |
||
1663 | ); // NEXT_MAJOR : throw an exception instead |
||
1664 | } |
||
1665 | |||
1666 | $this->subject = $subject; |
||
1667 | } |
||
1668 | |||
1669 | public function getSubject() |
||
1670 | { |
||
1671 | if (null === $this->subject && $this->request && !$this->hasParentFieldDescription()) { |
||
1672 | $id = $this->request->get($this->getIdParameter()); |
||
1673 | |||
1674 | if (null !== $id) { |
||
1675 | $this->subject = $this->getObject($id); |
||
1676 | } |
||
1677 | } |
||
1678 | |||
1679 | return $this->subject; |
||
1680 | } |
||
1681 | |||
1682 | public function hasSubject() |
||
1683 | { |
||
1684 | return (bool) $this->getSubject(); |
||
1685 | } |
||
1686 | |||
1687 | public function getFormFieldDescriptions() |
||
1688 | { |
||
1689 | $this->buildForm(); |
||
1690 | |||
1691 | return $this->formFieldDescriptions; |
||
1692 | } |
||
1693 | |||
1694 | public function getFormFieldDescription($name) |
||
1695 | { |
||
1696 | return $this->hasFormFieldDescription($name) ? $this->formFieldDescriptions[$name] : null; |
||
1697 | } |
||
1698 | |||
1699 | /** |
||
1700 | * Returns true if the admin has a FieldDescription with the given $name. |
||
1701 | * |
||
1702 | * @param string $name |
||
1703 | * |
||
1704 | * @return bool |
||
1705 | */ |
||
1706 | public function hasFormFieldDescription($name) |
||
1707 | { |
||
1708 | return \array_key_exists($name, $this->formFieldDescriptions) ? true : false; |
||
1709 | } |
||
1710 | |||
1711 | public function addFormFieldDescription($name, FieldDescriptionInterface $fieldDescription) |
||
1712 | { |
||
1713 | $this->formFieldDescriptions[$name] = $fieldDescription; |
||
1714 | } |
||
1715 | |||
1716 | /** |
||
1717 | * remove a FieldDescription. |
||
1718 | * |
||
1719 | * @param string $name |
||
1720 | */ |
||
1721 | public function removeFormFieldDescription($name) |
||
1722 | { |
||
1723 | unset($this->formFieldDescriptions[$name]); |
||
1724 | } |
||
1725 | |||
1726 | /** |
||
1727 | * build and return the collection of form FieldDescription. |
||
1728 | * |
||
1729 | * @return array collection of form FieldDescription |
||
1730 | */ |
||
1731 | public function getShowFieldDescriptions() |
||
1732 | { |
||
1733 | $this->buildShow(); |
||
1734 | |||
1735 | return $this->showFieldDescriptions; |
||
1736 | } |
||
1737 | |||
1738 | /** |
||
1739 | * Returns the form FieldDescription with the given $name. |
||
1740 | * |
||
1741 | * @param string $name |
||
1742 | * |
||
1743 | * @return FieldDescriptionInterface |
||
1744 | */ |
||
1745 | public function getShowFieldDescription($name) |
||
1746 | { |
||
1747 | $this->buildShow(); |
||
1748 | |||
1749 | return $this->hasShowFieldDescription($name) ? $this->showFieldDescriptions[$name] : null; |
||
1750 | } |
||
1751 | |||
1752 | public function hasShowFieldDescription($name) |
||
1753 | { |
||
1754 | return \array_key_exists($name, $this->showFieldDescriptions); |
||
1755 | } |
||
1756 | |||
1757 | public function addShowFieldDescription($name, FieldDescriptionInterface $fieldDescription) |
||
1758 | { |
||
1759 | $this->showFieldDescriptions[$name] = $fieldDescription; |
||
1760 | } |
||
1761 | |||
1762 | public function removeShowFieldDescription($name) |
||
1763 | { |
||
1764 | unset($this->showFieldDescriptions[$name]); |
||
1765 | } |
||
1766 | |||
1767 | public function getListFieldDescriptions() |
||
1768 | { |
||
1769 | $this->buildList(); |
||
1770 | |||
1771 | return $this->listFieldDescriptions; |
||
1772 | } |
||
1773 | |||
1774 | public function getListFieldDescription($name) |
||
1775 | { |
||
1776 | return $this->hasListFieldDescription($name) ? $this->listFieldDescriptions[$name] : null; |
||
1777 | } |
||
1778 | |||
1779 | public function hasListFieldDescription($name) |
||
1780 | { |
||
1781 | $this->buildList(); |
||
1782 | |||
1783 | return \array_key_exists($name, $this->listFieldDescriptions) ? true : false; |
||
1784 | } |
||
1785 | |||
1786 | public function addListFieldDescription($name, FieldDescriptionInterface $fieldDescription) |
||
1787 | { |
||
1788 | $this->listFieldDescriptions[$name] = $fieldDescription; |
||
1789 | } |
||
1790 | |||
1791 | public function removeListFieldDescription($name) |
||
1792 | { |
||
1793 | unset($this->listFieldDescriptions[$name]); |
||
1794 | } |
||
1795 | |||
1796 | public function getFilterFieldDescription($name) |
||
1797 | { |
||
1798 | return $this->hasFilterFieldDescription($name) ? $this->filterFieldDescriptions[$name] : null; |
||
1799 | } |
||
1800 | |||
1801 | public function hasFilterFieldDescription($name) |
||
1802 | { |
||
1803 | return \array_key_exists($name, $this->filterFieldDescriptions) ? true : false; |
||
1804 | } |
||
1805 | |||
1806 | public function addFilterFieldDescription($name, FieldDescriptionInterface $fieldDescription) |
||
1807 | { |
||
1808 | $this->filterFieldDescriptions[$name] = $fieldDescription; |
||
1809 | } |
||
1810 | |||
1811 | public function removeFilterFieldDescription($name) |
||
1812 | { |
||
1813 | unset($this->filterFieldDescriptions[$name]); |
||
1814 | } |
||
1815 | |||
1816 | public function getFilterFieldDescriptions() |
||
1817 | { |
||
1818 | $this->buildDatagrid(); |
||
1819 | |||
1820 | return $this->filterFieldDescriptions; |
||
1821 | } |
||
1822 | |||
1823 | public function addChild(AdminInterface $child) |
||
1824 | { |
||
1825 | for ($parentAdmin = $this; null !== $parentAdmin; $parentAdmin = $parentAdmin->getParent()) { |
||
1826 | if ($parentAdmin->getCode() !== $child->getCode()) { |
||
1827 | continue; |
||
1828 | } |
||
1829 | |||
1830 | throw new \RuntimeException(sprintf( |
||
1831 | 'Circular reference detected! The child admin `%s` is already in the parent tree of the `%s` admin.', |
||
1832 | $child->getCode(), |
||
1833 | $this->getCode() |
||
1834 | )); |
||
1835 | } |
||
1836 | |||
1837 | $this->children[$child->getCode()] = $child; |
||
1838 | |||
1839 | $child->setParent($this); |
||
1840 | |||
1841 | // NEXT_MAJOR: remove $args and add $field parameter to this function on next Major |
||
1842 | |||
1843 | $args = \func_get_args(); |
||
1844 | |||
1845 | if (isset($args[1])) { |
||
1846 | $child->addParentAssociationMapping($this->getCode(), $args[1]); |
||
1847 | } else { |
||
1848 | @trigger_error( |
||
1849 | 'Calling "addChild" without second argument is deprecated since' |
||
1850 | .' sonata-project/admin-bundle 3.35 and will not be allowed in 4.0.', |
||
1851 | E_USER_DEPRECATED |
||
1852 | ); |
||
1853 | } |
||
1854 | } |
||
1855 | |||
1856 | public function hasChild($code) |
||
1857 | { |
||
1858 | return isset($this->children[$code]); |
||
1859 | } |
||
1860 | |||
1861 | public function getChildren() |
||
1862 | { |
||
1863 | return $this->children; |
||
1864 | } |
||
1865 | |||
1866 | public function getChild($code) |
||
1867 | { |
||
1868 | return $this->hasChild($code) ? $this->children[$code] : null; |
||
1869 | } |
||
1870 | |||
1871 | public function setParent(AdminInterface $parent) |
||
1872 | { |
||
1873 | $this->parent = $parent; |
||
1874 | } |
||
1875 | |||
1876 | public function getParent() |
||
1877 | { |
||
1878 | return $this->parent; |
||
1879 | } |
||
1880 | |||
1881 | final public function getRootAncestor() |
||
1882 | { |
||
1883 | $parent = $this; |
||
1884 | |||
1885 | while ($parent->isChild()) { |
||
1886 | $parent = $parent->getParent(); |
||
1887 | } |
||
1888 | |||
1889 | return $parent; |
||
1890 | } |
||
1891 | |||
1892 | final public function getChildDepth() |
||
1893 | { |
||
1894 | $parent = $this; |
||
1895 | $depth = 0; |
||
1896 | |||
1897 | while ($parent->isChild()) { |
||
1898 | $parent = $parent->getParent(); |
||
1899 | ++$depth; |
||
1900 | } |
||
1901 | |||
1902 | return $depth; |
||
1903 | } |
||
1904 | |||
1905 | final public function getCurrentLeafChildAdmin() |
||
1906 | { |
||
1907 | $child = $this->getCurrentChildAdmin(); |
||
1908 | |||
1909 | if (null === $child) { |
||
1910 | return null; |
||
1911 | } |
||
1912 | |||
1913 | for ($c = $child; null !== $c; $c = $child->getCurrentChildAdmin()) { |
||
1914 | $child = $c; |
||
1915 | } |
||
1916 | |||
1917 | return $child; |
||
1918 | } |
||
1919 | |||
1920 | public function isChild() |
||
1921 | { |
||
1922 | return $this->parent instanceof AdminInterface; |
||
1923 | } |
||
1924 | |||
1925 | /** |
||
1926 | * Returns true if the admin has children, false otherwise. |
||
1927 | * |
||
1928 | * @return bool if the admin has children |
||
1929 | */ |
||
1930 | public function hasChildren() |
||
1931 | { |
||
1932 | return \count($this->children) > 0; |
||
1933 | } |
||
1934 | |||
1935 | public function setUniqid($uniqid) |
||
1936 | { |
||
1937 | $this->uniqid = $uniqid; |
||
1938 | } |
||
1939 | |||
1940 | public function getUniqid() |
||
1941 | { |
||
1942 | if (!$this->uniqid) { |
||
1943 | $this->uniqid = 's'.uniqid(); |
||
1944 | } |
||
1945 | |||
1946 | return $this->uniqid; |
||
1947 | } |
||
1948 | |||
1949 | /** |
||
1950 | * Returns the classname label. |
||
1951 | * |
||
1952 | * @return string the classname label |
||
1953 | */ |
||
1954 | public function getClassnameLabel() |
||
1955 | { |
||
1956 | return $this->classnameLabel; |
||
1957 | } |
||
1958 | |||
1959 | public function getPersistentParameters() |
||
1960 | { |
||
1961 | $parameters = []; |
||
1962 | |||
1963 | foreach ($this->getExtensions() as $extension) { |
||
1964 | $params = $extension->getPersistentParameters($this); |
||
1965 | |||
1966 | if (!\is_array($params)) { |
||
1967 | throw new \RuntimeException(sprintf('The %s::getPersistentParameters must return an array', \get_class($extension))); |
||
1968 | } |
||
1969 | |||
1970 | $parameters = array_merge($parameters, $params); |
||
1971 | } |
||
1972 | |||
1973 | return $parameters; |
||
1974 | } |
||
1975 | |||
1976 | /** |
||
1977 | * @param string $name |
||
1978 | * |
||
1979 | * @return mixed|null |
||
1980 | */ |
||
1981 | public function getPersistentParameter($name) |
||
1982 | { |
||
1983 | $parameters = $this->getPersistentParameters(); |
||
1984 | |||
1985 | return $parameters[$name] ?? null; |
||
1986 | } |
||
1987 | |||
1988 | public function getBreadcrumbs($action) |
||
1998 | |||
1999 | /** |
||
2000 | * Generates the breadcrumbs array. |
||
2001 | * |
||
2002 | * Note: the method will be called by the top admin instance (parent => child) |
||
2003 | * |
||
2004 | * @param string $action |
||
2005 | * |
||
2006 | * @return array |
||
2007 | */ |
||
2008 | public function buildBreadcrumbs($action, MenuItemInterface $menu = null) |
||
2022 | |||
2023 | /** |
||
2024 | * NEXT_MAJOR : remove this method. |
||
2025 | * |
||
2026 | * @return BreadcrumbsBuilderInterface |
||
2027 | */ |
||
2028 | final public function getBreadcrumbsBuilder() |
||
2043 | |||
2044 | /** |
||
2045 | * NEXT_MAJOR : remove this method. |
||
2046 | * |
||
2047 | * @return AbstractAdmin |
||
2048 | */ |
||
2049 | final public function setBreadcrumbsBuilder(BreadcrumbsBuilderInterface $value) |
||
2060 | |||
2061 | public function setCurrentChild($currentChild) |
||
2065 | |||
2066 | public function getCurrentChild() |
||
2070 | |||
2071 | /** |
||
2072 | * Returns the current child admin instance. |
||
2073 | * |
||
2074 | * @return AdminInterface|null the current child admin instance |
||
2075 | */ |
||
2076 | public function getCurrentChildAdmin() |
||
2084 | |||
2085 | public function trans($id, array $parameters = [], $domain = null, $locale = null) |
||
2096 | |||
2097 | /** |
||
2098 | * Translate a message id. |
||
2099 | * |
||
2100 | * NEXT_MAJOR: remove this method |
||
2101 | * |
||
2102 | * @param string $id |
||
2103 | * @param int $count |
||
2104 | * @param string|null $domain |
||
2105 | * @param string|null $locale |
||
2106 | * |
||
2107 | * @return string the translated string |
||
2108 | * |
||
2109 | * @deprecated since sonata-project/admin-bundle 3.9, to be removed with 4.0 |
||
2110 | */ |
||
2111 | public function transChoice($id, $count, array $parameters = [], $domain = null, $locale = null) |
||
2122 | |||
2123 | public function setTranslationDomain($translationDomain) |
||
2127 | |||
2128 | public function getTranslationDomain() |
||
2132 | |||
2133 | /** |
||
2134 | * {@inheritdoc} |
||
2135 | * |
||
2136 | * NEXT_MAJOR: remove this method |
||
2137 | * |
||
2138 | * @deprecated since sonata-project/admin-bundle 3.9, to be removed with 4.0 |
||
2139 | */ |
||
2140 | public function setTranslator(TranslatorInterface $translator) |
||
2152 | |||
2153 | /** |
||
2154 | * {@inheritdoc} |
||
2155 | * |
||
2156 | * NEXT_MAJOR: remove this method |
||
2157 | * |
||
2158 | * @deprecated since sonata-project/admin-bundle 3.9, to be removed with 4.0 |
||
2159 | */ |
||
2160 | public function getTranslator() |
||
2169 | |||
2170 | public function getTranslationLabel($label, $context = '', $type = '') |
||
2174 | |||
2175 | public function setRequest(Request $request) |
||
2183 | |||
2184 | public function getRequest() |
||
2192 | |||
2193 | public function hasRequest() |
||
2197 | |||
2198 | public function setFormContractor(FormContractorInterface $formBuilder) |
||
2202 | |||
2203 | /** |
||
2204 | * @return FormContractorInterface |
||
2205 | */ |
||
2206 | public function getFormContractor() |
||
2210 | |||
2211 | public function setDatagridBuilder(DatagridBuilderInterface $datagridBuilder) |
||
2215 | |||
2216 | public function getDatagridBuilder() |
||
2220 | |||
2221 | public function setListBuilder(ListBuilderInterface $listBuilder) |
||
2225 | |||
2226 | public function getListBuilder() |
||
2230 | |||
2231 | public function setShowBuilder(ShowBuilderInterface $showBuilder) |
||
2235 | |||
2236 | /** |
||
2237 | * @return ShowBuilderInterface |
||
2238 | */ |
||
2239 | public function getShowBuilder() |
||
2243 | |||
2244 | public function setConfigurationPool(Pool $configurationPool) |
||
2248 | |||
2249 | /** |
||
2250 | * @return Pool |
||
2251 | */ |
||
2252 | public function getConfigurationPool() |
||
2256 | |||
2257 | public function setRouteGenerator(RouteGeneratorInterface $routeGenerator) |
||
2261 | |||
2262 | /** |
||
2263 | * @return RouteGeneratorInterface |
||
2264 | */ |
||
2265 | public function getRouteGenerator() |
||
2269 | |||
2270 | public function getCode() |
||
2274 | |||
2275 | /** |
||
2276 | * NEXT_MAJOR: Remove this function. |
||
2277 | * |
||
2278 | * @deprecated This method is deprecated since sonata-project/admin-bundle 3.24 and will be removed in 4.0 |
||
2279 | * |
||
2280 | * @param string $baseCodeRoute |
||
2281 | */ |
||
2282 | public function setBaseCodeRoute($baseCodeRoute) |
||
2291 | |||
2292 | public function getBaseCodeRoute() |
||
2314 | |||
2315 | public function getModelManager() |
||
2319 | |||
2320 | public function setModelManager(ModelManagerInterface $modelManager) |
||
2324 | |||
2325 | public function getManagerType() |
||
2329 | |||
2330 | /** |
||
2331 | * @param string $type |
||
2332 | */ |
||
2333 | public function setManagerType($type) |
||
2337 | |||
2338 | public function getObjectIdentifier() |
||
2342 | |||
2343 | /** |
||
2344 | * Set the roles and permissions per role. |
||
2345 | */ |
||
2346 | public function setSecurityInformation(array $information) |
||
2350 | |||
2351 | public function getSecurityInformation() |
||
2355 | |||
2356 | /** |
||
2357 | * Return the list of permissions the user should have in order to display the admin. |
||
2358 | * |
||
2359 | * @param string $context |
||
2360 | * |
||
2361 | * @return array |
||
2362 | */ |
||
2363 | public function getPermissionsShow($context) |
||
2372 | |||
2373 | public function showIn($context) |
||
2382 | |||
2383 | public function createObjectSecurity($object) |
||
2387 | |||
2388 | public function setSecurityHandler(SecurityHandlerInterface $securityHandler) |
||
2392 | |||
2393 | public function getSecurityHandler() |
||
2394 | { |
||
2395 | return $this->securityHandler; |
||
2396 | } |
||
2397 | |||
2398 | public function isGranted($name, $object = null) |
||
2399 | { |
||
2400 | $key = md5(json_encode($name).($object ? '/'.spl_object_hash($object) : '')); |
||
2401 | |||
2402 | if (!\array_key_exists($key, $this->cacheIsGranted)) { |
||
2403 | $this->cacheIsGranted[$key] = $this->securityHandler->isGranted($this, $name, $object ?: $this); |
||
2404 | } |
||
2405 | |||
2406 | return $this->cacheIsGranted[$key]; |
||
2407 | } |
||
2408 | |||
2409 | public function getUrlsafeIdentifier($entity) |
||
2410 | { |
||
2411 | return $this->getModelManager()->getUrlsafeIdentifier($entity); |
||
2412 | } |
||
2413 | |||
2414 | public function getNormalizedIdentifier($entity) |
||
2415 | { |
||
2416 | return $this->getModelManager()->getNormalizedIdentifier($entity); |
||
2417 | } |
||
2418 | |||
2419 | public function id($entity) |
||
2420 | { |
||
2421 | return $this->getNormalizedIdentifier($entity); |
||
2422 | } |
||
2423 | |||
2424 | public function setValidator($validator) |
||
2425 | { |
||
2426 | // NEXT_MAJOR: Move ValidatorInterface check to method signature |
||
2427 | if (!$validator instanceof ValidatorInterface) { |
||
2428 | throw new \InvalidArgumentException( |
||
2429 | 'Argument 1 must be an instance of Symfony\Component\Validator\Validator\ValidatorInterface' |
||
2430 | ); |
||
2431 | } |
||
2432 | |||
2433 | $this->validator = $validator; |
||
2434 | } |
||
2435 | |||
2436 | public function getValidator() |
||
2437 | { |
||
2438 | return $this->validator; |
||
2439 | } |
||
2440 | |||
2441 | public function getShow() |
||
2442 | { |
||
2443 | $this->buildShow(); |
||
2444 | |||
2445 | return $this->show; |
||
2446 | } |
||
2447 | |||
2448 | public function setFormTheme(array $formTheme) |
||
2449 | { |
||
2450 | $this->formTheme = $formTheme; |
||
2451 | } |
||
2452 | |||
2453 | public function getFormTheme() |
||
2454 | { |
||
2455 | return $this->formTheme; |
||
2456 | } |
||
2457 | |||
2458 | public function setFilterTheme(array $filterTheme) |
||
2459 | { |
||
2460 | $this->filterTheme = $filterTheme; |
||
2461 | } |
||
2462 | |||
2463 | public function getFilterTheme() |
||
2464 | { |
||
2465 | return $this->filterTheme; |
||
2466 | } |
||
2467 | |||
2468 | public function addExtension(AdminExtensionInterface $extension) |
||
2469 | { |
||
2470 | $this->extensions[] = $extension; |
||
2471 | } |
||
2472 | |||
2473 | public function getExtensions() |
||
2477 | |||
2478 | public function setMenuFactory(MenuFactoryInterface $menuFactory) |
||
2482 | |||
2483 | public function getMenuFactory() |
||
2487 | |||
2488 | public function setRouteBuilder(RouteBuilderInterface $routeBuilder) |
||
2492 | |||
2493 | public function getRouteBuilder() |
||
2497 | |||
2498 | public function toString($object) |
||
2510 | |||
2511 | public function setLabelTranslatorStrategy(LabelTranslatorStrategyInterface $labelTranslatorStrategy) |
||
2515 | |||
2516 | public function getLabelTranslatorStrategy() |
||
2520 | |||
2521 | public function supportsPreviewMode() |
||
2525 | |||
2526 | /** |
||
2527 | * Set custom per page options. |
||
2528 | */ |
||
2529 | public function setPerPageOptions(array $options) |
||
2533 | |||
2534 | /** |
||
2535 | * Returns predefined per page options. |
||
2536 | * |
||
2537 | * @return array |
||
2538 | */ |
||
2539 | public function getPerPageOptions() |
||
2543 | |||
2544 | /** |
||
2545 | * Set pager type. |
||
2546 | * |
||
2547 | * @param string $pagerType |
||
2548 | */ |
||
2549 | public function setPagerType($pagerType) |
||
2553 | |||
2554 | /** |
||
2555 | * Get pager type. |
||
2556 | * |
||
2557 | * @return string |
||
2558 | */ |
||
2559 | public function getPagerType() |
||
2563 | |||
2564 | /** |
||
2565 | * Returns true if the per page value is allowed, false otherwise. |
||
2566 | * |
||
2567 | * @param int $perPage |
||
2568 | * |
||
2569 | * @return bool |
||
2570 | */ |
||
2571 | public function determinedPerPageValue($perPage) |
||
2575 | |||
2576 | public function isAclEnabled() |
||
2580 | |||
2581 | public function getObjectMetadata($object) |
||
2585 | |||
2586 | public function getListModes() |
||
2590 | |||
2591 | public function setListMode($mode) |
||
2599 | |||
2600 | public function getListMode() |
||
2608 | |||
2609 | public function getAccessMapping() |
||
2613 | |||
2614 | public function checkAccess($action, $object = null) |
||
2636 | |||
2637 | /** |
||
2638 | * Hook to handle access authorization, without throw Exception. |
||
2639 | * |
||
2640 | * @param string $action |
||
2641 | * @param object $object |
||
2642 | * |
||
2643 | * @return bool |
||
2644 | */ |
||
2645 | public function hasAccess($action, $object = null) |
||
2665 | |||
2666 | /** |
||
2667 | * @param string $action |
||
2668 | * @param object|null $object |
||
2669 | * |
||
2670 | * @return array |
||
2671 | */ |
||
2672 | public function configureActionButtons($action, $object = null) |
||
2673 | { |
||
2674 | $list = []; |
||
2675 | |||
2676 | if (\in_array($action, ['tree', 'show', 'edit', 'delete', 'list', 'batch'], true) |
||
2677 | && $this->hasAccess('create') |
||
2678 | && $this->hasRoute('create') |
||
2679 | ) { |
||
2680 | $list['create'] = [ |
||
2681 | // NEXT_MAJOR: Remove this line and use commented line below it instead |
||
2682 | 'template' => $this->getTemplate('button_create'), |
||
2683 | // 'template' => $this->getTemplateRegistry()->getTemplate('button_create'), |
||
2684 | ]; |
||
2685 | } |
||
2686 | |||
2687 | if (\in_array($action, ['show', 'delete', 'acl', 'history'], true) |
||
2688 | && $this->canAccessObject('edit', $object) |
||
2689 | && $this->hasRoute('edit') |
||
2690 | ) { |
||
2691 | $list['edit'] = [ |
||
2692 | // NEXT_MAJOR: Remove this line and use commented line below it instead |
||
2693 | 'template' => $this->getTemplate('button_edit'), |
||
2694 | //'template' => $this->getTemplateRegistry()->getTemplate('button_edit'), |
||
2695 | ]; |
||
2696 | } |
||
2697 | |||
2698 | if (\in_array($action, ['show', 'edit', 'acl'], true) |
||
2699 | && $this->canAccessObject('history', $object) |
||
2700 | && $this->hasRoute('history') |
||
2701 | ) { |
||
2702 | $list['history'] = [ |
||
2703 | // NEXT_MAJOR: Remove this line and use commented line below it instead |
||
2704 | 'template' => $this->getTemplate('button_history'), |
||
2705 | // 'template' => $this->getTemplateRegistry()->getTemplate('button_history'), |
||
2706 | ]; |
||
2707 | } |
||
2708 | |||
2709 | if (\in_array($action, ['edit', 'history'], true) |
||
2710 | && $this->isAclEnabled() |
||
2711 | && $this->canAccessObject('acl', $object) |
||
2712 | && $this->hasRoute('acl') |
||
2713 | ) { |
||
2714 | $list['acl'] = [ |
||
2715 | // NEXT_MAJOR: Remove this line and use commented line below it instead |
||
2716 | 'template' => $this->getTemplate('button_acl'), |
||
2717 | // 'template' => $this->getTemplateRegistry()->getTemplate('button_acl'), |
||
2718 | ]; |
||
2719 | } |
||
2720 | |||
2721 | if (\in_array($action, ['edit', 'history', 'acl'], true) |
||
2722 | && $this->canAccessObject('show', $object) |
||
2723 | && \count($this->getShow()) > 0 |
||
2724 | && $this->hasRoute('show') |
||
2725 | ) { |
||
2726 | $list['show'] = [ |
||
2727 | // NEXT_MAJOR: Remove this line and use commented line below it instead |
||
2728 | 'template' => $this->getTemplate('button_show'), |
||
2729 | // 'template' => $this->getTemplateRegistry()->getTemplate('button_show'), |
||
2730 | ]; |
||
2731 | } |
||
2732 | |||
2733 | if (\in_array($action, ['show', 'edit', 'delete', 'acl', 'batch'], true) |
||
2734 | && $this->hasAccess('list') |
||
2735 | && $this->hasRoute('list') |
||
2736 | ) { |
||
2737 | $list['list'] = [ |
||
2738 | // NEXT_MAJOR: Remove this line and use commented line below it instead |
||
2739 | 'template' => $this->getTemplate('button_list'), |
||
2740 | // 'template' => $this->getTemplateRegistry()->getTemplate('button_list'), |
||
2741 | ]; |
||
2742 | } |
||
2743 | |||
2744 | return $list; |
||
2745 | } |
||
2746 | |||
2747 | /** |
||
2748 | * @param string $action |
||
2749 | * @param object $object |
||
2750 | * |
||
2751 | * @return array |
||
2752 | */ |
||
2753 | public function getActionButtons($action, $object = null) |
||
2754 | { |
||
2755 | $list = $this->configureActionButtons($action, $object); |
||
2756 | |||
2757 | foreach ($this->getExtensions() as $extension) { |
||
2758 | // NEXT_MAJOR: remove method check |
||
2759 | if (method_exists($extension, 'configureActionButtons')) { |
||
2760 | $list = $extension->configureActionButtons($this, $list, $action, $object); |
||
2761 | } |
||
2762 | } |
||
2763 | |||
2764 | return $list; |
||
2765 | } |
||
2766 | |||
2767 | /** |
||
2768 | * Get the list of actions that can be accessed directly from the dashboard. |
||
2769 | * |
||
2770 | * @return array |
||
2771 | */ |
||
2772 | public function getDashboardActions() |
||
2773 | { |
||
2774 | $actions = []; |
||
2775 | |||
2776 | if ($this->hasRoute('create') && $this->hasAccess('create')) { |
||
2777 | $actions['create'] = [ |
||
2778 | 'label' => 'link_add', |
||
2779 | 'translation_domain' => 'SonataAdminBundle', |
||
2780 | // NEXT_MAJOR: Remove this line and use commented line below it instead |
||
2781 | 'template' => $this->getTemplate('action_create'), |
||
2782 | // 'template' => $this->getTemplateRegistry()->getTemplate('action_create'), |
||
2783 | 'url' => $this->generateUrl('create'), |
||
2784 | 'icon' => 'plus-circle', |
||
2785 | ]; |
||
2786 | } |
||
2787 | |||
2788 | if ($this->hasRoute('list') && $this->hasAccess('list')) { |
||
2789 | $actions['list'] = [ |
||
2790 | 'label' => 'link_list', |
||
2791 | 'translation_domain' => 'SonataAdminBundle', |
||
2792 | 'url' => $this->generateUrl('list'), |
||
2793 | 'icon' => 'list', |
||
2794 | ]; |
||
2795 | } |
||
2796 | |||
2797 | return $actions; |
||
2798 | } |
||
2799 | |||
2800 | /** |
||
2801 | * Setting to true will enable mosaic button for the admin screen. |
||
2802 | * Setting to false will hide mosaic button for the admin screen. |
||
2803 | * |
||
2804 | * @param bool $isShown |
||
2805 | */ |
||
2806 | final public function showMosaicButton($isShown) |
||
2807 | { |
||
2808 | if ($isShown) { |
||
2809 | $this->listModes['mosaic'] = ['class' => static::MOSAIC_ICON_CLASS]; |
||
2810 | } else { |
||
2811 | unset($this->listModes['mosaic']); |
||
2812 | } |
||
2813 | } |
||
2814 | |||
2815 | /** |
||
2816 | * @param object $object |
||
2817 | */ |
||
2818 | final public function getSearchResultLink($object) |
||
2819 | { |
||
2820 | foreach ($this->searchResultActions as $action) { |
||
2821 | if ($this->hasRoute($action) && $this->hasAccess($action, $object)) { |
||
2822 | return $this->generateObjectUrl($action, $object); |
||
2823 | } |
||
2824 | } |
||
2825 | } |
||
2826 | |||
2827 | /** |
||
2828 | * Checks if a filter type is set to a default value. |
||
2829 | * |
||
2830 | * @param string $name |
||
2831 | * |
||
2832 | * @return bool |
||
2833 | */ |
||
2834 | final public function isDefaultFilter($name) |
||
2835 | { |
||
2836 | $filter = $this->getFilterParameters(); |
||
2837 | $default = $this->getDefaultFilterValues(); |
||
2838 | |||
2839 | if (!\array_key_exists($name, $filter) || !\array_key_exists($name, $default)) { |
||
2840 | return false; |
||
2841 | } |
||
2842 | |||
2843 | return $filter[$name] === $default[$name]; |
||
2844 | } |
||
2845 | |||
2846 | /** |
||
2847 | * Check object existence and access, without throw Exception. |
||
2848 | * |
||
2849 | * @param string $action |
||
2850 | * @param object $object |
||
2851 | * |
||
2852 | * @return bool |
||
2853 | */ |
||
2854 | public function canAccessObject($action, $object) |
||
2855 | { |
||
2856 | return $object && $this->id($object) && $this->hasAccess($action, $object); |
||
2857 | } |
||
2858 | |||
2859 | protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface |
||
2860 | { |
||
2861 | return $query; |
||
2862 | } |
||
2863 | |||
2864 | /** |
||
2865 | * @return MutableTemplateRegistryInterface |
||
2866 | */ |
||
2867 | final protected function getTemplateRegistry() |
||
2868 | { |
||
2869 | return $this->templateRegistry; |
||
2870 | } |
||
2871 | |||
2872 | /** |
||
2873 | * Returns a list of default filters. |
||
2874 | * |
||
2875 | * @return array |
||
2876 | */ |
||
2877 | final protected function getDefaultFilterValues() |
||
2878 | { |
||
2879 | $defaultFilterValues = []; |
||
2880 | |||
2881 | $this->configureDefaultFilterValues($defaultFilterValues); |
||
2882 | |||
2883 | foreach ($this->getExtensions() as $extension) { |
||
2884 | // NEXT_MAJOR: remove method check |
||
2885 | if (method_exists($extension, 'configureDefaultFilterValues')) { |
||
2886 | $extension->configureDefaultFilterValues($this, $defaultFilterValues); |
||
2887 | } |
||
2888 | } |
||
2889 | |||
2890 | return $defaultFilterValues; |
||
2891 | } |
||
2892 | |||
2893 | protected function configureFormFields(FormMapper $form) |
||
2894 | { |
||
2895 | } |
||
2896 | |||
2897 | protected function configureListFields(ListMapper $list) |
||
2898 | { |
||
2899 | } |
||
2900 | |||
2901 | protected function configureDatagridFilters(DatagridMapper $filter) |
||
2902 | { |
||
2903 | } |
||
2904 | |||
2905 | protected function configureShowFields(ShowMapper $show) |
||
2906 | { |
||
2907 | } |
||
2908 | |||
2909 | protected function configureRoutes(RouteCollection $collection) |
||
2910 | { |
||
2911 | } |
||
2912 | |||
2913 | /** |
||
2914 | * Allows you to customize batch actions. |
||
2915 | * |
||
2916 | * @param array $actions List of actions |
||
2917 | * |
||
2918 | * @return array |
||
2919 | */ |
||
2920 | protected function configureBatchActions($actions) |
||
2921 | { |
||
2922 | return $actions; |
||
2923 | } |
||
2924 | |||
2925 | /** |
||
2926 | * NEXT_MAJOR: remove this method. |
||
2927 | * |
||
2928 | * @deprecated Use configureTabMenu instead |
||
2929 | */ |
||
2930 | protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null) |
||
2931 | { |
||
2932 | } |
||
2933 | |||
2934 | /** |
||
2935 | * Configures the tab menu in your admin. |
||
2936 | * |
||
2937 | * @param string $action |
||
2938 | */ |
||
2939 | protected function configureTabMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null) |
||
2945 | |||
2946 | /** |
||
2947 | * build the view FieldDescription array. |
||
2948 | */ |
||
2949 | protected function buildShow() |
||
2964 | |||
2965 | /** |
||
2966 | * build the list FieldDescription array. |
||
2967 | */ |
||
2968 | protected function buildList() |
||
3024 | |||
3025 | /** |
||
3026 | * Build the form FieldDescription collection. |
||
3027 | */ |
||
3028 | protected function buildForm() |
||
3029 | { |
||
3030 | if ($this->form) { |
||
3031 | return; |
||
3032 | } |
||
3033 | |||
3034 | // append parent object if any |
||
3035 | // todo : clean the way the Admin class can retrieve set the object |
||
3036 | if ($this->isChild() && $this->getParentAssociationMapping()) { |
||
3037 | $parent = $this->getParent()->getObject($this->request->get($this->getParent()->getIdParameter())); |
||
3038 | |||
3039 | $propertyAccessor = $this->getConfigurationPool()->getPropertyAccessor(); |
||
3040 | $propertyPath = new PropertyPath($this->getParentAssociationMapping()); |
||
3041 | |||
3042 | $object = $this->getSubject(); |
||
3043 | |||
3044 | $value = $propertyAccessor->getValue($object, $propertyPath); |
||
3045 | |||
3046 | if (\is_array($value) || $value instanceof \ArrayAccess) { |
||
3047 | $value[] = $parent; |
||
3048 | $propertyAccessor->setValue($object, $propertyPath, $value); |
||
3049 | } else { |
||
3050 | $propertyAccessor->setValue($object, $propertyPath, $parent); |
||
3051 | } |
||
3052 | } |
||
3053 | |||
3054 | $formBuilder = $this->getFormBuilder(); |
||
3055 | $formBuilder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) { |
||
3056 | $this->preValidate($event->getData()); |
||
3057 | }, 100); |
||
3058 | |||
3059 | $this->form = $formBuilder->getForm(); |
||
3060 | } |
||
3061 | |||
3062 | /** |
||
3063 | * Gets the subclass corresponding to the given name. |
||
3064 | * |
||
3065 | * @param string $name The name of the sub class |
||
3066 | * |
||
3067 | * @return string the subclass |
||
3068 | */ |
||
3069 | protected function getSubClass($name) |
||
3081 | |||
3082 | /** |
||
3083 | * Attach the inline validator to the model metadata, this must be done once per admin. |
||
3084 | */ |
||
3085 | protected function attachInlineValidator() |
||
3112 | |||
3113 | /** |
||
3114 | * Predefine per page options. |
||
3115 | */ |
||
3116 | protected function predefinePerPageOptions() |
||
3122 | |||
3123 | /** |
||
3124 | * Return list routes with permissions name. |
||
3125 | * |
||
3126 | * @return array<string, string> |
||
3127 | */ |
||
3128 | protected function getAccess() |
||
3153 | |||
3154 | /** |
||
3155 | * Configures a list of default filters. |
||
3156 | */ |
||
3157 | protected function configureDefaultFilterValues(array &$filterValues) |
||
3160 | |||
3161 | /** |
||
3162 | * Build all the related urls to the current admin. |
||
3163 | */ |
||
3164 | private function buildRoutes(): void |
||
3187 | } |
||
3188 | |||
3189 | class_exists(\Sonata\Form\Validator\ErrorElement::class); |
||
3190 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: