vendor/sonata-project/admin-bundle/src/Admin/AbstractAdmin.php line 2372

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <[email protected]>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\AdminBundle\Admin;
  12. use Doctrine\Common\Util\ClassUtils;
  13. use Knp\Menu\FactoryInterface as MenuFactoryInterface;
  14. use Knp\Menu\ItemInterface;
  15. use Knp\Menu\ItemInterface as MenuItemInterface;
  16. use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
  17. use Sonata\AdminBundle\Builder\FormContractorInterface;
  18. use Sonata\AdminBundle\Builder\ListBuilderInterface;
  19. use Sonata\AdminBundle\Builder\RouteBuilderInterface;
  20. use Sonata\AdminBundle\Builder\ShowBuilderInterface;
  21. use Sonata\AdminBundle\Datagrid\DatagridInterface;
  22. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  23. use Sonata\AdminBundle\Datagrid\ListMapper;
  24. use Sonata\AdminBundle\Datagrid\Pager;
  25. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  26. use Sonata\AdminBundle\Filter\Persister\FilterPersisterInterface;
  27. use Sonata\AdminBundle\Form\FormMapper;
  28. use Sonata\AdminBundle\Form\Type\ModelHiddenType;
  29. use Sonata\AdminBundle\Model\ModelManagerInterface;
  30. use Sonata\AdminBundle\Object\Metadata;
  31. use Sonata\AdminBundle\Route\RouteCollection;
  32. use Sonata\AdminBundle\Route\RouteGeneratorInterface;
  33. use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
  34. use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
  35. use Sonata\AdminBundle\Show\ShowMapper;
  36. use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface;
  37. use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
  38. use Sonata\Form\Validator\Constraints\InlineConstraint;
  39. use Sonata\Form\Validator\ErrorElement;
  40. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  41. use Symfony\Component\Form\Form;
  42. use Symfony\Component\Form\FormBuilderInterface;
  43. use Symfony\Component\Form\FormEvent;
  44. use Symfony\Component\Form\FormEvents;
  45. use Symfony\Component\HttpFoundation\Request;
  46. use Symfony\Component\PropertyAccess\PropertyPath;
  47. use Symfony\Component\Routing\Generator\UrlGeneratorInterface as RoutingUrlGeneratorInterface;
  48. use Symfony\Component\Security\Acl\Model\DomainObjectInterface;
  49. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  50. use Symfony\Component\Translation\TranslatorInterface;
  51. use Symfony\Component\Validator\Validator\ValidatorInterface;
  52. /**
  53.  * @author Thomas Rabaix <[email protected]>
  54.  */
  55. abstract class AbstractAdmin implements AdminInterfaceDomainObjectInterfaceAdminTreeInterface
  56. {
  57.     public const CONTEXT_MENU 'menu';
  58.     public const CONTEXT_DASHBOARD 'dashboard';
  59.     public const CLASS_REGEX =
  60.         '@
  61.         (?:([A-Za-z0-9]*)\\\)?        # vendor name / app name
  62.         (Bundle\\\)?                  # optional bundle directory
  63.         ([A-Za-z0-9]+?)(?:Bundle)?\\\ # bundle name, with optional suffix
  64.         (
  65.             Entity|Document|Model|PHPCR|CouchDocument|Phpcr|
  66.             Doctrine\\\Orm|Doctrine\\\Phpcr|Doctrine\\\MongoDB|Doctrine\\\CouchDB
  67.         )\\\(.*)@x';
  68.     public const MOSAIC_ICON_CLASS 'fa fa-th-large fa-fw';
  69.     /**
  70.      * The list FieldDescription constructed from the configureListField method.
  71.      *
  72.      * @var array
  73.      */
  74.     protected $listFieldDescriptions = [];
  75.     /**
  76.      * The show FieldDescription constructed from the configureShowFields method.
  77.      *
  78.      * @var array
  79.      */
  80.     protected $showFieldDescriptions = [];
  81.     /**
  82.      * The list FieldDescription constructed from the configureFormField method.
  83.      *
  84.      * @var array
  85.      */
  86.     protected $formFieldDescriptions = [];
  87.     /**
  88.      * The filter FieldDescription constructed from the configureFilterField method.
  89.      *
  90.      * @var array
  91.      */
  92.     protected $filterFieldDescriptions = [];
  93.     /**
  94.      * The number of result to display in the list.
  95.      *
  96.      * @var int
  97.      */
  98.     protected $maxPerPage 32;
  99.     /**
  100.      * The maximum number of page numbers to display in the list.
  101.      *
  102.      * @var int
  103.      */
  104.     protected $maxPageLinks 25;
  105.     /**
  106.      * The base route name used to generate the routing information.
  107.      *
  108.      * @var string
  109.      */
  110.     protected $baseRouteName;
  111.     /**
  112.      * The base route pattern used to generate the routing information.
  113.      *
  114.      * @var string
  115.      */
  116.     protected $baseRoutePattern;
  117.     /**
  118.      * The base name controller used to generate the routing information.
  119.      *
  120.      * @var string
  121.      */
  122.     protected $baseControllerName;
  123.     /**
  124.      * The label class name  (used in the title/breadcrumb ...).
  125.      *
  126.      * @var string
  127.      */
  128.     protected $classnameLabel;
  129.     /**
  130.      * The translation domain to be used to translate messages.
  131.      *
  132.      * @var string
  133.      */
  134.     protected $translationDomain 'messages';
  135.     /**
  136.      * Options to set to the form (ie, validation_groups).
  137.      *
  138.      * @var array
  139.      */
  140.     protected $formOptions = [];
  141.     /**
  142.      * Default values to the datagrid.
  143.      *
  144.      * @var array
  145.      */
  146.     protected $datagridValues = [
  147.         '_page' => 1,
  148.         '_per_page' => 32,
  149.     ];
  150.     /**
  151.      * Predefined per page options.
  152.      *
  153.      * @var array
  154.      */
  155.     protected $perPageOptions = [163264128256];
  156.     /**
  157.      * Pager type.
  158.      *
  159.      * @var string
  160.      */
  161.     protected $pagerType Pager::TYPE_DEFAULT;
  162.     /**
  163.      * The code related to the admin.
  164.      *
  165.      * @var string
  166.      */
  167.     protected $code;
  168.     /**
  169.      * The label.
  170.      *
  171.      * @var string
  172.      */
  173.     protected $label;
  174.     /**
  175.      * Whether or not to persist the filters in the session.
  176.      *
  177.      * NEXT_MAJOR: remove this property
  178.      *
  179.      * @var bool
  180.      *
  181.      * @deprecated since 3.34, to be removed in 4.0.
  182.      */
  183.     protected $persistFilters false;
  184.     /**
  185.      * Array of routes related to this admin.
  186.      *
  187.      * @var RouteCollection
  188.      */
  189.     protected $routes;
  190.     /**
  191.      * The subject only set in edit/update/create mode.
  192.      *
  193.      * @var object|null
  194.      */
  195.     protected $subject;
  196.     /**
  197.      * Define a Collection of child admin, ie /admin/order/{id}/order-element/{childId}.
  198.      *
  199.      * @var array
  200.      */
  201.     protected $children = [];
  202.     /**
  203.      * Reference the parent collection.
  204.      *
  205.      * @var AdminInterface|null
  206.      */
  207.     protected $parent null;
  208.     /**
  209.      * The base code route refer to the prefix used to generate the route name.
  210.      *
  211.      * NEXT_MAJOR: remove this attribute.
  212.      *
  213.      * @deprecated This attribute is deprecated since 3.24 and will be removed in 4.0
  214.      *
  215.      * @var string
  216.      */
  217.     protected $baseCodeRoute '';
  218.     /**
  219.      * NEXT_MAJOR: should be default array and private.
  220.      *
  221.      * @var string|array
  222.      */
  223.     protected $parentAssociationMapping null;
  224.     /**
  225.      * Reference the parent FieldDescription related to this admin
  226.      * only set for FieldDescription which is associated to an Sub Admin instance.
  227.      *
  228.      * @var FieldDescriptionInterface
  229.      */
  230.     protected $parentFieldDescription;
  231.     /**
  232.      * If true then the current admin is part of the nested admin set (from the url).
  233.      *
  234.      * @var bool
  235.      */
  236.     protected $currentChild false;
  237.     /**
  238.      * The uniqid is used to avoid clashing with 2 admin related to the code
  239.      * ie: a Block linked to a Block.
  240.      *
  241.      * @var string
  242.      */
  243.     protected $uniqid;
  244.     /**
  245.      * The Entity or Document manager.
  246.      *
  247.      * @var ModelManagerInterface
  248.      */
  249.     protected $modelManager;
  250.     /**
  251.      * The current request object.
  252.      *
  253.      * @var Request|null
  254.      */
  255.     protected $request;
  256.     /**
  257.      * The translator component.
  258.      *
  259.      * NEXT_MAJOR: remove this property
  260.      *
  261.      * @var \Symfony\Component\Translation\TranslatorInterface
  262.      *
  263.      * @deprecated since 3.9, to be removed with 4.0
  264.      */
  265.     protected $translator;
  266.     /**
  267.      * The related form contractor.
  268.      *
  269.      * @var FormContractorInterface
  270.      */
  271.     protected $formContractor;
  272.     /**
  273.      * The related list builder.
  274.      *
  275.      * @var ListBuilderInterface
  276.      */
  277.     protected $listBuilder;
  278.     /**
  279.      * The related view builder.
  280.      *
  281.      * @var ShowBuilderInterface
  282.      */
  283.     protected $showBuilder;
  284.     /**
  285.      * The related datagrid builder.
  286.      *
  287.      * @var DatagridBuilderInterface
  288.      */
  289.     protected $datagridBuilder;
  290.     /**
  291.      * @var RouteBuilderInterface
  292.      */
  293.     protected $routeBuilder;
  294.     /**
  295.      * The datagrid instance.
  296.      *
  297.      * @var DatagridInterface|null
  298.      */
  299.     protected $datagrid;
  300.     /**
  301.      * The router instance.
  302.      *
  303.      * @var RouteGeneratorInterface|null
  304.      */
  305.     protected $routeGenerator;
  306.     /**
  307.      * The generated breadcrumbs.
  308.      *
  309.      * NEXT_MAJOR : remove this property
  310.      *
  311.      * @var array
  312.      */
  313.     protected $breadcrumbs = [];
  314.     /**
  315.      * @var SecurityHandlerInterface
  316.      */
  317.     protected $securityHandler null;
  318.     /**
  319.      * @var ValidatorInterface
  320.      */
  321.     protected $validator null;
  322.     /**
  323.      * The configuration pool.
  324.      *
  325.      * @var Pool
  326.      */
  327.     protected $configurationPool;
  328.     /**
  329.      * @var MenuItemInterface
  330.      */
  331.     protected $menu;
  332.     /**
  333.      * @var MenuFactoryInterface
  334.      */
  335.     protected $menuFactory;
  336.     /**
  337.      * @var array<string, bool>
  338.      */
  339.     protected $loaded = [
  340.         'view_fields' => false,
  341.         'view_groups' => false,
  342.         'routes' => false,
  343.         'tab_menu' => false,
  344.     ];
  345.     /**
  346.      * @var string[]
  347.      */
  348.     protected $formTheme = [];
  349.     /**
  350.      * @var string[]
  351.      */
  352.     protected $filterTheme = [];
  353.     /**
  354.      * @var array<string, string>
  355.      *
  356.      * @deprecated since 3.34, will be dropped in 4.0. Use TemplateRegistry services instead
  357.      */
  358.     protected $templates = [];
  359.     /**
  360.      * @var AdminExtensionInterface[]
  361.      */
  362.     protected $extensions = [];
  363.     /**
  364.      * @var LabelTranslatorStrategyInterface
  365.      */
  366.     protected $labelTranslatorStrategy;
  367.     /**
  368.      * Setting to true will enable preview mode for
  369.      * the entity and show a preview button in the
  370.      * edit/create forms.
  371.      *
  372.      * @var bool
  373.      */
  374.     protected $supportsPreviewMode false;
  375.     /**
  376.      * Roles and permissions per role.
  377.      *
  378.      * @var array 'role' => ['permission', 'permission']
  379.      */
  380.     protected $securityInformation = [];
  381.     protected $cacheIsGranted = [];
  382.     /**
  383.      * Action list for the search result.
  384.      *
  385.      * @var string[]
  386.      */
  387.     protected $searchResultActions = ['edit''show'];
  388.     protected $listModes = [
  389.         'list' => [
  390.             'class' => 'fa fa-list fa-fw',
  391.         ],
  392.         'mosaic' => [
  393.             'class' => self::MOSAIC_ICON_CLASS,
  394.         ],
  395.     ];
  396.     /**
  397.      * The Access mapping.
  398.      *
  399.      * @var array [action1 => requiredRole1, action2 => [requiredRole2, requiredRole3]]
  400.      */
  401.     protected $accessMapping = [];
  402.     /**
  403.      * @var MutableTemplateRegistryInterface
  404.      */
  405.     private $templateRegistry;
  406.     /**
  407.      * The class name managed by the admin class.
  408.      *
  409.      * @var string
  410.      */
  411.     private $class;
  412.     /**
  413.      * The subclasses supported by the admin class.
  414.      *
  415.      * @var array<string, string>
  416.      */
  417.     private $subClasses = [];
  418.     /**
  419.      * The list collection.
  420.      *
  421.      * @var FieldDescriptionCollection
  422.      */
  423.     private $list;
  424.     /**
  425.      * @var FieldDescriptionCollection|null
  426.      */
  427.     private $show;
  428.     /**
  429.      * @var Form|null
  430.      */
  431.     private $form;
  432.     /**
  433.      * @var DatagridInterface
  434.      */
  435.     private $filter;
  436.     /**
  437.      * The cached base route name.
  438.      *
  439.      * @var string
  440.      */
  441.     private $cachedBaseRouteName;
  442.     /**
  443.      * The cached base route pattern.
  444.      *
  445.      * @var string
  446.      */
  447.     private $cachedBaseRoutePattern;
  448.     /**
  449.      * The form group disposition.
  450.      *
  451.      * @var array|bool
  452.      */
  453.     private $formGroups false;
  454.     /**
  455.      * The form tabs disposition.
  456.      *
  457.      * @var array|bool
  458.      */
  459.     private $formTabs false;
  460.     /**
  461.      * The view group disposition.
  462.      *
  463.      * @var array|bool
  464.      */
  465.     private $showGroups false;
  466.     /**
  467.      * The view tab disposition.
  468.      *
  469.      * @var array|bool
  470.      */
  471.     private $showTabs false;
  472.     /**
  473.      * The manager type to use for the admin.
  474.      *
  475.      * @var string
  476.      */
  477.     private $managerType;
  478.     /**
  479.      * The breadcrumbsBuilder component.
  480.      *
  481.      * @var BreadcrumbsBuilderInterface
  482.      */
  483.     private $breadcrumbsBuilder;
  484.     /**
  485.      * Component responsible for persisting filters.
  486.      *
  487.      * @var FilterPersisterInterface|null
  488.      */
  489.     private $filterPersister;
  490.     /**
  491.      * @param string $code
  492.      * @param string $class
  493.      * @param string $baseControllerName
  494.      */
  495.     public function __construct($code$class$baseControllerName)
  496.     {
  497.         $this->code $code;
  498.         $this->class $class;
  499.         $this->baseControllerName $baseControllerName;
  500.         $this->predefinePerPageOptions();
  501.         $this->datagridValues['_per_page'] = $this->maxPerPage;
  502.     }
  503.     /**
  504.      * {@inheritdoc}
  505.      *
  506.      * NEXT_MAJOR: return null to indicate no override
  507.      */
  508.     public function getExportFormats()
  509.     {
  510.         return [
  511.             'json''xml''csv''xls',
  512.         ];
  513.     }
  514.     /**
  515.      * @return array
  516.      */
  517.     public function getExportFields()
  518.     {
  519.         $fields $this->getModelManager()->getExportFields($this->getClass());
  520.         foreach ($this->getExtensions() as $extension) {
  521.             if (method_exists($extension'configureExportFields')) {
  522.                 $fields $extension->configureExportFields($this$fields);
  523.             }
  524.         }
  525.         return $fields;
  526.     }
  527.     public function getDataSourceIterator()
  528.     {
  529.         $datagrid $this->getDatagrid();
  530.         $datagrid->buildPager();
  531.         $fields = [];
  532.         foreach ($this->getExportFields() as $key => $field) {
  533.             $label $this->getTranslationLabel($field'export''label');
  534.             $transLabel $this->trans($label);
  535.             // NEXT_MAJOR: Remove this hack, because all field labels will be translated with the major release
  536.             // No translation key exists
  537.             if ($transLabel === $label) {
  538.                 $fields[$key] = $field;
  539.             } else {
  540.                 $fields[$transLabel] = $field;
  541.             }
  542.         }
  543.         return $this->getModelManager()->getDataSourceIterator($datagrid$fields);
  544.     }
  545.     public function validate(ErrorElement $errorElement$object)
  546.     {
  547.     }
  548.     /**
  549.      * define custom variable.
  550.      */
  551.     public function initialize()
  552.     {
  553.         if (!$this->classnameLabel) {
  554.             /* NEXT_MAJOR: remove cast to string, null is not supposed to be
  555.             supported but was documented as such */
  556.             $this->classnameLabel substr(
  557.                 (string) $this->getClass(),
  558.                 strrpos((string) $this->getClass(), '\\') + 1
  559.             );
  560.         }
  561.         // NEXT_MAJOR: Remove this line.
  562.         $this->baseCodeRoute $this->getCode();
  563.         $this->configure();
  564.     }
  565.     public function configure()
  566.     {
  567.     }
  568.     public function update($object)
  569.     {
  570.         $this->preUpdate($object);
  571.         foreach ($this->extensions as $extension) {
  572.             $extension->preUpdate($this$object);
  573.         }
  574.         $result $this->getModelManager()->update($object);
  575.         // BC compatibility
  576.         if (null !== $result) {
  577.             $object $result;
  578.         }
  579.         $this->postUpdate($object);
  580.         foreach ($this->extensions as $extension) {
  581.             $extension->postUpdate($this$object);
  582.         }
  583.         return $object;
  584.     }
  585.     public function create($object)
  586.     {
  587.         $this->prePersist($object);
  588.         foreach ($this->extensions as $extension) {
  589.             $extension->prePersist($this$object);
  590.         }
  591.         $result $this->getModelManager()->create($object);
  592.         // BC compatibility
  593.         if (null !== $result) {
  594.             $object $result;
  595.         }
  596.         $this->postPersist($object);
  597.         foreach ($this->extensions as $extension) {
  598.             $extension->postPersist($this$object);
  599.         }
  600.         $this->createObjectSecurity($object);
  601.         return $object;
  602.     }
  603.     public function delete($object)
  604.     {
  605.         $this->preRemove($object);
  606.         foreach ($this->extensions as $extension) {
  607.             $extension->preRemove($this$object);
  608.         }
  609.         $this->getSecurityHandler()->deleteObjectSecurity($this$object);
  610.         $this->getModelManager()->delete($object);
  611.         $this->postRemove($object);
  612.         foreach ($this->extensions as $extension) {
  613.             $extension->postRemove($this$object);
  614.         }
  615.     }
  616.     /**
  617.      * @param object $object
  618.      */
  619.     public function preValidate($object)
  620.     {
  621.     }
  622.     public function preUpdate($object)
  623.     {
  624.     }
  625.     public function postUpdate($object)
  626.     {
  627.     }
  628.     public function prePersist($object)
  629.     {
  630.     }
  631.     public function postPersist($object)
  632.     {
  633.     }
  634.     public function preRemove($object)
  635.     {
  636.     }
  637.     public function postRemove($object)
  638.     {
  639.     }
  640.     public function preBatchAction($actionNameProxyQueryInterface $query, array &$idx$allElements)
  641.     {
  642.     }
  643.     public function getFilterParameters()
  644.     {
  645.         $parameters = [];
  646.         // build the values array
  647.         if ($this->hasRequest()) {
  648.             $filters $this->request->query->get('filter', []);
  649.             if (isset($filters['_page'])) {
  650.                 $filters['_page'] = (int) $filters['_page'];
  651.             }
  652.             if (isset($filters['_per_page'])) {
  653.                 $filters['_per_page'] = (int) $filters['_per_page'];
  654.             }
  655.             // if filter persistence is configured
  656.             // NEXT_MAJOR: remove `$this->persistFilters !== false` from the condition
  657.             if (false !== $this->persistFilters && null !== $this->filterPersister) {
  658.                 // if reset filters is asked, remove from storage
  659.                 if ('reset' === $this->request->query->get('filters')) {
  660.                     $this->filterPersister->reset($this->getCode());
  661.                 }
  662.                 // if no filters, fetch from storage
  663.                 // otherwise save to storage
  664.                 if (empty($filters)) {
  665.                     $filters $this->filterPersister->get($this->getCode());
  666.                 } else {
  667.                     $this->filterPersister->set($this->getCode(), $filters);
  668.                 }
  669.             }
  670.             $parameters array_merge(
  671.                 $this->getModelManager()->getDefaultSortValues($this->getClass()),
  672.                 $this->datagridValues,
  673.                 $this->getDefaultFilterValues(),
  674.                 $filters
  675.             );
  676.             if (!$this->determinedPerPageValue($parameters['_per_page'])) {
  677.                 $parameters['_per_page'] = $this->maxPerPage;
  678.             }
  679.             // always force the parent value
  680.             if ($this->isChild() && $this->getParentAssociationMapping()) {
  681.                 $name str_replace('.''__'$this->getParentAssociationMapping());
  682.                 $parameters[$name] = ['value' => $this->request->get($this->getParent()->getIdParameter())];
  683.             }
  684.         }
  685.         return $parameters;
  686.     }
  687.     public function buildDatagrid()
  688.     {
  689.         if ($this->datagrid) {
  690.             return;
  691.         }
  692.         $filterParameters $this->getFilterParameters();
  693.         // transform _sort_by from a string to a FieldDescriptionInterface for the datagrid.
  694.         if (isset($filterParameters['_sort_by']) && \is_string($filterParameters['_sort_by'])) {
  695.             if ($this->hasListFieldDescription($filterParameters['_sort_by'])) {
  696.                 $filterParameters['_sort_by'] = $this->getListFieldDescription($filterParameters['_sort_by']);
  697.             } else {
  698.                 $filterParameters['_sort_by'] = $this->getModelManager()->getNewFieldDescriptionInstance(
  699.                     $this->getClass(),
  700.                     $filterParameters['_sort_by'],
  701.                     []
  702.                 );
  703.                 $this->getListBuilder()->buildField(null$filterParameters['_sort_by'], $this);
  704.             }
  705.         }
  706.         // initialize the datagrid
  707.         $this->datagrid $this->getDatagridBuilder()->getBaseDatagrid($this$filterParameters);
  708.         $this->datagrid->getPager()->setMaxPageLinks($this->maxPageLinks);
  709.         $mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid$this);
  710.         // build the datagrid filter
  711.         $this->configureDatagridFilters($mapper);
  712.         // ok, try to limit to add parent filter
  713.         if ($this->isChild() && $this->getParentAssociationMapping() && !$mapper->has($this->getParentAssociationMapping())) {
  714.             $mapper->add($this->getParentAssociationMapping(), null, [
  715.                 'show_filter' => false,
  716.                 'label' => false,
  717.                 'field_type' => ModelHiddenType::class,
  718.                 'field_options' => [
  719.                     'model_manager' => $this->getModelManager(),
  720.                 ],
  721.                 'operator_type' => HiddenType::class,
  722.             ], nullnull, [
  723.                 'admin_code' => $this->getParent()->getCode(),
  724.             ]);
  725.         }
  726.         foreach ($this->getExtensions() as $extension) {
  727.             $extension->configureDatagridFilters($mapper);
  728.         }
  729.     }
  730.     /**
  731.      * Returns the name of the parent related field, so the field can be use to set the default
  732.      * value (ie the parent object) or to filter the object.
  733.      *
  734.      * @throws \InvalidArgumentException
  735.      *
  736.      * @return string|null
  737.      */
  738.     public function getParentAssociationMapping()
  739.     {
  740.         // NEXT_MAJOR: remove array check
  741.         if (\is_array($this->parentAssociationMapping) && $this->getParent()) {
  742.             $parent $this->getParent()->getCode();
  743.             if (\array_key_exists($parent$this->parentAssociationMapping)) {
  744.                 return $this->parentAssociationMapping[$parent];
  745.             }
  746.             throw new \InvalidArgumentException(sprintf(
  747.                 "There's no association between %s and %s.",
  748.                 $this->getCode(),
  749.                 $this->getParent()->getCode()
  750.             ));
  751.         }
  752.         // NEXT_MAJOR: remove this line
  753.         return $this->parentAssociationMapping;
  754.     }
  755.     /**
  756.      * @param string $code
  757.      * @param string $value
  758.      */
  759.     final public function addParentAssociationMapping($code$value)
  760.     {
  761.         $this->parentAssociationMapping[$code] = $value;
  762.     }
  763.     /**
  764.      * Returns the baseRoutePattern used to generate the routing information.
  765.      *
  766.      * @throws \RuntimeException
  767.      *
  768.      * @return string the baseRoutePattern used to generate the routing information
  769.      */
  770.     public function getBaseRoutePattern()
  771.     {
  772.         if (null !== $this->cachedBaseRoutePattern) {
  773.             return $this->cachedBaseRoutePattern;
  774.         }
  775.         if ($this->isChild()) { // the admin class is a child, prefix it with the parent route pattern
  776.             $baseRoutePattern $this->baseRoutePattern;
  777.             if (!$this->baseRoutePattern) {
  778.                 preg_match(self::CLASS_REGEX$this->class$matches);
  779.                 if (!$matches) {
  780.                     throw new \RuntimeException(sprintf('Please define a default `baseRoutePattern` value for the admin class `%s`', static::class));
  781.                 }
  782.                 $baseRoutePattern $this->urlize($matches[5], '-');
  783.             }
  784.             $this->cachedBaseRoutePattern sprintf(
  785.                 '%s/%s/%s',
  786.                 $this->getParent()->getBaseRoutePattern(),
  787.                 $this->getParent()->getRouterIdParameter(),
  788.                 $baseRoutePattern
  789.             );
  790.         } elseif ($this->baseRoutePattern) {
  791.             $this->cachedBaseRoutePattern $this->baseRoutePattern;
  792.         } else {
  793.             preg_match(self::CLASS_REGEX$this->class$matches);
  794.             if (!$matches) {
  795.                 throw new \RuntimeException(sprintf('Please define a default `baseRoutePattern` value for the admin class `%s`', static::class));
  796.             }
  797.             $this->cachedBaseRoutePattern sprintf(
  798.                 '/%s%s/%s',
  799.                 empty($matches[1]) ? '' $this->urlize($matches[1], '-').'/',
  800.                 $this->urlize($matches[3], '-'),
  801.                 $this->urlize($matches[5], '-')
  802.             );
  803.         }
  804.         return $this->cachedBaseRoutePattern;
  805.     }
  806.     /**
  807.      * Returns the baseRouteName used to generate the routing information.
  808.      *
  809.      * @throws \RuntimeException
  810.      *
  811.      * @return string the baseRouteName used to generate the routing information
  812.      */
  813.     public function getBaseRouteName()
  814.     {
  815.         if (null !== $this->cachedBaseRouteName) {
  816.             return $this->cachedBaseRouteName;
  817.         }
  818.         if ($this->isChild()) { // the admin class is a child, prefix it with the parent route name
  819.             $baseRouteName $this->baseRouteName;
  820.             if (!$this->baseRouteName) {
  821.                 preg_match(self::CLASS_REGEX$this->class$matches);
  822.                 if (!$matches) {
  823.                     throw new \RuntimeException(sprintf('Cannot automatically determine base route name, please define a default `baseRouteName` value for the admin class `%s`', static::class));
  824.                 }
  825.                 $baseRouteName $this->urlize($matches[5]);
  826.             }
  827.             $this->cachedBaseRouteName sprintf(
  828.                 '%s_%s',
  829.                 $this->getParent()->getBaseRouteName(),
  830.                 $baseRouteName
  831.             );
  832.         } elseif ($this->baseRouteName) {
  833.             $this->cachedBaseRouteName $this->baseRouteName;
  834.         } else {
  835.             preg_match(self::CLASS_REGEX$this->class$matches);
  836.             if (!$matches) {
  837.                 throw new \RuntimeException(sprintf('Cannot automatically determine base route name, please define a default `baseRouteName` value for the admin class `%s`', static::class));
  838.             }
  839.             $this->cachedBaseRouteName sprintf('admin_%s%s_%s',
  840.                 empty($matches[1]) ? '' $this->urlize($matches[1]).'_',
  841.                 $this->urlize($matches[3]),
  842.                 $this->urlize($matches[5])
  843.             );
  844.         }
  845.         return $this->cachedBaseRouteName;
  846.     }
  847.     /**
  848.      * urlize the given word.
  849.      *
  850.      * @param string $word
  851.      * @param string $sep  the separator
  852.      *
  853.      * @return string
  854.      */
  855.     public function urlize($word$sep '_')
  856.     {
  857.         return strtolower(preg_replace('/[^a-z0-9_]/i'$sep.'$1'$word));
  858.     }
  859.     public function getClass()
  860.     {
  861.         if ($this->hasActiveSubClass()) {
  862.             if ($this->getParentFieldDescription()) {
  863.                 throw new \RuntimeException('Feature not implemented: an embedded admin cannot have subclass');
  864.             }
  865.             $subClass $this->getRequest()->query->get('subclass');
  866.             if (!$this->hasSubClass($subClass)) {
  867.                 throw new \RuntimeException(sprintf('Subclass "%s" is not defined.'$subClass));
  868.             }
  869.             return $this->getSubClass($subClass);
  870.         }
  871.         // see https://github.com/sonata-project/SonataCoreBundle/commit/247eeb0a7ca7211142e101754769d70bc402a5b4
  872.         if ($this->subject && \is_object($this->subject)) {
  873.             return ClassUtils::getClass($this->subject);
  874.         }
  875.         return $this->class;
  876.     }
  877.     public function getSubClasses()
  878.     {
  879.         return $this->subClasses;
  880.     }
  881.     /**
  882.      * NEXT_MAJOR: remove this method.
  883.      */
  884.     public function addSubClass($subClass)
  885.     {
  886.         @trigger_error(sprintf(
  887.             'Method "%s" is deprecated since 3.30 and will be removed in 4.0.',
  888.             __METHOD__
  889.         ), E_USER_DEPRECATED);
  890.         if (!\in_array($subClass$this->subClassestrue)) {
  891.             $this->subClasses[] = $subClass;
  892.         }
  893.     }
  894.     public function setSubClasses(array $subClasses)
  895.     {
  896.         $this->subClasses $subClasses;
  897.     }
  898.     public function hasSubClass($name)
  899.     {
  900.         return isset($this->subClasses[$name]);
  901.     }
  902.     public function hasActiveSubClass()
  903.     {
  904.         if (\count($this->subClasses) > && $this->request) {
  905.             return null !== $this->getRequest()->query->get('subclass');
  906.         }
  907.         return false;
  908.     }
  909.     public function getActiveSubClass()
  910.     {
  911.         if (!$this->hasActiveSubClass()) {
  912.             @trigger_error(sprintf(
  913.                 '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. '.
  914.                 'Use %s::hasActiveSubClass() to know if there is an active subclass.',
  915.                 __METHOD__,
  916.                 __CLASS__
  917.             ), E_USER_DEPRECATED);
  918.             // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare string as return type
  919.             // throw new \LogicException(sprintf(
  920.             //    'Admin "%s" has no active subclass.',
  921.             //    static::class
  922.             // ));
  923.             return null;
  924.         }
  925.         return $this->getSubClass($this->getActiveSubclassCode());
  926.     }
  927.     public function getActiveSubclassCode()
  928.     {
  929.         if (!$this->hasActiveSubClass()) {
  930.             @trigger_error(sprintf(
  931.                 '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. '.
  932.                 'Use %s::hasActiveSubClass() to know if there is an active subclass.',
  933.                 __METHOD__,
  934.                 __CLASS__
  935.             ), E_USER_DEPRECATED);
  936.             // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare string as return type
  937.             // throw new \LogicException(sprintf(
  938.             //    'Admin "%s" has no active subclass.',
  939.             //    static::class
  940.             // ));
  941.             return null;
  942.         }
  943.         $subClass $this->getRequest()->query->get('subclass');
  944.         if (!$this->hasSubClass($subClass)) {
  945.             @trigger_error(sprintf(
  946.                 '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. '.
  947.                 'Use %s::hasActiveSubClass() to know if there is an active subclass.',
  948.                 __METHOD__,
  949.                 __CLASS__
  950.             ), E_USER_DEPRECATED);
  951.             // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare string as return type
  952.             // throw new \LogicException(sprintf(
  953.             //    'Admin "%s" has no active subclass.',
  954.             //    static::class
  955.             // ));
  956.             return null;
  957.         }
  958.         return $subClass;
  959.     }
  960.     public function getBatchActions()
  961.     {
  962.         $actions = [];
  963.         if ($this->hasRoute('delete') && $this->hasAccess('delete')) {
  964.             $actions['delete'] = [
  965.                 'label' => 'action_delete',
  966.                 'translation_domain' => 'SonataAdminBundle',
  967.                 'ask_confirmation' => true// by default always true
  968.             ];
  969.         }
  970.         $actions $this->configureBatchActions($actions);
  971.         foreach ($this->getExtensions() as $extension) {
  972.             // TODO: remove method check in next major release
  973.             if (method_exists($extension'configureBatchActions')) {
  974.                 $actions $extension->configureBatchActions($this$actions);
  975.             }
  976.         }
  977.         foreach ($actions  as $name => &$action) {
  978.             if (!\array_key_exists('label'$action)) {
  979.                 $action['label'] = $this->getTranslationLabel($name'batch''label');
  980.             }
  981.             if (!\array_key_exists('translation_domain'$action)) {
  982.                 $action['translation_domain'] = $this->getTranslationDomain();
  983.             }
  984.         }
  985.         return $actions;
  986.     }
  987.     public function getRoutes()
  988.     {
  989.         $this->buildRoutes();
  990.         return $this->routes;
  991.     }
  992.     public function getRouterIdParameter()
  993.     {
  994.         return '{'.$this->getIdParameter().'}';
  995.     }
  996.     public function getIdParameter()
  997.     {
  998.         $parameter 'id';
  999.         for ($i 0$i $this->getChildDepth(); ++$i) {
  1000.             $parameter 'child'.ucfirst($parameter);
  1001.         }
  1002.         return $parameter;
  1003.     }
  1004.     public function hasRoute($name)
  1005.     {
  1006.         if (!$this->routeGenerator) {
  1007.             throw new \RuntimeException('RouteGenerator cannot be null');
  1008.         }
  1009.         return $this->routeGenerator->hasAdminRoute($this$name);
  1010.     }
  1011.     /**
  1012.      * @param string      $name
  1013.      * @param string|null $adminCode
  1014.      *
  1015.      * @return bool
  1016.      */
  1017.     public function isCurrentRoute($name$adminCode null)
  1018.     {
  1019.         if (!$this->hasRequest()) {
  1020.             return false;
  1021.         }
  1022.         $request $this->getRequest();
  1023.         $route $request->get('_route');
  1024.         if ($adminCode) {
  1025.             $admin $this->getConfigurationPool()->getAdminByAdminCode($adminCode);
  1026.         } else {
  1027.             $admin $this;
  1028.         }
  1029.         if (!$admin) {
  1030.             return false;
  1031.         }
  1032.         return ($admin->getBaseRouteName().'_'.$name) === $route;
  1033.     }
  1034.     public function generateObjectUrl($name$object, array $parameters = [], $absolute RoutingUrlGeneratorInterface::ABSOLUTE_PATH)
  1035.     {
  1036.         $parameters['id'] = $this->getUrlsafeIdentifier($object);
  1037.         return $this->generateUrl($name$parameters$absolute);
  1038.     }
  1039.     public function generateUrl($name, array $parameters = [], $absolute RoutingUrlGeneratorInterface::ABSOLUTE_PATH)
  1040.     {
  1041.         return $this->routeGenerator->generateUrl($this$name$parameters$absolute);
  1042.     }
  1043.     public function generateMenuUrl($name, array $parameters = [], $absolute RoutingUrlGeneratorInterface::ABSOLUTE_PATH)
  1044.     {
  1045.         return $this->routeGenerator->generateMenuUrl($this$name$parameters$absolute);
  1046.     }
  1047.     final public function setTemplateRegistry(MutableTemplateRegistryInterface $templateRegistry)
  1048.     {
  1049.         $this->templateRegistry $templateRegistry;
  1050.     }
  1051.     /**
  1052.      * @param array<string, string> $templates
  1053.      */
  1054.     public function setTemplates(array $templates)
  1055.     {
  1056.         // NEXT_MAJOR: Remove this line
  1057.         $this->templates $templates;
  1058.         $this->getTemplateRegistry()->setTemplates($templates);
  1059.     }
  1060.     /**
  1061.      * @param string $name
  1062.      * @param string $template
  1063.      */
  1064.     public function setTemplate($name$template)
  1065.     {
  1066.         // NEXT_MAJOR: Remove this line
  1067.         $this->templates[$name] = $template;
  1068.         $this->getTemplateRegistry()->setTemplate($name$template);
  1069.     }
  1070.     /**
  1071.      * @deprecated since 3.34, will be dropped in 4.0. Use TemplateRegistry services instead
  1072.      *
  1073.      * @return array<string, string>
  1074.      */
  1075.     public function getTemplates()
  1076.     {
  1077.         return $this->getTemplateRegistry()->getTemplates();
  1078.     }
  1079.     /**
  1080.      * @deprecated since 3.34, will be dropped in 4.0. Use TemplateRegistry services instead
  1081.      *
  1082.      * @param string $name
  1083.      *
  1084.      * @return string|null
  1085.      */
  1086.     public function getTemplate($name)
  1087.     {
  1088.         return $this->getTemplateRegistry()->getTemplate($name);
  1089.     }
  1090.     public function getNewInstance()
  1091.     {
  1092.         $object $this->getModelManager()->getModelInstance($this->getClass());
  1093.         foreach ($this->getExtensions() as $extension) {
  1094.             $extension->alterNewInstance($this$object);
  1095.         }
  1096.         return $object;
  1097.     }
  1098.     public function getFormBuilder()
  1099.     {
  1100.         $this->formOptions['data_class'] = $this->getClass();
  1101.         $formBuilder $this->getFormContractor()->getFormBuilder(
  1102.             $this->getUniqid(),
  1103.             $this->formOptions
  1104.         );
  1105.         $this->defineFormBuilder($formBuilder);
  1106.         return $formBuilder;
  1107.     }
  1108.     /**
  1109.      * This method is being called by the main admin class and the child class,
  1110.      * the getFormBuilder is only call by the main admin class.
  1111.      */
  1112.     public function defineFormBuilder(FormBuilderInterface $formBuilder)
  1113.     {
  1114.         $mapper = new FormMapper($this->getFormContractor(), $formBuilder$this);
  1115.         $this->configureFormFields($mapper);
  1116.         foreach ($this->getExtensions() as $extension) {
  1117.             $extension->configureFormFields($mapper);
  1118.         }
  1119.         $this->attachInlineValidator();
  1120.     }
  1121.     public function attachAdminClass(FieldDescriptionInterface $fieldDescription)
  1122.     {
  1123.         $pool $this->getConfigurationPool();
  1124.         $adminCode $fieldDescription->getOption('admin_code');
  1125.         if (null !== $adminCode) {
  1126.             $admin $pool->getAdminByAdminCode($adminCode);
  1127.         } else {
  1128.             $admin $pool->getAdminByClass($fieldDescription->getTargetEntity());
  1129.         }
  1130.         if (!$admin) {
  1131.             return;
  1132.         }
  1133.         if ($this->hasRequest()) {
  1134.             $admin->setRequest($this->getRequest());
  1135.         }
  1136.         $fieldDescription->setAssociationAdmin($admin);
  1137.     }
  1138.     public function getObject($id)
  1139.     {
  1140.         $object $this->getModelManager()->find($this->getClass(), $id);
  1141.         foreach ($this->getExtensions() as $extension) {
  1142.             $extension->alterObject($this$object);
  1143.         }
  1144.         return $object;
  1145.     }
  1146.     public function getForm()
  1147.     {
  1148.         $this->buildForm();
  1149.         return $this->form;
  1150.     }
  1151.     public function getList()
  1152.     {
  1153.         $this->buildList();
  1154.         return $this->list;
  1155.     }
  1156.     public function createQuery($context 'list')
  1157.     {
  1158.         if (\func_num_args() > 0) {
  1159.             @trigger_error(
  1160.                 'The $context argument of '.__METHOD__.' is deprecated since 3.3, to be removed in 4.0.',
  1161.                 E_USER_DEPRECATED
  1162.             );
  1163.         }
  1164.         $query $this->getModelManager()->createQuery($this->getClass());
  1165.         foreach ($this->extensions as $extension) {
  1166.             $extension->configureQuery($this$query$context);
  1167.         }
  1168.         return $query;
  1169.     }
  1170.     public function getDatagrid()
  1171.     {
  1172.         $this->buildDatagrid();
  1173.         return $this->datagrid;
  1174.     }
  1175.     public function buildTabMenu($actionAdminInterface $childAdmin null)
  1176.     {
  1177.         if ($this->loaded['tab_menu']) {
  1178.             return $this->menu;
  1179.         }
  1180.         $this->loaded['tab_menu'] = true;
  1181.         $menu $this->menuFactory->createItem('root');
  1182.         $menu->setChildrenAttribute('class''nav navbar-nav');
  1183.         $menu->setExtra('translation_domain'$this->translationDomain);
  1184.         // Prevents BC break with KnpMenuBundle v1.x
  1185.         if (method_exists($menu'setCurrentUri')) {
  1186.             $menu->setCurrentUri($this->getRequest()->getBaseUrl().$this->getRequest()->getPathInfo());
  1187.         }
  1188.         $this->configureTabMenu($menu$action$childAdmin);
  1189.         foreach ($this->getExtensions() as $extension) {
  1190.             $extension->configureTabMenu($this$menu$action$childAdmin);
  1191.         }
  1192.         $this->menu $menu;
  1193.         return $this->menu;
  1194.     }
  1195.     public function buildSideMenu($actionAdminInterface $childAdmin null)
  1196.     {
  1197.         return $this->buildTabMenu($action$childAdmin);
  1198.     }
  1199.     /**
  1200.      * @param string $action
  1201.      *
  1202.      * @return ItemInterface
  1203.      */
  1204.     public function getSideMenu($actionAdminInterface $childAdmin null)
  1205.     {
  1206.         if ($this->isChild()) {
  1207.             return $this->getParent()->getSideMenu($action$this);
  1208.         }
  1209.         $this->buildSideMenu($action$childAdmin);
  1210.         return $this->menu;
  1211.     }
  1212.     /**
  1213.      * Returns the root code.
  1214.      *
  1215.      * @return string the root code
  1216.      */
  1217.     public function getRootCode()
  1218.     {
  1219.         return $this->getRoot()->getCode();
  1220.     }
  1221.     /**
  1222.      * Returns the master admin.
  1223.      *
  1224.      * @return AbstractAdmin the root admin class
  1225.      */
  1226.     public function getRoot()
  1227.     {
  1228.         $parentFieldDescription $this->getParentFieldDescription();
  1229.         if (!$parentFieldDescription) {
  1230.             return $this;
  1231.         }
  1232.         return $parentFieldDescription->getAdmin()->getRoot();
  1233.     }
  1234.     public function setBaseControllerName($baseControllerName)
  1235.     {
  1236.         $this->baseControllerName $baseControllerName;
  1237.     }
  1238.     public function getBaseControllerName()
  1239.     {
  1240.         return $this->baseControllerName;
  1241.     }
  1242.     /**
  1243.      * @param string $label
  1244.      */
  1245.     public function setLabel($label)
  1246.     {
  1247.         $this->label $label;
  1248.     }
  1249.     public function getLabel()
  1250.     {
  1251.         return $this->label;
  1252.     }
  1253.     /**
  1254.      * @param bool $persist
  1255.      *
  1256.      * NEXT_MAJOR: remove this method
  1257.      *
  1258.      * @deprecated since 3.34, to be removed in 4.0.
  1259.      */
  1260.     public function setPersistFilters($persist)
  1261.     {
  1262.         @trigger_error(
  1263.             'The '.__METHOD__.' method is deprecated since version 3.34 and will be removed in 4.0.',
  1264.             E_USER_DEPRECATED
  1265.         );
  1266.         $this->persistFilters $persist;
  1267.     }
  1268.     public function setFilterPersister(FilterPersisterInterface $filterPersister null)
  1269.     {
  1270.         $this->filterPersister $filterPersister;
  1271.         // NEXT_MAJOR remove the deprecated property will be removed. Needed for persisted filter condition.
  1272.         $this->persistFilters true;
  1273.     }
  1274.     /**
  1275.      * @param int $maxPerPage
  1276.      */
  1277.     public function setMaxPerPage($maxPerPage)
  1278.     {
  1279.         $this->maxPerPage $maxPerPage;
  1280.     }
  1281.     /**
  1282.      * @return int
  1283.      */
  1284.     public function getMaxPerPage()
  1285.     {
  1286.         return $this->maxPerPage;
  1287.     }
  1288.     /**
  1289.      * @param int $maxPageLinks
  1290.      */
  1291.     public function setMaxPageLinks($maxPageLinks)
  1292.     {
  1293.         $this->maxPageLinks $maxPageLinks;
  1294.     }
  1295.     /**
  1296.      * @return int
  1297.      */
  1298.     public function getMaxPageLinks()
  1299.     {
  1300.         return $this->maxPageLinks;
  1301.     }
  1302.     public function getFormGroups()
  1303.     {
  1304.         return $this->formGroups;
  1305.     }
  1306.     public function setFormGroups(array $formGroups)
  1307.     {
  1308.         $this->formGroups $formGroups;
  1309.     }
  1310.     public function removeFieldFromFormGroup($key)
  1311.     {
  1312.         foreach ($this->formGroups as $name => $formGroup) {
  1313.             unset($this->formGroups[$name]['fields'][$key]);
  1314.             if (empty($this->formGroups[$name]['fields'])) {
  1315.                 unset($this->formGroups[$name]);
  1316.             }
  1317.         }
  1318.     }
  1319.     /**
  1320.      * @param array $group
  1321.      */
  1322.     public function reorderFormGroup($group, array $keys)
  1323.     {
  1324.         $formGroups $this->getFormGroups();
  1325.         $formGroups[$group]['fields'] = array_merge(array_flip($keys), $formGroups[$group]['fields']);
  1326.         $this->setFormGroups($formGroups);
  1327.     }
  1328.     public function getFormTabs()
  1329.     {
  1330.         return $this->formTabs;
  1331.     }
  1332.     public function setFormTabs(array $formTabs)
  1333.     {
  1334.         $this->formTabs $formTabs;
  1335.     }
  1336.     public function getShowTabs()
  1337.     {
  1338.         return $this->showTabs;
  1339.     }
  1340.     public function setShowTabs(array $showTabs)
  1341.     {
  1342.         $this->showTabs $showTabs;
  1343.     }
  1344.     public function getShowGroups()
  1345.     {
  1346.         return $this->showGroups;
  1347.     }
  1348.     public function setShowGroups(array $showGroups)
  1349.     {
  1350.         $this->showGroups $showGroups;
  1351.     }
  1352.     public function reorderShowGroup($group, array $keys)
  1353.     {
  1354.         $showGroups $this->getShowGroups();
  1355.         $showGroups[$group]['fields'] = array_merge(array_flip($keys), $showGroups[$group]['fields']);
  1356.         $this->setShowGroups($showGroups);
  1357.     }
  1358.     public function setParentFieldDescription(FieldDescriptionInterface $parentFieldDescription)
  1359.     {
  1360.         $this->parentFieldDescription $parentFieldDescription;
  1361.     }
  1362.     public function getParentFieldDescription()
  1363.     {
  1364.         return $this->parentFieldDescription;
  1365.     }
  1366.     public function hasParentFieldDescription()
  1367.     {
  1368.         return $this->parentFieldDescription instanceof FieldDescriptionInterface;
  1369.     }
  1370.     public function setSubject($subject)
  1371.     {
  1372.         if (\is_object($subject) && !is_a($subject$this->getClass(), true)) {
  1373.             $message = <<<'EOT'
  1374. You are trying to set entity an instance of "%s",
  1375. which is not the one registered with this admin class ("%s").
  1376. This is deprecated since 3.5 and will no longer be supported in 4.0.
  1377. EOT;
  1378.             @trigger_error(
  1379.                 sprintf($message, \get_class($subject), $this->getClass()),
  1380.                 E_USER_DEPRECATED
  1381.             ); // NEXT_MAJOR : throw an exception instead
  1382.         }
  1383.         $this->subject $subject;
  1384.     }
  1385.     public function getSubject()
  1386.     {
  1387.         if (null === $this->subject && $this->request && !$this->hasParentFieldDescription()) {
  1388.             $id $this->request->get($this->getIdParameter());
  1389.             if (null !== $id) {
  1390.                 $this->subject $this->getObject($id);
  1391.             }
  1392.         }
  1393.         return $this->subject;
  1394.     }
  1395.     public function hasSubject()
  1396.     {
  1397.         return (bool) $this->getSubject();
  1398.     }
  1399.     public function getFormFieldDescriptions()
  1400.     {
  1401.         $this->buildForm();
  1402.         return $this->formFieldDescriptions;
  1403.     }
  1404.     public function getFormFieldDescription($name)
  1405.     {
  1406.         return $this->hasFormFieldDescription($name) ? $this->formFieldDescriptions[$name] : null;
  1407.     }
  1408.     /**
  1409.      * Returns true if the admin has a FieldDescription with the given $name.
  1410.      *
  1411.      * @param string $name
  1412.      *
  1413.      * @return bool
  1414.      */
  1415.     public function hasFormFieldDescription($name)
  1416.     {
  1417.         return \array_key_exists($name$this->formFieldDescriptions) ? true false;
  1418.     }
  1419.     public function addFormFieldDescription($nameFieldDescriptionInterface $fieldDescription)
  1420.     {
  1421.         $this->formFieldDescriptions[$name] = $fieldDescription;
  1422.     }
  1423.     /**
  1424.      * remove a FieldDescription.
  1425.      *
  1426.      * @param string $name
  1427.      */
  1428.     public function removeFormFieldDescription($name)
  1429.     {
  1430.         unset($this->formFieldDescriptions[$name]);
  1431.     }
  1432.     /**
  1433.      * build and return the collection of form FieldDescription.
  1434.      *
  1435.      * @return array collection of form FieldDescription
  1436.      */
  1437.     public function getShowFieldDescriptions()
  1438.     {
  1439.         $this->buildShow();
  1440.         return $this->showFieldDescriptions;
  1441.     }
  1442.     /**
  1443.      * Returns the form FieldDescription with the given $name.
  1444.      *
  1445.      * @param string $name
  1446.      *
  1447.      * @return FieldDescriptionInterface
  1448.      */
  1449.     public function getShowFieldDescription($name)
  1450.     {
  1451.         $this->buildShow();
  1452.         return $this->hasShowFieldDescription($name) ? $this->showFieldDescriptions[$name] : null;
  1453.     }
  1454.     public function hasShowFieldDescription($name)
  1455.     {
  1456.         return \array_key_exists($name$this->showFieldDescriptions);
  1457.     }
  1458.     public function addShowFieldDescription($nameFieldDescriptionInterface $fieldDescription)
  1459.     {
  1460.         $this->showFieldDescriptions[$name] = $fieldDescription;
  1461.     }
  1462.     public function removeShowFieldDescription($name)
  1463.     {
  1464.         unset($this->showFieldDescriptions[$name]);
  1465.     }
  1466.     public function getListFieldDescriptions()
  1467.     {
  1468.         $this->buildList();
  1469.         return $this->listFieldDescriptions;
  1470.     }
  1471.     public function getListFieldDescription($name)
  1472.     {
  1473.         return $this->hasListFieldDescription($name) ? $this->listFieldDescriptions[$name] : null;
  1474.     }
  1475.     public function hasListFieldDescription($name)
  1476.     {
  1477.         $this->buildList();
  1478.         return \array_key_exists($name$this->listFieldDescriptions) ? true false;
  1479.     }
  1480.     public function addListFieldDescription($nameFieldDescriptionInterface $fieldDescription)
  1481.     {
  1482.         $this->listFieldDescriptions[$name] = $fieldDescription;
  1483.     }
  1484.     public function removeListFieldDescription($name)
  1485.     {
  1486.         unset($this->listFieldDescriptions[$name]);
  1487.     }
  1488.     public function getFilterFieldDescription($name)
  1489.     {
  1490.         return $this->hasFilterFieldDescription($name) ? $this->filterFieldDescriptions[$name] : null;
  1491.     }
  1492.     public function hasFilterFieldDescription($name)
  1493.     {
  1494.         return \array_key_exists($name$this->filterFieldDescriptions) ? true false;
  1495.     }
  1496.     public function addFilterFieldDescription($nameFieldDescriptionInterface $fieldDescription)
  1497.     {
  1498.         $this->filterFieldDescriptions[$name] = $fieldDescription;
  1499.     }
  1500.     public function removeFilterFieldDescription($name)
  1501.     {
  1502.         unset($this->filterFieldDescriptions[$name]);
  1503.     }
  1504.     public function getFilterFieldDescriptions()
  1505.     {
  1506.         $this->buildDatagrid();
  1507.         return $this->filterFieldDescriptions;
  1508.     }
  1509.     public function addChild(AdminInterface $child)
  1510.     {
  1511.         for ($parentAdmin $thisnull !== $parentAdmin$parentAdmin $parentAdmin->getParent()) {
  1512.             if ($parentAdmin->getCode() !== $child->getCode()) {
  1513.                 continue;
  1514.             }
  1515.             throw new \RuntimeException(sprintf(
  1516.                 'Circular reference detected! The child admin `%s` is already in the parent tree of the `%s` admin.',
  1517.                 $child->getCode(), $this->getCode()
  1518.             ));
  1519.         }
  1520.         $this->children[$child->getCode()] = $child;
  1521.         $child->setParent($this);
  1522.         // NEXT_MAJOR: remove $args and add $field parameter to this function on next Major
  1523.         $args = \func_get_args();
  1524.         if (isset($args[1])) {
  1525.             $child->addParentAssociationMapping($this->getCode(), $args[1]);
  1526.         } else {
  1527.             @trigger_error(
  1528.                 'Calling "addChild" without second argument is deprecated since 3.35'
  1529.                 .' and will not be allowed in 4.0.',
  1530.                 E_USER_DEPRECATED
  1531.             );
  1532.         }
  1533.     }
  1534.     public function hasChild($code)
  1535.     {
  1536.         return isset($this->children[$code]);
  1537.     }
  1538.     public function getChildren()
  1539.     {
  1540.         return $this->children;
  1541.     }
  1542.     public function getChild($code)
  1543.     {
  1544.         return $this->hasChild($code) ? $this->children[$code] : null;
  1545.     }
  1546.     public function setParent(AdminInterface $parent)
  1547.     {
  1548.         $this->parent $parent;
  1549.     }
  1550.     public function getParent()
  1551.     {
  1552.         return $this->parent;
  1553.     }
  1554.     final public function getRootAncestor()
  1555.     {
  1556.         $parent $this;
  1557.         while ($parent->isChild()) {
  1558.             $parent $parent->getParent();
  1559.         }
  1560.         return $parent;
  1561.     }
  1562.     final public function getChildDepth()
  1563.     {
  1564.         $parent $this;
  1565.         $depth 0;
  1566.         while ($parent->isChild()) {
  1567.             $parent $parent->getParent();
  1568.             ++$depth;
  1569.         }
  1570.         return $depth;
  1571.     }
  1572.     final public function getCurrentLeafChildAdmin()
  1573.     {
  1574.         $child $this->getCurrentChildAdmin();
  1575.         if (null === $child) {
  1576.             return null;
  1577.         }
  1578.         for ($c $childnull !== $c$c $child->getCurrentChildAdmin()) {
  1579.             $child $c;
  1580.         }
  1581.         return $child;
  1582.     }
  1583.     public function isChild()
  1584.     {
  1585.         return $this->parent instanceof AdminInterface;
  1586.     }
  1587.     /**
  1588.      * Returns true if the admin has children, false otherwise.
  1589.      *
  1590.      * @return bool if the admin has children
  1591.      */
  1592.     public function hasChildren()
  1593.     {
  1594.         return \count($this->children) > 0;
  1595.     }
  1596.     public function setUniqid($uniqid)
  1597.     {
  1598.         $this->uniqid $uniqid;
  1599.     }
  1600.     public function getUniqid()
  1601.     {
  1602.         if (!$this->uniqid) {
  1603.             $this->uniqid 's'.substr(md5($this->getBaseCodeRoute()), 010);
  1604.         }
  1605.         return $this->uniqid;
  1606.     }
  1607.     /**
  1608.      * Returns the classname label.
  1609.      *
  1610.      * @return string the classname label
  1611.      */
  1612.     public function getClassnameLabel()
  1613.     {
  1614.         return $this->classnameLabel;
  1615.     }
  1616.     public function getPersistentParameters()
  1617.     {
  1618.         $parameters = [];
  1619.         foreach ($this->getExtensions() as $extension) {
  1620.             $params $extension->getPersistentParameters($this);
  1621.             if (!\is_array($params)) {
  1622.                 throw new \RuntimeException(sprintf('The %s::getPersistentParameters must return an array', \get_class($extension)));
  1623.             }
  1624.             $parameters array_merge($parameters$params);
  1625.         }
  1626.         return $parameters;
  1627.     }
  1628.     /**
  1629.      * @param string $name
  1630.      *
  1631.      * @return mixed|null
  1632.      */
  1633.     public function getPersistentParameter($name)
  1634.     {
  1635.         $parameters $this->getPersistentParameters();
  1636.         return $parameters[$name] ?? null;
  1637.     }
  1638.     public function getBreadcrumbs($action)
  1639.     {
  1640.         @trigger_error(
  1641.             'The '.__METHOD__.' method is deprecated since version 3.2 and will be removed in 4.0.'.
  1642.             ' Use Sonata\AdminBundle\Admin\BreadcrumbsBuilder::getBreadcrumbs instead.',
  1643.             E_USER_DEPRECATED
  1644.         );
  1645.         return $this->getBreadcrumbsBuilder()->getBreadcrumbs($this$action);
  1646.     }
  1647.     /**
  1648.      * Generates the breadcrumbs array.
  1649.      *
  1650.      * Note: the method will be called by the top admin instance (parent => child)
  1651.      *
  1652.      * @param string $action
  1653.      *
  1654.      * @return array
  1655.      */
  1656.     public function buildBreadcrumbs($actionMenuItemInterface $menu null)
  1657.     {
  1658.         @trigger_error(
  1659.             'The '.__METHOD__.' method is deprecated since version 3.2 and will be removed in 4.0.',
  1660.             E_USER_DEPRECATED
  1661.         );
  1662.         if (isset($this->breadcrumbs[$action])) {
  1663.             return $this->breadcrumbs[$action];
  1664.         }
  1665.         return $this->breadcrumbs[$action] = $this->getBreadcrumbsBuilder()
  1666.             ->buildBreadcrumbs($this$action$menu);
  1667.     }
  1668.     /**
  1669.      * NEXT_MAJOR : remove this method.
  1670.      *
  1671.      * @return BreadcrumbsBuilderInterface
  1672.      */
  1673.     final public function getBreadcrumbsBuilder()
  1674.     {
  1675.         @trigger_error(
  1676.             'The '.__METHOD__.' method is deprecated since version 3.2 and will be removed in 4.0.'.
  1677.             ' Use the sonata.admin.breadcrumbs_builder service instead.',
  1678.             E_USER_DEPRECATED
  1679.         );
  1680.         if (null === $this->breadcrumbsBuilder) {
  1681.             $this->breadcrumbsBuilder = new BreadcrumbsBuilder(
  1682.                 $this->getConfigurationPool()->getContainer()->getParameter('sonata.admin.configuration.breadcrumbs')
  1683.             );
  1684.         }
  1685.         return $this->breadcrumbsBuilder;
  1686.     }
  1687.     /**
  1688.      * NEXT_MAJOR : remove this method.
  1689.      *
  1690.      * @return AbstractAdmin
  1691.      */
  1692.     final public function setBreadcrumbsBuilder(BreadcrumbsBuilderInterface $value)
  1693.     {
  1694.         @trigger_error(
  1695.             'The '.__METHOD__.' method is deprecated since version 3.2 and will be removed in 4.0.'.
  1696.             ' Use the sonata.admin.breadcrumbs_builder service instead.',
  1697.             E_USER_DEPRECATED
  1698.         );
  1699.         $this->breadcrumbsBuilder $value;
  1700.         return $this;
  1701.     }
  1702.     public function setCurrentChild($currentChild)
  1703.     {
  1704.         $this->currentChild $currentChild;
  1705.     }
  1706.     public function getCurrentChild()
  1707.     {
  1708.         return $this->currentChild;
  1709.     }
  1710.     /**
  1711.      * Returns the current child admin instance.
  1712.      *
  1713.      * @return AdminInterface|null the current child admin instance
  1714.      */
  1715.     public function getCurrentChildAdmin()
  1716.     {
  1717.         foreach ($this->children as $children) {
  1718.             if ($children->getCurrentChild()) {
  1719.                 return $children;
  1720.             }
  1721.         }
  1722.     }
  1723.     public function trans($id, array $parameters = [], $domain null$locale null)
  1724.     {
  1725.         @trigger_error(
  1726.             'The '.__METHOD__.' method is deprecated since version 3.9 and will be removed in 4.0.',
  1727.             E_USER_DEPRECATED
  1728.         );
  1729.         $domain $domain ?: $this->getTranslationDomain();
  1730.         return $this->translator->trans($id$parameters$domain$locale);
  1731.     }
  1732.     /**
  1733.      * Translate a message id.
  1734.      *
  1735.      * NEXT_MAJOR: remove this method
  1736.      *
  1737.      * @param string      $id
  1738.      * @param int         $count
  1739.      * @param string|null $domain
  1740.      * @param string|null $locale
  1741.      *
  1742.      * @return string the translated string
  1743.      *
  1744.      * @deprecated since 3.9, to be removed with 4.0
  1745.      */
  1746.     public function transChoice($id$count, array $parameters = [], $domain null$locale null)
  1747.     {
  1748.         @trigger_error(
  1749.             'The '.__METHOD__.' method is deprecated since version 3.9 and will be removed in 4.0.',
  1750.             E_USER_DEPRECATED
  1751.         );
  1752.         $domain $domain ?: $this->getTranslationDomain();
  1753.         return $this->translator->transChoice($id$count$parameters$domain$locale);
  1754.     }
  1755.     public function setTranslationDomain($translationDomain)
  1756.     {
  1757.         $this->translationDomain $translationDomain;
  1758.     }
  1759.     public function getTranslationDomain()
  1760.     {
  1761.         return $this->translationDomain;
  1762.     }
  1763.     /**
  1764.      * {@inheritdoc}
  1765.      *
  1766.      * NEXT_MAJOR: remove this method
  1767.      *
  1768.      * @deprecated since 3.9, to be removed with 4.0
  1769.      */
  1770.     public function setTranslator(TranslatorInterface $translator)
  1771.     {
  1772.         $args = \func_get_args();
  1773.         if (isset($args[1]) && $args[1]) {
  1774.             @trigger_error(
  1775.                 'The '.__METHOD__.' method is deprecated since version 3.9 and will be removed in 4.0.',
  1776.                 E_USER_DEPRECATED
  1777.             );
  1778.         }
  1779.         $this->translator $translator;
  1780.     }
  1781.     /**
  1782.      * {@inheritdoc}
  1783.      *
  1784.      * NEXT_MAJOR: remove this method
  1785.      *
  1786.      * @deprecated since 3.9, to be removed with 4.0
  1787.      */
  1788.     public function getTranslator()
  1789.     {
  1790.         @trigger_error(
  1791.             'The '.__METHOD__.' method is deprecated since version 3.9 and will be removed in 4.0.',
  1792.             E_USER_DEPRECATED
  1793.         );
  1794.         return $this->translator;
  1795.     }
  1796.     public function getTranslationLabel($label$context ''$type '')
  1797.     {
  1798.         return $this->getLabelTranslatorStrategy()->getLabel($label$context$type);
  1799.     }
  1800.     public function setRequest(Request $request)
  1801.     {
  1802.         $this->request $request;
  1803.         foreach ($this->getChildren() as $children) {
  1804.             $children->setRequest($request);
  1805.         }
  1806.     }
  1807.     public function getRequest()
  1808.     {
  1809.         if (!$this->request) {
  1810.             throw new \RuntimeException('The Request object has not been set');
  1811.         }
  1812.         return $this->request;
  1813.     }
  1814.     public function hasRequest()
  1815.     {
  1816.         return null !== $this->request;
  1817.     }
  1818.     public function setFormContractor(FormContractorInterface $formBuilder)
  1819.     {
  1820.         $this->formContractor $formBuilder;
  1821.     }
  1822.     /**
  1823.      * @return FormContractorInterface
  1824.      */
  1825.     public function getFormContractor()
  1826.     {
  1827.         return $this->formContractor;
  1828.     }
  1829.     public function setDatagridBuilder(DatagridBuilderInterface $datagridBuilder)
  1830.     {
  1831.         $this->datagridBuilder $datagridBuilder;
  1832.     }
  1833.     public function getDatagridBuilder()
  1834.     {
  1835.         return $this->datagridBuilder;
  1836.     }
  1837.     public function setListBuilder(ListBuilderInterface $listBuilder)
  1838.     {
  1839.         $this->listBuilder $listBuilder;
  1840.     }
  1841.     public function getListBuilder()
  1842.     {
  1843.         return $this->listBuilder;
  1844.     }
  1845.     public function setShowBuilder(ShowBuilderInterface $showBuilder)
  1846.     {
  1847.         $this->showBuilder $showBuilder;
  1848.     }
  1849.     /**
  1850.      * @return ShowBuilderInterface
  1851.      */
  1852.     public function getShowBuilder()
  1853.     {
  1854.         return $this->showBuilder;
  1855.     }
  1856.     public function setConfigurationPool(Pool $configurationPool)
  1857.     {
  1858.         $this->configurationPool $configurationPool;
  1859.     }
  1860.     /**
  1861.      * @return Pool
  1862.      */
  1863.     public function getConfigurationPool()
  1864.     {
  1865.         return $this->configurationPool;
  1866.     }
  1867.     public function setRouteGenerator(RouteGeneratorInterface $routeGenerator)
  1868.     {
  1869.         $this->routeGenerator $routeGenerator;
  1870.     }
  1871.     /**
  1872.      * @return RouteGeneratorInterface
  1873.      */
  1874.     public function getRouteGenerator()
  1875.     {
  1876.         return $this->routeGenerator;
  1877.     }
  1878.     public function getCode()
  1879.     {
  1880.         return $this->code;
  1881.     }
  1882.     /**
  1883.      * NEXT_MAJOR: Remove this function.
  1884.      *
  1885.      * @deprecated This method is deprecated since 3.24 and will be removed in 4.0
  1886.      *
  1887.      * @param string $baseCodeRoute
  1888.      */
  1889.     public function setBaseCodeRoute($baseCodeRoute)
  1890.     {
  1891.         @trigger_error(
  1892.             'The '.__METHOD__.' is deprecated since 3.24 and will be removed in 4.0.',
  1893.             E_USER_DEPRECATED
  1894.         );
  1895.         $this->baseCodeRoute $baseCodeRoute;
  1896.     }
  1897.     public function getBaseCodeRoute()
  1898.     {
  1899.         // NEXT_MAJOR: Uncomment the following lines.
  1900.         // if ($this->isChild()) {
  1901.         //     return $this->getParent()->getBaseCodeRoute().'|'.$this->getCode();
  1902.         // }
  1903.         //
  1904.         // return $this->getCode();
  1905.         // NEXT_MAJOR: Remove all the code below.
  1906.         if ($this->isChild()) {
  1907.             $parentCode $this->getParent()->getCode();
  1908.             if ($this->getParent()->isChild()) {
  1909.                 $parentCode $this->getParent()->getBaseCodeRoute();
  1910.             }
  1911.             return $parentCode.'|'.$this->getCode();
  1912.         }
  1913.         return $this->baseCodeRoute;
  1914.     }
  1915.     public function getModelManager()
  1916.     {
  1917.         return $this->modelManager;
  1918.     }
  1919.     public function setModelManager(ModelManagerInterface $modelManager)
  1920.     {
  1921.         $this->modelManager $modelManager;
  1922.     }
  1923.     public function getManagerType()
  1924.     {
  1925.         return $this->managerType;
  1926.     }
  1927.     /**
  1928.      * @param string $type
  1929.      */
  1930.     public function setManagerType($type)
  1931.     {
  1932.         $this->managerType $type;
  1933.     }
  1934.     public function getObjectIdentifier()
  1935.     {
  1936.         return $this->getCode();
  1937.     }
  1938.     /**
  1939.      * Set the roles and permissions per role.
  1940.      */
  1941.     public function setSecurityInformation(array $information)
  1942.     {
  1943.         $this->securityInformation $information;
  1944.     }
  1945.     public function getSecurityInformation()
  1946.     {
  1947.         return $this->securityInformation;
  1948.     }
  1949.     /**
  1950.      * Return the list of permissions the user should have in order to display the admin.
  1951.      *
  1952.      * @param string $context
  1953.      *
  1954.      * @return array
  1955.      */
  1956.     public function getPermissionsShow($context)
  1957.     {
  1958.         switch ($context) {
  1959.             case self::CONTEXT_DASHBOARD:
  1960.             case self::CONTEXT_MENU:
  1961.             default:
  1962.                 return ['LIST'];
  1963.         }
  1964.     }
  1965.     public function showIn($context)
  1966.     {
  1967.         switch ($context) {
  1968.             case self::CONTEXT_DASHBOARD:
  1969.             case self::CONTEXT_MENU:
  1970.             default:
  1971.                 return $this->isGranted($this->getPermissionsShow($context));
  1972.         }
  1973.     }
  1974.     public function createObjectSecurity($object)
  1975.     {
  1976.         $this->getSecurityHandler()->createObjectSecurity($this$object);
  1977.     }
  1978.     public function setSecurityHandler(SecurityHandlerInterface $securityHandler)
  1979.     {
  1980.         $this->securityHandler $securityHandler;
  1981.     }
  1982.     public function getSecurityHandler()
  1983.     {
  1984.         return $this->securityHandler;
  1985.     }
  1986.     public function isGranted($name$object null)
  1987.     {
  1988.         $key md5(json_encode($name).($object '/'.spl_object_hash($object) : ''));
  1989.         if (!\array_key_exists($key$this->cacheIsGranted)) {
  1990.             $this->cacheIsGranted[$key] = $this->securityHandler->isGranted($this$name$object ?: $this);
  1991.         }
  1992.         return $this->cacheIsGranted[$key];
  1993.     }
  1994.     public function getUrlsafeIdentifier($entity)
  1995.     {
  1996.         return $this->getModelManager()->getUrlsafeIdentifier($entity);
  1997.     }
  1998.     public function getNormalizedIdentifier($entity)
  1999.     {
  2000.         return $this->getModelManager()->getNormalizedIdentifier($entity);
  2001.     }
  2002.     public function id($entity)
  2003.     {
  2004.         return $this->getNormalizedIdentifier($entity);
  2005.     }
  2006.     public function setValidator($validator)
  2007.     {
  2008.         // NEXT_MAJOR: Move ValidatorInterface check to method signature
  2009.         if (!$validator instanceof ValidatorInterface) {
  2010.             throw new \InvalidArgumentException(
  2011.                 'Argument 1 must be an instance of Symfony\Component\Validator\Validator\ValidatorInterface'
  2012.             );
  2013.         }
  2014.         $this->validator $validator;
  2015.     }
  2016.     public function getValidator()
  2017.     {
  2018.         return $this->validator;
  2019.     }
  2020.     public function getShow()
  2021.     {
  2022.         $this->buildShow();
  2023.         return $this->show;
  2024.     }
  2025.     public function setFormTheme(array $formTheme)
  2026.     {
  2027.         $this->formTheme $formTheme;
  2028.     }
  2029.     public function getFormTheme()
  2030.     {
  2031.         return $this->formTheme;
  2032.     }
  2033.     public function setFilterTheme(array $filterTheme)
  2034.     {
  2035.         $this->filterTheme $filterTheme;
  2036.     }
  2037.     public function getFilterTheme()
  2038.     {
  2039.         return $this->filterTheme;
  2040.     }
  2041.     public function addExtension(AdminExtensionInterface $extension)
  2042.     {
  2043.         $this->extensions[] = $extension;
  2044.     }
  2045.     public function getExtensions()
  2046.     {
  2047.         return $this->extensions;
  2048.     }
  2049.     public function setMenuFactory(MenuFactoryInterface $menuFactory)
  2050.     {
  2051.         $this->menuFactory $menuFactory;
  2052.     }
  2053.     public function getMenuFactory()
  2054.     {
  2055.         return $this->menuFactory;
  2056.     }
  2057.     public function setRouteBuilder(RouteBuilderInterface $routeBuilder)
  2058.     {
  2059.         $this->routeBuilder $routeBuilder;
  2060.     }
  2061.     public function getRouteBuilder()
  2062.     {
  2063.         return $this->routeBuilder;
  2064.     }
  2065.     public function toString($object)
  2066.     {
  2067.         if (!\is_object($object)) {
  2068.             return '';
  2069.         }
  2070.         if (method_exists($object'__toString') && null !== $object->__toString()) {
  2071.             return (string) $object;
  2072.         }
  2073.         return sprintf('%s:%s'ClassUtils::getClass($object), spl_object_hash($object));
  2074.     }
  2075.     public function setLabelTranslatorStrategy(LabelTranslatorStrategyInterface $labelTranslatorStrategy)
  2076.     {
  2077.         $this->labelTranslatorStrategy $labelTranslatorStrategy;
  2078.     }
  2079.     public function getLabelTranslatorStrategy()
  2080.     {
  2081.         return $this->labelTranslatorStrategy;
  2082.     }
  2083.     public function supportsPreviewMode()
  2084.     {
  2085.         return $this->supportsPreviewMode;
  2086.     }
  2087.     /**
  2088.      * Set custom per page options.
  2089.      */
  2090.     public function setPerPageOptions(array $options)
  2091.     {
  2092.         $this->perPageOptions $options;
  2093.     }
  2094.     /**
  2095.      * Returns predefined per page options.
  2096.      *
  2097.      * @return array
  2098.      */
  2099.     public function getPerPageOptions()
  2100.     {
  2101.         return $this->perPageOptions;
  2102.     }
  2103.     /**
  2104.      * Set pager type.
  2105.      *
  2106.      * @param string $pagerType
  2107.      */
  2108.     public function setPagerType($pagerType)
  2109.     {
  2110.         $this->pagerType $pagerType;
  2111.     }
  2112.     /**
  2113.      * Get pager type.
  2114.      *
  2115.      * @return string
  2116.      */
  2117.     public function getPagerType()
  2118.     {
  2119.         return $this->pagerType;
  2120.     }
  2121.     /**
  2122.      * Returns true if the per page value is allowed, false otherwise.
  2123.      *
  2124.      * @param int $perPage
  2125.      *
  2126.      * @return bool
  2127.      */
  2128.     public function determinedPerPageValue($perPage)
  2129.     {
  2130.         return \in_array($perPage$this->perPageOptionstrue);
  2131.     }
  2132.     public function isAclEnabled()
  2133.     {
  2134.         return $this->getSecurityHandler() instanceof AclSecurityHandlerInterface;
  2135.     }
  2136.     public function getObjectMetadata($object)
  2137.     {
  2138.         return new Metadata($this->toString($object));
  2139.     }
  2140.     public function getListModes()
  2141.     {
  2142.         return $this->listModes;
  2143.     }
  2144.     public function setListMode($mode)
  2145.     {
  2146.         if (!$this->hasRequest()) {
  2147.             throw new \RuntimeException(sprintf('No request attached to the current admin: %s'$this->getCode()));
  2148.         }
  2149.         $this->getRequest()->getSession()->set(sprintf('%s.list_mode'$this->getCode()), $mode);
  2150.     }
  2151.     public function getListMode()
  2152.     {
  2153.         if (!$this->hasRequest()) {
  2154.             return 'list';
  2155.         }
  2156.         return $this->getRequest()->getSession()->get(sprintf('%s.list_mode'$this->getCode()), 'list');
  2157.     }
  2158.     public function getAccessMapping()
  2159.     {
  2160.         return $this->accessMapping;
  2161.     }
  2162.     public function checkAccess($action$object null)
  2163.     {
  2164.         $access $this->getAccess();
  2165.         if (!\array_key_exists($action$access)) {
  2166.             throw new \InvalidArgumentException(sprintf(
  2167.                 'Action "%s" could not be found in access mapping.'
  2168.                 .' Please make sure your action is defined into your admin class accessMapping property.',
  2169.                 $action
  2170.             ));
  2171.         }
  2172.         if (!\is_array($access[$action])) {
  2173.             $access[$action] = [$access[$action]];
  2174.         }
  2175.         foreach ($access[$action] as $role) {
  2176.             if (false === $this->isGranted($role$object)) {
  2177.                 throw new AccessDeniedException(sprintf('Access Denied to the action %s and role %s'$action$role));
  2178.             }
  2179.         }
  2180.     }
  2181.     /**
  2182.      * Hook to handle access authorization, without throw Exception.
  2183.      *
  2184.      * @param string $action
  2185.      * @param object $object
  2186.      *
  2187.      * @return bool
  2188.      */
  2189.     public function hasAccess($action$object null)
  2190.     {
  2191.         $access $this->getAccess();
  2192.         if (!\array_key_exists($action$access)) {
  2193.             return false;
  2194.         }
  2195.         if (!\is_array($access[$action])) {
  2196.             $access[$action] = [$access[$action]];
  2197.         }
  2198.         foreach ($access[$action] as $role) {
  2199.             if (false === $this->isGranted($role$object)) {
  2200.                 return false;
  2201.             }
  2202.         }
  2203.         return true;
  2204.     }
  2205.     /**
  2206.      * @param string      $action
  2207.      * @param object|null $object
  2208.      *
  2209.      * @return array
  2210.      */
  2211.     public function configureActionButtons($action$object null)
  2212.     {
  2213.         $list = [];
  2214.         if (\in_array($action, ['tree''show''edit''delete''list''batch'], true)
  2215.             && $this->hasAccess('create')
  2216.             && $this->hasRoute('create')
  2217.         ) {
  2218.             $list['create'] = [
  2219.                 // NEXT_MAJOR: Remove this line and use commented line below it instead
  2220.                 'template' => $this->getTemplate('button_create'),
  2221. //                'template' => $this->getTemplateRegistry()->getTemplate('button_create'),
  2222.             ];
  2223.         }
  2224.         if (\in_array($action, ['show''delete''acl''history'], true)
  2225.             && $this->canAccessObject('edit'$object)
  2226.             && $this->hasRoute('edit')
  2227.         ) {
  2228.             $list['edit'] = [
  2229.                 // NEXT_MAJOR: Remove this line and use commented line below it instead
  2230.                 'template' => $this->getTemplate('button_edit'),
  2231.                 //'template' => $this->getTemplateRegistry()->getTemplate('button_edit'),
  2232.             ];
  2233.         }
  2234.         if (\in_array($action, ['show''edit''acl'], true)
  2235.             && $this->canAccessObject('history'$object)
  2236.             && $this->hasRoute('history')
  2237.         ) {
  2238.             $list['history'] = [
  2239.                 // NEXT_MAJOR: Remove this line and use commented line below it instead
  2240.                 'template' => $this->getTemplate('button_history'),
  2241.                 // 'template' => $this->getTemplateRegistry()->getTemplate('button_history'),
  2242.             ];
  2243.         }
  2244.         if (\in_array($action, ['edit''history'], true)
  2245.             && $this->isAclEnabled()
  2246.             && $this->canAccessObject('acl'$object)
  2247.             && $this->hasRoute('acl')
  2248.         ) {
  2249.             $list['acl'] = [
  2250.                 // NEXT_MAJOR: Remove this line and use commented line below it instead
  2251.                 'template' => $this->getTemplate('button_acl'),
  2252.                 // 'template' => $this->getTemplateRegistry()->getTemplate('button_acl'),
  2253.             ];
  2254.         }
  2255.         if (\in_array($action, ['edit''history''acl'], true)
  2256.             && $this->canAccessObject('show'$object)
  2257.             && \count($this->getShow()) > 0
  2258.             && $this->hasRoute('show')
  2259.         ) {
  2260.             $list['show'] = [
  2261.                 // NEXT_MAJOR: Remove this line and use commented line below it instead
  2262.                 'template' => $this->getTemplate('button_show'),
  2263.                 // 'template' => $this->getTemplateRegistry()->getTemplate('button_show'),
  2264.             ];
  2265.         }
  2266.         if (\in_array($action, ['show''edit''delete''acl''batch'], true)
  2267.             && $this->hasAccess('list')
  2268.             && $this->hasRoute('list')
  2269.         ) {
  2270.             $list['list'] = [
  2271.                 // NEXT_MAJOR: Remove this line and use commented line below it instead
  2272.                 'template' => $this->getTemplate('button_list'),
  2273.                 // 'template' => $this->getTemplateRegistry()->getTemplate('button_list'),
  2274.             ];
  2275.         }
  2276.         return $list;
  2277.     }
  2278.     /**
  2279.      * @param string $action
  2280.      * @param object $object
  2281.      *
  2282.      * @return array
  2283.      */
  2284.     public function getActionButtons($action$object null)
  2285.     {
  2286.         $list $this->configureActionButtons($action$object);
  2287.         foreach ($this->getExtensions() as $extension) {
  2288.             // TODO: remove method check in next major release
  2289.             if (method_exists($extension'configureActionButtons')) {
  2290.                 $list $extension->configureActionButtons($this$list$action$object);
  2291.             }
  2292.         }
  2293.         return $list;
  2294.     }
  2295.     /**
  2296.      * Get the list of actions that can be accessed directly from the dashboard.
  2297.      *
  2298.      * @return array
  2299.      */
  2300.     public function getDashboardActions()
  2301.     {
  2302.         $actions = [];
  2303.         if ($this->hasRoute('create') && $this->hasAccess('create')) {
  2304.             $actions['create'] = [
  2305.                 'label' => 'link_add',
  2306.                 'translation_domain' => 'SonataAdminBundle',
  2307.                 // NEXT_MAJOR: Remove this line and use commented line below it instead
  2308.                 'template' => $this->getTemplate('action_create'),
  2309.                 // 'template' => $this->getTemplateRegistry()->getTemplate('action_create'),
  2310.                 'url' => $this->generateUrl('create'),
  2311.                 'icon' => 'plus-circle',
  2312.             ];
  2313.         }
  2314.         if ($this->hasRoute('list') && $this->hasAccess('list')) {
  2315.             $actions['list'] = [
  2316.                 'label' => 'link_list',
  2317.                 'translation_domain' => 'SonataAdminBundle',
  2318.                 'url' => $this->generateUrl('list'),
  2319.                 'icon' => 'list',
  2320.             ];
  2321.         }
  2322.         return $actions;
  2323.     }
  2324.     /**
  2325.      * Setting to true will enable mosaic button for the admin screen.
  2326.      * Setting to false will hide mosaic button for the admin screen.
  2327.      *
  2328.      * @param bool $isShown
  2329.      */
  2330.     final public function showMosaicButton($isShown)
  2331.     {
  2332.         if ($isShown) {
  2333.             $this->listModes['mosaic'] = ['class' => static::MOSAIC_ICON_CLASS];
  2334.         } else {
  2335.             unset($this->listModes['mosaic']);
  2336.         }
  2337.     }
  2338.     /**
  2339.      * @param object $object
  2340.      */
  2341.     final public function getSearchResultLink($object)
  2342.     {
  2343.         foreach ($this->searchResultActions as $action) {
  2344.             if ($this->hasRoute($action) && $this->hasAccess($action$object)) {
  2345.                 return $this->generateObjectUrl($action$object);
  2346.             }
  2347.         }
  2348.     }
  2349.     /**
  2350.      * Checks if a filter type is set to a default value.
  2351.      *
  2352.      * @param string $name
  2353.      *
  2354.      * @return bool
  2355.      */
  2356.     final public function isDefaultFilter($name)
  2357.     {
  2358.         $filter $this->getFilterParameters();
  2359.         $default $this->getDefaultFilterValues();
  2360.         if (!\array_key_exists($name$filter) || !\array_key_exists($name$default)) {
  2361.             return false;
  2362.         }
  2363.         return $filter[$name] === $default[$name];
  2364.     }
  2365.     /**
  2366.      * Check object existence and access, without throw Exception.
  2367.      *
  2368.      * @param string $action
  2369.      * @param object $object
  2370.      *
  2371.      * @return bool
  2372.      */
  2373.     public function canAccessObject($action$object)
  2374.     {
  2375.         return $object && $this->id($object) && $this->hasAccess($action$object);
  2376.     }
  2377.     /**
  2378.      * @return MutableTemplateRegistryInterface
  2379.      */
  2380.     final protected function getTemplateRegistry()
  2381.     {
  2382.         return $this->templateRegistry;
  2383.     }
  2384.     /**
  2385.      * Returns a list of default filters.
  2386.      *
  2387.      * @return array
  2388.      */
  2389.     final protected function getDefaultFilterValues()
  2390.     {
  2391.         $defaultFilterValues = [];
  2392.         $this->configureDefaultFilterValues($defaultFilterValues);
  2393.         foreach ($this->getExtensions() as $extension) {
  2394.             // NEXT_MAJOR: remove method check in next major release
  2395.             if (method_exists($extension'configureDefaultFilterValues')) {
  2396.                 $extension->configureDefaultFilterValues($this$defaultFilterValues);
  2397.             }
  2398.         }
  2399.         return $defaultFilterValues;
  2400.     }
  2401.     protected function configureFormFields(FormMapper $form)
  2402.     {
  2403.     }
  2404.     protected function configureListFields(ListMapper $list)
  2405.     {
  2406.     }
  2407.     protected function configureDatagridFilters(DatagridMapper $filter)
  2408.     {
  2409.     }
  2410.     protected function configureShowFields(ShowMapper $show)
  2411.     {
  2412.     }
  2413.     protected function configureRoutes(RouteCollection $collection)
  2414.     {
  2415.     }
  2416.     /**
  2417.      * Allows you to customize batch actions.
  2418.      *
  2419.      * @param array $actions List of actions
  2420.      *
  2421.      * @return array
  2422.      */
  2423.     protected function configureBatchActions($actions)
  2424.     {
  2425.         return $actions;
  2426.     }
  2427.     /**
  2428.      * NEXT_MAJOR: remove this method.
  2429.      *
  2430.      * @deprecated Use configureTabMenu instead
  2431.      */
  2432.     protected function configureSideMenu(MenuItemInterface $menu$actionAdminInterface $childAdmin null)
  2433.     {
  2434.     }
  2435.     /**
  2436.      * Configures the tab menu in your admin.
  2437.      *
  2438.      * @param string $action
  2439.      */
  2440.     protected function configureTabMenu(MenuItemInterface $menu$actionAdminInterface $childAdmin null)
  2441.     {
  2442.         // Use configureSideMenu not to mess with previous overrides
  2443.         // TODO remove once deprecation period is over
  2444.         $this->configureSideMenu($menu$action$childAdmin);
  2445.     }
  2446.     /**
  2447.      * build the view FieldDescription array.
  2448.      */
  2449.     protected function buildShow()
  2450.     {
  2451.         if ($this->show) {
  2452.             return;
  2453.         }
  2454.         $this->show = new FieldDescriptionCollection();
  2455.         $mapper = new ShowMapper($this->showBuilder$this->show$this);
  2456.         $this->configureShowFields($mapper);
  2457.         foreach ($this->getExtensions() as $extension) {
  2458.             $extension->configureShowFields($mapper);
  2459.         }
  2460.     }
  2461.     /**
  2462.      * build the list FieldDescription array.
  2463.      */
  2464.     protected function buildList()
  2465.     {
  2466.         if ($this->list) {
  2467.             return;
  2468.         }
  2469.         $this->list $this->getListBuilder()->getBaseList();
  2470.         $mapper = new ListMapper($this->getListBuilder(), $this->list$this);
  2471.         if (\count($this->getBatchActions()) > && $this->hasRequest() && !$this->getRequest()->isXmlHttpRequest()) {
  2472.             $fieldDescription $this->getModelManager()->getNewFieldDescriptionInstance(
  2473.                 $this->getClass(),
  2474.                 'batch',
  2475.                 [
  2476.                     'label' => 'batch',
  2477.                     'code' => '_batch',
  2478.                     'sortable' => false,
  2479.                     'virtual_field' => true,
  2480.                 ]
  2481.             );
  2482.             $fieldDescription->setAdmin($this);
  2483.             // NEXT_MAJOR: Remove this line and use commented line below it instead
  2484.             $fieldDescription->setTemplate($this->getTemplate('batch'));
  2485.             // $fieldDescription->setTemplate($this->getTemplateRegistry()->getTemplate('batch'));
  2486.             $mapper->add($fieldDescription'batch');
  2487.         }
  2488.         $this->configureListFields($mapper);
  2489.         foreach ($this->getExtensions() as $extension) {
  2490.             $extension->configureListFields($mapper);
  2491.         }
  2492.         if ($this->hasRequest() && $this->getRequest()->isXmlHttpRequest()) {
  2493.             $fieldDescription $this->getModelManager()->getNewFieldDescriptionInstance(
  2494.                 $this->getClass(),
  2495.                 'select',
  2496.                 [
  2497.                     'label' => false,
  2498.                     'code' => '_select',
  2499.                     'sortable' => false,
  2500.                     'virtual_field' => false,
  2501.                 ]
  2502.             );
  2503.             $fieldDescription->setAdmin($this);
  2504.             // NEXT_MAJOR: Remove this line and use commented line below it instead
  2505.             $fieldDescription->setTemplate($this->getTemplate('select'));
  2506.             // $fieldDescription->setTemplate($this->getTemplateRegistry()->getTemplate('select'));
  2507.             $mapper->add($fieldDescription'select');
  2508.         }
  2509.     }
  2510.     /**
  2511.      * Build the form FieldDescription collection.
  2512.      */
  2513.     protected function buildForm()
  2514.     {
  2515.         if ($this->form) {
  2516.             return;
  2517.         }
  2518.         // append parent object if any
  2519.         // todo : clean the way the Admin class can retrieve set the object
  2520.         if ($this->isChild() && $this->getParentAssociationMapping()) {
  2521.             $parent $this->getParent()->getObject($this->request->get($this->getParent()->getIdParameter()));
  2522.             $propertyAccessor $this->getConfigurationPool()->getPropertyAccessor();
  2523.             $propertyPath = new PropertyPath($this->getParentAssociationMapping());
  2524.             $object $this->getSubject();
  2525.             $value $propertyAccessor->getValue($object$propertyPath);
  2526.             if (\is_array($value) || $value instanceof \ArrayAccess) {
  2527.                 $value[] = $parent;
  2528.                 $propertyAccessor->setValue($object$propertyPath$value);
  2529.             } else {
  2530.                 $propertyAccessor->setValue($object$propertyPath$parent);
  2531.             }
  2532.         }
  2533.         $formBuilder $this->getFormBuilder();
  2534.         $formBuilder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  2535.             $this->preValidate($event->getData());
  2536.         }, 100);
  2537.         $this->form $formBuilder->getForm();
  2538.     }
  2539.     /**
  2540.      * Gets the subclass corresponding to the given name.
  2541.      *
  2542.      * @param string $name The name of the sub class
  2543.      *
  2544.      * @return string the subclass
  2545.      */
  2546.     protected function getSubClass($name)
  2547.     {
  2548.         if ($this->hasSubClass($name)) {
  2549.             return $this->subClasses[$name];
  2550.         }
  2551.         throw new \RuntimeException(sprintf(
  2552.             'Unable to find the subclass `%s` for admin `%s`',
  2553.             $name,
  2554.             static::class
  2555.         ));
  2556.     }
  2557.     /**
  2558.      * Attach the inline validator to the model metadata, this must be done once per admin.
  2559.      */
  2560.     protected function attachInlineValidator()
  2561.     {
  2562.         $admin $this;
  2563.         // add the custom inline validation option
  2564.         $metadata $this->validator->getMetadataFor($this->getClass());
  2565.         $metadata->addConstraint(new InlineConstraint([
  2566.             'service' => $this,
  2567.             'method' => static function (ErrorElement $errorElement$object) use ($admin) {
  2568.                 /* @var \Sonata\AdminBundle\Admin\AdminInterface $admin */
  2569.                 // This avoid the main validation to be cascaded to children
  2570.                 // The problem occurs when a model Page has a collection of Page as property
  2571.                 if ($admin->hasSubject() && spl_object_hash($object) !== spl_object_hash($admin->getSubject())) {
  2572.                     return;
  2573.                 }
  2574.                 $admin->validate($errorElement$object);
  2575.                 foreach ($admin->getExtensions() as $extension) {
  2576.                     $extension->validate($admin$errorElement$object);
  2577.                 }
  2578.             },
  2579.             'serializingWarning' => true,
  2580.         ]));
  2581.     }
  2582.     /**
  2583.      * Predefine per page options.
  2584.      */
  2585.     protected function predefinePerPageOptions()
  2586.     {
  2587.         array_unshift($this->perPageOptions$this->maxPerPage);
  2588.         $this->perPageOptions array_unique($this->perPageOptions);
  2589.         sort($this->perPageOptions);
  2590.     }
  2591.     /**
  2592.      * Return list routes with permissions name.
  2593.      *
  2594.      * @return array<string, string>
  2595.      */
  2596.     protected function getAccess()
  2597.     {
  2598.         $access array_merge([
  2599.             'acl' => 'MASTER',
  2600.             'export' => 'EXPORT',
  2601.             'historyCompareRevisions' => 'EDIT',
  2602.             'historyViewRevision' => 'EDIT',
  2603.             'history' => 'EDIT',
  2604.             'edit' => 'EDIT',
  2605.             'show' => 'VIEW',
  2606.             'create' => 'CREATE',
  2607.             'delete' => 'DELETE',
  2608.             'batchDelete' => 'DELETE',
  2609.             'list' => 'LIST',
  2610.         ], $this->getAccessMapping());
  2611.         foreach ($this->extensions as $extension) {
  2612.             // TODO: remove method check in next major release
  2613.             if (method_exists($extension'getAccessMapping')) {
  2614.                 $access array_merge($access$extension->getAccessMapping($this));
  2615.             }
  2616.         }
  2617.         return $access;
  2618.     }
  2619.     /**
  2620.      * Configures a list of default filters.
  2621.      */
  2622.     protected function configureDefaultFilterValues(array &$filterValues)
  2623.     {
  2624.     }
  2625.     /**
  2626.      * Build all the related urls to the current admin.
  2627.      */
  2628.     private function buildRoutes(): void
  2629.     {
  2630.         if ($this->loaded['routes']) {
  2631.             return;
  2632.         }
  2633.         $this->loaded['routes'] = true;
  2634.         $this->routes = new RouteCollection(
  2635.             $this->getBaseCodeRoute(),
  2636.             $this->getBaseRouteName(),
  2637.             $this->getBaseRoutePattern(),
  2638.             $this->getBaseControllerName()
  2639.         );
  2640.         $this->routeBuilder->build($this$this->routes);
  2641.         $this->configureRoutes($this->routes);
  2642.         foreach ($this->getExtensions() as $extension) {
  2643.             $extension->configureRoutes($this$this->routes);
  2644.         }
  2645.     }
  2646. }
  2647. class_exists(\Sonata\Form\Validator\ErrorElement::class);