PageRenderTime 67ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/Admin/Admin.php

https://github.com/dunglas/SonataAdminBundle
PHP | 2187 lines | 1184 code | 251 blank | 752 comment | 37 complexity | fbde8263b7581cc26d1359cd89fbd464 MD5 | raw file
Possible License(s): Apache-2.0, JSON

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

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