Admin.php 55 KB

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