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

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 <thomas.rabaix@sonata-project.org>
  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 <thomas.rabaix@sonata-project.org>
  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;
  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.     }
  1194.     public function buildSideMenu($actionAdminInterface $childAdmin null)
  1195.     {
  1196.         return $this->buildTabMenu($action$childAdmin);
  1197.     }
  1198.     /**
  1199.      * @param string $action
  1200.      *
  1201.      * @return ItemInterface
  1202.      */
  1203.     public function getSideMenu($actionAdminInterface $childAdmin null)
  1204.     {
  1205.         if ($this->isChild()) {
  1206.             return $this->getParent()->getSideMenu($action$this);
  1207.         }
  1208.         $this->buildSideMenu($action$childAdmin);
  1209.         return $this->menu;
  1210.     }
  1211.     /**
  1212.      * Returns the root code.
  1213.      *
  1214.      * @return string the root code
  1215.      */
  1216.     public function getRootCode()
  1217.     {
  1218.         return $this->getRoot()->getCode();
  1219.     }
  1220.     /**
  1221.      * Returns the master admin.
  1222.      *
  1223.      * @return AbstractAdmin the root admin class
  1224.      */
  1225.     public function getRoot()
  1226.     {
  1227.         $parentFieldDescription $this->getParentFieldDescription();
  1228.         if (!$parentFieldDescription) {
  1229.             return $this;
  1230.         }
  1231.         return $parentFieldDescription->getAdmin()->getRoot();
  1232.     }
  1233.     public function setBaseControllerName($baseControllerName)
  1234.     {
  1235.         $this->baseControllerName $baseControllerName;
  1236.     }
  1237.     public function getBaseControllerName()
  1238.     {
  1239.         return $this->baseControllerName;
  1240.     }
  1241.     /**
  1242.      * @param string $label
  1243.      */
  1244.     public function setLabel($label)
  1245.     {
  1246.         $this->label $label;
  1247.     }
  1248.     public function getLabel()
  1249.     {
  1250.         return $this->label;
  1251.     }
  1252.     /**
  1253.      * @param bool $persist
  1254.      *
  1255.      * NEXT_MAJOR: remove this method
  1256.      *
  1257.      * @deprecated since 3.34, to be removed in 4.0.
  1258.      */
  1259.     public function setPersistFilters($persist)
  1260.     {
  1261.         @trigger_error(
  1262.             'The '.__METHOD__.' method is deprecated since version 3.34 and will be removed in 4.0.',
  1263.             E_USER_DEPRECATED
  1264.         );
  1265.         $this->persistFilters $persist;
  1266.     }
  1267.     public function setFilterPersister(FilterPersisterInterface $filterPersister null)
  1268.     {
  1269.         $this->filterPersister $filterPersister;
  1270.         // NEXT_MAJOR remove the deprecated property will be removed. Needed for persisted filter condition.
  1271.         $this->persistFilters true;
  1272.     }
  1273.     /**
  1274.      * @param int $maxPerPage
  1275.      */
  1276.     public function setMaxPerPage($maxPerPage)
  1277.     {
  1278.         $this->maxPerPage $maxPerPage;
  1279.     }
  1280.     /**
  1281.      * @return int
  1282.      */
  1283.     public function getMaxPerPage()
  1284.     {
  1285.         return $this->maxPerPage;
  1286.     }
  1287.     /**
  1288.      * @param int $maxPageLinks
  1289.      */
  1290.     public function setMaxPageLinks($maxPageLinks)
  1291.     {
  1292.         $this->maxPageLinks $maxPageLinks;
  1293.     }
  1294.     /**
  1295.      * @return int
  1296.      */
  1297.     public function getMaxPageLinks()
  1298.     {
  1299.         return $this->maxPageLinks;
  1300.     }
  1301.     public function getFormGroups()
  1302.     {
  1303.         return $this->formGroups;
  1304.     }
  1305.     public function setFormGroups(array $formGroups)
  1306.     {
  1307.         $this->formGroups $formGroups;
  1308.     }
  1309.     public function removeFieldFromFormGroup($key)
  1310.     {
  1311.         foreach ($this->formGroups as $name => $formGroup) {
  1312.             unset($this->formGroups[$name]['fields'][$key]);
  1313.             if (empty($this->formGroups[$name]['fields'])) {
  1314.                 unset($this->formGroups[$name]);
  1315.             }
  1316.         }
  1317.     }
  1318.     /**
  1319.      * @param array $group
  1320.      */
  1321.     public function reorderFormGroup($group, array $keys)
  1322.     {
  1323.         $formGroups $this->getFormGroups();
  1324.         $formGroups[$group]['fields'] = array_merge(array_flip($keys), $formGroups[$group]['fields']);
  1325.         $this->setFormGroups($formGroups);
  1326.     }
  1327.     public function getFormTabs()
  1328.     {
  1329.         return $this->formTabs;
  1330.     }
  1331.     public function setFormTabs(array $formTabs)
  1332.     {
  1333.         $this->formTabs $formTabs;
  1334.     }
  1335.     public function getShowTabs()
  1336.     {
  1337.         return $this->showTabs;
  1338.     }
  1339.     public function setShowTabs(array $showTabs)
  1340.     {
  1341.         $this->showTabs $showTabs;
  1342.     }
  1343.     public function getShowGroups()
  1344.     {
  1345.         return $this->showGroups;
  1346.     }
  1347.     public function setShowGroups(array $showGroups)
  1348.     {
  1349.         $this->showGroups $showGroups;
  1350.     }
  1351.     public function reorderShowGroup($group, array $keys)
  1352.     {
  1353.         $showGroups $this->getShowGroups();
  1354.         $showGroups[$group]['fields'] = array_merge(array_flip($keys), $showGroups[$group]['fields']);
  1355.         $this->setShowGroups($showGroups);
  1356.     }
  1357.     public function setParentFieldDescription(FieldDescriptionInterface $parentFieldDescription)
  1358.     {
  1359.         $this->parentFieldDescription $parentFieldDescription;
  1360.     }
  1361.     public function getParentFieldDescription()
  1362.     {
  1363.         return $this->parentFieldDescription;
  1364.     }
  1365.     public function hasParentFieldDescription()
  1366.     {
  1367.         return $this->parentFieldDescription instanceof FieldDescriptionInterface;
  1368.     }
  1369.     public function setSubject($subject)
  1370.     {
  1371.         if (\is_object($subject) && !is_a($subject$this->getClass(), true)) {
  1372.             $message = <<<'EOT'
  1373. You are trying to set entity an instance of "%s",
  1374. which is not the one registered with this admin class ("%s").
  1375. This is deprecated since 3.5 and will no longer be supported in 4.0.
  1376. EOT;
  1377.             @trigger_error(
  1378.                 sprintf($message, \get_class($subject), $this->getClass()),
  1379.                 E_USER_DEPRECATED
  1380.             ); // NEXT_MAJOR : throw an exception instead
  1381.         }
  1382.         $this->subject $subject;
  1383.     }
  1384.     public function getSubject()
  1385.     {
  1386.         if (null === $this->subject && $this->request && !$this->hasParentFieldDescription()) {
  1387.             $id $this->request->get($this->getIdParameter());
  1388.             if (null !== $id) {
  1389.                 $this->subject $this->getObject($id);
  1390.             }
  1391.         }
  1392.         return $this->subject;
  1393.     }
  1394.     public function hasSubject()
  1395.     {
  1396.         return (bool) $this->getSubject();
  1397.     }
  1398.     public function getFormFieldDescriptions()
  1399.     {
  1400.         $this->buildForm();
  1401.         return $this->formFieldDescriptions;
  1402.     }
  1403.     public function getFormFieldDescription($name)
  1404.     {
  1405.         return $this->hasFormFieldDescription($name) ? $this->formFieldDescriptions[$name] : null;
  1406.     }
  1407.     /**
  1408.      * Returns true if the admin has a FieldDescription with the given $name.
  1409.      *
  1410.      * @param string $name
  1411.      *
  1412.      * @return bool
  1413.      */
  1414.     public function hasFormFieldDescription($name)
  1415.     {
  1416.         return \array_key_exists($name$this->formFieldDescriptions) ? true false;
  1417.     }
  1418.     public function addFormFieldDescription($nameFieldDescriptionInterface $fieldDescription)
  1419.     {
  1420.         $this->formFieldDescriptions[$name] = $fieldDescription;
  1421.     }
  1422.     /**
  1423.      * remove a FieldDescription.
  1424.      *
  1425.      * @param string $name
  1426.      */
  1427.     public function removeFormFieldDescription($name)
  1428.     {
  1429.         unset($this->formFieldDescriptions[$name]);
  1430.     }
  1431.     /**
  1432.      * build and return the collection of form FieldDescription.
  1433.      *
  1434.      * @return array collection of form FieldDescription
  1435.      */
  1436.     public function getShowFieldDescriptions()
  1437.     {
  1438.         $this->buildShow();
  1439.         return $this->showFieldDescriptions;
  1440.     }
  1441.     /**
  1442.      * Returns the form FieldDescription with the given $name.
  1443.      *
  1444.      * @param string $name
  1445.      *
  1446.      * @return FieldDescriptionInterface
  1447.      */
  1448.     public function getShowFieldDescription($name)
  1449.     {
  1450.         $this->buildShow();
  1451.         return $this->hasShowFieldDescription($name) ? $this->showFieldDescriptions[$name] : null;
  1452.     }
  1453.     public function hasShowFieldDescription($name)
  1454.     {
  1455.         return \array_key_exists($name$this->showFieldDescriptions);
  1456.     }
  1457.     public function addShowFieldDescription($nameFieldDescriptionInterface $fieldDescription)
  1458.     {
  1459.         $this->showFieldDescriptions[$name] = $fieldDescription;
  1460.     }
  1461.     public function removeShowFieldDescription($name)
  1462.     {
  1463.         unset($this->showFieldDescriptions[$name]);
  1464.     }
  1465.     public function getListFieldDescriptions()
  1466.     {
  1467.         $this->buildList();
  1468.         return $this->listFieldDescriptions;
  1469.     }
  1470.     public function getListFieldDescription($name)
  1471.     {
  1472.         return $this->hasListFieldDescription($name) ? $this->listFieldDescriptions[$name] : null;
  1473.     }
  1474.     public function hasListFieldDescription($name)
  1475.     {
  1476.         $this->buildList();
  1477.         return \array_key_exists($name$this->listFieldDescriptions) ? true false;
  1478.     }
  1479.     public function addListFieldDescription($nameFieldDescriptionInterface $fieldDescription)
  1480.     {
  1481.         $this->listFieldDescriptions[$name] = $fieldDescription;
  1482.     }
  1483.     public function removeListFieldDescription($name)
  1484.     {
  1485.         unset($this->listFieldDescriptions[$name]);
  1486.     }
  1487.     public function getFilterFieldDescription($name)
  1488.     {
  1489.         return $this->hasFilterFieldDescription($name) ? $this->filterFieldDescriptions[$name] : null;
  1490.     }
  1491.     public function hasFilterFieldDescription($name)
  1492.     {
  1493.         return \array_key_exists($name$this->filterFieldDescriptions) ? true false;
  1494.     }
  1495.     public function addFilterFieldDescription($nameFieldDescriptionInterface $fieldDescription)
  1496.     {
  1497.         $this->filterFieldDescriptions[$name] = $fieldDescription;
  1498.     }
  1499.     public function removeFilterFieldDescription($name)
  1500.     {
  1501.         unset($this->filterFieldDescriptions[$name]);
  1502.     }
  1503.     public function getFilterFieldDescriptions()
  1504.     {
  1505.         $this->buildDatagrid();
  1506.         return $this->filterFieldDescriptions;
  1507.     }
  1508.     public function addChild(AdminInterface $child)
  1509.     {
  1510.         for ($parentAdmin $thisnull !== $parentAdmin$parentAdmin $parentAdmin->getParent()) {
  1511.             if ($parentAdmin->getCode() !== $child->getCode()) {
  1512.                 continue;
  1513.             }
  1514.             throw new \RuntimeException(sprintf(
  1515.                 'Circular reference detected! The child admin `%s` is already in the parent tree of the `%s` admin.',
  1516.                 $child->getCode(), $this->getCode()
  1517.             ));
  1518.         }
  1519.         $this->children[$child->getCode()] = $child;
  1520.         $child->setParent($this);
  1521.         // NEXT_MAJOR: remove $args and add $field parameter to this function on next Major
  1522.         $args = \func_get_args();
  1523.         if (isset($args[1])) {
  1524.             $child->addParentAssociationMapping($this->getCode(), $args[1]);
  1525.         } else {
  1526.             @trigger_error(
  1527.                 'Calling "addChild" without second argument is deprecated since 3.35'
  1528.                 .' and will not be allowed in 4.0.',
  1529.                 E_USER_DEPRECATED
  1530.             );
  1531.         }
  1532.     }
  1533.     public function hasChild($code)
  1534.     {
  1535.         return isset($this->children[$code]);
  1536.     }
  1537.     public function getChildren()
  1538.     {
  1539.         return $this->children;
  1540.     }
  1541.     public function getChild($code)
  1542.     {
  1543.         return $this->hasChild($code) ? $this->children[$code] : null;
  1544.     }
  1545.     public function setParent(AdminInterface $parent)
  1546.     {
  1547.         $this->parent $parent;
  1548.     }
  1549.     public function getParent()
  1550.     {
  1551.         return $this->parent;
  1552.     }
  1553.     final public function getRootAncestor()
  1554.     {
  1555.         $parent $this;
  1556.         while ($parent->isChild()) {
  1557.             $parent $parent->getParent();
  1558.         }
  1559.         return $parent;
  1560.     }
  1561.     final public function getChildDepth()
  1562.     {
  1563.         $parent $this;
  1564.         $depth 0;
  1565.         while ($parent->isChild()) {
  1566.             $parent $parent->getParent();
  1567.             ++$depth;
  1568.         }
  1569.         return $depth;
  1570.     }
  1571.     final public function getCurrentLeafChildAdmin()
  1572.     {
  1573.         $child $this->getCurrentChildAdmin();
  1574.         if (null === $child) {
  1575.             return null;
  1576.         }
  1577.         for ($c $childnull !== $c$c $child->getCurrentChildAdmin()) {
  1578.             $child $c;
  1579.         }
  1580.         return $child;
  1581.     }
  1582.     public function isChild()
  1583.     {
  1584.         return $this->parent instanceof AdminInterface;
  1585.     }
  1586.     /**
  1587.      * Returns true if the admin has children, false otherwise.
  1588.      *
  1589.      * @return bool if the admin has children
  1590.      */
  1591.     public function hasChildren()
  1592.     {
  1593.         return \count($this->children) > 0;
  1594.     }
  1595.     public function setUniqid($uniqid)
  1596.     {
  1597.         $this->uniqid $uniqid;
  1598.     }
  1599.     public function getUniqid()
  1600.     {
  1601.         if (!$this->uniqid) {
  1602.             $this->uniqid 's'.substr(md5($this->getBaseCodeRoute()), 010);
  1603.         }
  1604.         return $this->uniqid;
  1605.     }
  1606.     /**
  1607.      * Returns the classname label.
  1608.      *
  1609.      * @return string the classname label
  1610.      */
  1611.     public function getClassnameLabel()
  1612.     {
  1613.         return $this->classnameLabel;
  1614.     }
  1615.     public function getPersistentParameters()
  1616.     {
  1617.         $parameters = [];
  1618.         foreach ($this->getExtensions() as $extension) {
  1619.             $params $extension->getPersistentParameters($this);
  1620.             if (!\is_array($params)) {
  1621.                 throw new \RuntimeException(sprintf('The %s::getPersistentParameters must return an array', \get_class($extension)));
  1622.             }
  1623.             $parameters array_merge($parameters$params);
  1624.         }
  1625.         return $parameters;
  1626.     }
  1627.     /**
  1628.      * @param string $name
  1629.      *
  1630.      * @return mixed|null
  1631.      */
  1632.     public function getPersistentParameter($name)
  1633.     {
  1634.         $parameters $this->getPersistentParameters();
  1635.         return $parameters[$name] ?? null;
  1636.     }
  1637.     public function getBreadcrumbs($action)
  1638.     {
  1639.         @trigger_error(
  1640.             'The '.__METHOD__.' method is deprecated since version 3.2 and will be removed in 4.0.'.
  1641.             ' Use Sonata\AdminBundle\Admin\BreadcrumbsBuilder::getBreadcrumbs instead.',
  1642.             E_USER_DEPRECATED
  1643.         );
  1644.         return $this->getBreadcrumbsBuilder()->getBreadcrumbs($this$action);
  1645.     }
  1646.     /**
  1647.      * Generates the breadcrumbs array.
  1648.      *
  1649.      * Note: the method will be called by the top admin instance (parent => child)
  1650.      *
  1651.      * @param string $action
  1652.      *
  1653.      * @return array
  1654.      */
  1655.     public function buildBreadcrumbs($actionMenuItemInterface $menu null)
  1656.     {
  1657.         @trigger_error(
  1658.             'The '.__METHOD__.' method is deprecated since version 3.2 and will be removed in 4.0.',
  1659.             E_USER_DEPRECATED
  1660.         );
  1661.         if (isset($this->breadcrumbs[$action])) {
  1662.             return $this->breadcrumbs[$action];
  1663.         }
  1664.         return $this->breadcrumbs[$action] = $this->getBreadcrumbsBuilder()
  1665.             ->buildBreadcrumbs($this$action$menu);
  1666.     }
  1667.     /**
  1668.      * NEXT_MAJOR : remove this method.
  1669.      *
  1670.      * @return BreadcrumbsBuilderInterface
  1671.      */
  1672.     final public function getBreadcrumbsBuilder()
  1673.     {
  1674.         @trigger_error(
  1675.             'The '.__METHOD__.' method is deprecated since version 3.2 and will be removed in 4.0.'.
  1676.             ' Use the sonata.admin.breadcrumbs_builder service instead.',
  1677.             E_USER_DEPRECATED
  1678.         );
  1679.         if (null === $this->breadcrumbsBuilder) {
  1680.             $this->breadcrumbsBuilder = new BreadcrumbsBuilder(
  1681.                 $this->getConfigurationPool()->getContainer()->getParameter('sonata.admin.configuration.breadcrumbs')
  1682.             );
  1683.         }
  1684.         return $this->breadcrumbsBuilder;
  1685.     }
  1686.     /**
  1687.      * NEXT_MAJOR : remove this method.
  1688.      *
  1689.      * @return AbstractAdmin
  1690.      */
  1691.     final public function setBreadcrumbsBuilder(BreadcrumbsBuilderInterface $value)
  1692.     {
  1693.         @trigger_error(
  1694.             'The '.__METHOD__.' method is deprecated since version 3.2 and will be removed in 4.0.'.
  1695.             ' Use the sonata.admin.breadcrumbs_builder service instead.',
  1696.             E_USER_DEPRECATED
  1697.         );
  1698.         $this->breadcrumbsBuilder $value;
  1699.         return $this;
  1700.     }
  1701.     public function setCurrentChild($currentChild)
  1702.     {
  1703.         $this->currentChild $currentChild;
  1704.     }
  1705.     public function getCurrentChild()
  1706.     {
  1707.         return $this->currentChild;
  1708.     }
  1709.     /**
  1710.      * Returns the current child admin instance.
  1711.      *
  1712.      * @return AdminInterface|null the current child admin instance
  1713.      */
  1714.     public function getCurrentChildAdmin()
  1715.     {
  1716.         foreach ($this->children as $children) {
  1717.             if ($children->getCurrentChild()) {
  1718.                 return $children;
  1719.             }
  1720.         }
  1721.     }
  1722.     public function trans($id, array $parameters = [], $domain null$locale null)
  1723.     {
  1724.         @trigger_error(
  1725.             'The '.__METHOD__.' method is deprecated since version 3.9 and will be removed in 4.0.',
  1726.             E_USER_DEPRECATED
  1727.         );
  1728.         $domain $domain ?: $this->getTranslationDomain();
  1729.         return $this->translator->trans($id$parameters$domain$locale);
  1730.     }
  1731.     /**
  1732.      * Translate a message id.
  1733.      *
  1734.      * NEXT_MAJOR: remove this method
  1735.      *
  1736.      * @param string      $id
  1737.      * @param int         $count
  1738.      * @param string|null $domain
  1739.      * @param string|null $locale
  1740.      *
  1741.      * @return string the translated string
  1742.      *
  1743.      * @deprecated since 3.9, to be removed with 4.0
  1744.      */
  1745.     public function transChoice($id$count, array $parameters = [], $domain null$locale null)
  1746.     {
  1747.         @trigger_error(
  1748.             'The '.__METHOD__.' method is deprecated since version 3.9 and will be removed in 4.0.',
  1749.             E_USER_DEPRECATED
  1750.         );
  1751.         $domain $domain ?: $this->getTranslationDomain();
  1752.         return $this->translator->transChoice($id$count$parameters$domain$locale);
  1753.     }
  1754.     public function setTranslationDomain($translationDomain)
  1755.     {
  1756.         $this->translationDomain $translationDomain;
  1757.     }
  1758.     public function getTranslationDomain()
  1759.     {
  1760.         return $this->translationDomain;
  1761.     }
  1762.     /**
  1763.      * {@inheritdoc}
  1764.      *
  1765.      * NEXT_MAJOR: remove this method
  1766.      *
  1767.      * @deprecated since 3.9, to be removed with 4.0
  1768.      */
  1769.     public function setTranslator(TranslatorInterface $translator)
  1770.     {
  1771.         $args = \func_get_args();
  1772.         if (isset($args[1]) && $args[1]) {
  1773.             @trigger_error(
  1774.                 'The '.__METHOD__.' method is deprecated since version 3.9 and will be removed in 4.0.',
  1775.                 E_USER_DEPRECATED
  1776.             );
  1777.         }
  1778.         $this->translator $translator;
  1779.     }
  1780.     /**
  1781.      * {@inheritdoc}
  1782.      *
  1783.      * NEXT_MAJOR: remove this method
  1784.      *
  1785.      * @deprecated since 3.9, to be removed with 4.0
  1786.      */
  1787.     public function getTranslator()
  1788.     {
  1789.         @trigger_error(
  1790.             'The '.__METHOD__.' method is deprecated since version 3.9 and will be removed in 4.0.',
  1791.             E_USER_DEPRECATED
  1792.         );
  1793.         return $this->translator;
  1794.     }
  1795.     public function getTranslationLabel($label$context ''$type '')
  1796.     {
  1797.         return $this->getLabelTranslatorStrategy()->getLabel($label$context$type);
  1798.     }
  1799.     public function setRequest(Request $request)
  1800.     {
  1801.         $this->request $request;
  1802.         foreach ($this->getChildren() as $children) {
  1803.             $children->setRequest($request);
  1804.         }
  1805.     }
  1806.     public function getRequest()
  1807.     {
  1808.         if (!$this->request) {
  1809.             throw new \RuntimeException('The Request object has not been set');
  1810.         }
  1811.         return $this->request;
  1812.     }
  1813.     public function hasRequest()
  1814.     {
  1815.         return null !== $this->request;
  1816.     }
  1817.     public function setFormContractor(FormContractorInterface $formBuilder)
  1818.     {
  1819.         $this->formContractor $formBuilder;
  1820.     }
  1821.     /**
  1822.      * @return FormContractorInterface
  1823.      */
  1824.     public function getFormContractor()
  1825.     {
  1826.         return $this->formContractor;
  1827.     }
  1828.     public function setDatagridBuilder(DatagridBuilderInterface $datagridBuilder)
  1829.     {
  1830.         $this->datagridBuilder $datagridBuilder;
  1831.     }
  1832.     public function getDatagridBuilder()
  1833.     {
  1834.         return $this->datagridBuilder;
  1835.     }
  1836.     public function setListBuilder(ListBuilderInterface $listBuilder)
  1837.     {
  1838.         $this->listBuilder $listBuilder;
  1839.     }
  1840.     public function getListBuilder()
  1841.     {
  1842.         return $this->listBuilder;
  1843.     }
  1844.     public function setShowBuilder(ShowBuilderInterface $showBuilder)
  1845.     {
  1846.         $this->showBuilder $showBuilder;
  1847.     }
  1848.     /**
  1849.      * @return ShowBuilderInterface
  1850.      */
  1851.     public function getShowBuilder()
  1852.     {
  1853.         return $this->showBuilder;
  1854.     }
  1855.     public function setConfigurationPool(Pool $configurationPool)
  1856.     {
  1857.         $this->configurationPool $configurationPool;
  1858.     }
  1859.     /**
  1860.      * @return Pool
  1861.      */
  1862.     public function getConfigurationPool()
  1863.     {
  1864.         return $this->configurationPool;
  1865.     }
  1866.     public function setRouteGenerator(RouteGeneratorInterface $routeGenerator)
  1867.     {
  1868.         $this->routeGenerator $routeGenerator;
  1869.     }
  1870.     /**
  1871.      * @return RouteGeneratorInterface
  1872.      */
  1873.     public function getRouteGenerator()
  1874.     {
  1875.         return $this->routeGenerator;
  1876.     }
  1877.     public function getCode()
  1878.     {
  1879.         return $this->code;
  1880.     }
  1881.     /**
  1882.      * NEXT_MAJOR: Remove this function.
  1883.      *
  1884.      * @deprecated This method is deprecated since 3.24 and will be removed in 4.0
  1885.      *
  1886.      * @param string $baseCodeRoute
  1887.      */
  1888.     public function setBaseCodeRoute($baseCodeRoute)
  1889.     {
  1890.         @trigger_error(
  1891.             'The '.__METHOD__.' is deprecated since 3.24 and will be removed in 4.0.',
  1892.             E_USER_DEPRECATED
  1893.         );
  1894.         $this->baseCodeRoute $baseCodeRoute;
  1895.     }
  1896.     public function getBaseCodeRoute()
  1897.     {
  1898.         // NEXT_MAJOR: Uncomment the following lines.
  1899.         // if ($this->isChild()) {
  1900.         //     return $this->getParent()->getBaseCodeRoute().'|'.$this->getCode();
  1901.         // }
  1902.         //
  1903.         // return $this->getCode();
  1904.         // NEXT_MAJOR: Remove all the code below.
  1905.         if ($this->isChild()) {
  1906.             $parentCode $this->getParent()->getCode();
  1907.             if ($this->getParent()->isChild()) {
  1908.                 $parentCode $this->getParent()->getBaseCodeRoute();
  1909.             }
  1910.             return $parentCode.'|'.$this->getCode();
  1911.         }
  1912.         return $this->baseCodeRoute;
  1913.     }
  1914.     public function getModelManager()
  1915.     {
  1916.         return $this->modelManager;
  1917.     }
  1918.     public function setModelManager(ModelManagerInterface $modelManager)
  1919.     {
  1920.         $this->modelManager $modelManager;
  1921.     }
  1922.     public function getManagerType()
  1923.     {
  1924.         return $this->managerType;
  1925.     }
  1926.     /**
  1927.      * @param string $type
  1928.      */
  1929.     public function setManagerType($type)
  1930.     {
  1931.         $this->managerType $type;
  1932.     }
  1933.     public function getObjectIdentifier()
  1934.     {
  1935.         return $this->getCode();
  1936.     }
  1937.     /**
  1938.      * Set the roles and permissions per role.
  1939.      */
  1940.     public function setSecurityInformation(array $information)
  1941.     {
  1942.         $this->securityInformation $information;
  1943.     }
  1944.     public function getSecurityInformation()
  1945.     {
  1946.         return $this->securityInformation;
  1947.     }
  1948.     /**
  1949.      * Return the list of permissions the user should have in order to display the admin.
  1950.      *
  1951.      * @param string $context
  1952.      *
  1953.      * @return array
  1954.      */
  1955.     public function getPermissionsShow($context)
  1956.     {
  1957.         switch ($context) {
  1958.             case self::CONTEXT_DASHBOARD:
  1959.             case self::CONTEXT_MENU:
  1960.             default:
  1961.                 return ['LIST'];
  1962.         }
  1963.     }
  1964.     public function showIn($context)
  1965.     {
  1966.         switch ($context) {
  1967.             case self::CONTEXT_DASHBOARD:
  1968.             case self::CONTEXT_MENU:
  1969.             default:
  1970.                 return $this->isGranted($this->getPermissionsShow($context));
  1971.         }
  1972.     }
  1973.     public function createObjectSecurity($object)
  1974.     {
  1975.         $this->getSecurityHandler()->createObjectSecurity($this$object);
  1976.     }
  1977.     public function setSecurityHandler(SecurityHandlerInterface $securityHandler)
  1978.     {
  1979.         $this->securityHandler $securityHandler;
  1980.     }
  1981.     public function getSecurityHandler()
  1982.     {
  1983.         return $this->securityHandler;
  1984.     }
  1985.     public function isGranted($name$object null)
  1986.     {
  1987.         $key md5(json_encode($name).($object '/'.spl_object_hash($object) : ''));
  1988.         if (!\array_key_exists($key$this->cacheIsGranted)) {
  1989.             $this->cacheIsGranted[$key] = $this->securityHandler->isGranted($this$name$object ?: $this);
  1990.         }
  1991.         return $this->cacheIsGranted[$key];
  1992.     }
  1993.     public function getUrlsafeIdentifier($entity)
  1994.     {
  1995.         return $this->getModelManager()->getUrlsafeIdentifier($entity);
  1996.     }
  1997.     public function getNormalizedIdentifier($entity)
  1998.     {
  1999.         return $this->getModelManager()->getNormalizedIdentifier($entity);
  2000.     }
  2001.     public function id($entity)
  2002.     {
  2003.         return $this->getNormalizedIdentifier($entity);
  2004.     }
  2005.     public function setValidator($validator)
  2006.     {
  2007.         // NEXT_MAJOR: Move ValidatorInterface check to method signature
  2008.         if (!$validator instanceof ValidatorInterface) {
  2009.             throw new \InvalidArgumentException(
  2010.                 'Argument 1 must be an instance of Symfony\Component\Validator\Validator\ValidatorInterface'
  2011.             );
  2012.         }
  2013.         $this->validator $validator;
  2014.     }
  2015.     public function getValidator()
  2016.     {
  2017.         return $this->validator;
  2018.     }
  2019.     public function getShow()
  2020.     {
  2021.         $this->buildShow();
  2022.         return $this->show;
  2023.     }
  2024.     public function setFormTheme(array $formTheme)
  2025.     {
  2026.         $this->formTheme $formTheme;
  2027.     }
  2028.     public function getFormTheme()
  2029.     {
  2030.         return $this->formTheme;
  2031.     }
  2032.     public function setFilterTheme(array $filterTheme)
  2033.     {
  2034.         $this->filterTheme $filterTheme;
  2035.     }
  2036.     public function getFilterTheme()
  2037.     {
  2038.         return $this->filterTheme;
  2039.     }
  2040.     public function addExtension(AdminExtensionInterface $extension)
  2041.     {
  2042.         $this->extensions[] = $extension;
  2043.     }
  2044.     public function getExtensions()
  2045.     {
  2046.         return $this->extensions;
  2047.     }
  2048.     public function setMenuFactory(MenuFactoryInterface $menuFactory)
  2049.     {
  2050.         $this->menuFactory $menuFactory;
  2051.     }
  2052.     public function getMenuFactory()
  2053.     {
  2054.         return $this->menuFactory;
  2055.     }
  2056.     public function setRouteBuilder(RouteBuilderInterface $routeBuilder)
  2057.     {
  2058.         $this->routeBuilder $routeBuilder;
  2059.     }
  2060.     public function getRouteBuilder()
  2061.     {
  2062.         return $this->routeBuilder;
  2063.     }
  2064.     public function toString($object)
  2065.     {
  2066.         if (!\is_object($object)) {
  2067.             return '';
  2068.         }
  2069.         if (method_exists($object'__toString') && null !== $object->__toString()) {
  2070.             return (string) $object;
  2071.         }
  2072.         return sprintf('%s:%s'ClassUtils::getClass($object), spl_object_hash($object));
  2073.     }
  2074.     public function setLabelTranslatorStrategy(LabelTranslatorStrategyInterface $labelTranslatorStrategy)
  2075.     {
  2076.         $this->labelTranslatorStrategy $labelTranslatorStrategy;
  2077.     }
  2078.     public function getLabelTranslatorStrategy()
  2079.     {
  2080.         return $this->labelTranslatorStrategy;
  2081.     }
  2082.     public function supportsPreviewMode()
  2083.     {
  2084.         return $this->supportsPreviewMode;
  2085.     }
  2086.     /**
  2087.      * Set custom per page options.
  2088.      */
  2089.     public function setPerPageOptions(array $options)
  2090.     {
  2091.         $this->perPageOptions $options;
  2092.     }
  2093.     /**
  2094.      * Returns predefined per page options.
  2095.      *
  2096.      * @return array
  2097.      */
  2098.     public function getPerPageOptions()
  2099.     {
  2100.         return $this->perPageOptions;
  2101.     }
  2102.     /**
  2103.      * Set pager type.
  2104.      *
  2105.      * @param string $pagerType
  2106.      */
  2107.     public function setPagerType($pagerType)
  2108.     {
  2109.         $this->pagerType $pagerType;
  2110.     }
  2111.     /**
  2112.      * Get pager type.
  2113.      *
  2114.      * @return string
  2115.      */
  2116.     public function getPagerType()
  2117.     {
  2118.         return $this->pagerType;
  2119.     }
  2120.     /**
  2121.      * Returns true if the per page value is allowed, false otherwise.
  2122.      *
  2123.      * @param int $perPage
  2124.      *
  2125.      * @return bool
  2126.      */
  2127.     public function determinedPerPageValue($perPage)
  2128.     {
  2129.         return \in_array($perPage$this->perPageOptionstrue);
  2130.     }
  2131.     public function isAclEnabled()
  2132.     {
  2133.         return $this->getSecurityHandler() instanceof AclSecurityHandlerInterface;
  2134.     }
  2135.     public function getObjectMetadata($object)
  2136.     {
  2137.         return new Metadata($this->toString($object));
  2138.     }
  2139.     public function getListModes()
  2140.     {
  2141.         return $this->listModes;
  2142.     }
  2143.     public function setListMode($mode)
  2144.     {
  2145.         if (!$this->hasRequest()) {
  2146.             throw new \RuntimeException(sprintf('No request attached to the current admin: %s'$this->getCode()));
  2147.         }
  2148.         $this->getRequest()->getSession()->set(sprintf('%s.list_mode'$this->getCode()), $mode);
  2149.     }
  2150.     public function getListMode()
  2151.     {
  2152.         if (!$this->hasRequest()) {
  2153.             return 'list';
  2154.         }
  2155.         return $this->getRequest()->getSession()->get(sprintf('%s.list_mode'$this->getCode()), 'list');
  2156.     }
  2157.     public function getAccessMapping()
  2158.     {
  2159.         return $this->accessMapping;
  2160.     }
  2161.     public function checkAccess($action$object null)
  2162.     {
  2163.         $access $this->getAccess();
  2164.         if (!\array_key_exists($action$access)) {
  2165.             throw new \InvalidArgumentException(sprintf(
  2166.                 'Action "%s" could not be found in access mapping.'
  2167.                 .' Please make sure your action is defined into your admin class accessMapping property.',
  2168.                 $action
  2169.             ));
  2170.         }
  2171.         if (!\is_array($access[$action])) {
  2172.             $access[$action] = [$access[$action]];
  2173.         }
  2174.         foreach ($access[$action] as $role) {
  2175.             if (false === $this->isGranted($role$object)) {
  2176.                 throw new AccessDeniedException(sprintf('Access Denied to the action %s and role %s'$action$role));
  2177.             }
  2178.         }
  2179.     }
  2180.     /**
  2181.      * Hook to handle access authorization, without throw Exception.
  2182.      *
  2183.      * @param string $action
  2184.      * @param object $object
  2185.      *
  2186.      * @return bool
  2187.      */
  2188.     public function hasAccess($action$object null)
  2189.     {
  2190.         $access $this->getAccess();
  2191.         if (!\array_key_exists($action$access)) {
  2192.             return false;
  2193.         }
  2194.         if (!\is_array($access[$action])) {
  2195.             $access[$action] = [$access[$action]];
  2196.         }
  2197.         foreach ($access[$action] as $role) {
  2198.             if (false === $this->isGranted($role$object)) {
  2199.                 return false;
  2200.             }
  2201.         }
  2202.         return true;
  2203.     }
  2204.     /**
  2205.      * @param string      $action
  2206.      * @param object|null $object
  2207.      *
  2208.      * @return array
  2209.      */
  2210.     public function configureActionButtons($action$object null)
  2211.     {
  2212.         $list = [];
  2213.         if (\in_array($action, ['tree''show''edit''delete''list''batch'], true)
  2214.             && $this->hasAccess('create')
  2215.             && $this->hasRoute('create')
  2216.         ) {
  2217.             $list['create'] = [
  2218.                 // NEXT_MAJOR: Remove this line and use commented line below it instead
  2219.                 'template' => $this->getTemplate('button_create'),
  2220. //                'template' => $this->getTemplateRegistry()->getTemplate('button_create'),
  2221.             ];
  2222.         }
  2223.         if (\in_array($action, ['show''delete''acl''history'], true)
  2224.             && $this->canAccessObject('edit'$object)
  2225.             && $this->hasRoute('edit')
  2226.         ) {
  2227.             $list['edit'] = [
  2228.                 // NEXT_MAJOR: Remove this line and use commented line below it instead
  2229.                 'template' => $this->getTemplate('button_edit'),
  2230.                 //'template' => $this->getTemplateRegistry()->getTemplate('button_edit'),
  2231.             ];
  2232.         }
  2233.         if (\in_array($action, ['show''edit''acl'], true)
  2234.             && $this->canAccessObject('history'$object)
  2235.             && $this->hasRoute('history')
  2236.         ) {
  2237.             $list['history'] = [
  2238.                 // NEXT_MAJOR: Remove this line and use commented line below it instead
  2239.                 'template' => $this->getTemplate('button_history'),
  2240.                 // 'template' => $this->getTemplateRegistry()->getTemplate('button_history'),
  2241.             ];
  2242.         }
  2243.         if (\in_array($action, ['edit''history'], true)
  2244.             && $this->isAclEnabled()
  2245.             && $this->canAccessObject('acl'$object)
  2246.             && $this->hasRoute('acl')
  2247.         ) {
  2248.             $list['acl'] = [
  2249.                 // NEXT_MAJOR: Remove this line and use commented line below it instead
  2250.                 'template' => $this->getTemplate('button_acl'),
  2251.                 // 'template' => $this->getTemplateRegistry()->getTemplate('button_acl'),
  2252.             ];
  2253.         }
  2254.         if (\in_array($action, ['edit''history''acl'], true)
  2255.             && $this->canAccessObject('show'$object)
  2256.             && \count($this->getShow()) > 0
  2257.             && $this->hasRoute('show')
  2258.         ) {
  2259.             $list['show'] = [
  2260.                 // NEXT_MAJOR: Remove this line and use commented line below it instead
  2261.                 'template' => $this->getTemplate('button_show'),
  2262.                 // 'template' => $this->getTemplateRegistry()->getTemplate('button_show'),
  2263.             ];
  2264.         }
  2265.         if (\in_array($action, ['show''edit''delete''acl''batch'], true)
  2266.             && $this->hasAccess('list')
  2267.             && $this->hasRoute('list')
  2268.         ) {
  2269.             $list['list'] = [
  2270.                 // NEXT_MAJOR: Remove this line and use commented line below it instead
  2271.                 'template' => $this->getTemplate('button_list'),
  2272.                 // 'template' => $this->getTemplateRegistry()->getTemplate('button_list'),
  2273.             ];
  2274.         }
  2275.         return $list;
  2276.     }
  2277.     /**
  2278.      * @param string $action
  2279.      * @param object $object
  2280.      *
  2281.      * @return array
  2282.      */
  2283.     public function getActionButtons($action$object null)
  2284.     {
  2285.         $list $this->configureActionButtons($action$object);
  2286.         foreach ($this->getExtensions() as $extension) {
  2287.             // TODO: remove method check in next major release
  2288.             if (method_exists($extension'configureActionButtons')) {
  2289.                 $list $extension->configureActionButtons($this$list$action$object);
  2290.             }
  2291.         }
  2292.         return $list;
  2293.     }
  2294.     /**
  2295.      * Get the list of actions that can be accessed directly from the dashboard.
  2296.      *
  2297.      * @return array
  2298.      */
  2299.     public function getDashboardActions()
  2300.     {
  2301.         $actions = [];
  2302.         if ($this->hasRoute('create') && $this->hasAccess('create')) {
  2303.             $actions['create'] = [
  2304.                 'label' => 'link_add',
  2305.                 'translation_domain' => 'SonataAdminBundle',
  2306.                 // NEXT_MAJOR: Remove this line and use commented line below it instead
  2307.                 'template' => $this->getTemplate('action_create'),
  2308.                 // 'template' => $this->getTemplateRegistry()->getTemplate('action_create'),
  2309.                 'url' => $this->generateUrl('create'),
  2310.                 'icon' => 'plus-circle',
  2311.             ];
  2312.         }
  2313.         if ($this->hasRoute('list') && $this->hasAccess('list')) {
  2314.             $actions['list'] = [
  2315.                 'label' => 'link_list',
  2316.                 'translation_domain' => 'SonataAdminBundle',
  2317.                 'url' => $this->generateUrl('list'),
  2318.                 'icon' => 'list',
  2319.             ];
  2320.         }
  2321.         return $actions;
  2322.     }
  2323.     /**
  2324.      * Setting to true will enable mosaic button for the admin screen.
  2325.      * Setting to false will hide mosaic button for the admin screen.
  2326.      *
  2327.      * @param bool $isShown
  2328.      */
  2329.     final public function showMosaicButton($isShown)
  2330.     {
  2331.         if ($isShown) {
  2332.             $this->listModes['mosaic'] = ['class' => static::MOSAIC_ICON_CLASS];
  2333.         } else {
  2334.             unset($this->listModes['mosaic']);
  2335.         }
  2336.     }
  2337.     /**
  2338.      * @param object $object
  2339.      */
  2340.     final public function getSearchResultLink($object)
  2341.     {
  2342.         foreach ($this->searchResultActions as $action) {
  2343.             if ($this->hasRoute($action) && $this->hasAccess($action$object)) {
  2344.                 return $this->generateObjectUrl($action$object);
  2345.             }
  2346.         }
  2347.     }
  2348.     /**
  2349.      * Checks if a filter type is set to a default value.
  2350.      *
  2351.      * @param string $name
  2352.      *
  2353.      * @return bool
  2354.      */
  2355.     final public function isDefaultFilter($name)
  2356.     {
  2357.         $filter $this->getFilterParameters();
  2358.         $default $this->getDefaultFilterValues();
  2359.         if (!\array_key_exists($name$filter) || !\array_key_exists($name$default)) {
  2360.             return false;
  2361.         }
  2362.         return $filter[$name] === $default[$name];
  2363.     }
  2364.     /**
  2365.      * Check object existence and access, without throw Exception.
  2366.      *
  2367.      * @param string $action
  2368.      * @param object $object
  2369.      *
  2370.      * @return bool
  2371.      */
  2372.     public function canAccessObject($action$object)
  2373.     {
  2374.         return $object && $this->id($object) && $this->hasAccess($action$object);
  2375.     }
  2376.     /**
  2377.      * @return MutableTemplateRegistryInterface
  2378.      */
  2379.     final protected function getTemplateRegistry()
  2380.     {
  2381.         return $this->templateRegistry;
  2382.     }
  2383.     /**
  2384.      * Returns a list of default filters.
  2385.      *
  2386.      * @return array
  2387.      */
  2388.     final protected function getDefaultFilterValues()
  2389.     {
  2390.         $defaultFilterValues = [];
  2391.         $this->configureDefaultFilterValues($defaultFilterValues);
  2392.         foreach ($this->getExtensions() as $extension) {
  2393.             // NEXT_MAJOR: remove method check in next major release
  2394.             if (method_exists($extension'configureDefaultFilterValues')) {
  2395.                 $extension->configureDefaultFilterValues($this$defaultFilterValues);
  2396.             }
  2397.         }
  2398.         return $defaultFilterValues;
  2399.     }
  2400.     protected function configureFormFields(FormMapper $form)
  2401.     {
  2402.     }
  2403.     protected function configureListFields(ListMapper $list)
  2404.     {
  2405.     }
  2406.     protected function configureDatagridFilters(DatagridMapper $filter)
  2407.     {
  2408.     }
  2409.     protected function configureShowFields(ShowMapper $show)
  2410.     {
  2411.     }
  2412.     protected function configureRoutes(RouteCollection $collection)
  2413.     {
  2414.     }
  2415.     /**
  2416.      * Allows you to customize batch actions.
  2417.      *
  2418.      * @param array $actions List of actions
  2419.      *
  2420.      * @return array
  2421.      */
  2422.     protected function configureBatchActions($actions)
  2423.     {
  2424.         return $actions;
  2425.     }
  2426.     /**
  2427.      * NEXT_MAJOR: remove this method.
  2428.      *
  2429.      * @deprecated Use configureTabMenu instead
  2430.      */
  2431.     protected function configureSideMenu(MenuItemInterface $menu$actionAdminInterface $childAdmin null)
  2432.     {
  2433.     }
  2434.     /**
  2435.      * Configures the tab menu in your admin.
  2436.      *
  2437.      * @param string $action
  2438.      */
  2439.     protected function configureTabMenu(MenuItemInterface $menu$actionAdminInterface $childAdmin null)
  2440.     {
  2441.         // Use configureSideMenu not to mess with previous overrides
  2442.         // TODO remove once deprecation period is over
  2443.         $this->configureSideMenu($menu$action$childAdmin);
  2444.     }
  2445.     /**
  2446.      * build the view FieldDescription array.
  2447.      */
  2448.     protected function buildShow()
  2449.     {
  2450.         if ($this->show) {
  2451.             return;
  2452.         }
  2453.         $this->show = new FieldDescriptionCollection();
  2454.         $mapper = new ShowMapper($this->showBuilder$this->show$this);
  2455.         $this->configureShowFields($mapper);
  2456.         foreach ($this->getExtensions() as $extension) {
  2457.             $extension->configureShowFields($mapper);
  2458.         }
  2459.     }
  2460.     /**
  2461.      * build the list FieldDescription array.
  2462.      */
  2463.     protected function buildList()
  2464.     {
  2465.         if ($this->list) {
  2466.             return;
  2467.         }
  2468.         $this->list $this->getListBuilder()->getBaseList();
  2469.         $mapper = new ListMapper($this->getListBuilder(), $this->list$this);
  2470.         if (\count($this->getBatchActions()) > && $this->hasRequest() && !$this->getRequest()->isXmlHttpRequest()) {
  2471.             $fieldDescription $this->getModelManager()->getNewFieldDescriptionInstance(
  2472.                 $this->getClass(),
  2473.                 'batch',
  2474.                 [
  2475.                     'label' => 'batch',
  2476.                     'code' => '_batch',
  2477.                     'sortable' => false,
  2478.                     'virtual_field' => true,
  2479.                 ]
  2480.             );
  2481.             $fieldDescription->setAdmin($this);
  2482.             // NEXT_MAJOR: Remove this line and use commented line below it instead
  2483.             $fieldDescription->setTemplate($this->getTemplate('batch'));
  2484.             // $fieldDescription->setTemplate($this->getTemplateRegistry()->getTemplate('batch'));
  2485.             $mapper->add($fieldDescription'batch');
  2486.         }
  2487.         $this->configureListFields($mapper);
  2488.         foreach ($this->getExtensions() as $extension) {
  2489.             $extension->configureListFields($mapper);
  2490.         }
  2491.         if ($this->hasRequest() && $this->getRequest()->isXmlHttpRequest()) {
  2492.             $fieldDescription $this->getModelManager()->getNewFieldDescriptionInstance(
  2493.                 $this->getClass(),
  2494.                 'select',
  2495.                 [
  2496.                     'label' => false,
  2497.                     'code' => '_select',
  2498.                     'sortable' => false,
  2499.                     'virtual_field' => false,
  2500.                 ]
  2501.             );
  2502.             $fieldDescription->setAdmin($this);
  2503.             // NEXT_MAJOR: Remove this line and use commented line below it instead
  2504.             $fieldDescription->setTemplate($this->getTemplate('select'));
  2505.             // $fieldDescription->setTemplate($this->getTemplateRegistry()->getTemplate('select'));
  2506.             $mapper->add($fieldDescription'select');
  2507.         }
  2508.     }
  2509.     /**
  2510.      * Build the form FieldDescription collection.
  2511.      */
  2512.     protected function buildForm()
  2513.     {
  2514.         if ($this->form) {
  2515.             return;
  2516.         }
  2517.         // append parent object if any
  2518.         // todo : clean the way the Admin class can retrieve set the object
  2519.         if ($this->isChild() && $this->getParentAssociationMapping()) {
  2520.             $parent $this->getParent()->getObject($this->request->get($this->getParent()->getIdParameter()));
  2521.             $propertyAccessor $this->getConfigurationPool()->getPropertyAccessor();
  2522.             $propertyPath = new PropertyPath($this->getParentAssociationMapping());
  2523.             $object $this->getSubject();
  2524.             $value $propertyAccessor->getValue($object$propertyPath);
  2525.             if (\is_array($value) || ($value instanceof \Traversable && $value instanceof \ArrayAccess)) {
  2526.                 $value[] = $parent;
  2527.                 $propertyAccessor->setValue($object$propertyPath$value);
  2528.             } else {
  2529.                 $propertyAccessor->setValue($object$propertyPath$parent);
  2530.             }
  2531.         }
  2532.         $formBuilder $this->getFormBuilder();
  2533.         $formBuilder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  2534.             $this->preValidate($event->getData());
  2535.         }, 100);
  2536.         $this->form $formBuilder->getForm();
  2537.     }
  2538.     /**
  2539.      * Gets the subclass corresponding to the given name.
  2540.      *
  2541.      * @param string $name The name of the sub class
  2542.      *
  2543.      * @return string the subclass
  2544.      */
  2545.     protected function getSubClass($name)
  2546.     {
  2547.         if ($this->hasSubClass($name)) {
  2548.             return $this->subClasses[$name];
  2549.         }
  2550.         throw new \RuntimeException(sprintf(
  2551.             'Unable to find the subclass `%s` for admin `%s`',
  2552.             $name,
  2553.             static::class
  2554.         ));
  2555.     }
  2556.     /**
  2557.      * Attach the inline validator to the model metadata, this must be done once per admin.
  2558.      */
  2559.     protected function attachInlineValidator()
  2560.     {
  2561.         $admin $this;
  2562.         // add the custom inline validation option
  2563.         $metadata $this->validator->getMetadataFor($this->getClass());
  2564.         $metadata->addConstraint(new InlineConstraint([
  2565.             'service' => $this,
  2566.             'method' => static function (ErrorElement $errorElement$object) use ($admin) {
  2567.                 /* @var \Sonata\AdminBundle\Admin\AdminInterface $admin */
  2568.                 // This avoid the main validation to be cascaded to children
  2569.                 // The problem occurs when a model Page has a collection of Page as property
  2570.                 if ($admin->hasSubject() && spl_object_hash($object) !== spl_object_hash($admin->getSubject())) {
  2571.                     return;
  2572.                 }
  2573.                 $admin->validate($errorElement$object);
  2574.                 foreach ($admin->getExtensions() as $extension) {
  2575.                     $extension->validate($admin$errorElement$object);
  2576.                 }
  2577.             },
  2578.             'serializingWarning' => true,
  2579.         ]));
  2580.     }
  2581.     /**
  2582.      * Predefine per page options.
  2583.      */
  2584.     protected function predefinePerPageOptions()
  2585.     {
  2586.         array_unshift($this->perPageOptions$this->maxPerPage);
  2587.         $this->perPageOptions array_unique($this->perPageOptions);
  2588.         sort($this->perPageOptions);
  2589.     }
  2590.     /**
  2591.      * Return list routes with permissions name.
  2592.      *
  2593.      * @return array<string, string>
  2594.      */
  2595.     protected function getAccess()
  2596.     {
  2597.         $access array_merge([
  2598.             'acl' => 'MASTER',
  2599.             'export' => 'EXPORT',
  2600.             'historyCompareRevisions' => 'EDIT',
  2601.             'historyViewRevision' => 'EDIT',
  2602.             'history' => 'EDIT',
  2603.             'edit' => 'EDIT',
  2604.             'show' => 'VIEW',
  2605.             'create' => 'CREATE',
  2606.             'delete' => 'DELETE',
  2607.             'batchDelete' => 'DELETE',
  2608.             'list' => 'LIST',
  2609.         ], $this->getAccessMapping());
  2610.         foreach ($this->extensions as $extension) {
  2611.             // TODO: remove method check in next major release
  2612.             if (method_exists($extension'getAccessMapping')) {
  2613.                 $access array_merge($access$extension->getAccessMapping($this));
  2614.             }
  2615.         }
  2616.         return $access;
  2617.     }
  2618.     /**
  2619.      * Configures a list of default filters.
  2620.      */
  2621.     protected function configureDefaultFilterValues(array &$filterValues)
  2622.     {
  2623.     }
  2624.     /**
  2625.      * Build all the related urls to the current admin.
  2626.      */
  2627.     private function buildRoutes(): void
  2628.     {
  2629.         if ($this->loaded['routes']) {
  2630.             return;
  2631.         }
  2632.         $this->loaded['routes'] = true;
  2633.         $this->routes = new RouteCollection(
  2634.             $this->getBaseCodeRoute(),
  2635.             $this->getBaseRouteName(),
  2636.             $this->getBaseRoutePattern(),
  2637.             $this->getBaseControllerName()
  2638.         );
  2639.         $this->routeBuilder->build($this$this->routes);
  2640.         $this->configureRoutes($this->routes);
  2641.         foreach ($this->getExtensions() as $extension) {
  2642.             $extension->configureRoutes($this$this->routes);
  2643.         }
  2644.     }
  2645. }
  2646. class_exists(\Sonata\Form\Validator\ErrorElement::class);