Admin.php 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  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. public function setSecurityHandler(SecurityHandlerInterface $securityHandler)
  1698. {
  1699. $this->securityHandler = $securityHandler;
  1700. }
  1701. public function getSecurityHandler()
  1702. {
  1703. return $this->securityHandler;
  1704. }
  1705. /**
  1706. * @param string $name
  1707. * @return boolean
  1708. */
  1709. public function isGranted($name)
  1710. {
  1711. return $this->securityHandler->isGranted($name, $this);
  1712. }
  1713. public function getNormalizedIdentifier($entity)
  1714. {
  1715. return $this->getModelManager()->getNormalizedIdentifier($entity);
  1716. }
  1717. /**
  1718. * Shorthand method for templating
  1719. *
  1720. * @param object $entity
  1721. * @return mixed
  1722. */
  1723. public function id($entity)
  1724. {
  1725. return $this->getNormalizedIdentifier($entity);
  1726. }
  1727. /**
  1728. * @param \Symfony\Component\Validator\ValidatorInterface $validator
  1729. * @return void
  1730. */
  1731. public function setValidator(ValidatorInterface $validator)
  1732. {
  1733. $this->validator = $validator;
  1734. }
  1735. /**
  1736. * @return \Symfony\Component\Validator\ValidatorInterface
  1737. */
  1738. public function getValidator()
  1739. {
  1740. return $this->validator;
  1741. }
  1742. /**
  1743. * @return array
  1744. */
  1745. public function getShow()
  1746. {
  1747. $this->buildShow();
  1748. return $this->show;
  1749. }
  1750. /**
  1751. * @param array $formTheme
  1752. * @return void
  1753. */
  1754. public function setFormTheme(array $formTheme)
  1755. {
  1756. $this->formTheme = $formTheme;
  1757. }
  1758. /**
  1759. * @return array
  1760. */
  1761. public function getFormTheme()
  1762. {
  1763. return $this->formTheme;
  1764. }
  1765. /**
  1766. * @param array $filterTheme
  1767. * @return void
  1768. */
  1769. public function setFilterTheme(array $filterTheme)
  1770. {
  1771. $this->filterTheme = $filterTheme;
  1772. }
  1773. /**
  1774. * @return array
  1775. */
  1776. public function getFilterTheme()
  1777. {
  1778. return $this->filterTheme;
  1779. }
  1780. /**
  1781. * @param AdminExtensionInterface $extension
  1782. * @return void
  1783. */
  1784. public function addExtension(AdminExtensionInterface $extension)
  1785. {
  1786. $this->extensions[] = $extension;
  1787. }
  1788. /**
  1789. * @param \Knp\Menu\FactoryInterface $menuFactory
  1790. * @return void
  1791. */
  1792. public function setMenuFactory(MenuFactoryInterface $menuFactory)
  1793. {
  1794. $this->menuFactory = $menuFactory;
  1795. }
  1796. /**
  1797. * @return \Knp\Menu\FactoryInterface
  1798. */
  1799. public function getMenuFactory()
  1800. {
  1801. return $this->menuFactory;
  1802. }
  1803. }