Admin.php 53 KB

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