-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
AbstractAdmin.php
2596 lines (2132 loc) · 75.4 KB
/
AbstractAdmin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Admin;
use Knp\Menu\ItemInterface;
use Sonata\AdminBundle\BCLayer\BCHelper;
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\DependencyInjection\Admin\AbstractTaggedAdmin;
use Sonata\AdminBundle\Exception\AdminClassNotFoundException;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionCollection;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelHiddenType;
use Sonata\AdminBundle\Manipulator\ObjectManipulator;
use Sonata\AdminBundle\Model\ProxyResolverInterface;
use Sonata\AdminBundle\Object\Metadata;
use Sonata\AdminBundle\Object\MetadataInterface;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap;
use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Util\Instantiator;
use Sonata\AdminBundle\Util\ParametersManipulator;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface as RoutingUrlGeneratorInterface;
use Symfony\Component\Security\Acl\Model\DomainObjectInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
/**
* @author Thomas Rabaix <[email protected]>
*
* @phpstan-template T of object
* @phpstan-extends AbstractTaggedAdmin<T>
* @phpstan-implements AdminInterface<T>
*/
abstract class AbstractAdmin extends AbstractTaggedAdmin implements AdminInterface, DomainObjectInterface, AdminTreeInterface
{
// NEXT_MAJOR: Remove the CONTEXT constants.
/** @deprecated */
public const CONTEXT_MENU = 'menu';
/** @deprecated */
public const CONTEXT_DASHBOARD = 'dashboard';
public const CLASS_REGEX =
'@
(?:([A-Za-z0-9]*)\\\)? # vendor name / app name
(Bundle\\\)? # optional bundle directory
([A-Za-z0-9]+?)(?:Bundle)?\\\ # bundle name, with optional suffix
(
Entity|Document|Model|PHPCR|CouchDocument|Phpcr|
Doctrine\\\Orm|Doctrine\\\Phpcr|Doctrine\\\MongoDB|Doctrine\\\CouchDB
)\\\(.*)@x';
private const ACTION_TREE = 1;
private const ACTION_SHOW = 2;
private const ACTION_EDIT = 4;
private const ACTION_DELETE = 8;
private const ACTION_ACL = 16;
private const ACTION_HISTORY = 32;
private const ACTION_LIST = 64;
private const ACTION_BATCH = 128;
private const INTERNAL_ACTIONS = [
'tree' => self::ACTION_TREE,
'show' => self::ACTION_SHOW,
'edit' => self::ACTION_EDIT,
'delete' => self::ACTION_DELETE,
'acl' => self::ACTION_ACL,
'history' => self::ACTION_HISTORY,
'list' => self::ACTION_LIST,
'batch' => self::ACTION_BATCH,
];
private const MASK_OF_ACTION_CREATE = self::ACTION_TREE | self::ACTION_SHOW | self::ACTION_EDIT | self::ACTION_DELETE | self::ACTION_LIST | self::ACTION_BATCH;
private const MASK_OF_ACTION_SHOW = self::ACTION_EDIT | self::ACTION_HISTORY | self::ACTION_ACL;
private const MASK_OF_ACTION_EDIT = self::ACTION_SHOW | self::ACTION_DELETE | self::ACTION_ACL | self::ACTION_HISTORY;
private const MASK_OF_ACTION_HISTORY = self::ACTION_SHOW | self::ACTION_EDIT | self::ACTION_ACL;
private const MASK_OF_ACTION_ACL = self::ACTION_EDIT | self::ACTION_HISTORY;
private const MASK_OF_ACTION_LIST = self::ACTION_SHOW | self::ACTION_EDIT | self::ACTION_DELETE | self::ACTION_ACL | self::ACTION_BATCH;
private const MASK_OF_ACTIONS_USING_OBJECT = self::MASK_OF_ACTION_SHOW | self::MASK_OF_ACTION_EDIT | self::MASK_OF_ACTION_HISTORY | self::MASK_OF_ACTION_ACL;
private const DEFAULT_LIST_PER_PAGE_RESULTS = 25;
private const DEFAULT_LIST_PER_PAGE_OPTIONS = [10, 25, 50, 100, 250];
/**
* @deprecated since sonata-project/admin-bundle 4.15, will be removed in 5.0.
*
* The base route name used to generate the routing information.
*
* @var string|null
*/
protected $baseRouteName;
/**
* @deprecated since sonata-project/admin-bundle 4.15, will be removed in 5.0.
*
* The base route pattern used to generate the routing information.
*
* @var string|null
*/
protected $baseRoutePattern;
/**
* The label class name (used in the title/breadcrumb ...).
*
* @var string|null
*/
protected $classnameLabel;
/**
* Setting to true will enable preview mode for
* the entity and show a preview button in the
* edit/create forms.
*
* @var bool
*/
protected $supportsPreviewMode = false;
/**
* The list FieldDescription constructed from the configureListField method.
*
* @var array<string, FieldDescriptionInterface>
*/
private array $listFieldDescriptions = [];
/**
* The show FieldDescription constructed from the configureShowFields method.
*
* @var FieldDescriptionInterface[]
*/
private array $showFieldDescriptions = [];
/**
* The list FieldDescription constructed from the configureFormField method.
*
* @var FieldDescriptionInterface[]
*/
private array $formFieldDescriptions = [];
/**
* The filter FieldDescription constructed from the configureFilterField method.
*
* @var FieldDescriptionInterface[]
*/
private array $filterFieldDescriptions = [];
/**
* The maximum number of page numbers to display in the list.
*/
private int $maxPageLinks = 25;
/**
* The translation domain to be used to translate messages.
*/
private string $translationDomain = 'messages';
/**
* Array of routes related to this admin.
*/
private ?RouteCollectionInterface $routes = null;
/**
* The subject only set in edit/update/create mode.
*
* @phpstan-var T|null
*/
private ?object $subject = null;
/**
* Define a Collection of child admin, ie /admin/order/{id}/order-element/{childId}.
*
* @var array<string, AdminInterface<object>>
*/
private array $children = [];
/**
* Reference the parent admin.
*
* @var AdminInterface<object>|null
*/
private ?AdminInterface $parent = null;
/**
* Reference the parent FieldDescription related to this admin
* only set for FieldDescription which is associated to an Sub Admin instance.
*/
private ?FieldDescriptionInterface $parentFieldDescription = null;
/**
* If true then the current admin is part of the nested admin set (from the url).
*/
private bool $currentChild = false;
/**
* The uniqId is used to avoid clashing with 2 admin related to the code
* ie: a Block linked to a Block.
*/
private ?string $uniqId = null;
/**
* The current request object.
*/
private ?Request $request = null;
/**
* @phpstan-var DatagridInterface<ProxyQueryInterface<T>>|null
*/
private ?DatagridInterface $datagrid = null;
private ?ItemInterface $menu = null;
/**
* @var string[]
*/
private array $formTheme = [];
/**
* @var string[]
*/
private array $filterTheme = [];
/**
* @var AdminExtensionInterface[]
*
* @phpstan-var array<AdminExtensionInterface<T>>
*/
private array $extensions = [];
/**
* @var array<string, bool>
*/
private array $cacheIsGranted = [];
/**
* @var array<string, string|null>
*/
private array $parentAssociationMapping = [];
/**
* The subclasses supported by the admin class.
*
* @var string[]
*
* @phpstan-var array<string, class-string<T>>
*/
private array $subClasses = [];
/**
* The list collection.
*
* @var FieldDescriptionCollection<FieldDescriptionInterface>|null
*/
private ?FieldDescriptionCollection $list = null;
/**
* @var FieldDescriptionCollection<FieldDescriptionInterface>|null
*/
private ?FieldDescriptionCollection $show = null;
private ?FormInterface $form = null;
/**
* The cached base route name.
*/
private ?string $cachedBaseRouteName = null;
/**
* The cached base route pattern.
*/
private ?string $cachedBaseRoutePattern = null;
/**
* The form group disposition.
*
* @var array<string, array<string, mixed>>
*/
private array $formGroups = [];
/**
* The form tabs disposition.
*
* @var array<string, array<string, mixed>>
*/
private array $formTabs = [];
/**
* The view group disposition.
*
* @var array<string, array<string, mixed>>
*/
private array $showGroups = [];
/**
* The view tab disposition.
*
* @var array<string, array<string, mixed>>
*/
private array $showTabs = [];
/**
* @var array<string, bool>
*/
private array $loaded = [
'routes' => false,
'tab_menu' => false,
'show' => false,
'list' => false,
'form' => false,
'datagrid' => false,
];
public function getExportFormats(): array
{
return [];
}
final public function getExportFields(): array
{
$fields = $this->configureExportFields();
foreach ($this->getExtensions() as $extension) {
$fields = $extension->configureExportFields($this, $fields);
}
return $fields;
}
final public function getDataSourceIterator(): \Iterator
{
$datagrid = $this->getDatagrid();
$datagrid->buildPager();
$fields = [];
foreach ($this->getExportFields() as $key => $field) {
if (!\is_string($key)) {
$label = $this->getTranslationLabel($field, 'export', 'label');
$key = $this->getTranslator()->trans($label, [], $this->getTranslationDomain());
}
$fields[$key] = $field;
}
$query = $datagrid->getQuery();
return $this->getDataSource()->createIterator($query, $fields);
}
final public function initialize(): void
{
if (null === $this->classnameLabel) {
$namespaceSeparatorPos = strrpos($this->getClass(), '\\');
$this->classnameLabel = false !== $namespaceSeparatorPos
? substr($this->getClass(), $namespaceSeparatorPos + 1)
: $this->getClass();
}
$this->configure();
foreach ($this->getExtensions() as $extension) {
$extension->configure($this);
}
}
final public function update(object $object): object
{
$this->preUpdate($object);
foreach ($this->getExtensions() as $extension) {
$extension->preUpdate($this, $object);
}
$this->getModelManager()->update($object);
$this->postUpdate($object);
foreach ($this->getExtensions() as $extension) {
$extension->postUpdate($this, $object);
}
return $object;
}
final public function create(object $object): object
{
$this->prePersist($object);
foreach ($this->getExtensions() as $extension) {
$extension->prePersist($this, $object);
}
$this->getModelManager()->create($object);
$this->postPersist($object);
foreach ($this->getExtensions() as $extension) {
$extension->postPersist($this, $object);
}
$this->createObjectSecurity($object);
return $object;
}
final public function delete(object $object): void
{
$this->preRemove($object);
foreach ($this->getExtensions() as $extension) {
$extension->preRemove($this, $object);
}
$this->getSecurityHandler()->deleteObjectSecurity($this, $object);
$this->getModelManager()->delete($object);
$this->postRemove($object);
foreach ($this->getExtensions() as $extension) {
$extension->postRemove($this, $object);
}
}
public function preBatchAction(string $actionName, ProxyQueryInterface $query, array &$idx, bool $allElements = false): void
{
}
final public function getDefaultFilterParameters(): array
{
return array_merge(
$this->getDefaultSortValues(),
$this->getDefaultFilterValues()
);
}
final public function getFilterParameters(): array
{
$parameters = $this->getDefaultFilterParameters();
// build the values array
if ($this->hasRequest()) {
$bag = $this->getRequest()->query;
$filters = $bag->all('filter');
if (isset($filters[DatagridInterface::PAGE])) {
$filters[DatagridInterface::PAGE] = (int) $filters[DatagridInterface::PAGE];
}
if (isset($filters[DatagridInterface::PER_PAGE])) {
$filters[DatagridInterface::PER_PAGE] = (int) $filters[DatagridInterface::PER_PAGE];
}
// if filter persistence is configured
if ($this->hasFilterPersister()) {
// if reset filters is asked, remove from storage
if ('reset' === $this->getRequest()->query->get('filters')) {
$this->getFilterPersister()->reset($this->getCode());
}
// if no filters, fetch from storage
// otherwise save to storage
if ([] === $filters) {
$filters = $this->getFilterPersister()->get($this->getCode());
} else {
$this->getFilterPersister()->set($this->getCode(), $filters);
}
}
$parameters = ParametersManipulator::merge($parameters, $filters);
// always force the parent value
if ($this->isChild()) {
$parentAssociationMapping = $this->getParentAssociationMapping();
if (null !== $parentAssociationMapping) {
$name = str_replace('.', '__', $parentAssociationMapping);
$parameters[$name] = ['value' => $this->getRequest()->get($this->getParent()->getIdParameter())];
}
}
}
if (
!isset($parameters[DatagridInterface::PER_PAGE])
|| !\is_int($parameters[DatagridInterface::PER_PAGE])
|| !$this->determinedPerPageValue($parameters[DatagridInterface::PER_PAGE])
) {
$parameters[DatagridInterface::PER_PAGE] = $this->getMaxPerPage();
}
$parameters = $this->configureFilterParameters($parameters);
foreach ($this->getExtensions() as $extension) {
$parameters = $extension->configureFilterParameters($this, $parameters);
}
return $parameters;
}
/**
* Returns the name of the parent related field, so the field can be use to set the default
* value (ie the parent object) or to filter the object.
*
* @throws \LogicException
*/
final public function getParentAssociationMapping(): ?string
{
if (!$this->isChild()) {
throw new \LogicException(\sprintf(
'Admin "%s" has no parent.',
static::class
));
}
$parent = $this->getParent()->getCode();
return $this->parentAssociationMapping[$parent];
}
final public function getBaseRoutePattern(): string
{
if (null !== $this->cachedBaseRoutePattern) {
return $this->cachedBaseRoutePattern;
}
if ($this->isChild()) { // the admin class is a child, prefix it with the parent route pattern
$this->cachedBaseRoutePattern = \sprintf(
'%s/%s/%s',
$this->getParent()->getBaseRoutePattern(),
$this->getParent()->getRouterIdParameter(),
$this->generateBaseRoutePattern(true)
);
} else {
$this->cachedBaseRoutePattern = $this->generateBaseRoutePattern();
}
return $this->cachedBaseRoutePattern;
}
/**
* Returns the baseRouteName used to generate the routing information.
*
* @return string the baseRouteName used to generate the routing information
*/
final public function getBaseRouteName(): string
{
if (null !== $this->cachedBaseRouteName) {
return $this->cachedBaseRouteName;
}
if ($this->isChild()) { // the admin class is a child, prefix it with the parent route name
$this->cachedBaseRouteName = \sprintf(
'%s_%s',
$this->getParent()->getBaseRouteName(),
$this->generateBaseRouteName(true)
);
} else {
$this->cachedBaseRouteName = $this->generateBaseRouteName();
}
return $this->cachedBaseRouteName;
}
final public function getClass(): string
{
if ($this->hasActiveSubClass()) {
if ($this->hasParentFieldDescription()) {
throw new \LogicException('Feature not implemented: an embedded admin cannot have subclass');
}
$subClass = $this->getRequest()->query->get('subclass');
\assert(\is_string($subClass));
if (!$this->hasSubClass($subClass)) {
throw new \LogicException(\sprintf('Subclass "%s" is not defined.', $subClass));
}
return $this->getSubClass($subClass);
}
// Do not use `$this->hasSubject()` and `$this->getSubject()` here to avoid infinite loop.
// `getSubject` use `hasSubject()` which use `getObject()` which use `getClass()`.
if (null !== $this->subject) {
$modelManager = $this->getModelManager();
/** @phpstan-var class-string<T> $class */
$class = $modelManager instanceof ProxyResolverInterface
? $modelManager->getRealClass($this->subject)
// NEXT_MAJOR: Change to `\get_class($this->subject)` instead
: BCHelper::getClass($this->subject);
return $class;
}
return $this->getModelClass();
}
final public function getSubClasses(): array
{
return $this->subClasses;
}
final public function setSubClasses(array $subClasses): void
{
$this->subClasses = $subClasses;
}
final public function hasSubClass(string $name): bool
{
return isset($this->subClasses[$name]);
}
final public function hasActiveSubClass(): bool
{
if (\count($this->subClasses) > 0 && $this->hasRequest()) {
return \is_string($this->getRequest()->query->get('subclass'));
}
return false;
}
final public function getActiveSubClass(): string
{
if (!$this->hasActiveSubClass()) {
throw new \LogicException(\sprintf(
'Admin "%s" has no active subclass.',
static::class
));
}
return $this->getSubClass($this->getActiveSubclassCode());
}
final public function getActiveSubclassCode(): string
{
if (!$this->hasActiveSubClass()) {
throw new \LogicException(\sprintf(
'Admin "%s" has no active subclass.',
static::class
));
}
$subClass = (string) $this->getRequest()->query->get('subclass');
if (!$this->hasSubClass($subClass)) {
throw new \LogicException(\sprintf(
'Admin "%s" has no active subclass.',
static::class
));
}
return $subClass;
}
final public function getBatchActions(): array
{
if (!$this->hasRoute('batch')) {
return [];
}
$actions = [];
if ($this->hasRoute('delete') && $this->hasAccess('delete')) {
$actions['delete'] = [
'label' => 'action_delete',
'translation_domain' => 'SonataAdminBundle',
'ask_confirmation' => true, // by default always true
];
}
$actions = $this->configureBatchActions($actions);
foreach ($this->getExtensions() as $extension) {
$actions = $extension->configureBatchActions($this, $actions);
}
foreach ($actions as $name => &$action) {
if (!\array_key_exists('label', $action)) {
$action['label'] = $this->getTranslationLabel($name, 'batch', 'label');
}
if (!\array_key_exists('translation_domain', $action)) {
$action['translation_domain'] = $this->getTranslationDomain();
}
}
return $actions;
}
final public function getRoutes(): RouteCollectionInterface
{
$routes = $this->buildRoutes();
if (null === $routes) {
throw new \LogicException('Cannot access routes during the building process.');
}
return $routes;
}
public function getRouterIdParameter(): string
{
return \sprintf('{%s}', $this->getIdParameter());
}
public function getIdParameter(): string
{
$parameter = 'id';
for ($i = 0; $i < $this->getChildDepth(); ++$i) {
$parameter = \sprintf('child%s', ucfirst($parameter));
}
return $parameter;
}
final public function hasRoute(string $name): bool
{
return $this->getRouteGenerator()->hasAdminRoute($this, $name);
}
final public function isCurrentRoute(string $name, ?string $adminCode = null): bool
{
if (!$this->hasRequest()) {
return false;
}
$request = $this->getRequest();
$route = $request->get('_route');
if (null !== $adminCode) {
$pool = $this->getConfigurationPool();
if ($pool->hasAdminByAdminCode($adminCode)) {
$admin = $pool->getAdminByAdminCode($adminCode);
} else {
return false;
}
} else {
$admin = $this;
}
return $admin->getRoutes()->getRouteName($name) === $route;
}
final public function generateObjectUrl(string $name, object $object, array $parameters = [], int $referenceType = RoutingUrlGeneratorInterface::ABSOLUTE_PATH): string
{
$parameters[$this->getIdParameter()] = $this->getUrlSafeIdentifier($object);
return $this->generateUrl($name, $parameters, $referenceType);
}
final public function generateUrl(string $name, array $parameters = [], int $referenceType = RoutingUrlGeneratorInterface::ABSOLUTE_PATH): string
{
return $this->getRouteGenerator()->generateUrl($this, $name, $parameters, $referenceType);
}
final public function generateMenuUrl(string $name, array $parameters = [], int $referenceType = RoutingUrlGeneratorInterface::ABSOLUTE_PATH): array
{
return $this->getRouteGenerator()->generateMenuUrl($this, $name, $parameters, $referenceType);
}
final public function getNewInstance(): object
{
$object = $this->createNewInstance();
$this->alterNewInstance($object);
foreach ($this->getExtensions() as $extension) {
$extension->alterNewInstance($this, $object);
}
return $object;
}
final public function getFormBuilder(): FormBuilderInterface
{
$formBuilder = $this->getFormContractor()->getFormBuilder(
$this->getUniqId(),
['data_class' => $this->getClass()] + $this->getFormOptions(),
);
$this->defineFormBuilder($formBuilder);
return $formBuilder;
}
/**
* This method is being called by the main admin class and the child class,
* the getFormBuilder is only call by the main admin class.
*/
final public function defineFormBuilder(FormBuilderInterface $formBuilder): void
{
if (!$this->hasSubject()) {
throw new \LogicException(\sprintf(
'Admin "%s" has no subject.',
static::class
));
}
$mapper = new FormMapper($this->getFormContractor(), $formBuilder, $this);
$this->configureFormFields($mapper);
foreach ($this->getExtensions() as $extension) {
$extension->configureFormFields($mapper);
}
}
final public function attachAdminClass(FieldDescriptionInterface $fieldDescription): void
{
$pool = $this->getConfigurationPool();
try {
$admin = $pool->getAdminByFieldDescription($fieldDescription);
} catch (AdminClassNotFoundException) {
// Using a fieldDescription with no admin class for the target model is a valid case.
// Since there is no easy way to check for this case, we catch the exception instead.
return;
}
if ($this->hasRequest()) {
$admin->setRequest($this->getRequest());
}
$fieldDescription->setAssociationAdmin($admin);
}
/**
* @param string|int|null $id
*
* @phpstan-return T|null
*/
final public function getObject($id): ?object
{
if (null === $id) {
return null;
}
$object = $this->getModelManager()->find($this->getClass(), $id);
if (null === $object) {
return null;
}
$this->alterObject($object);
foreach ($this->getExtensions() as $extension) {
$extension->alterObject($this, $object);
}
return $object;
}
final public function getForm(): FormInterface
{
$form = $this->buildForm();
if (null === $form) {
throw new \LogicException('Cannot access form during the building process.');
}
return $form;
}
final public function getList(): FieldDescriptionCollection
{
$list = $this->buildList();
if (null === $list) {
throw new \LogicException('Cannot access list during the building process.');
}
return $list;
}
final public function createQuery(): ProxyQueryInterface
{
$query = $this->getModelManager()->createQuery($this->getClass());
$query = $this->configureQuery($query);
foreach ($this->getExtensions() as $extension) {
$extension->configureQuery($this, $query);
}
return $query;
}
final public function getDatagrid(): DatagridInterface
{
$datagrid = $this->buildDatagrid();
if (null === $datagrid) {
throw new \LogicException('Cannot access datagrid during the building process.');
}
return $datagrid;
}
final public function getSideMenu(string $action, ?AdminInterface $childAdmin = null): ItemInterface
{
if ($this->isChild()) {
return $this->getParent()->getSideMenu($action, $this);
}
$menu = $this->buildTabMenu($action, $childAdmin);
if (null === $menu) {
throw new \LogicException('Cannot access menu during the building process.');
}
return $menu;
}
final public function getRootCode(): string
{
return $this->getRoot()->getCode();
}
final public function getRoot(): AdminInterface
{
if (!$this->hasParentFieldDescription()) {
return $this;
}
return $this->getParentFieldDescription()->getAdmin()->getRoot();
}
final public function getMaxPerPage(): int
{
$sortValues = $this->getDefaultSortValues();
return $sortValues[DatagridInterface::PER_PAGE] ?? self::DEFAULT_LIST_PER_PAGE_RESULTS;
}
final public function setMaxPageLinks(int $maxPageLinks): void
{
$this->maxPageLinks = $maxPageLinks;
}
final public function getMaxPageLinks(): int
{
return $this->maxPageLinks;
}
final public function getFormGroups(): array
{
return $this->formGroups;
}
final public function setFormGroups(array $formGroups): void
{
$this->formGroups = $formGroups;
}
final public function removeFieldFromFormGroup(string $key): void
{
foreach ($this->formGroups as $name => $_formGroup) {
unset($this->formGroups[$name]['fields'][$key]);
if ([] === $this->formGroups[$name]['fields']) {
unset($this->formGroups[$name]);
}
}
}
final public function reorderFormGroup(string $group, array $keys): void
{
$formGroups = $this->getFormGroups();
$formGroups[$group]['fields'] = array_merge(array_flip($keys), $formGroups[$group]['fields']);
$this->setFormGroups($formGroups);
}
final public function getFormTabs(): array
{
return $this->formTabs;
}
final public function setFormTabs(array $formTabs): void
{
$this->formTabs = $formTabs;
}
final public function getShowTabs(): array
{
return $this->showTabs;
}
final public function setShowTabs(array $showTabs): void
{
$this->showTabs = $showTabs;
}
final public function getShowGroups(): array
{
return $this->showGroups;
}