PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/Admin/Admin.php

https://github.com/DelphineGx/SonataAdminBundle
PHP | 2630 lines | 1701 code | 227 blank | 702 comment | 30 complexity | 413e55859209de7f8a6afd99e3bde473 MD5 | raw file
Possible License(s): JSON, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Admin;
  11. use Symfony\Component\Form\Form;
  12. use Symfony\Component\Form\FormBuilder;
  13. use Symfony\Component\PropertyAccess\PropertyPath;
  14. use Symfony\Component\PropertyAccess\PropertyAccess;
  15. use Symfony\Component\Validator\ValidatorInterface;
  16. use Symfony\Component\Translation\TranslatorInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\Security\Acl\Model\DomainObjectInterface;
  19. use Sonata\AdminBundle\Form\FormMapper;
  20. use Sonata\AdminBundle\Datagrid\ListMapper;
  21. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  22. use Sonata\AdminBundle\Show\ShowMapper;
  23. use Sonata\AdminBundle\Admin\Pool;
  24. use Sonata\AdminBundle\Validator\ErrorElement;
  25. use Sonata\AdminBundle\Validator\Constraints\InlineConstraint;
  26. use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
  27. use Sonata\AdminBundle\Builder\FormContractorInterface;
  28. use Sonata\AdminBundle\Builder\ListBuilderInterface;
  29. use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
  30. use Sonata\AdminBundle\Builder\ShowBuilderInterface;
  31. use Sonata\AdminBundle\Builder\RouteBuilderInterface;
  32. use Sonata\AdminBundle\Route\RouteGeneratorInterface;
  33. use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
  34. use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
  35. use Sonata\AdminBundle\Route\RouteCollection;
  36. use Sonata\AdminBundle\Model\ModelManagerInterface;
  37. use Knp\Menu\FactoryInterface as MenuFactoryInterface;
  38. use Knp\Menu\ItemInterface as MenuItemInterface;
  39. abstract class Admin implements AdminInterface, DomainObjectInterface
  40. {
  41. const CONTEXT_MENU = 'menu';
  42. const CONTEXT_DASHBOARD = 'dashboard';
  43. const CLASS_REGEX = '@([A-Za-z0-9]*)\\\(Bundle\\\)?([A-Za-z0-9]+)Bundle\\\([A-Za-z0-9].*)\\\([A-Za-z0-9]*)$@';
  44. /**
  45. * The class name managed by the admin class
  46. *
  47. * @var string
  48. */
  49. private $class;
  50. /**
  51. * The subclasses supported by the admin class
  52. *
  53. * @var array
  54. */
  55. private $subClasses = array();
  56. /**
  57. * The list collection
  58. *
  59. * @var array
  60. */
  61. private $list;
  62. /**
  63. * The list FieldDescription constructed from the configureListField method
  64. *
  65. * @var array
  66. */
  67. protected $listFieldDescriptions = array();
  68. private $show;
  69. /**
  70. * The show FieldDescription constructed from the configureShowFields method
  71. *
  72. * @var array
  73. */
  74. protected $showFieldDescriptions = array();
  75. /**
  76. * @var Form
  77. */
  78. private $form;
  79. /**
  80. * The list FieldDescription constructed from the configureFormField method
  81. *
  82. * @var array
  83. */
  84. protected $formFieldDescriptions = array();
  85. /**
  86. * @var \Sonata\AdminBundle\Datagrid\DatagridInterface
  87. */
  88. private $filter;
  89. /**
  90. * The filter FieldDescription constructed from the configureFilterField method
  91. *
  92. * @var array
  93. */
  94. protected $filterFieldDescriptions = array();
  95. /**
  96. * The number of result to display in the list
  97. *
  98. * @var integer
  99. */
  100. protected $maxPerPage = 25;
  101. /**
  102. * The maximum number of page numbers to display in the list
  103. *
  104. * @var integer
  105. */
  106. protected $maxPageLinks = 25;
  107. /**
  108. * The base route name used to generate the routing information
  109. *
  110. * @var string
  111. */
  112. protected $baseRouteName;
  113. /**
  114. * The base route pattern used to generate the routing information
  115. *
  116. * @var string
  117. */
  118. protected $baseRoutePattern;
  119. /**
  120. * The base name controller used to generate the routing information
  121. *
  122. * @var string
  123. */
  124. protected $baseControllerName;
  125. /**
  126. * The form group disposition
  127. *
  128. * @var array|boolean
  129. */
  130. private $formGroups = false;
  131. /**
  132. * The view group disposition
  133. *
  134. * @var array|boolean
  135. */
  136. private $showGroups = false;
  137. /**
  138. * The label class name (used in the title/breadcrumb ...)
  139. *
  140. * @var string
  141. */
  142. protected $classnameLabel;
  143. /**
  144. * The translation domain to be used to translate messages
  145. *
  146. * @var string
  147. */
  148. protected $translationDomain = 'messages';
  149. /**
  150. * Options to set to the form (ie, validation_groups)
  151. *
  152. * @var array
  153. */
  154. protected $formOptions = array();
  155. /**
  156. * Default values to the datagrid
  157. *
  158. * @var array
  159. */
  160. protected $datagridValues = array(
  161. '_page' => 1,
  162. '_per_page' => 25,
  163. );
  164. /**
  165. * Predefined per page options
  166. *
  167. * @var array
  168. */
  169. protected $perPageOptions = array(15, 25, 50, 100, 150, 200);
  170. /**
  171. * The code related to the admin
  172. *
  173. * @var string
  174. */
  175. protected $code;
  176. /**
  177. * The label
  178. *
  179. * @var string
  180. */
  181. protected $label;
  182. /**
  183. * Whether or not to persist the filters in the session
  184. *
  185. * @var boolean
  186. */
  187. protected $persistFilters = false;
  188. /**
  189. * Array of routes related to this admin
  190. *
  191. * @var \Sonata\AdminBundle\Route\RouteCollection
  192. */
  193. protected $routes;
  194. /**
  195. * The subject only set in edit/update/create mode
  196. *
  197. * @var object
  198. */
  199. protected $subject;
  200. /**
  201. * Define a Collection of child admin, ie /admin/order/{id}/order-element/{childId}
  202. *
  203. * @var array
  204. */
  205. protected $children = array();
  206. /**
  207. * Reference the parent collection
  208. *
  209. * @var Admin
  210. */
  211. protected $parent = null;
  212. /**
  213. * The base code route refer to the prefix used to generate the route name
  214. *
  215. * @var string
  216. */
  217. protected $baseCodeRoute = '';
  218. /**
  219. * The related field reflection, ie if OrderElement is linked to Order,
  220. * then the $parentReflectionProperty must be the ReflectionProperty of
  221. * the order (OrderElement::$order)
  222. *
  223. * @var \ReflectionProperty $parentReflectionProperty
  224. */
  225. protected $parentAssociationMapping = null;
  226. /**
  227. * Reference the parent FieldDescription related to this admin
  228. * only set for FieldDescription which is associated to an Sub Admin instance
  229. *
  230. * @var FieldDescriptionInterface
  231. */
  232. protected $parentFieldDescription;
  233. /**
  234. * If true then the current admin is part of the nested admin set (from the url)
  235. *
  236. * @var boolean
  237. */
  238. protected $currentChild = false;
  239. /**
  240. * The uniqid is used to avoid clashing with 2 admin related to the code
  241. * ie: a Block linked to a Block
  242. *
  243. * @var string
  244. */
  245. protected $uniqid;
  246. /**
  247. * The Entity or Document manager
  248. *
  249. * @var \Sonata\AdminBundle\Model\ModelManagerInterface
  250. */
  251. protected $modelManager;
  252. /**
  253. * The manager type to use for the admin
  254. *
  255. * @var string
  256. */
  257. private $managerType;
  258. /**
  259. * The current request object
  260. *
  261. * @var \Symfony\Component\HttpFoundation\Request
  262. */
  263. protected $request;
  264. /**
  265. * The translator component
  266. *
  267. * @var \Symfony\Component\Translation\TranslatorInterface
  268. */
  269. protected $translator;
  270. /**
  271. * The related form contractor
  272. *
  273. * @var \Sonata\AdminBundle\Builder\FormContractorInterface
  274. */
  275. protected $formContractor;
  276. /**
  277. * The related list builder
  278. *
  279. * @var \Sonata\AdminBundle\Builder\ListBuilderInterface
  280. */
  281. protected $listBuilder;
  282. /**
  283. * The related view builder
  284. *
  285. * @var ShowBuilderInterface
  286. */
  287. protected $showBuilder;
  288. /**
  289. * The related datagrid builder
  290. *
  291. * @var \Sonata\AdminBundle\Builder\DatagridBuilderInterface
  292. */
  293. protected $datagridBuilder;
  294. /**
  295. * @var \Sonata\AdminBundle\Builder\RouteBuilderInterface
  296. */
  297. protected $routeBuilder;
  298. /**
  299. * The datagrid instance
  300. *
  301. * @var \Sonata\AdminBundle\Datagrid\DatagridInterface
  302. */
  303. protected $datagrid;
  304. /**
  305. * The router instance
  306. *
  307. * @var RouteGeneratorInterface
  308. */
  309. protected $routeGenerator;
  310. /**
  311. * The generated breadcrumbs
  312. *
  313. * @var array
  314. */
  315. protected $breadcrumbs = array();
  316. protected $securityHandler = null;
  317. /**
  318. * @var \Symfony\Component\Validator\ValidatorInterface $validator
  319. */
  320. protected $validator = null;
  321. /**
  322. * The configuration pool
  323. *
  324. * @var Pool
  325. */
  326. protected $configurationPool;
  327. protected $menu;
  328. /**
  329. * @var \Knp\Menu\FactoryInterface
  330. */
  331. protected $menuFactory;
  332. protected $loaded = array(
  333. 'view_fields' => false,
  334. 'view_groups' => false,
  335. 'routes' => false,
  336. 'side_menu' => false,
  337. );
  338. protected $formTheme = array();
  339. protected $filterTheme = array();
  340. protected $templates = array();
  341. protected $extensions = array();
  342. protected $labelTranslatorStrategy;
  343. /**
  344. * Setting to true will enable preview mode for
  345. * the entity and show a preview button in the
  346. * edit/create forms
  347. *
  348. * @var boolean
  349. */
  350. protected $supportsPreviewMode = false;
  351. /**
  352. * Roles and permissions per role
  353. *
  354. * @var array [role] => array([permission], [permission])
  355. */
  356. protected $securityInformation = array();
  357. /**
  358. * {@inheritdoc}
  359. */
  360. protected function configureFormFields(FormMapper $form)
  361. {
  362. }
  363. /**
  364. * {@inheritdoc}
  365. */
  366. protected function configureListFields(ListMapper $list)
  367. {
  368. }
  369. /**
  370. * {@inheritdoc}
  371. */
  372. protected function configureDatagridFilters(DatagridMapper $filter)
  373. {
  374. }
  375. /**
  376. * @deprecated removed with Symfony 2.2
  377. *
  378. * {@inheritdoc}
  379. */
  380. protected function configureShowField(ShowMapper $show)
  381. {
  382. }
  383. /**
  384. * {@inheritdoc}
  385. */
  386. protected function configureShowFields(ShowMapper $filter)
  387. {
  388. }
  389. /**
  390. * {@inheritdoc}
  391. */
  392. protected function configureRoutes(RouteCollection $collection)
  393. {
  394. }
  395. /**
  396. * {@inheritdoc}
  397. */
  398. protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
  399. {
  400. }
  401. /**
  402. * {@inheritdoc}
  403. */
  404. public function getExportFormats()
  405. {
  406. return array(
  407. 'json', 'xml', 'csv', 'xls'
  408. );
  409. }
  410. /**
  411. * @return array
  412. */
  413. public function getExportFields()
  414. {
  415. return $this->getModelManager()->getExportFields($this->getClass());
  416. }
  417. /**
  418. * @return
  419. */
  420. public function getDataSourceIterator()
  421. {
  422. $datagrid = $this->getDatagrid();
  423. $datagrid->buildPager();
  424. return $this->getModelManager()->getDataSourceIterator($datagrid, $this->getExportFields());
  425. }
  426. /**
  427. * {@inheritdoc}
  428. */
  429. public function validate(ErrorElement $errorElement, $object)
  430. {
  431. }
  432. /**
  433. * @param string $code
  434. * @param string $class
  435. * @param string $baseControllerName
  436. */
  437. public function __construct($code, $class, $baseControllerName)
  438. {
  439. $this->code = $code;
  440. $this->class = $class;
  441. $this->baseControllerName = $baseControllerName;
  442. $this->predefinePerPageOptions();
  443. $this->datagridValues['_per_page'] = $this->maxPerPage;
  444. }
  445. /**
  446. * define custom variable
  447. */
  448. public function initialize()
  449. {
  450. $this->uniqid = "s".uniqid();
  451. if (!$this->classnameLabel) {
  452. $this->classnameLabel = substr($this->getClass(), strrpos($this->getClass(), '\\') + 1);
  453. }
  454. $this->baseCodeRoute = $this->getCode();
  455. $this->configure();
  456. }
  457. /**
  458. * {@inheritdoc}
  459. */
  460. public function configure()
  461. {
  462. }
  463. /**
  464. * {@inheritdoc}
  465. */
  466. public function update($object)
  467. {
  468. $this->preUpdate($object);
  469. $this->getModelManager()->update($object);
  470. $this->postUpdate($object);
  471. }
  472. /**
  473. * {@inheritdoc}
  474. */
  475. public function create($object)
  476. {
  477. $this->prePersist($object);
  478. $this->getModelManager()->create($object);
  479. $this->postPersist($object);
  480. $this->createObjectSecurity($object);
  481. }
  482. /**
  483. * {@inheritdoc}
  484. */
  485. public function delete($object)
  486. {
  487. $this->preRemove($object);
  488. $this->getSecurityHandler()->deleteObjectSecurity($this, $object);
  489. $this->getModelManager()->delete($object);
  490. $this->postRemove($object);
  491. }
  492. /**
  493. * {@inheritdoc}
  494. */
  495. public function preUpdate($object)
  496. {
  497. }
  498. /**
  499. * {@inheritdoc}
  500. */
  501. public function postUpdate($object)
  502. {
  503. }
  504. /**
  505. * {@inheritdoc}
  506. */
  507. public function prePersist($object)
  508. {
  509. }
  510. /**
  511. * {@inheritdoc}
  512. */
  513. public function postPersist($object)
  514. {
  515. }
  516. /**
  517. * {@inheritdoc}
  518. */
  519. public function preRemove($object)
  520. {
  521. }
  522. /**
  523. * {@inheritdoc}
  524. */
  525. public function postRemove($object)
  526. {
  527. }
  528. /**
  529. * build the view FieldDescription array
  530. *
  531. * @return void
  532. */
  533. protected function buildShow()
  534. {
  535. if ($this->show) {
  536. return;
  537. }
  538. $this->show = new FieldDescriptionCollection();
  539. $mapper = new ShowMapper($this->showBuilder, $this->show, $this);
  540. $this->configureShowField($mapper); // deprecated, use configureShowFields instead
  541. $this->configureShowFields($mapper);
  542. foreach ($this->getExtensions() as $extension) {
  543. $extension->configureShowFields($mapper);
  544. }
  545. }
  546. /**
  547. * build the list FieldDescription array
  548. *
  549. * @return void
  550. */
  551. protected function buildList()
  552. {
  553. if ($this->list) {
  554. return;
  555. }
  556. $this->list = $this->getListBuilder()->getBaseList();
  557. $mapper = new ListMapper($this->getListBuilder(), $this->list, $this);
  558. if (count($this->getBatchActions()) > 0) {
  559. $fieldDescription = $this->getModelManager()->getNewFieldDescriptionInstance($this->getClass(), 'batch', array(
  560. 'label' => 'batch',
  561. 'code' => '_batch',
  562. 'sortable' => false
  563. ));
  564. $fieldDescription->setAdmin($this);
  565. $fieldDescription->setTemplate($this->getTemplate('batch'));
  566. $mapper->add($fieldDescription, 'batch');
  567. }
  568. $this->configureListFields($mapper);
  569. foreach ($this->getExtensions() as $extension) {
  570. $extension->configureListFields($mapper);
  571. }
  572. if ($this->hasRequest() && $this->getRequest()->isXmlHttpRequest()) {
  573. $fieldDescription = $this->getModelManager()->getNewFieldDescriptionInstance($this->getClass(), 'select', array(
  574. 'label' => false,
  575. 'code' => '_select',
  576. 'sortable' => false,
  577. ));
  578. $fieldDescription->setAdmin($this);
  579. $fieldDescription->setTemplate($this->getTemplate('select'));
  580. $mapper->add($fieldDescription, 'select');
  581. }
  582. }
  583. /**
  584. * {@inheritdoc}
  585. */
  586. public function getFilterParameters()
  587. {
  588. $parameters = array();
  589. // build the values array
  590. if ($this->hasRequest()) {
  591. $filters = $this->request->query->get('filter', array());
  592. // if persisting filters, save filters to session, or pull them out of session if no new filters set
  593. if ($this->persistFilters) {
  594. if ($filters == array() && $this->request->query->get('filters') != 'reset') {
  595. $filters = $this->request->getSession()->get($this->getCode().'.filter.parameters', array());
  596. } else {
  597. $this->request->getSession()->set($this->getCode().'.filter.parameters', $filters);
  598. }
  599. }
  600. $parameters = array_merge(
  601. $this->getModelManager()->getDefaultSortValues($this->getClass()),
  602. $this->datagridValues,
  603. $filters
  604. );
  605. if (!$this->determinedPerPageValue($parameters['_per_page'])) {
  606. $parameters['_per_page'] = $this->maxPerPage;
  607. }
  608. // always force the parent value
  609. if ($this->isChild() && $this->getParentAssociationMapping()) {
  610. $parameters[$this->getParentAssociationMapping()] = array('value' => $this->request->get($this->getParent()->getIdParameter()));
  611. }
  612. }
  613. return $parameters;
  614. }
  615. /**
  616. * {@inheritdoc}
  617. */
  618. public function buildDatagrid()
  619. {
  620. if ($this->datagrid) {
  621. return;
  622. }
  623. $filterParameters = $this->getFilterParameters();
  624. // transform _sort_by from a string to a FieldDescriptionInterface for the datagrid.
  625. if (isset($filterParameters['_sort_by']) && is_string($filterParameters['_sort_by'])) {
  626. if ($this->hasListFieldDescription($filterParameters['_sort_by'])) {
  627. $filterParameters['_sort_by'] = $this->getListFieldDescription($filterParameters['_sort_by']);
  628. } else {
  629. $filterParameters['_sort_by'] = $this->getModelManager()->getNewFieldDescriptionInstance(
  630. $this->getClass(),
  631. $filterParameters['_sort_by'],
  632. array()
  633. );
  634. $this->getListBuilder()->buildField(null, $filterParameters['_sort_by'], $this);
  635. }
  636. }
  637. // initialize the datagrid
  638. $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $filterParameters);
  639. $this->datagrid->getPager()->setMaxPageLinks($this->maxPageLinks);
  640. $mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);
  641. // build the datagrid filter
  642. $this->configureDatagridFilters($mapper);
  643. // ok, try to limit to add parent filter
  644. if ($this->isChild() && $this->getParentAssociationMapping() && !$mapper->has($this->getParentAssociationMapping())) {
  645. $mapper->add($this->getParentAssociationMapping(), null, array(
  646. 'field_type' => 'sonata_type_model_reference',
  647. 'field_options' => array(
  648. 'model_manager' => $this->getModelManager()
  649. ),
  650. 'operator_type' => 'hidden'
  651. ));
  652. }
  653. foreach ($this->getExtensions() as $extension) {
  654. $extension->configureDatagridFilters($mapper);
  655. }
  656. }
  657. /**
  658. * Returns the name of the parent related field, so the field can be use to set the default
  659. * value (ie the parent object) or to filter the object
  660. *
  661. * @return string the name of the parent related field
  662. */
  663. public function getParentAssociationMapping()
  664. {
  665. return $this->parentAssociationMapping;
  666. }
  667. /**
  668. * Build the form FieldDescription collection
  669. *
  670. * @return void
  671. */
  672. protected function buildForm()
  673. {
  674. if ($this->form) {
  675. return;
  676. }
  677. // append parent object if any
  678. // todo : clean the way the Admin class can retrieve set the object
  679. if ($this->isChild() && $this->getParentAssociationMapping()) {
  680. $parent = $this->getParent()->getObject($this->request->get($this->getParent()->getIdParameter()));
  681. $propertyAccessor = PropertyAccess::getPropertyAccessor();
  682. $propertyPath = new PropertyPath($this->getParentAssociationMapping());
  683. $object = $this->getSubject();
  684. $propertyAccessor->setValue($object, $propertyPath, $parent);
  685. }
  686. $this->form = $this->getFormBuilder()->getForm();
  687. }
  688. /**
  689. * Returns the baseRoutePattern used to generate the routing information
  690. *
  691. * @throws \RuntimeException
  692. *
  693. * @return string the baseRoutePattern used to generate the routing information
  694. */
  695. public function getBaseRoutePattern()
  696. {
  697. if (!$this->baseRoutePattern) {
  698. preg_match(self::CLASS_REGEX, $this->getClass(), $matches);
  699. if (!$matches) {
  700. throw new \RuntimeException(sprintf('Please define a default `baseRoutePattern` value for the admin class `%s`', get_class($this)));
  701. }
  702. if ($this->isChild()) { // the admin class is a child, prefix it with the parent route name
  703. $this->baseRoutePattern = sprintf('%s/{id}/%s',
  704. $this->getParent()->getBaseRoutePattern(),
  705. $this->urlize($matches[5], '-')
  706. );
  707. } else {
  708. $this->baseRoutePattern = sprintf('/%s/%s/%s',
  709. $this->urlize($matches[1], '-'),
  710. $this->urlize($matches[3], '-'),
  711. $this->urlize($matches[5], '-')
  712. );
  713. }
  714. }
  715. return $this->baseRoutePattern;
  716. }
  717. /**
  718. * Returns the baseRouteName used to generate the routing information
  719. *
  720. * @throws \RuntimeException
  721. *
  722. * @return string the baseRouteName used to generate the routing information
  723. */
  724. public function getBaseRouteName()
  725. {
  726. if (!$this->baseRouteName) {
  727. preg_match(self::CLASS_REGEX, $this->getClass(), $matches);
  728. if (!$matches) {
  729. throw new \RuntimeException(sprintf('Cannot automatically determine base route name, please define a default `baseRouteName` value for the admin class `%s`', get_class($this)));
  730. }
  731. if ($this->isChild()) { // the admin class is a child, prefix it with the parent route name
  732. $this->baseRouteName = sprintf('%s_%s',
  733. $this->getParent()->getBaseRouteName(),
  734. $this->urlize($matches[5])
  735. );
  736. } else {
  737. $this->baseRouteName = sprintf('admin_%s_%s_%s',
  738. $this->urlize($matches[1]),
  739. $this->urlize($matches[3]),
  740. $this->urlize($matches[5])
  741. );
  742. }
  743. }
  744. return $this->baseRouteName;
  745. }
  746. /**
  747. * urlize the given word
  748. *
  749. * @param string $word
  750. * @param string $sep the separator
  751. *
  752. * @return string
  753. */
  754. public function urlize($word, $sep = '_')
  755. {
  756. return strtolower(preg_replace('/[^a-z0-9_]/i', $sep.'$1', $word));
  757. }
  758. /**
  759. * {@inheritdoc}
  760. */
  761. public function getClass()
  762. {
  763. return $this->class;
  764. }
  765. /**
  766. * Returns the list of supported sub classes
  767. *
  768. * @return array the list of sub classes
  769. */
  770. public function getSubClasses()
  771. {
  772. return $this->subClasses;
  773. }
  774. /**
  775. * Sets the list of supported sub classes
  776. *
  777. * @param array $subClasses the list of sub classes
  778. */
  779. public function setSubClasses(array $subClasses)
  780. {
  781. $this->subClasses = $subClasses;
  782. }
  783. /**
  784. * Gets the subclass corresponding to the given name
  785. *
  786. * @param string $name The name of the sub class
  787. *
  788. * @return string the subclass
  789. */
  790. protected function getSubClass($name)
  791. {
  792. if ($this->hasSubClass($name)) {
  793. return $this->subClasses[$name];
  794. }
  795. return null;
  796. }
  797. /**
  798. * Returns true if the admin has the sub classes
  799. *
  800. * @param string $name The name of the sub class
  801. *
  802. * @return bool
  803. */
  804. public function hasSubClass($name)
  805. {
  806. return isset($this->subClasses[$name]);
  807. }
  808. /**
  809. * Returns true if a subclass is currently active
  810. *
  811. * @return bool
  812. */
  813. public function hasActiveSubClass()
  814. {
  815. if ($this->request) {
  816. return null !== $this->getRequest()->get('subclass');
  817. }
  818. return false;
  819. }
  820. /**
  821. * Returns the currently active sub class
  822. *
  823. * @return string the active sub class
  824. */
  825. public function getActiveSubClass()
  826. {
  827. if (!$this->hasActiveSubClass()) {
  828. return null;
  829. }
  830. $subClass = $this->getRequest()->get('subclass');
  831. return $this->getSubClass($subClass);
  832. }
  833. /**
  834. * Returns the list of batchs actions
  835. *
  836. * @return array the list of batchs actions
  837. */
  838. public function getBatchActions()
  839. {
  840. $actions = array();
  841. if ($this->hasRoute('delete') && $this->isGranted('DELETE')) {
  842. $actions['delete'] = array(
  843. 'label' => $this->trans('action_delete', array(), 'SonataAdminBundle'),
  844. 'ask_confirmation' => true, // by default always true
  845. );
  846. }
  847. return $actions;
  848. }
  849. /**
  850. * Returns the list of available urls
  851. *
  852. * @return \Sonata\AdminBundle\Route\RouteCollection the list of available urls
  853. */
  854. public function getRoutes()
  855. {
  856. $this->buildRoutes();
  857. return $this->routes;
  858. }
  859. /**
  860. * {@inheritdoc}
  861. */
  862. public function getRouterIdParameter()
  863. {
  864. return $this->isChild() ? '{childId}' : '{id}';
  865. }
  866. /**
  867. * Returns the parameter representing request id, ie: id or childId
  868. *
  869. * @return string
  870. */
  871. public function getIdParameter()
  872. {
  873. return $this->isChild() ? 'childId' : 'id';
  874. }
  875. /**
  876. * Build all the related urls to the current admin
  877. *
  878. * @return void
  879. */
  880. public function buildRoutes()
  881. {
  882. if ($this->loaded['routes']) {
  883. return;
  884. }
  885. $this->loaded['routes'] = true;
  886. $this->routes = new RouteCollection(
  887. $this->getBaseCodeRoute(),
  888. $this->getBaseRouteName(),
  889. $this->getBaseRoutePattern(),
  890. $this->getBaseControllerName()
  891. );
  892. $this->routeBuilder->build($this, $this->routes);
  893. $this->configureRoutes($this->routes);
  894. foreach ($this->getExtensions() as $extension) {
  895. $extension->configureRoutes($this, $this->routes);
  896. }
  897. }
  898. /**
  899. * {@inheritdoc}
  900. */
  901. public function getRoute($name)
  902. {
  903. $this->buildRoutes();
  904. if (!$this->routes->has($name)) {
  905. return false;
  906. }
  907. return $this->routes->get($name);
  908. }
  909. /**
  910. * @param string $name
  911. *
  912. * @return bool
  913. */
  914. public function hasRoute($name)
  915. {
  916. $this->buildRoutes();
  917. if (
  918. ! $this->isChild()
  919. && strpos($name, '.') !== false
  920. && strpos($name, $this->getBaseCodeRoute() . '|') !== 0
  921. && strpos($name, $this->getBaseCodeRoute() . '.') !== 0
  922. ) {
  923. $name = $this->getCode() . '|' . $name;
  924. }
  925. return $this->routes->has($name);
  926. }
  927. /**
  928. * {@inheritdoc}
  929. */
  930. public function generateObjectUrl($name, $object, array $parameters = array(), $absolute = false)
  931. {
  932. $parameters['id'] = $this->getUrlsafeIdentifier($object);
  933. return $this->generateUrl($name, $parameters, $absolute);
  934. }
  935. /**
  936. * {@inheritdoc}
  937. */
  938. public function generateUrl($name, array $parameters = array(), $absolute = false)
  939. {
  940. return $this->routeGenerator->generateUrl($this, $name, $parameters, $absolute);
  941. }
  942. /**
  943. * @param array $templates
  944. *
  945. * @return void
  946. */
  947. public function setTemplates(array $templates)
  948. {
  949. $this->templates = $templates;
  950. }
  951. /**
  952. * @param string $name
  953. * @param string $template
  954. *
  955. * @return void
  956. */
  957. public function setTemplate($name, $template)
  958. {
  959. $this->templates[$name] = $template;
  960. }
  961. /**
  962. * @return array
  963. */
  964. public function getTemplates()
  965. {
  966. return $this->templates;
  967. }
  968. /**
  969. * @param string $name
  970. *
  971. * @return null|string
  972. */
  973. public function getTemplate($name)
  974. {
  975. if (isset($this->templates[$name])) {
  976. return $this->templates[$name];
  977. }
  978. return null;
  979. }
  980. /**
  981. * {@inheritdoc}
  982. */
  983. public function getNewInstance()
  984. {
  985. $object = $this->getModelManager()->getModelInstance($this->getActiveSubClass() ?: $this->getClass());
  986. foreach($this->getExtensions() as $extension) {
  987. $extension->alterNewInstance($this, $object);
  988. }
  989. return $object;
  990. }
  991. /**
  992. * {@inheritdoc}
  993. */
  994. public function getFormBuilder()
  995. {
  996. $this->formOptions['data_class'] = $this->getActiveSubClass() ?: $this->getClass();
  997. $formBuilder = $this->getFormContractor()->getFormBuilder(
  998. $this->getUniqid(),
  999. $this->formOptions
  1000. );
  1001. $this->defineFormBuilder($formBuilder);
  1002. return $formBuilder;
  1003. }
  1004. /**
  1005. * This method is being called by the main admin class and the child class,
  1006. * the getFormBuilder is only call by the main admin class
  1007. *
  1008. * @param \Symfony\Component\Form\FormBuilder $formBuilder
  1009. *
  1010. * @return void
  1011. */
  1012. public function defineFormBuilder(FormBuilder $formBuilder)
  1013. {
  1014. $mapper = new FormMapper($this->getFormContractor(), $formBuilder, $this);
  1015. $this->configureFormFields($mapper);
  1016. foreach ($this->getExtensions() as $extension) {
  1017. $extension->configureFormFields($mapper);
  1018. }
  1019. $this->attachInlineValidator();
  1020. }
  1021. /**
  1022. * Attach the inline validator to the model metadata, this must be done once per admin
  1023. */
  1024. protected function attachInlineValidator()
  1025. {
  1026. $admin = $this;
  1027. // add the custom inline validation option
  1028. $metadata = $this->validator->getMetadataFactory()->getMetadataFor($this->getClass());
  1029. $metadata->addConstraint(new InlineConstraint(array(
  1030. 'service' => $this,
  1031. 'method' => function(ErrorElement $errorElement, $object) use ($admin) {
  1032. /* @var \Sonata\AdminBundle\Admin\AdminInterface $admin */
  1033. // This avoid the main validation to be cascaded to children
  1034. // The problem occurs when a model Page has a collection of Page as property
  1035. if ($admin->hasSubject() && spl_object_hash($object) !== spl_object_hash($admin->getSubject())) {
  1036. return;
  1037. }
  1038. $admin->validate($errorElement, $object);
  1039. foreach ($admin->getExtensions() as $extension) {
  1040. $extension->validate($admin, $errorElement, $object);
  1041. }
  1042. }
  1043. )));
  1044. }
  1045. /**
  1046. * {@inheritdoc}
  1047. */
  1048. public function attachAdminClass(FieldDescriptionInterface $fieldDescription)
  1049. {
  1050. $pool = $this->getConfigurationPool();
  1051. $adminCode = $fieldDescription->getOption('admin_code');
  1052. if ($adminCode !== null) {
  1053. $admin = $pool->getAdminByAdminCode($adminCode);
  1054. } else {
  1055. $admin = $pool->getAdminByClass($fieldDescription->getTargetEntity());
  1056. }
  1057. if (!$admin) {
  1058. return;
  1059. }
  1060. if ($this->hasRequest()) {
  1061. $admin->setRequest($this->getRequest());
  1062. }
  1063. $fieldDescription->setAssociationAdmin($admin);
  1064. }
  1065. /**
  1066. * {@inheritdoc}
  1067. */
  1068. public function getObject($id)
  1069. {
  1070. return $this->getModelManager()->find($this->getClass(), $id);
  1071. }
  1072. /**
  1073. * Returns a form depend on the given $object
  1074. *
  1075. * @return \Symfony\Component\Form\Form
  1076. */
  1077. public function getForm()
  1078. {
  1079. $this->buildForm();
  1080. return $this->form;
  1081. }
  1082. /**
  1083. * {@inheritdoc}
  1084. */
  1085. public function getList()
  1086. {
  1087. $this->buildList();
  1088. return $this->list;
  1089. }
  1090. /**
  1091. * {@inheritdoc}
  1092. */
  1093. public function createQuery($context = 'list')
  1094. {
  1095. $query = $this->getModelManager()->createQuery($this->class);
  1096. foreach ($this->extensions as $extension) {
  1097. $extension->configureQuery($this, $query, $context);
  1098. }
  1099. return $query;
  1100. }
  1101. /**
  1102. * {@inheritdoc}
  1103. */
  1104. public function getDatagrid()
  1105. {
  1106. $this->buildDatagrid();
  1107. return $this->datagrid;
  1108. }
  1109. /**
  1110. * Build the side menu related to the current action
  1111. *
  1112. * @param string $action
  1113. * @param \Sonata\AdminBundle\Admin\AdminInterface $childAdmin
  1114. *
  1115. * @return \Knp\Menu\ItemInterface|boolean
  1116. */
  1117. public function buildSideMenu($action, AdminInterface $childAdmin = null)
  1118. {
  1119. if ($this->loaded['side_menu']) {
  1120. return;
  1121. }
  1122. $this->loaded['side_menu'] = true;
  1123. $menu = $this->menuFactory->createItem('root');
  1124. $menu->setChildrenAttribute('class', 'nav nav-list');
  1125. $menu->setCurrentUri($this->getRequest()->getBaseUrl().$this->getRequest()->getPathInfo());
  1126. $this->configureSideMenu($menu, $action, $childAdmin);
  1127. foreach ($this->getExtensions() as $extension) {
  1128. $extension->configureSideMenu($this, $menu, $action, $childAdmin);
  1129. }
  1130. $this->menu = $menu;
  1131. }
  1132. /**
  1133. * @param string $action
  1134. * @param \Sonata\AdminBundle\Admin\AdminInterface $childAdmin
  1135. *
  1136. * @return \Knp\Menu\ItemInterface
  1137. */
  1138. public function getSideMenu($action, AdminInterface $childAdmin = null)
  1139. {
  1140. if ($this->isChild()) {
  1141. return $this->getParent()->getSideMenu($action, $this);
  1142. }
  1143. $this->buildSideMenu($action, $childAdmin);
  1144. return $this->menu;
  1145. }
  1146. /**
  1147. * Returns the root code
  1148. *
  1149. * @return string the root code
  1150. */
  1151. public function getRootCode()
  1152. {
  1153. return $this->getRoot()->getCode();
  1154. }
  1155. /**
  1156. * Returns the master admin
  1157. *
  1158. * @return \Sonata\AdminBundle\Admin\Admin the root admin class
  1159. */
  1160. public function getRoot()
  1161. {
  1162. $parentFieldDescription = $this->getParentFieldDescription();
  1163. if (!$parentFieldDescription) {
  1164. return $this;
  1165. }
  1166. return $parentFieldDescription->getAdmin()->getRoot();
  1167. }
  1168. /**
  1169. * @param string $baseControllerName
  1170. */
  1171. public function setBaseControllerName($baseControllerName)
  1172. {
  1173. $this->baseControllerName = $baseControllerName;
  1174. }
  1175. /**
  1176. * @return string
  1177. */
  1178. public function getBaseControllerName()
  1179. {
  1180. return $this->baseControllerName;
  1181. }
  1182. /**
  1183. * @param string $label
  1184. */
  1185. public function setLabel($label)
  1186. {
  1187. $this->label = $label;
  1188. }
  1189. /**
  1190. * @return string
  1191. */
  1192. public function getLabel()
  1193. {
  1194. return $this->label;
  1195. }
  1196. /**
  1197. * @param boolean $persist
  1198. */
  1199. public function setPersistFilters($persist)
  1200. {
  1201. $this->persistFilters = $persist;
  1202. }
  1203. /**
  1204. * @param int $maxPerPage
  1205. */
  1206. public function setMaxPerPage($maxPerPage)
  1207. {
  1208. $this->maxPerPage = $maxPerPage;
  1209. }
  1210. /**
  1211. * @return int
  1212. */
  1213. public function getMaxPerPage()
  1214. {
  1215. return $this->maxPerPage;
  1216. }
  1217. /**
  1218. * @param int $maxPageLinks
  1219. */
  1220. public function setMaxPageLinks($maxPageLinks)
  1221. {
  1222. $this->maxPageLinks = $maxPageLinks;
  1223. }
  1224. /**
  1225. * @return int
  1226. */
  1227. public function getMaxPageLinks()
  1228. {
  1229. return $this->maxPageLinks;
  1230. }
  1231. /**
  1232. * {@inheritdoc}
  1233. */
  1234. public function getFormGroups()
  1235. {
  1236. return $this->formGroups;
  1237. }
  1238. /**
  1239. * {@inheritdoc}
  1240. */
  1241. public function setFormGroups(array $formGroups)
  1242. {
  1243. $this->formGroups = $formGroups;
  1244. }
  1245. /**
  1246. * @param array $group
  1247. * @param array $keys
  1248. */
  1249. public function reorderFormGroup($group, array $keys)
  1250. {
  1251. $formGroups = $this->getFormGroups();
  1252. $formGroups[$group]['fields'] = array_merge(array_flip($keys), $formGroups[$group]['fields']);
  1253. $this->setFormGroups($formGroups);
  1254. }
  1255. /**
  1256. * @return array
  1257. */
  1258. public function getShowGroups()
  1259. {
  1260. return $this->showGroups;
  1261. }
  1262. /**
  1263. * @param array $showGroups
  1264. */
  1265. public function setShowGroups(array $showGroups)
  1266. {
  1267. $this->showGroups = $showGroups;
  1268. }
  1269. /**
  1270. * @param string $group
  1271. * @param array $keys
  1272. */
  1273. public function reorderShowGroup($group, array $keys)
  1274. {
  1275. $showGroups = $this->getShowGroups();
  1276. $showGroups[$group]['fields'] = array_merge(array_flip($keys), $showGroups[$group]['fields']);
  1277. $this->setShowGroups($showGroups);
  1278. }
  1279. /**
  1280. * {@inheritdoc}
  1281. */
  1282. public function setParentFieldDescription(FieldDescriptionInterface $parentFieldDescription)
  1283. {
  1284. $this->parentFieldDescription = $parentFieldDescription;
  1285. }
  1286. /**
  1287. *
  1288. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface the parent field description
  1289. */
  1290. public function getParentFieldDescription()
  1291. {
  1292. return $this->parentFieldDescription;
  1293. }
  1294. /**
  1295. * Returns true if the Admin is linked to a parent FieldDescription
  1296. *
  1297. * @return bool
  1298. */
  1299. public function hasParentFieldDescription()
  1300. {
  1301. return $this->parentFieldDescription instanceof FieldDescriptionInterface;
  1302. }
  1303. /**
  1304. * {@inheritdoc}
  1305. */
  1306. public function setSubject($subject)
  1307. {
  1308. $this->subject = $subject;
  1309. }
  1310. /**
  1311. * {@inheritdoc}
  1312. */
  1313. public function getSubject()
  1314. {
  1315. if ($this->subject === null && $this->request) {
  1316. $id = $this->request->get($this->getIdParameter());
  1317. if (!preg_match('#^[0-9A-Fa-f]+$#', $id)) {
  1318. $this->subject = false;
  1319. } else {
  1320. $this->subject = $this->getModelManager()->find($this->getClass(), $id);
  1321. }
  1322. }
  1323. return $this->subject;
  1324. }
  1325. /**
  1326. * {@inheritdoc}
  1327. */
  1328. public function hasSubject()
  1329. {
  1330. return $this->subject != null;
  1331. }
  1332. /**
  1333. * build and return the collection of form FieldDescription
  1334. *
  1335. * @return array collection of form FieldDescription
  1336. */
  1337. public function getFormFieldDescriptions()
  1338. {
  1339. $this->buildForm();
  1340. return $this->formFieldDescriptions;
  1341. }
  1342. /**
  1343. * {@inheritdoc}
  1344. */
  1345. public function getFormFieldDescription($name)
  1346. {
  1347. return $this->hasFormFieldDescription($name) ? $this->formFieldDescriptions[$name] : null;
  1348. }
  1349. /**
  1350. * Returns true if the admin has a FieldDescription with the given $name
  1351. *
  1352. * @param string $name
  1353. *
  1354. * @return bool
  1355. */
  1356. public function hasFormFieldDescription($name)
  1357. {
  1358. return array_key_exists($name, $this->formFieldDescriptions) ? true : false;
  1359. }
  1360. /**
  1361. * {@inheritdoc}
  1362. */
  1363. public function addFormFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1364. {
  1365. $this->formFieldDescriptions[$name] = $fieldDescription;
  1366. }
  1367. /**
  1368. * remove a FieldDescription
  1369. *
  1370. * @param string $name
  1371. *
  1372. * @return void
  1373. */
  1374. public function removeFormFieldDescription($name)
  1375. {
  1376. unset($this->formFieldDescriptions[$name]);
  1377. }
  1378. /**
  1379. * build and return the collection of form FieldDescription
  1380. *
  1381. * @return array collection of form FieldDescription
  1382. */
  1383. public function getShowFieldDescriptions()
  1384. {
  1385. $this->buildShow();
  1386. return $this->showFieldDescriptions;
  1387. }
  1388. /**
  1389. * Returns the form FieldDescription with the given $name
  1390. *
  1391. * @param string $name
  1392. *
  1393. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  1394. */
  1395. public function getShowFieldDescription($name)
  1396. {
  1397. $this->buildShow();
  1398. return $this->hasShowFieldDescription($name) ? $this->showFieldDescriptions[$name] : null;
  1399. }
  1400. /**
  1401. * Returns true if the admin has a FieldDescription with the given $name
  1402. *
  1403. * @param string $name
  1404. *
  1405. * @return bool
  1406. */
  1407. public function hasShowFieldDescription($name)
  1408. {
  1409. return array_key_exists($name, $this->showFieldDescriptions);
  1410. }
  1411. /**
  1412. * {@inheritdoc}
  1413. */
  1414. public function addShowFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1415. {
  1416. $this->showFieldDescriptions[$name] = $fieldDescription;
  1417. }
  1418. /**
  1419. * remove a FieldDescription
  1420. *
  1421. * @param string $name
  1422. *
  1423. * @return void
  1424. */
  1425. public function removeShowFieldDescription($name)
  1426. {
  1427. unset($this->showFieldDescriptions[$name]);
  1428. }
  1429. /**
  1430. * Returns the collection of list FieldDescriptions
  1431. *
  1432. * @return array
  1433. */
  1434. public function getListFieldDescriptions()
  1435. {
  1436. $this->buildList();
  1437. return $this->listFieldDescriptions;
  1438. }
  1439. /**
  1440. * {@inheritdoc}
  1441. */
  1442. public function getListFieldDescription($name)
  1443. {
  1444. return $this->hasListFieldDescription($name) ? $this->listFieldDescriptions[$name] : null;
  1445. }
  1446. /**
  1447. * Returns true if the list FieldDescription exists
  1448. *
  1449. * @param string $name
  1450. *
  1451. * @return bool
  1452. */
  1453. public function hasListFieldDescription($name)
  1454. {
  1455. $this->buildList();
  1456. return array_key_exists($name, $this->listFieldDescriptions) ? true : false;
  1457. }
  1458. /**
  1459. * {@inheritdoc}
  1460. */
  1461. public function addListFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1462. {
  1463. $this->listFieldDescriptions[$name] = $fieldDescription;
  1464. }
  1465. /**
  1466. * remove a list FieldDescription
  1467. *
  1468. * @param string $name
  1469. *
  1470. * @return void
  1471. */
  1472. public function removeListFieldDescription($name)
  1473. {
  1474. unset($this->listFieldDescriptions[$name]);
  1475. }
  1476. /**
  1477. * Returns a filter FieldDescription
  1478. *
  1479. * @param string $name
  1480. *
  1481. * @return array|null
  1482. */
  1483. public function getFilterFieldDescription($name)
  1484. {
  1485. return $this->hasFilterFieldDescription($name) ? $this->filterFieldDescriptions[$name] : null;
  1486. }
  1487. /**
  1488. * Returns true if the filter FieldDescription exists
  1489. *
  1490. * @param string $name
  1491. *
  1492. * @return bool
  1493. */
  1494. public function hasFilterFieldDescription($name)
  1495. {
  1496. return array_key_exists($name, $this->filterFieldDescriptions) ? true : false;
  1497. }
  1498. /**
  1499. * {@inheritdoc}
  1500. */
  1501. public function addFilterFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1502. {
  1503. $this->filterFieldDescriptions[$name] = $fieldDescription;
  1504. }
  1505. /**
  1506. * remove a filter FieldDescription
  1507. *
  1508. * @param string $name
  1509. */
  1510. public function removeFilterFieldDescription($name)
  1511. {
  1512. unset($this->filterFieldDescriptions[$name]);
  1513. }
  1514. /**
  1515. * Returns the filter FieldDescription collection
  1516. *
  1517. * @return FieldDescriptionInterface[]
  1518. */
  1519. public function getFilterFieldDescriptions()
  1520. {
  1521. $this->buildDatagrid();
  1522. return $this->filterFieldDescriptions;
  1523. }
  1524. /**
  1525. * {@inheritdoc}
  1526. */
  1527. public function addChild(AdminInterface $child)
  1528. {
  1529. $this->children[$child->getCode()] = $child;
  1530. $child->setBaseCodeRoute($this->getCode().'|'.$child->getCode());
  1531. $child->setParent($this);
  1532. }
  1533. /**
  1534. * {@inheritdoc}
  1535. */
  1536. public function hasChild($code)
  1537. {
  1538. return isset($this->children[$code]);
  1539. }
  1540. /**
  1541. * {@inheritdoc}
  1542. */
  1543. public function getChildren()
  1544. {
  1545. return $this->children;
  1546. }
  1547. /**
  1548. * {@inheritdoc}
  1549. */
  1550. public function getChild($code)
  1551. {
  1552. return $this->hasChild($code) ? $this->children[$code] : null;
  1553. }
  1554. /**
  1555. * {@inheritdoc}
  1556. */
  1557. public function setParent(AdminInterface $parent)
  1558. {
  1559. $this->parent = $parent;
  1560. }
  1561. /**
  1562. * {@inheritdoc}
  1563. */
  1564. public function getParent()
  1565. {
  1566. return $this->parent;
  1567. }
  1568. /**
  1569. * Returns true if the Admin class has an Parent Admin defined
  1570. *
  1571. * @return boolean
  1572. */
  1573. public function isChild()
  1574. {
  1575. return $this->parent instanceof AdminInterface;
  1576. }
  1577. /**
  1578. * Returns true if the admin has children, false otherwise
  1579. *
  1580. * @return bool if the admin has children
  1581. */
  1582. public function hasChildren()
  1583. {
  1584. return count($this->children) > 0;
  1585. }
  1586. /**
  1587. * {@inheritdoc}
  1588. */
  1589. public function setUniqid($uniqid)
  1590. {
  1591. $this->uniqid = $uniqid;
  1592. }
  1593. /**
  1594. * Returns the uniqid
  1595. *
  1596. * @return integer
  1597. */
  1598. public function getUniqid()
  1599. {
  1600. return $this->uniqid;
  1601. }
  1602. /**
  1603. * Returns the classname label
  1604. *
  1605. * @return string the classname label
  1606. */
  1607. public function getClassnameLabel()
  1608. {
  1609. return $this->classnameLabel;
  1610. }
  1611. /**
  1612. * Returns an array of persistent parameters
  1613. *
  1614. * @return array
  1615. */
  1616. public function getPersistentParameters()
  1617. {
  1618. return array();
  1619. }
  1620. /**
  1621. * @param string $name
  1622. *
  1623. * @return null|mixed
  1624. */
  1625. public function getPersistentParameter($name)
  1626. {
  1627. $parameters = $this->getPersistentParameters();
  1628. return isset($parameters[$name]) ? $parameters[$name] : null;
  1629. }
  1630. /**
  1631. * @param string $action
  1632. *
  1633. * @return array
  1634. */
  1635. public function getBreadcrumbs($action)
  1636. {
  1637. if ($this->isChild()) {
  1638. return $this->getParent()->getBreadcrumbs($action);
  1639. }
  1640. $menu = $this->buildBreadcrumbs($action);
  1641. do {
  1642. $breadcrumbs[] = $menu;
  1643. } while ($menu = $menu->getParent());
  1644. $breadcrumbs = array_reverse($breadcrumbs);
  1645. array_shift($breadcrumbs);
  1646. return $breadcrumbs;
  1647. }
  1648. /**
  1649. * Generates the breadcrumbs array
  1650. *
  1651. * Note: the method will be called by the top admin instance (parent => child)
  1652. *
  1653. * @param string $action
  1654. * @param \Knp\Menu\ItemInterface|null $menu
  1655. *
  1656. * @return array
  1657. */
  1658. public function buildBreadcrumbs($action, MenuItemInterface $menu = null)
  1659. {
  1660. if (isset($this->breadcrumbs[$action])) {
  1661. return $this->breadcrumbs[$action];
  1662. }
  1663. if (!$menu) {
  1664. $menu = $this->menuFactory->createItem('root');
  1665. $menu = $menu->addChild(
  1666. $this->trans($this->getLabelTranslatorStrategy()->getLabel('dashboard', 'breadcrumb', 'link'), array(), 'SonataAdminBundle'),
  1667. array('uri' => $this->routeGenerator->generate('sonata_admin_dashboard'))
  1668. );
  1669. }
  1670. $menu = $menu->addChild(
  1671. $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_list', $this->getClassnameLabel()), 'breadcrumb', 'link')),
  1672. array('uri' => $this->hasRoute('list') && $this->isGranted('LIST') ? $this->generateUrl('list') : null)
  1673. );
  1674. $childAdmin = $this->getCurrentChildAdmin();
  1675. if ($childAdmin) {
  1676. $id = $this->request->get($this->getIdParameter());
  1677. $menu = $menu->addChild(
  1678. $this->toString($this->getSubject()),
  1679. array('uri' => $this->hasRoute('edit') && $this->isGranted('EDIT') ? $this->generateUrl('edit', array('id' => $id)) : null)
  1680. );
  1681. return $childAdmin->buildBreadcrumbs($action, $menu);
  1682. } elseif ($this->isChild()) {
  1683. if ($action == 'list') {
  1684. $menu->setUri(false);
  1685. } elseif ($action != 'create' && $this->hasSubject()) {
  1686. $menu = $menu->addChild($this->toString($this->getSubject()));
  1687. } else {
  1688. $menu = $menu->addChild(
  1689. $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_%s', $this->getClassnameLabel(), $action), 'breadcrumb', 'link'))
  1690. );
  1691. }
  1692. } elseif ($action != 'list' && $this->hasSubject()) {
  1693. $menu = $menu->addChild($this->toString($this->getSubject()));
  1694. } elseif ($action != 'list') {
  1695. $menu = $menu->addChild(
  1696. $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_%s', $this->getClassnameLabel(), $action), 'breadcrumb', 'link'))
  1697. );
  1698. } else {
  1699. $menu->getBreadcrumbsArray();
  1700. }
  1701. return $this->breadcrumbs[$action] = $menu;
  1702. }
  1703. /**
  1704. * set the current child status
  1705. *
  1706. * @param boolean $currentChild
  1707. *
  1708. * @return void
  1709. */
  1710. public function setCurrentChild($currentChild)
  1711. {
  1712. $this->currentChild = $currentChild;
  1713. }
  1714. /**
  1715. * Returns the current child status
  1716. *
  1717. * @return bool
  1718. */
  1719. public function getCurrentChild()
  1720. {
  1721. return $this->currentChild;
  1722. }
  1723. /**
  1724. * Returns the current child admin instance
  1725. *
  1726. * @return \Sonata\AdminBundle\Admin\AdminInterface|null the current child admin instance
  1727. */
  1728. public function getCurrentChildAdmin()
  1729. {
  1730. foreach ($this->children as $children) {
  1731. if ($children->getCurrentChild()) {
  1732. return $children;
  1733. }
  1734. }
  1735. return null;
  1736. }
  1737. /**
  1738. * {@inheritdoc}
  1739. */
  1740. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  1741. {
  1742. $domain = $domain ?: $this->translationDomain;
  1743. if (!$this->translator) {
  1744. return

Large files files are truncated, but you can click here to view the full file