Admin.php 48 KB

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