Admin.php 51 KB

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