Admin.php 50 KB

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