Admin.php 51 KB

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