Admin.php 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693
  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\PropertyAccess\PropertyPath;
  14. use Symfony\Component\PropertyAccess\PropertyAccess;
  15. use Symfony\Component\Validator\ValidatorInterface;
  16. use Symfony\Component\Translation\TranslatorInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\Security\Acl\Model\DomainObjectInterface;
  19. use Sonata\AdminBundle\Form\FormMapper;
  20. use Sonata\AdminBundle\Datagrid\ListMapper;
  21. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  22. use Sonata\AdminBundle\Show\ShowMapper;
  23. use Sonata\AdminBundle\Admin\Pool;
  24. use Sonata\AdminBundle\Validator\ErrorElement;
  25. use Sonata\AdminBundle\Validator\Constraints\InlineConstraint;
  26. use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
  27. use Sonata\AdminBundle\Builder\FormContractorInterface;
  28. use Sonata\AdminBundle\Builder\ListBuilderInterface;
  29. use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
  30. use Sonata\AdminBundle\Builder\ShowBuilderInterface;
  31. use Sonata\AdminBundle\Builder\RouteBuilderInterface;
  32. use Sonata\AdminBundle\Route\RouteGeneratorInterface;
  33. use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
  34. use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
  35. use Sonata\AdminBundle\Route\RouteCollection;
  36. use Sonata\AdminBundle\Model\ModelManagerInterface;
  37. use Knp\Menu\FactoryInterface as MenuFactoryInterface;
  38. use Knp\Menu\ItemInterface as MenuItemInterface;
  39. use Doctrine\Common\Util\ClassUtils;
  40. abstract class Admin implements AdminInterface, DomainObjectInterface
  41. {
  42. const CONTEXT_MENU = 'menu';
  43. const CONTEXT_DASHBOARD = 'dashboard';
  44. const CLASS_REGEX = '@([A-Za-z0-9]*)\\\(Bundle\\\)?([A-Za-z0-9]+)Bundle\\\(Entity|Document|Model|PHPCR|CouchDocument|Phpcr|Doctrine\\\Orm|Doctrine\\\Phpcr|Doctrine\\\MongoDB|Doctrine\\\CouchDB)\\\(.*)@';
  45. /**
  46. * The class name managed by the admin class
  47. *
  48. * @var string
  49. */
  50. private $class;
  51. /**
  52. * The subclasses supported by the admin class
  53. *
  54. * @var array
  55. */
  56. private $subClasses = array();
  57. /**
  58. * The list collection
  59. *
  60. * @var array
  61. */
  62. private $list;
  63. /**
  64. * The list FieldDescription constructed from the configureListField method
  65. *
  66. * @var array
  67. */
  68. protected $listFieldDescriptions = array();
  69. private $show;
  70. /**
  71. * The show FieldDescription constructed from the configureShowFields method
  72. *
  73. * @var array
  74. */
  75. protected $showFieldDescriptions = array();
  76. /**
  77. * @var Form
  78. */
  79. private $form;
  80. /**
  81. * The list FieldDescription constructed from the configureFormField method
  82. *
  83. * @var array
  84. */
  85. protected $formFieldDescriptions = array();
  86. /**
  87. * @var \Sonata\AdminBundle\Datagrid\DatagridInterface
  88. */
  89. private $filter;
  90. /**
  91. * The filter FieldDescription constructed from the configureFilterField method
  92. *
  93. * @var array
  94. */
  95. protected $filterFieldDescriptions = array();
  96. /**
  97. * The number of result to display in the list
  98. *
  99. * @var integer
  100. */
  101. protected $maxPerPage = 25;
  102. /**
  103. * The maximum number of page numbers to display in the list
  104. *
  105. * @var integer
  106. */
  107. protected $maxPageLinks = 25;
  108. /**
  109. * The base route name used to generate the routing information
  110. *
  111. * @var string
  112. */
  113. protected $baseRouteName;
  114. /**
  115. * The base route pattern used to generate the routing information
  116. *
  117. * @var string
  118. */
  119. protected $baseRoutePattern;
  120. /**
  121. * The base name controller used to generate the routing information
  122. *
  123. * @var string
  124. */
  125. protected $baseControllerName;
  126. /**
  127. * The form group disposition
  128. *
  129. * @var array|boolean
  130. */
  131. private $formGroups = false;
  132. /**
  133. * The view group disposition
  134. *
  135. * @var array|boolean
  136. */
  137. private $showGroups = false;
  138. /**
  139. * The label class name (used in the title/breadcrumb ...)
  140. *
  141. * @var string
  142. */
  143. protected $classnameLabel;
  144. /**
  145. * The translation domain to be used to translate messages
  146. *
  147. * @var string
  148. */
  149. protected $translationDomain = 'messages';
  150. /**
  151. * Options to set to the form (ie, validation_groups)
  152. *
  153. * @var array
  154. */
  155. protected $formOptions = array();
  156. /**
  157. * Default values to the datagrid
  158. *
  159. * @var array
  160. */
  161. protected $datagridValues = array(
  162. '_page' => 1,
  163. '_per_page' => 25,
  164. );
  165. /**
  166. * Predefined per page options
  167. *
  168. * @var array
  169. */
  170. protected $perPageOptions = array(15, 25, 50, 100, 150, 200);
  171. /**
  172. * The code related to the admin
  173. *
  174. * @var string
  175. */
  176. protected $code;
  177. /**
  178. * The label
  179. *
  180. * @var string
  181. */
  182. protected $label;
  183. /**
  184. * Whether or not to persist the filters in the session
  185. *
  186. * @var boolean
  187. */
  188. protected $persistFilters = false;
  189. /**
  190. * Array of routes related to this admin
  191. *
  192. * @var \Sonata\AdminBundle\Route\RouteCollection
  193. */
  194. protected $routes;
  195. /**
  196. * The subject only set in edit/update/create mode
  197. *
  198. * @var object
  199. */
  200. protected $subject;
  201. /**
  202. * Define a Collection of child admin, ie /admin/order/{id}/order-element/{childId}
  203. *
  204. * @var array
  205. */
  206. protected $children = array();
  207. /**
  208. * Reference the parent collection
  209. *
  210. * @var Admin
  211. */
  212. protected $parent = null;
  213. /**
  214. * The base code route refer to the prefix used to generate the route name
  215. *
  216. * @var string
  217. */
  218. protected $baseCodeRoute = '';
  219. /**
  220. * The related field reflection, ie if OrderElement is linked to Order,
  221. * then the $parentReflectionProperty must be the ReflectionProperty of
  222. * the order (OrderElement::$order)
  223. *
  224. * @var \ReflectionProperty $parentReflectionProperty
  225. */
  226. protected $parentAssociationMapping = null;
  227. /**
  228. * Reference the parent FieldDescription related to this admin
  229. * only set for FieldDescription which is associated to an Sub Admin instance
  230. *
  231. * @var FieldDescriptionInterface
  232. */
  233. protected $parentFieldDescription;
  234. /**
  235. * If true then the current admin is part of the nested admin set (from the url)
  236. *
  237. * @var boolean
  238. */
  239. protected $currentChild = false;
  240. /**
  241. * The uniqid is used to avoid clashing with 2 admin related to the code
  242. * ie: a Block linked to a Block
  243. *
  244. * @var string
  245. */
  246. protected $uniqid;
  247. /**
  248. * The Entity or Document manager
  249. *
  250. * @var \Sonata\AdminBundle\Model\ModelManagerInterface
  251. */
  252. protected $modelManager;
  253. /**
  254. * The manager type to use for the admin
  255. *
  256. * @var string
  257. */
  258. private $managerType;
  259. /**
  260. * The current request object
  261. *
  262. * @var \Symfony\Component\HttpFoundation\Request
  263. */
  264. protected $request;
  265. /**
  266. * The translator component
  267. *
  268. * @var \Symfony\Component\Translation\TranslatorInterface
  269. */
  270. protected $translator;
  271. /**
  272. * The related form contractor
  273. *
  274. * @var \Sonata\AdminBundle\Builder\FormContractorInterface
  275. */
  276. protected $formContractor;
  277. /**
  278. * The related list builder
  279. *
  280. * @var \Sonata\AdminBundle\Builder\ListBuilderInterface
  281. */
  282. protected $listBuilder;
  283. /**
  284. * The related view builder
  285. *
  286. * @var ShowBuilderInterface
  287. */
  288. protected $showBuilder;
  289. /**
  290. * The related datagrid builder
  291. *
  292. * @var \Sonata\AdminBundle\Builder\DatagridBuilderInterface
  293. */
  294. protected $datagridBuilder;
  295. /**
  296. * @var \Sonata\AdminBundle\Builder\RouteBuilderInterface
  297. */
  298. protected $routeBuilder;
  299. /**
  300. * The datagrid instance
  301. *
  302. * @var \Sonata\AdminBundle\Datagrid\DatagridInterface
  303. */
  304. protected $datagrid;
  305. /**
  306. * The router instance
  307. *
  308. * @var RouteGeneratorInterface
  309. */
  310. protected $routeGenerator;
  311. /**
  312. * The generated breadcrumbs
  313. *
  314. * @var array
  315. */
  316. protected $breadcrumbs = array();
  317. /**
  318. * @var SecurityHandlerInterface
  319. */
  320. protected $securityHandler = null;
  321. /**
  322. * @var ValidatorInterface $validator
  323. */
  324. protected $validator = null;
  325. /**
  326. * The configuration pool
  327. *
  328. * @var Pool
  329. */
  330. protected $configurationPool;
  331. /**
  332. * @var MenuItemInterface
  333. */
  334. protected $menu;
  335. /**
  336. * @var MenuFactoryInterface
  337. */
  338. protected $menuFactory;
  339. /**
  340. * @var array
  341. */
  342. protected $loaded = array(
  343. 'view_fields' => false,
  344. 'view_groups' => false,
  345. 'routes' => false,
  346. 'side_menu' => false,
  347. );
  348. /**
  349. * @var array
  350. */
  351. protected $formTheme = array();
  352. /**
  353. * @var array
  354. */
  355. protected $filterTheme = array();
  356. /**
  357. * @var array
  358. */
  359. protected $templates = array();
  360. /**
  361. * @var AdminExtensionInterface[]
  362. */
  363. protected $extensions = array();
  364. /**
  365. * @var LabelTranslatorStrategyInterface
  366. */
  367. protected $labelTranslatorStrategy;
  368. /**
  369. * Setting to true will enable preview mode for
  370. * the entity and show a preview button in the
  371. * edit/create forms
  372. *
  373. * @var boolean
  374. */
  375. protected $supportsPreviewMode = false;
  376. /**
  377. * Roles and permissions per role
  378. *
  379. * @var array [role] => array([permission], [permission])
  380. */
  381. protected $securityInformation = array();
  382. /**
  383. * {@inheritdoc}
  384. */
  385. protected function configureFormFields(FormMapper $form)
  386. {
  387. }
  388. /**
  389. * {@inheritdoc}
  390. */
  391. protected function configureListFields(ListMapper $list)
  392. {
  393. }
  394. /**
  395. * {@inheritdoc}
  396. */
  397. protected function configureDatagridFilters(DatagridMapper $filter)
  398. {
  399. }
  400. /**
  401. * @deprecated removed with Symfony 2.2
  402. *
  403. * {@inheritdoc}
  404. */
  405. protected function configureShowField(ShowMapper $show)
  406. {
  407. }
  408. /**
  409. * {@inheritdoc}
  410. */
  411. protected function configureShowFields(ShowMapper $filter)
  412. {
  413. }
  414. /**
  415. * {@inheritdoc}
  416. */
  417. protected function configureRoutes(RouteCollection $collection)
  418. {
  419. }
  420. /**
  421. * {@inheritdoc}
  422. */
  423. protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
  424. {
  425. }
  426. /**
  427. * {@inheritdoc}
  428. */
  429. public function getExportFormats()
  430. {
  431. return array(
  432. 'json', 'xml', 'csv', 'xls'
  433. );
  434. }
  435. /**
  436. * @return array
  437. */
  438. public function getExportFields()
  439. {
  440. return $this->getModelManager()->getExportFields($this->getClass());
  441. }
  442. /**
  443. * {@inheritdoc}
  444. */
  445. public function getDataSourceIterator()
  446. {
  447. $datagrid = $this->getDatagrid();
  448. $datagrid->buildPager();
  449. return $this->getModelManager()->getDataSourceIterator($datagrid, $this->getExportFields());
  450. }
  451. /**
  452. * {@inheritdoc}
  453. */
  454. public function validate(ErrorElement $errorElement, $object)
  455. {
  456. }
  457. /**
  458. * @param string $code
  459. * @param string $class
  460. * @param string $baseControllerName
  461. */
  462. public function __construct($code, $class, $baseControllerName)
  463. {
  464. $this->code = $code;
  465. $this->class = $class;
  466. $this->baseControllerName = $baseControllerName;
  467. $this->predefinePerPageOptions();
  468. $this->datagridValues['_per_page'] = $this->maxPerPage;
  469. }
  470. /**
  471. * define custom variable
  472. */
  473. public function initialize()
  474. {
  475. if (!$this->classnameLabel) {
  476. $this->classnameLabel = substr($this->getClass(), strrpos($this->getClass(), '\\') + 1);
  477. }
  478. $this->baseCodeRoute = $this->getCode();
  479. $this->configure();
  480. }
  481. /**
  482. * {@inheritdoc}
  483. */
  484. public function configure()
  485. {
  486. }
  487. /**
  488. * {@inheritdoc}
  489. */
  490. public function update($object)
  491. {
  492. $this->preUpdate($object);
  493. foreach ($this->extensions as $extension) {
  494. $extension->preUpdate($this, $object);
  495. }
  496. $this->getModelManager()->update($object);
  497. $this->postUpdate($object);
  498. foreach ($this->extensions as $extension) {
  499. $extension->postUpdate($this, $object);
  500. }
  501. }
  502. /**
  503. * {@inheritdoc}
  504. */
  505. public function create($object)
  506. {
  507. $this->prePersist($object);
  508. foreach ($this->extensions as $extension) {
  509. $extension->prePersist($this, $object);
  510. }
  511. $this->getModelManager()->create($object);
  512. $this->postPersist($object);
  513. foreach ($this->extensions as $extension) {
  514. $extension->postPersist($this, $object);
  515. }
  516. $this->createObjectSecurity($object);
  517. }
  518. /**
  519. * {@inheritdoc}
  520. */
  521. public function delete($object)
  522. {
  523. $this->preRemove($object);
  524. foreach ($this->extensions as $extension) {
  525. $extension->preRemove($this, $object);
  526. }
  527. $this->getSecurityHandler()->deleteObjectSecurity($this, $object);
  528. $this->getModelManager()->delete($object);
  529. $this->postRemove($object);
  530. foreach ($this->extensions as $extension) {
  531. $extension->postRemove($this, $object);
  532. }
  533. }
  534. /**
  535. * {@inheritdoc}
  536. */
  537. public function preUpdate($object)
  538. {}
  539. /**
  540. * {@inheritdoc}
  541. */
  542. public function postUpdate($object)
  543. {}
  544. /**
  545. * {@inheritdoc}
  546. */
  547. public function prePersist($object)
  548. {}
  549. /**
  550. * {@inheritdoc}
  551. */
  552. public function postPersist($object)
  553. {}
  554. /**
  555. * {@inheritdoc}
  556. */
  557. public function preRemove($object)
  558. {}
  559. /**
  560. * {@inheritdoc}
  561. */
  562. public function postRemove($object)
  563. {}
  564. /**
  565. * build the view FieldDescription array
  566. *
  567. * @return void
  568. */
  569. protected function buildShow()
  570. {
  571. if ($this->show) {
  572. return;
  573. }
  574. $this->show = new FieldDescriptionCollection();
  575. $mapper = new ShowMapper($this->showBuilder, $this->show, $this);
  576. $this->configureShowField($mapper); // deprecated, use configureShowFields instead
  577. $this->configureShowFields($mapper);
  578. foreach ($this->getExtensions() as $extension) {
  579. $extension->configureShowFields($mapper);
  580. }
  581. }
  582. /**
  583. * build the list FieldDescription array
  584. *
  585. * @return void
  586. */
  587. protected function buildList()
  588. {
  589. if ($this->list) {
  590. return;
  591. }
  592. $this->list = $this->getListBuilder()->getBaseList();
  593. $mapper = new ListMapper($this->getListBuilder(), $this->list, $this);
  594. if (count($this->getBatchActions()) > 0) {
  595. $fieldDescription = $this->getModelManager()->getNewFieldDescriptionInstance($this->getClass(), 'batch', array(
  596. 'label' => 'batch',
  597. 'code' => '_batch',
  598. 'sortable' => false
  599. ));
  600. $fieldDescription->setAdmin($this);
  601. $fieldDescription->setTemplate($this->getTemplate('batch'));
  602. $mapper->add($fieldDescription, 'batch');
  603. }
  604. $this->configureListFields($mapper);
  605. foreach ($this->getExtensions() as $extension) {
  606. $extension->configureListFields($mapper);
  607. }
  608. if ($this->hasRequest() && $this->getRequest()->isXmlHttpRequest()) {
  609. $fieldDescription = $this->getModelManager()->getNewFieldDescriptionInstance($this->getClass(), 'select', array(
  610. 'label' => false,
  611. 'code' => '_select',
  612. 'sortable' => false,
  613. ));
  614. $fieldDescription->setAdmin($this);
  615. $fieldDescription->setTemplate($this->getTemplate('select'));
  616. $mapper->add($fieldDescription, 'select');
  617. }
  618. }
  619. /**
  620. * {@inheritdoc}
  621. */
  622. public function getFilterParameters()
  623. {
  624. $parameters = array();
  625. // build the values array
  626. if ($this->hasRequest()) {
  627. $filters = $this->request->query->get('filter', array());
  628. // if persisting filters, save filters to session, or pull them out of session if no new filters set
  629. if ($this->persistFilters) {
  630. if ($filters == array() && $this->request->query->get('filters') != 'reset') {
  631. $filters = $this->request->getSession()->get($this->getCode().'.filter.parameters', array());
  632. } else {
  633. $this->request->getSession()->set($this->getCode().'.filter.parameters', $filters);
  634. }
  635. }
  636. $parameters = array_merge(
  637. $this->getModelManager()->getDefaultSortValues($this->getClass()),
  638. $this->datagridValues,
  639. $filters
  640. );
  641. if (!$this->determinedPerPageValue($parameters['_per_page'])) {
  642. $parameters['_per_page'] = $this->maxPerPage;
  643. }
  644. // always force the parent value
  645. if ($this->isChild() && $this->getParentAssociationMapping()) {
  646. $parameters[$this->getParentAssociationMapping()] = array('value' => $this->request->get($this->getParent()->getIdParameter()));
  647. }
  648. }
  649. return $parameters;
  650. }
  651. /**
  652. * {@inheritdoc}
  653. */
  654. public function buildDatagrid()
  655. {
  656. if ($this->datagrid) {
  657. return;
  658. }
  659. $filterParameters = $this->getFilterParameters();
  660. // transform _sort_by from a string to a FieldDescriptionInterface for the datagrid.
  661. if (isset($filterParameters['_sort_by']) && is_string($filterParameters['_sort_by'])) {
  662. if ($this->hasListFieldDescription($filterParameters['_sort_by'])) {
  663. $filterParameters['_sort_by'] = $this->getListFieldDescription($filterParameters['_sort_by']);
  664. } else {
  665. $filterParameters['_sort_by'] = $this->getModelManager()->getNewFieldDescriptionInstance(
  666. $this->getClass(),
  667. $filterParameters['_sort_by'],
  668. array()
  669. );
  670. $this->getListBuilder()->buildField(null, $filterParameters['_sort_by'], $this);
  671. }
  672. }
  673. // initialize the datagrid
  674. $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $filterParameters);
  675. $this->datagrid->getPager()->setMaxPageLinks($this->maxPageLinks);
  676. $mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);
  677. // build the datagrid filter
  678. $this->configureDatagridFilters($mapper);
  679. // ok, try to limit to add parent filter
  680. if ($this->isChild() && $this->getParentAssociationMapping() && !$mapper->has($this->getParentAssociationMapping())) {
  681. $mapper->add($this->getParentAssociationMapping(), null, array(
  682. 'field_type' => 'sonata_type_model_reference',
  683. 'field_options' => array(
  684. 'model_manager' => $this->getModelManager()
  685. ),
  686. 'operator_type' => 'hidden'
  687. ));
  688. }
  689. foreach ($this->getExtensions() as $extension) {
  690. $extension->configureDatagridFilters($mapper);
  691. }
  692. }
  693. /**
  694. * Returns the name of the parent related field, so the field can be use to set the default
  695. * value (ie the parent object) or to filter the object
  696. *
  697. * @return string the name of the parent related field
  698. */
  699. public function getParentAssociationMapping()
  700. {
  701. return $this->parentAssociationMapping;
  702. }
  703. /**
  704. * Build the form FieldDescription collection
  705. *
  706. * @return void
  707. */
  708. protected function buildForm()
  709. {
  710. if ($this->form) {
  711. return;
  712. }
  713. // append parent object if any
  714. // todo : clean the way the Admin class can retrieve set the object
  715. if ($this->isChild() && $this->getParentAssociationMapping()) {
  716. $parent = $this->getParent()->getObject($this->request->get($this->getParent()->getIdParameter()));
  717. $propertyAccessor = PropertyAccess::getPropertyAccessor();
  718. $propertyPath = new PropertyPath($this->getParentAssociationMapping());
  719. $object = $this->getSubject();
  720. $value = $propertyAccessor->getValue($object, $propertyPath);
  721. if (is_array($value) || ($value instanceof \Traversable && $value instanceof \ArrayAccess)) {
  722. $value[] = $parent;
  723. $propertyAccessor->setValue($object, $propertyPath, $value);
  724. } else {
  725. $propertyAccessor->setValue($object, $propertyPath, $parent);
  726. }
  727. }
  728. $this->form = $this->getFormBuilder()->getForm();
  729. }
  730. /**
  731. * Returns the baseRoutePattern used to generate the routing information
  732. *
  733. * @throws \RuntimeException
  734. *
  735. * @return string the baseRoutePattern used to generate the routing information
  736. */
  737. public function getBaseRoutePattern()
  738. {
  739. if (!$this->baseRoutePattern) {
  740. preg_match(self::CLASS_REGEX, $this->class, $matches);
  741. if (!$matches) {
  742. throw new \RuntimeException(sprintf('Please define a default `baseRoutePattern` value for the admin class `%s`', get_class($this)));
  743. }
  744. if ($this->isChild()) { // the admin class is a child, prefix it with the parent route name
  745. $this->baseRoutePattern = sprintf('%s/{id}/%s',
  746. $this->getParent()->getBaseRoutePattern(),
  747. $this->urlize($matches[5], '-')
  748. );
  749. } else {
  750. $this->baseRoutePattern = sprintf('/%s/%s/%s',
  751. $this->urlize($matches[1], '-'),
  752. $this->urlize($matches[3], '-'),
  753. $this->urlize($matches[5], '-')
  754. );
  755. }
  756. }
  757. return $this->baseRoutePattern;
  758. }
  759. /**
  760. * Returns the baseRouteName used to generate the routing information
  761. *
  762. * @throws \RuntimeException
  763. *
  764. * @return string the baseRouteName used to generate the routing information
  765. */
  766. public function getBaseRouteName()
  767. {
  768. if (!$this->baseRouteName) {
  769. preg_match(self::CLASS_REGEX, $this->class, $matches);
  770. if (!$matches) {
  771. throw new \RuntimeException(sprintf('Cannot automatically determine base route name, please define a default `baseRouteName` value for the admin class `%s`', get_class($this)));
  772. }
  773. if ($this->isChild()) { // the admin class is a child, prefix it with the parent route name
  774. $this->baseRouteName = sprintf('%s_%s',
  775. $this->getParent()->getBaseRouteName(),
  776. $this->urlize($matches[5])
  777. );
  778. } else {
  779. $this->baseRouteName = sprintf('admin_%s_%s_%s',
  780. $this->urlize($matches[1]),
  781. $this->urlize($matches[3]),
  782. $this->urlize($matches[5])
  783. );
  784. }
  785. }
  786. return $this->baseRouteName;
  787. }
  788. /**
  789. * urlize the given word
  790. *
  791. * @param string $word
  792. * @param string $sep the separator
  793. *
  794. * @return string
  795. */
  796. public function urlize($word, $sep = '_')
  797. {
  798. return strtolower(preg_replace('/[^a-z0-9_]/i', $sep.'$1', $word));
  799. }
  800. /**
  801. * {@inheritdoc}
  802. */
  803. public function getClass()
  804. {
  805. if ($this->hasSubject()) {
  806. return ClassUtils::getClass($this->getSubject());
  807. }
  808. if (!$this->hasActiveSubClass()) {
  809. if (count($this->getSubClasses()) > 0) {
  810. $subject = $this->getSubject();
  811. if ($subject) {
  812. return ClassUtils::getClass($subject);
  813. }
  814. }
  815. return $this->class;
  816. }
  817. if ($this->getParentFieldDescription() && $this->hasActiveSubClass()) {
  818. throw new \RuntimeException('Feature not implemented: an embedded admin cannot have subclass');
  819. }
  820. $subClass = $this->getRequest()->query->get('subclass');
  821. return $this->getSubClass($subClass);
  822. }
  823. /**
  824. * {@inheritdoc}
  825. */
  826. public function getSubClasses()
  827. {
  828. return $this->subClasses;
  829. }
  830. /**
  831. * {@inheritdoc}
  832. */
  833. public function setSubClasses(array $subClasses)
  834. {
  835. $this->subClasses = $subClasses;
  836. }
  837. /**
  838. * Gets the subclass corresponding to the given name
  839. *
  840. * @param string $name The name of the sub class
  841. *
  842. * @return string the subclass
  843. */
  844. protected function getSubClass($name)
  845. {
  846. if ($this->hasSubClass($name)) {
  847. return $this->subClasses[$name];
  848. }
  849. throw new \RuntimeException(sprintf('Unable to find the subclass `%s` for admin `%s`', $name, get_class($this)));
  850. }
  851. /**
  852. * {@inheritdoc}
  853. */
  854. public function hasSubClass($name)
  855. {
  856. return isset($this->subClasses[$name]);
  857. }
  858. /**
  859. * {@inheritdoc}
  860. */
  861. public function hasActiveSubClass()
  862. {
  863. if (count($this->subClasses) > 1 && $this->request) {
  864. return null !== $this->getRequest()->query->get('subclass');
  865. }
  866. return false;
  867. }
  868. /**
  869. * {@inheritdoc}
  870. */
  871. public function getActiveSubClass()
  872. {
  873. if (!$this->hasActiveSubClass()) {
  874. return null;
  875. }
  876. return $this->getClass();
  877. }
  878. /**
  879. * {@inheritdoc}
  880. */
  881. public function getActiveSubclassCode()
  882. {
  883. if (!$this->hasActiveSubClass()) {
  884. return null;
  885. }
  886. $subClass = $this->getRequest()->query->get('subclass');
  887. if (!$this->hasSubClass($subClass)) {
  888. return null;
  889. }
  890. return $subClass;
  891. }
  892. /**
  893. * {@inheritdoc}
  894. */
  895. public function getBatchActions()
  896. {
  897. $actions = array();
  898. if ($this->hasRoute('delete') && $this->isGranted('DELETE')) {
  899. $actions['delete'] = array(
  900. 'label' => $this->trans('action_delete', array(), 'SonataAdminBundle'),
  901. 'ask_confirmation' => true, // by default always true
  902. );
  903. }
  904. return $actions;
  905. }
  906. /**
  907. * {@inheritdoc}
  908. */
  909. public function getRoutes()
  910. {
  911. $this->buildRoutes();
  912. return $this->routes;
  913. }
  914. /**
  915. * {@inheritdoc}
  916. */
  917. public function getRouterIdParameter()
  918. {
  919. return $this->isChild() ? '{childId}' : '{id}';
  920. }
  921. /**
  922. * {@inheritdoc}
  923. */
  924. public function getIdParameter()
  925. {
  926. return $this->isChild() ? 'childId' : 'id';
  927. }
  928. /**
  929. * Build all the related urls to the current admin
  930. *
  931. * @return void
  932. */
  933. public function buildRoutes()
  934. {
  935. if ($this->loaded['routes']) {
  936. return;
  937. }
  938. $this->loaded['routes'] = true;
  939. $this->routes = new RouteCollection(
  940. $this->getBaseCodeRoute(),
  941. $this->getBaseRouteName(),
  942. $this->getBaseRoutePattern(),
  943. $this->getBaseControllerName()
  944. );
  945. $this->routeBuilder->build($this, $this->routes);
  946. $this->configureRoutes($this->routes);
  947. foreach ($this->getExtensions() as $extension) {
  948. $extension->configureRoutes($this, $this->routes);
  949. }
  950. }
  951. /**
  952. * {@inheritdoc}
  953. */
  954. public function getRoute($name)
  955. {
  956. $this->buildRoutes();
  957. if (!$this->routes->has($name)) {
  958. return false;
  959. }
  960. return $this->routes->get($name);
  961. }
  962. /**
  963. * @param string $name
  964. *
  965. * @return bool
  966. */
  967. public function hasRoute($name)
  968. {
  969. $this->buildRoutes();
  970. if (
  971. ! $this->isChild()
  972. && strpos($name, '.') !== false
  973. && strpos($name, $this->getBaseCodeRoute() . '|') !== 0
  974. && strpos($name, $this->getBaseCodeRoute() . '.') !== 0
  975. ) {
  976. $name = $this->getCode() . '|' . $name;
  977. }
  978. return $this->routes->has($name);
  979. }
  980. /**
  981. * {@inheritdoc}
  982. */
  983. public function generateObjectUrl($name, $object, array $parameters = array(), $absolute = false)
  984. {
  985. $parameters['id'] = $this->getUrlsafeIdentifier($object);
  986. return $this->generateUrl($name, $parameters, $absolute);
  987. }
  988. /**
  989. * {@inheritdoc}
  990. */
  991. public function generateUrl($name, array $parameters = array(), $absolute = false)
  992. {
  993. return $this->routeGenerator->generateUrl($this, $name, $parameters, $absolute);
  994. }
  995. /**
  996. * {@inheritdoc}
  997. */
  998. public function generateMenuUrl($name, array $parameters = array(), $absolute = false)
  999. {
  1000. return $this->routeGenerator->generateMenuUrl($this, $name,$parameters, $absolute);
  1001. }
  1002. /**
  1003. * @param array $templates
  1004. *
  1005. * @return void
  1006. */
  1007. public function setTemplates(array $templates)
  1008. {
  1009. $this->templates = $templates;
  1010. }
  1011. /**
  1012. * @param string $name
  1013. * @param string $template
  1014. *
  1015. * @return void
  1016. */
  1017. public function setTemplate($name, $template)
  1018. {
  1019. $this->templates[$name] = $template;
  1020. }
  1021. /**
  1022. * @return array
  1023. */
  1024. public function getTemplates()
  1025. {
  1026. return $this->templates;
  1027. }
  1028. /**
  1029. * {@inheritdoc}
  1030. */
  1031. public function getTemplate($name)
  1032. {
  1033. if (isset($this->templates[$name])) {
  1034. return $this->templates[$name];
  1035. }
  1036. return null;
  1037. }
  1038. /**
  1039. * {@inheritdoc}
  1040. */
  1041. public function getNewInstance()
  1042. {
  1043. $object = $this->getModelManager()->getModelInstance($this->getClass());
  1044. foreach ($this->getExtensions() as $extension) {
  1045. $extension->alterNewInstance($this, $object);
  1046. }
  1047. return $object;
  1048. }
  1049. /**
  1050. * {@inheritdoc}
  1051. */
  1052. public function getFormBuilder()
  1053. {
  1054. $this->formOptions['data_class'] = $this->getClass();
  1055. $formBuilder = $this->getFormContractor()->getFormBuilder(
  1056. $this->getUniqid(),
  1057. $this->formOptions
  1058. );
  1059. $this->defineFormBuilder($formBuilder);
  1060. return $formBuilder;
  1061. }
  1062. /**
  1063. * This method is being called by the main admin class and the child class,
  1064. * the getFormBuilder is only call by the main admin class
  1065. *
  1066. * @param \Symfony\Component\Form\FormBuilder $formBuilder
  1067. *
  1068. * @return void
  1069. */
  1070. public function defineFormBuilder(FormBuilder $formBuilder)
  1071. {
  1072. $mapper = new FormMapper($this->getFormContractor(), $formBuilder, $this);
  1073. $this->configureFormFields($mapper);
  1074. foreach ($this->getExtensions() as $extension) {
  1075. $extension->configureFormFields($mapper);
  1076. }
  1077. $this->attachInlineValidator();
  1078. }
  1079. /**
  1080. * Attach the inline validator to the model metadata, this must be done once per admin
  1081. */
  1082. protected function attachInlineValidator()
  1083. {
  1084. $admin = $this;
  1085. // add the custom inline validation option
  1086. $metadata = $this->validator->getMetadataFactory()->getMetadataFor($this->getClass());
  1087. $metadata->addConstraint(new InlineConstraint(array(
  1088. 'service' => $this,
  1089. 'method' => function(ErrorElement $errorElement, $object) use ($admin) {
  1090. /* @var \Sonata\AdminBundle\Admin\AdminInterface $admin */
  1091. // This avoid the main validation to be cascaded to children
  1092. // The problem occurs when a model Page has a collection of Page as property
  1093. if ($admin->hasSubject() && spl_object_hash($object) !== spl_object_hash($admin->getSubject())) {
  1094. return;
  1095. }
  1096. $admin->validate($errorElement, $object);
  1097. foreach ($admin->getExtensions() as $extension) {
  1098. $extension->validate($admin, $errorElement, $object);
  1099. }
  1100. }
  1101. )));
  1102. }
  1103. /**
  1104. * {@inheritdoc}
  1105. */
  1106. public function attachAdminClass(FieldDescriptionInterface $fieldDescription)
  1107. {
  1108. $pool = $this->getConfigurationPool();
  1109. $adminCode = $fieldDescription->getOption('admin_code');
  1110. if ($adminCode !== null) {
  1111. $admin = $pool->getAdminByAdminCode($adminCode);
  1112. } else {
  1113. $admin = $pool->getAdminByClass($fieldDescription->getTargetEntity());
  1114. }
  1115. if (!$admin) {
  1116. return;
  1117. }
  1118. if ($this->hasRequest()) {
  1119. $admin->setRequest($this->getRequest());
  1120. }
  1121. $fieldDescription->setAssociationAdmin($admin);
  1122. }
  1123. /**
  1124. * {@inheritdoc}
  1125. */
  1126. public function getObject($id)
  1127. {
  1128. $object = $this->getModelManager()->find($this->getClass(), $id);
  1129. foreach ($this->getExtensions() as $extension) {
  1130. $extension->alterObject($this, $object);
  1131. }
  1132. return $object;
  1133. }
  1134. /**
  1135. * {@inheritdoc}
  1136. */
  1137. public function getForm()
  1138. {
  1139. $this->buildForm();
  1140. return $this->form;
  1141. }
  1142. /**
  1143. * {@inheritdoc}
  1144. */
  1145. public function getList()
  1146. {
  1147. $this->buildList();
  1148. return $this->list;
  1149. }
  1150. /**
  1151. * {@inheritdoc}
  1152. */
  1153. public function createQuery($context = 'list')
  1154. {
  1155. $query = $this->getModelManager()->createQuery($this->class);
  1156. foreach ($this->extensions as $extension) {
  1157. $extension->configureQuery($this, $query, $context);
  1158. }
  1159. return $query;
  1160. }
  1161. /**
  1162. * {@inheritdoc}
  1163. */
  1164. public function getDatagrid()
  1165. {
  1166. $this->buildDatagrid();
  1167. return $this->datagrid;
  1168. }
  1169. /**
  1170. * Build the side menu related to the current action
  1171. *
  1172. * @param string $action
  1173. * @param \Sonata\AdminBundle\Admin\AdminInterface $childAdmin
  1174. *
  1175. * @return \Knp\Menu\ItemInterface|boolean
  1176. */
  1177. public function buildSideMenu($action, AdminInterface $childAdmin = null)
  1178. {
  1179. if ($this->loaded['side_menu']) {
  1180. return;
  1181. }
  1182. $this->loaded['side_menu'] = true;
  1183. $menu = $this->menuFactory->createItem('root');
  1184. $menu->setChildrenAttribute('class', 'nav nav-list');
  1185. // Prevents BC break with KnpMenuBundle v1.x
  1186. if (method_exists($menu, "setCurrentUri")) {
  1187. $menu->setCurrentUri($this->getRequest()->getBaseUrl().$this->getRequest()->getPathInfo());
  1188. }
  1189. $this->configureSideMenu($menu, $action, $childAdmin);
  1190. foreach ($this->getExtensions() as $extension) {
  1191. $extension->configureSideMenu($this, $menu, $action, $childAdmin);
  1192. }
  1193. $this->menu = $menu;
  1194. }
  1195. /**
  1196. * @param string $action
  1197. * @param \Sonata\AdminBundle\Admin\AdminInterface $childAdmin
  1198. *
  1199. * @return \Knp\Menu\ItemInterface
  1200. */
  1201. public function getSideMenu($action, AdminInterface $childAdmin = null)
  1202. {
  1203. if ($this->isChild()) {
  1204. return $this->getParent()->getSideMenu($action, $this);
  1205. }
  1206. $this->buildSideMenu($action, $childAdmin);
  1207. return $this->menu;
  1208. }
  1209. /**
  1210. * Returns the root code
  1211. *
  1212. * @return string the root code
  1213. */
  1214. public function getRootCode()
  1215. {
  1216. return $this->getRoot()->getCode();
  1217. }
  1218. /**
  1219. * Returns the master admin
  1220. *
  1221. * @return \Sonata\AdminBundle\Admin\Admin the root admin class
  1222. */
  1223. public function getRoot()
  1224. {
  1225. $parentFieldDescription = $this->getParentFieldDescription();
  1226. if (!$parentFieldDescription) {
  1227. return $this;
  1228. }
  1229. return $parentFieldDescription->getAdmin()->getRoot();
  1230. }
  1231. /**
  1232. * {@inheritdoc}
  1233. */
  1234. public function setBaseControllerName($baseControllerName)
  1235. {
  1236. $this->baseControllerName = $baseControllerName;
  1237. }
  1238. /**
  1239. * {@inheritdoc}
  1240. */
  1241. public function getBaseControllerName()
  1242. {
  1243. return $this->baseControllerName;
  1244. }
  1245. /**
  1246. * @param string $label
  1247. */
  1248. public function setLabel($label)
  1249. {
  1250. $this->label = $label;
  1251. }
  1252. /**
  1253. * {@inheritdoc}
  1254. */
  1255. public function getLabel()
  1256. {
  1257. return $this->label;
  1258. }
  1259. /**
  1260. * @param boolean $persist
  1261. */
  1262. public function setPersistFilters($persist)
  1263. {
  1264. $this->persistFilters = $persist;
  1265. }
  1266. /**
  1267. * @param int $maxPerPage
  1268. */
  1269. public function setMaxPerPage($maxPerPage)
  1270. {
  1271. $this->maxPerPage = $maxPerPage;
  1272. }
  1273. /**
  1274. * @return int
  1275. */
  1276. public function getMaxPerPage()
  1277. {
  1278. return $this->maxPerPage;
  1279. }
  1280. /**
  1281. * @param int $maxPageLinks
  1282. */
  1283. public function setMaxPageLinks($maxPageLinks)
  1284. {
  1285. $this->maxPageLinks = $maxPageLinks;
  1286. }
  1287. /**
  1288. * @return int
  1289. */
  1290. public function getMaxPageLinks()
  1291. {
  1292. return $this->maxPageLinks;
  1293. }
  1294. /**
  1295. * {@inheritdoc}
  1296. */
  1297. public function getFormGroups()
  1298. {
  1299. return $this->formGroups;
  1300. }
  1301. /**
  1302. * {@inheritdoc}
  1303. */
  1304. public function setFormGroups(array $formGroups)
  1305. {
  1306. $this->formGroups = $formGroups;
  1307. }
  1308. /**
  1309. * {@inheritdoc}
  1310. */
  1311. public function removeFieldFromFormGroup($key)
  1312. {
  1313. foreach ($this->formGroups as $name => $formGroup) {
  1314. unset($this->formGroups[$name]['fields'][$key]);
  1315. if (empty($this->formGroups[$name]['fields'])) {
  1316. unset($this->formGroups[$name]);
  1317. }
  1318. }
  1319. }
  1320. /**
  1321. * @param array $group
  1322. * @param array $keys
  1323. */
  1324. public function reorderFormGroup($group, array $keys)
  1325. {
  1326. $formGroups = $this->getFormGroups();
  1327. $formGroups[$group]['fields'] = array_merge(array_flip($keys), $formGroups[$group]['fields']);
  1328. $this->setFormGroups($formGroups);
  1329. }
  1330. /**
  1331. * {@inheritdoc}
  1332. */
  1333. public function getShowGroups()
  1334. {
  1335. return $this->showGroups;
  1336. }
  1337. /**
  1338. * {@inheritdoc}
  1339. */
  1340. public function setShowGroups(array $showGroups)
  1341. {
  1342. $this->showGroups = $showGroups;
  1343. }
  1344. /**
  1345. * {@inheritdoc}
  1346. */
  1347. public function reorderShowGroup($group, array $keys)
  1348. {
  1349. $showGroups = $this->getShowGroups();
  1350. $showGroups[$group]['fields'] = array_merge(array_flip($keys), $showGroups[$group]['fields']);
  1351. $this->setShowGroups($showGroups);
  1352. }
  1353. /**
  1354. * {@inheritdoc}
  1355. */
  1356. public function setParentFieldDescription(FieldDescriptionInterface $parentFieldDescription)
  1357. {
  1358. $this->parentFieldDescription = $parentFieldDescription;
  1359. }
  1360. /**
  1361. * {@inheritdoc}
  1362. */
  1363. public function getParentFieldDescription()
  1364. {
  1365. return $this->parentFieldDescription;
  1366. }
  1367. /**
  1368. * {@inheritdoc}
  1369. */
  1370. public function hasParentFieldDescription()
  1371. {
  1372. return $this->parentFieldDescription instanceof FieldDescriptionInterface;
  1373. }
  1374. /**
  1375. * {@inheritdoc}
  1376. */
  1377. public function setSubject($subject)
  1378. {
  1379. $this->subject = $subject;
  1380. }
  1381. /**
  1382. * {@inheritdoc}
  1383. */
  1384. public function getSubject()
  1385. {
  1386. if ($this->subject === null && $this->request) {
  1387. $id = $this->request->get($this->getIdParameter());
  1388. if (!preg_match('#^[0-9A-Fa-f]+$#', $id)) {
  1389. $this->subject = false;
  1390. } else {
  1391. $this->subject = $this->getModelManager()->find($this->class, $id);
  1392. }
  1393. }
  1394. return $this->subject;
  1395. }
  1396. /**
  1397. * {@inheritdoc}
  1398. */
  1399. public function hasSubject()
  1400. {
  1401. return $this->subject != null;
  1402. }
  1403. /**
  1404. * {@inheritdoc}
  1405. */
  1406. public function getFormFieldDescriptions()
  1407. {
  1408. $this->buildForm();
  1409. return $this->formFieldDescriptions;
  1410. }
  1411. /**
  1412. * {@inheritdoc}
  1413. */
  1414. public function getFormFieldDescription($name)
  1415. {
  1416. return $this->hasFormFieldDescription($name) ? $this->formFieldDescriptions[$name] : null;
  1417. }
  1418. /**
  1419. * Returns true if the admin has a FieldDescription with the given $name
  1420. *
  1421. * @param string $name
  1422. *
  1423. * @return bool
  1424. */
  1425. public function hasFormFieldDescription($name)
  1426. {
  1427. return array_key_exists($name, $this->formFieldDescriptions) ? true : false;
  1428. }
  1429. /**
  1430. * {@inheritdoc}
  1431. */
  1432. public function addFormFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1433. {
  1434. $this->formFieldDescriptions[$name] = $fieldDescription;
  1435. }
  1436. /**
  1437. * remove a FieldDescription
  1438. *
  1439. * @param string $name
  1440. *
  1441. * @return void
  1442. */
  1443. public function removeFormFieldDescription($name)
  1444. {
  1445. unset($this->formFieldDescriptions[$name]);
  1446. }
  1447. /**
  1448. * build and return the collection of form FieldDescription
  1449. *
  1450. * @return array collection of form FieldDescription
  1451. */
  1452. public function getShowFieldDescriptions()
  1453. {
  1454. $this->buildShow();
  1455. return $this->showFieldDescriptions;
  1456. }
  1457. /**
  1458. * Returns the form FieldDescription with the given $name
  1459. *
  1460. * @param string $name
  1461. *
  1462. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  1463. */
  1464. public function getShowFieldDescription($name)
  1465. {
  1466. $this->buildShow();
  1467. return $this->hasShowFieldDescription($name) ? $this->showFieldDescriptions[$name] : null;
  1468. }
  1469. /**
  1470. * {@inheritdoc}
  1471. */
  1472. public function hasShowFieldDescription($name)
  1473. {
  1474. return array_key_exists($name, $this->showFieldDescriptions);
  1475. }
  1476. /**
  1477. * {@inheritdoc}
  1478. */
  1479. public function addShowFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1480. {
  1481. $this->showFieldDescriptions[$name] = $fieldDescription;
  1482. }
  1483. /**
  1484. * {@inheritdoc}
  1485. */
  1486. public function removeShowFieldDescription($name)
  1487. {
  1488. unset($this->showFieldDescriptions[$name]);
  1489. }
  1490. /**
  1491. * {@inheritdoc}
  1492. */
  1493. public function getListFieldDescriptions()
  1494. {
  1495. $this->buildList();
  1496. return $this->listFieldDescriptions;
  1497. }
  1498. /**
  1499. * {@inheritdoc}
  1500. */
  1501. public function getListFieldDescription($name)
  1502. {
  1503. return $this->hasListFieldDescription($name) ? $this->listFieldDescriptions[$name] : null;
  1504. }
  1505. /**
  1506. * {@inheritdoc}
  1507. */
  1508. public function hasListFieldDescription($name)
  1509. {
  1510. $this->buildList();
  1511. return array_key_exists($name, $this->listFieldDescriptions) ? true : false;
  1512. }
  1513. /**
  1514. * {@inheritdoc}
  1515. */
  1516. public function addListFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1517. {
  1518. $this->listFieldDescriptions[$name] = $fieldDescription;
  1519. }
  1520. /**
  1521. * {@inheritdoc}
  1522. */
  1523. public function removeListFieldDescription($name)
  1524. {
  1525. unset($this->listFieldDescriptions[$name]);
  1526. }
  1527. /**
  1528. * Returns a filter FieldDescription
  1529. *
  1530. * @param string $name
  1531. *
  1532. * @return array|null
  1533. */
  1534. public function getFilterFieldDescription($name)
  1535. {
  1536. return $this->hasFilterFieldDescription($name) ? $this->filterFieldDescriptions[$name] : null;
  1537. }
  1538. /**
  1539. * {@inheritdoc}
  1540. */
  1541. public function hasFilterFieldDescription($name)
  1542. {
  1543. return array_key_exists($name, $this->filterFieldDescriptions) ? true : false;
  1544. }
  1545. /**
  1546. * {@inheritdoc}
  1547. */
  1548. public function addFilterFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1549. {
  1550. $this->filterFieldDescriptions[$name] = $fieldDescription;
  1551. }
  1552. /**
  1553. * {@inheritdoc}
  1554. */
  1555. public function removeFilterFieldDescription($name)
  1556. {
  1557. unset($this->filterFieldDescriptions[$name]);
  1558. }
  1559. /**
  1560. * {@inheritdoc}
  1561. */
  1562. public function getFilterFieldDescriptions()
  1563. {
  1564. $this->buildDatagrid();
  1565. return $this->filterFieldDescriptions;
  1566. }
  1567. /**
  1568. * {@inheritdoc}
  1569. */
  1570. public function addChild(AdminInterface $child)
  1571. {
  1572. $this->children[$child->getCode()] = $child;
  1573. $child->setBaseCodeRoute($this->getCode().'|'.$child->getCode());
  1574. $child->setParent($this);
  1575. }
  1576. /**
  1577. * {@inheritdoc}
  1578. */
  1579. public function hasChild($code)
  1580. {
  1581. return isset($this->children[$code]);
  1582. }
  1583. /**
  1584. * {@inheritdoc}
  1585. */
  1586. public function getChildren()
  1587. {
  1588. return $this->children;
  1589. }
  1590. /**
  1591. * {@inheritdoc}
  1592. */
  1593. public function getChild($code)
  1594. {
  1595. return $this->hasChild($code) ? $this->children[$code] : null;
  1596. }
  1597. /**
  1598. * {@inheritdoc}
  1599. */
  1600. public function setParent(AdminInterface $parent)
  1601. {
  1602. $this->parent = $parent;
  1603. }
  1604. /**
  1605. * {@inheritdoc}
  1606. */
  1607. public function getParent()
  1608. {
  1609. return $this->parent;
  1610. }
  1611. /**
  1612. * {@inheritdoc}
  1613. */
  1614. public function isChild()
  1615. {
  1616. return $this->parent instanceof AdminInterface;
  1617. }
  1618. /**
  1619. * Returns true if the admin has children, false otherwise
  1620. *
  1621. * @return bool if the admin has children
  1622. */
  1623. public function hasChildren()
  1624. {
  1625. return count($this->children) > 0;
  1626. }
  1627. /**
  1628. * {@inheritdoc}
  1629. */
  1630. public function setUniqid($uniqid)
  1631. {
  1632. $this->uniqid = $uniqid;
  1633. }
  1634. /**
  1635. * {@inheritdoc}
  1636. */
  1637. public function getUniqid()
  1638. {
  1639. if (!$this->uniqid) {
  1640. $this->uniqid = "s".uniqid();
  1641. }
  1642. return $this->uniqid;
  1643. }
  1644. /**
  1645. * Returns the classname label
  1646. *
  1647. * @return string the classname label
  1648. */
  1649. public function getClassnameLabel()
  1650. {
  1651. return $this->classnameLabel;
  1652. }
  1653. /**
  1654. * {@inheritdoc}
  1655. */
  1656. public function getPersistentParameters()
  1657. {
  1658. $parameters = array();
  1659. foreach ($this->getExtensions() as $extension) {
  1660. $params = $extension->getPersistentParameters($this);
  1661. if (!is_array($params)) {
  1662. throw new \RuntimeException(sprintf('The %s::getPersistentParameters must return an array', get_class($extension)));
  1663. }
  1664. $parameters = array_merge($parameters, $params);
  1665. }
  1666. return $parameters;
  1667. }
  1668. /**
  1669. * @param string $name
  1670. *
  1671. * @return null|mixed
  1672. */
  1673. public function getPersistentParameter($name)
  1674. {
  1675. $parameters = $this->getPersistentParameters();
  1676. return isset($parameters[$name]) ? $parameters[$name] : null;
  1677. }
  1678. /**
  1679. * {@inheritdoc}
  1680. */
  1681. public function getBreadcrumbs($action)
  1682. {
  1683. if ($this->isChild()) {
  1684. return $this->getParent()->getBreadcrumbs($action);
  1685. }
  1686. $menu = $this->buildBreadcrumbs($action);
  1687. do {
  1688. $breadcrumbs[] = $menu;
  1689. } while ($menu = $menu->getParent());
  1690. $breadcrumbs = array_reverse($breadcrumbs);
  1691. array_shift($breadcrumbs);
  1692. return $breadcrumbs;
  1693. }
  1694. /**
  1695. * Generates the breadcrumbs array
  1696. *
  1697. * Note: the method will be called by the top admin instance (parent => child)
  1698. *
  1699. * @param string $action
  1700. * @param \Knp\Menu\ItemInterface|null $menu
  1701. *
  1702. * @return array
  1703. */
  1704. public function buildBreadcrumbs($action, MenuItemInterface $menu = null)
  1705. {
  1706. if (isset($this->breadcrumbs[$action])) {
  1707. return $this->breadcrumbs[$action];
  1708. }
  1709. if (!$menu) {
  1710. $menu = $this->menuFactory->createItem('root');
  1711. $menu = $menu->addChild(
  1712. $this->trans($this->getLabelTranslatorStrategy()->getLabel('dashboard', 'breadcrumb', 'link'), array(), 'SonataAdminBundle'),
  1713. array('uri' => $this->routeGenerator->generate('sonata_admin_dashboard'))
  1714. );
  1715. }
  1716. $menu = $menu->addChild(
  1717. $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_list', $this->getClassnameLabel()), 'breadcrumb', 'link')),
  1718. array('uri' => $this->hasRoute('list') && $this->isGranted('LIST') ? $this->generateUrl('list') : null)
  1719. );
  1720. $childAdmin = $this->getCurrentChildAdmin();
  1721. if ($childAdmin) {
  1722. $id = $this->request->get($this->getIdParameter());
  1723. $menu = $menu->addChild(
  1724. $this->toString($this->getSubject()),
  1725. array('uri' => $this->hasRoute('edit') && $this->isGranted('EDIT') ? $this->generateUrl('edit', array('id' => $id)) : null)
  1726. );
  1727. return $childAdmin->buildBreadcrumbs($action, $menu);
  1728. } elseif ($this->isChild()) {
  1729. if ($action == 'list') {
  1730. $menu->setUri(false);
  1731. } elseif ($action != 'create' && $this->hasSubject()) {
  1732. $menu = $menu->addChild($this->toString($this->getSubject()));
  1733. } else {
  1734. $menu = $menu->addChild(
  1735. $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_%s', $this->getClassnameLabel(), $action), 'breadcrumb', 'link'))
  1736. );
  1737. }
  1738. } elseif ($action != 'list' && $this->hasSubject()) {
  1739. $menu = $menu->addChild($this->toString($this->getSubject()));
  1740. } elseif ($action != 'list') {
  1741. $menu = $menu->addChild(
  1742. $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_%s', $this->getClassnameLabel(), $action), 'breadcrumb', 'link'))
  1743. );
  1744. }
  1745. return $this->breadcrumbs[$action] = $menu;
  1746. }
  1747. /**
  1748. * {@inheritdoc}
  1749. */
  1750. public function setCurrentChild($currentChild)
  1751. {
  1752. $this->currentChild = $currentChild;
  1753. }
  1754. /**
  1755. * {@inheritdoc}
  1756. */
  1757. public function getCurrentChild()
  1758. {
  1759. return $this->currentChild;
  1760. }
  1761. /**
  1762. * Returns the current child admin instance
  1763. *
  1764. * @return \Sonata\AdminBundle\Admin\AdminInterface|null the current child admin instance
  1765. */
  1766. public function getCurrentChildAdmin()
  1767. {
  1768. foreach ($this->children as $children) {
  1769. if ($children->getCurrentChild()) {
  1770. return $children;
  1771. }
  1772. }
  1773. return null;
  1774. }
  1775. /**
  1776. * {@inheritdoc}
  1777. */
  1778. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  1779. {
  1780. $domain = $domain ?: $this->getTranslationDomain();
  1781. if (!$this->translator) {
  1782. return $id;
  1783. }
  1784. return $this->translator->trans($id, $parameters, $domain, $locale);
  1785. }
  1786. /**
  1787. * translate a message id
  1788. *
  1789. * @param string $id
  1790. * @param integer $count
  1791. * @param array $parameters
  1792. * @param null $domain
  1793. * @param null $locale
  1794. *
  1795. * @return string the translated string
  1796. */
  1797. public function transChoice($id, $count, array $parameters = array(), $domain = null, $locale = null)
  1798. {
  1799. $domain = $domain ?: $this->getTranslationDomain();
  1800. if (!$this->translator) {
  1801. return $id;
  1802. }
  1803. return $this->translator->transChoice($id, $count, $parameters, $domain, $locale);
  1804. }
  1805. /**
  1806. * {@inheritdoc}
  1807. */
  1808. public function setTranslationDomain($translationDomain)
  1809. {
  1810. $this->translationDomain = $translationDomain;
  1811. }
  1812. /**
  1813. * {@inheritdoc}
  1814. */
  1815. public function getTranslationDomain()
  1816. {
  1817. return $this->translationDomain;
  1818. }
  1819. /**
  1820. * {@inheritdoc}
  1821. */
  1822. public function setTranslator(TranslatorInterface $translator)
  1823. {
  1824. $this->translator = $translator;
  1825. }
  1826. /**
  1827. * {@inheritdoc}
  1828. */
  1829. public function getTranslator()
  1830. {
  1831. return $this->translator;
  1832. }
  1833. /**
  1834. * {@inheritdoc}
  1835. */
  1836. public function getTranslationLabel($label, $context = '', $type = '')
  1837. {
  1838. return $this->getLabelTranslatorStrategy()->getLabel($label, $context, $type);
  1839. }
  1840. /**
  1841. * {@inheritdoc}
  1842. */
  1843. public function setRequest(Request $request)
  1844. {
  1845. $this->request = $request;
  1846. foreach ($this->getChildren() as $children) {
  1847. $children->setRequest($request);
  1848. }
  1849. }
  1850. /**
  1851. * {@inheritdoc}
  1852. */
  1853. public function getRequest()
  1854. {
  1855. if (!$this->request) {
  1856. throw new \RuntimeException('The Request object has not been set');
  1857. }
  1858. return $this->request;
  1859. }
  1860. /**
  1861. * {@inheritdoc}
  1862. */
  1863. public function hasRequest()
  1864. {
  1865. return $this->request !== null;
  1866. }
  1867. /**
  1868. * {@inheritdoc}
  1869. */
  1870. public function setFormContractor(FormContractorInterface $formBuilder)
  1871. {
  1872. $this->formContractor = $formBuilder;
  1873. }
  1874. /**
  1875. * @return \Sonata\AdminBundle\Builder\FormContractorInterface
  1876. */
  1877. public function getFormContractor()
  1878. {
  1879. return $this->formContractor;
  1880. }
  1881. /**
  1882. * {@inheritdoc}
  1883. */
  1884. public function setDatagridBuilder(DatagridBuilderInterface $datagridBuilder)
  1885. {
  1886. $this->datagridBuilder = $datagridBuilder;
  1887. }
  1888. /**
  1889. * {@inheritdoc}
  1890. */
  1891. public function getDatagridBuilder()
  1892. {
  1893. return $this->datagridBuilder;
  1894. }
  1895. /**
  1896. * {@inheritdoc}
  1897. */
  1898. public function setListBuilder(ListBuilderInterface $listBuilder)
  1899. {
  1900. $this->listBuilder = $listBuilder;
  1901. }
  1902. /**
  1903. * {@inheritdoc}
  1904. */
  1905. public function getListBuilder()
  1906. {
  1907. return $this->listBuilder;
  1908. }
  1909. /**
  1910. * @param \Sonata\AdminBundle\Builder\ShowBuilderInterface $showBuilder
  1911. *
  1912. * @return void
  1913. */
  1914. public function setShowBuilder(ShowBuilderInterface $showBuilder)
  1915. {
  1916. $this->showBuilder = $showBuilder;
  1917. }
  1918. /**
  1919. * @return \Sonata\AdminBundle\Builder\ShowBuilderInterface
  1920. */
  1921. public function getShowBuilder()
  1922. {
  1923. return $this->showBuilder;
  1924. }
  1925. /**
  1926. * {@inheritdoc}
  1927. */
  1928. public function setConfigurationPool(Pool $configurationPool)
  1929. {
  1930. $this->configurationPool = $configurationPool;
  1931. }
  1932. /**
  1933. * @return Pool
  1934. */
  1935. public function getConfigurationPool()
  1936. {
  1937. return $this->configurationPool;
  1938. }
  1939. /**
  1940. * {@inheritdoc}
  1941. */
  1942. public function setRouteGenerator(RouteGeneratorInterface $routeGenerator)
  1943. {
  1944. $this->routeGenerator = $routeGenerator;
  1945. }
  1946. /**
  1947. * @return \Sonata\AdminBundle\Route\RouteGeneratorInterface
  1948. */
  1949. public function getRouteGenerator()
  1950. {
  1951. return $this->routeGenerator;
  1952. }
  1953. /**
  1954. * {@inheritdoc}
  1955. */
  1956. public function getCode()
  1957. {
  1958. return $this->code;
  1959. }
  1960. /**
  1961. * @param string $baseCodeRoute
  1962. */
  1963. public function setBaseCodeRoute($baseCodeRoute)
  1964. {
  1965. $this->baseCodeRoute = $baseCodeRoute;
  1966. }
  1967. /**
  1968. * {@inheritdoc}
  1969. */
  1970. public function getBaseCodeRoute()
  1971. {
  1972. return $this->baseCodeRoute;
  1973. }
  1974. /**
  1975. * {@inheritdoc}
  1976. */
  1977. public function getModelManager()
  1978. {
  1979. return $this->modelManager;
  1980. }
  1981. /**
  1982. * @param \Sonata\AdminBundle\Model\ModelManagerInterface $modelManager
  1983. */
  1984. public function setModelManager(ModelManagerInterface $modelManager)
  1985. {
  1986. $this->modelManager = $modelManager;
  1987. }
  1988. /**
  1989. * {@inheritdoc}
  1990. */
  1991. public function getManagerType()
  1992. {
  1993. return $this->managerType;
  1994. }
  1995. /**
  1996. * @param string $type
  1997. */
  1998. public function setManagerType($type)
  1999. {
  2000. $this->managerType = $type;
  2001. }
  2002. /**
  2003. * {@inheritdoc}
  2004. */
  2005. public function getObjectIdentifier()
  2006. {
  2007. return $this->getCode();
  2008. }
  2009. /**
  2010. * Set the roles and permissions per role
  2011. *
  2012. * @param array $information
  2013. */
  2014. public function setSecurityInformation(array $information)
  2015. {
  2016. $this->securityInformation = $information;
  2017. }
  2018. /**
  2019. * {@inheritdoc}
  2020. */
  2021. public function getSecurityInformation()
  2022. {
  2023. return $this->securityInformation;
  2024. }
  2025. /**
  2026. * Return the list of permissions the user should have in order to display the admin
  2027. *
  2028. * @param string $context
  2029. *
  2030. * @return array
  2031. */
  2032. public function getPermissionsShow($context)
  2033. {
  2034. switch ($context) {
  2035. case self::CONTEXT_DASHBOARD:
  2036. case self::CONTEXT_MENU:
  2037. default:
  2038. return array('LIST');
  2039. }
  2040. }
  2041. /**
  2042. * {@inheritdoc}
  2043. */
  2044. public function showIn($context)
  2045. {
  2046. switch ($context) {
  2047. case self::CONTEXT_DASHBOARD:
  2048. case self::CONTEXT_MENU:
  2049. default:
  2050. return $this->isGranted($this->getPermissionsShow($context));
  2051. }
  2052. }
  2053. /**
  2054. * {@inheritdoc}
  2055. */
  2056. public function createObjectSecurity($object)
  2057. {
  2058. $this->getSecurityHandler()->createObjectSecurity($this, $object);
  2059. }
  2060. /**
  2061. * {@inheritdoc}
  2062. */
  2063. public function setSecurityHandler(SecurityHandlerInterface $securityHandler)
  2064. {
  2065. $this->securityHandler = $securityHandler;
  2066. }
  2067. /**
  2068. * {@inheritdoc}
  2069. */
  2070. public function getSecurityHandler()
  2071. {
  2072. return $this->securityHandler;
  2073. }
  2074. /**
  2075. * {@inheritdoc}
  2076. */
  2077. public function isGranted($name, $object = null)
  2078. {
  2079. return $this->securityHandler->isGranted($this, $name, $object ?: $this);
  2080. }
  2081. /**
  2082. * {@inheritdoc}
  2083. */
  2084. public function getUrlsafeIdentifier($entity)
  2085. {
  2086. return $this->getModelManager()->getUrlsafeIdentifier($entity);
  2087. }
  2088. /**
  2089. * {@inheritdoc}
  2090. */
  2091. public function getNormalizedIdentifier($entity)
  2092. {
  2093. return $this->getModelManager()->getNormalizedIdentifier($entity);
  2094. }
  2095. /**
  2096. * {@inheritdoc}
  2097. */
  2098. public function id($entity)
  2099. {
  2100. return $this->getNormalizedIdentifier($entity);
  2101. }
  2102. /**
  2103. * {@inheritdoc}
  2104. */
  2105. public function setValidator(ValidatorInterface $validator)
  2106. {
  2107. $this->validator = $validator;
  2108. }
  2109. /**
  2110. * {@inheritdoc}
  2111. */
  2112. public function getValidator()
  2113. {
  2114. return $this->validator;
  2115. }
  2116. /**
  2117. * {@inheritdoc}
  2118. */
  2119. public function getShow()
  2120. {
  2121. $this->buildShow();
  2122. return $this->show;
  2123. }
  2124. /**
  2125. * {@inheritdoc}
  2126. */
  2127. public function setFormTheme(array $formTheme)
  2128. {
  2129. $this->formTheme = $formTheme;
  2130. }
  2131. /**
  2132. * {@inheritdoc}
  2133. */
  2134. public function getFormTheme()
  2135. {
  2136. return $this->formTheme;
  2137. }
  2138. /**
  2139. * {@inheritdoc}
  2140. */
  2141. public function setFilterTheme(array $filterTheme)
  2142. {
  2143. $this->filterTheme = $filterTheme;
  2144. }
  2145. /**
  2146. * {@inheritdoc}
  2147. */
  2148. public function getFilterTheme()
  2149. {
  2150. return $this->filterTheme;
  2151. }
  2152. /**
  2153. * {@inheritdoc}
  2154. */
  2155. public function addExtension(AdminExtensionInterface $extension)
  2156. {
  2157. $this->extensions[] = $extension;
  2158. }
  2159. /**
  2160. * {@inheritdoc}
  2161. */
  2162. public function getExtensions()
  2163. {
  2164. return $this->extensions;
  2165. }
  2166. /**
  2167. * {@inheritdoc}
  2168. */
  2169. public function setMenuFactory(MenuFactoryInterface $menuFactory)
  2170. {
  2171. $this->menuFactory = $menuFactory;
  2172. }
  2173. /**
  2174. * {@inheritdoc}
  2175. */
  2176. public function getMenuFactory()
  2177. {
  2178. return $this->menuFactory;
  2179. }
  2180. /**
  2181. * {@inheritdoc}
  2182. */
  2183. public function setRouteBuilder(RouteBuilderInterface $routeBuilder)
  2184. {
  2185. $this->routeBuilder = $routeBuilder;
  2186. }
  2187. /**
  2188. * {@inheritdoc}
  2189. */
  2190. public function getRouteBuilder()
  2191. {
  2192. return $this->routeBuilder;
  2193. }
  2194. /**
  2195. * {@inheritdoc}
  2196. */
  2197. public function toString($object)
  2198. {
  2199. if (!is_object($object)) {
  2200. return '';
  2201. }
  2202. if (method_exists($object, '__toString') && null !== $object->__toString()) {
  2203. return (string) $object;
  2204. }
  2205. return sprintf("%s:%s", ClassUtils::getClass($object), spl_object_hash($object));
  2206. }
  2207. /**
  2208. * {@inheritdoc}
  2209. */
  2210. public function setLabelTranslatorStrategy(LabelTranslatorStrategyInterface $labelTranslatorStrategy)
  2211. {
  2212. $this->labelTranslatorStrategy = $labelTranslatorStrategy;
  2213. }
  2214. /**
  2215. * {@inheritdoc}
  2216. */
  2217. public function getLabelTranslatorStrategy()
  2218. {
  2219. return $this->labelTranslatorStrategy;
  2220. }
  2221. /**
  2222. * {@inheritdoc}
  2223. */
  2224. public function supportsPreviewMode()
  2225. {
  2226. return $this->supportsPreviewMode;
  2227. }
  2228. /**
  2229. * Set custom per page options
  2230. *
  2231. * @param array $options
  2232. */
  2233. public function setPerPageOptions(array $options)
  2234. {
  2235. $this->perPageOptions = $options;
  2236. }
  2237. /**
  2238. * Returns predefined per page options
  2239. *
  2240. * @return array
  2241. */
  2242. public function getPerPageOptions()
  2243. {
  2244. return $this->perPageOptions;
  2245. }
  2246. /**
  2247. * Returns true if the per page value is allowed, false otherwise
  2248. *
  2249. * @param int $perPage
  2250. *
  2251. * @return bool
  2252. */
  2253. public function determinedPerPageValue($perPage)
  2254. {
  2255. return in_array($perPage, $this->perPageOptions);
  2256. }
  2257. /**
  2258. * Predefine per page options
  2259. */
  2260. protected function predefinePerPageOptions()
  2261. {
  2262. array_unshift($this->perPageOptions, $this->maxPerPage);
  2263. $this->perPageOptions = array_unique($this->perPageOptions);
  2264. sort($this->perPageOptions);
  2265. }
  2266. /**
  2267. * {@inheritdoc}
  2268. */
  2269. public function isAclEnabled()
  2270. {
  2271. return $this->getSecurityHandler() instanceof AclSecurityHandlerInterface;
  2272. }
  2273. }