Admin.php 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669
  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. * @param array $templates
  997. *
  998. * @return void
  999. */
  1000. public function setTemplates(array $templates)
  1001. {
  1002. $this->templates = $templates;
  1003. }
  1004. /**
  1005. * @param string $name
  1006. * @param string $template
  1007. *
  1008. * @return void
  1009. */
  1010. public function setTemplate($name, $template)
  1011. {
  1012. $this->templates[$name] = $template;
  1013. }
  1014. /**
  1015. * @return array
  1016. */
  1017. public function getTemplates()
  1018. {
  1019. return $this->templates;
  1020. }
  1021. /**
  1022. * {@inheritdoc}
  1023. */
  1024. public function getTemplate($name)
  1025. {
  1026. if (isset($this->templates[$name])) {
  1027. return $this->templates[$name];
  1028. }
  1029. return null;
  1030. }
  1031. /**
  1032. * {@inheritdoc}
  1033. */
  1034. public function getNewInstance()
  1035. {
  1036. $object = $this->getModelManager()->getModelInstance($this->getClass());
  1037. foreach ($this->getExtensions() as $extension) {
  1038. $extension->alterNewInstance($this, $object);
  1039. }
  1040. return $object;
  1041. }
  1042. /**
  1043. * {@inheritdoc}
  1044. */
  1045. public function getFormBuilder()
  1046. {
  1047. $this->formOptions['data_class'] = $this->getClass();
  1048. $formBuilder = $this->getFormContractor()->getFormBuilder(
  1049. $this->getUniqid(),
  1050. $this->formOptions
  1051. );
  1052. $this->defineFormBuilder($formBuilder);
  1053. return $formBuilder;
  1054. }
  1055. /**
  1056. * This method is being called by the main admin class and the child class,
  1057. * the getFormBuilder is only call by the main admin class
  1058. *
  1059. * @param \Symfony\Component\Form\FormBuilder $formBuilder
  1060. *
  1061. * @return void
  1062. */
  1063. public function defineFormBuilder(FormBuilder $formBuilder)
  1064. {
  1065. $mapper = new FormMapper($this->getFormContractor(), $formBuilder, $this);
  1066. $this->configureFormFields($mapper);
  1067. foreach ($this->getExtensions() as $extension) {
  1068. $extension->configureFormFields($mapper);
  1069. }
  1070. $this->attachInlineValidator();
  1071. }
  1072. /**
  1073. * Attach the inline validator to the model metadata, this must be done once per admin
  1074. */
  1075. protected function attachInlineValidator()
  1076. {
  1077. $admin = $this;
  1078. // add the custom inline validation option
  1079. $metadata = $this->validator->getMetadataFactory()->getMetadataFor($this->getClass());
  1080. $metadata->addConstraint(new InlineConstraint(array(
  1081. 'service' => $this,
  1082. 'method' => function(ErrorElement $errorElement, $object) use ($admin) {
  1083. /* @var \Sonata\AdminBundle\Admin\AdminInterface $admin */
  1084. // This avoid the main validation to be cascaded to children
  1085. // The problem occurs when a model Page has a collection of Page as property
  1086. if ($admin->hasSubject() && spl_object_hash($object) !== spl_object_hash($admin->getSubject())) {
  1087. return;
  1088. }
  1089. $admin->validate($errorElement, $object);
  1090. foreach ($admin->getExtensions() as $extension) {
  1091. $extension->validate($admin, $errorElement, $object);
  1092. }
  1093. }
  1094. )));
  1095. }
  1096. /**
  1097. * {@inheritdoc}
  1098. */
  1099. public function attachAdminClass(FieldDescriptionInterface $fieldDescription)
  1100. {
  1101. $pool = $this->getConfigurationPool();
  1102. $adminCode = $fieldDescription->getOption('admin_code');
  1103. if ($adminCode !== null) {
  1104. $admin = $pool->getAdminByAdminCode($adminCode);
  1105. } else {
  1106. $admin = $pool->getAdminByClass($fieldDescription->getTargetEntity());
  1107. }
  1108. if (!$admin) {
  1109. return;
  1110. }
  1111. if ($this->hasRequest()) {
  1112. $admin->setRequest($this->getRequest());
  1113. }
  1114. $fieldDescription->setAssociationAdmin($admin);
  1115. }
  1116. /**
  1117. * {@inheritdoc}
  1118. */
  1119. public function getObject($id)
  1120. {
  1121. $object = $this->getModelManager()->find($this->getClass(), $id);
  1122. foreach ($this->getExtensions() as $extension) {
  1123. $extension->alterObject($this, $object);
  1124. }
  1125. return $object;
  1126. }
  1127. /**
  1128. * {@inheritdoc}
  1129. */
  1130. public function getForm()
  1131. {
  1132. $this->buildForm();
  1133. return $this->form;
  1134. }
  1135. /**
  1136. * {@inheritdoc}
  1137. */
  1138. public function getList()
  1139. {
  1140. $this->buildList();
  1141. return $this->list;
  1142. }
  1143. /**
  1144. * {@inheritdoc}
  1145. */
  1146. public function createQuery($context = 'list')
  1147. {
  1148. $query = $this->getModelManager()->createQuery($this->class);
  1149. foreach ($this->extensions as $extension) {
  1150. $extension->configureQuery($this, $query, $context);
  1151. }
  1152. return $query;
  1153. }
  1154. /**
  1155. * {@inheritdoc}
  1156. */
  1157. public function getDatagrid()
  1158. {
  1159. $this->buildDatagrid();
  1160. return $this->datagrid;
  1161. }
  1162. /**
  1163. * Build the side menu related to the current action
  1164. *
  1165. * @param string $action
  1166. * @param \Sonata\AdminBundle\Admin\AdminInterface $childAdmin
  1167. *
  1168. * @return \Knp\Menu\ItemInterface|boolean
  1169. */
  1170. public function buildSideMenu($action, AdminInterface $childAdmin = null)
  1171. {
  1172. if ($this->loaded['side_menu']) {
  1173. return;
  1174. }
  1175. $this->loaded['side_menu'] = true;
  1176. $menu = $this->menuFactory->createItem('root');
  1177. $menu->setChildrenAttribute('class', 'nav nav-list');
  1178. $menu->setCurrentUri($this->getRequest()->getBaseUrl().$this->getRequest()->getPathInfo());
  1179. $this->configureSideMenu($menu, $action, $childAdmin);
  1180. foreach ($this->getExtensions() as $extension) {
  1181. $extension->configureSideMenu($this, $menu, $action, $childAdmin);
  1182. }
  1183. $this->menu = $menu;
  1184. }
  1185. /**
  1186. * @param string $action
  1187. * @param \Sonata\AdminBundle\Admin\AdminInterface $childAdmin
  1188. *
  1189. * @return \Knp\Menu\ItemInterface
  1190. */
  1191. public function getSideMenu($action, AdminInterface $childAdmin = null)
  1192. {
  1193. if ($this->isChild()) {
  1194. return $this->getParent()->getSideMenu($action, $this);
  1195. }
  1196. $this->buildSideMenu($action, $childAdmin);
  1197. return $this->menu;
  1198. }
  1199. /**
  1200. * Returns the root code
  1201. *
  1202. * @return string the root code
  1203. */
  1204. public function getRootCode()
  1205. {
  1206. return $this->getRoot()->getCode();
  1207. }
  1208. /**
  1209. * Returns the master admin
  1210. *
  1211. * @return \Sonata\AdminBundle\Admin\Admin the root admin class
  1212. */
  1213. public function getRoot()
  1214. {
  1215. $parentFieldDescription = $this->getParentFieldDescription();
  1216. if (!$parentFieldDescription) {
  1217. return $this;
  1218. }
  1219. return $parentFieldDescription->getAdmin()->getRoot();
  1220. }
  1221. /**
  1222. * {@inheritdoc}
  1223. */
  1224. public function setBaseControllerName($baseControllerName)
  1225. {
  1226. $this->baseControllerName = $baseControllerName;
  1227. }
  1228. /**
  1229. * {@inheritdoc}
  1230. */
  1231. public function getBaseControllerName()
  1232. {
  1233. return $this->baseControllerName;
  1234. }
  1235. /**
  1236. * @param string $label
  1237. */
  1238. public function setLabel($label)
  1239. {
  1240. $this->label = $label;
  1241. }
  1242. /**
  1243. * {@inheritdoc}
  1244. */
  1245. public function getLabel()
  1246. {
  1247. return $this->label;
  1248. }
  1249. /**
  1250. * @param boolean $persist
  1251. */
  1252. public function setPersistFilters($persist)
  1253. {
  1254. $this->persistFilters = $persist;
  1255. }
  1256. /**
  1257. * @param int $maxPerPage
  1258. */
  1259. public function setMaxPerPage($maxPerPage)
  1260. {
  1261. $this->maxPerPage = $maxPerPage;
  1262. }
  1263. /**
  1264. * @return int
  1265. */
  1266. public function getMaxPerPage()
  1267. {
  1268. return $this->maxPerPage;
  1269. }
  1270. /**
  1271. * @param int $maxPageLinks
  1272. */
  1273. public function setMaxPageLinks($maxPageLinks)
  1274. {
  1275. $this->maxPageLinks = $maxPageLinks;
  1276. }
  1277. /**
  1278. * @return int
  1279. */
  1280. public function getMaxPageLinks()
  1281. {
  1282. return $this->maxPageLinks;
  1283. }
  1284. /**
  1285. * {@inheritdoc}
  1286. */
  1287. public function getFormGroups()
  1288. {
  1289. return $this->formGroups;
  1290. }
  1291. /**
  1292. * {@inheritdoc}
  1293. */
  1294. public function setFormGroups(array $formGroups)
  1295. {
  1296. $this->formGroups = $formGroups;
  1297. }
  1298. /**
  1299. * @param array $group
  1300. * @param array $keys
  1301. */
  1302. public function reorderFormGroup($group, array $keys)
  1303. {
  1304. $formGroups = $this->getFormGroups();
  1305. $formGroups[$group]['fields'] = array_merge(array_flip($keys), $formGroups[$group]['fields']);
  1306. $this->setFormGroups($formGroups);
  1307. }
  1308. /**
  1309. * {@inheritdoc}
  1310. */
  1311. public function getShowGroups()
  1312. {
  1313. return $this->showGroups;
  1314. }
  1315. /**
  1316. * {@inheritdoc}
  1317. */
  1318. public function setShowGroups(array $showGroups)
  1319. {
  1320. $this->showGroups = $showGroups;
  1321. }
  1322. /**
  1323. * {@inheritdoc}
  1324. */
  1325. public function reorderShowGroup($group, array $keys)
  1326. {
  1327. $showGroups = $this->getShowGroups();
  1328. $showGroups[$group]['fields'] = array_merge(array_flip($keys), $showGroups[$group]['fields']);
  1329. $this->setShowGroups($showGroups);
  1330. }
  1331. /**
  1332. * {@inheritdoc}
  1333. */
  1334. public function setParentFieldDescription(FieldDescriptionInterface $parentFieldDescription)
  1335. {
  1336. $this->parentFieldDescription = $parentFieldDescription;
  1337. }
  1338. /**
  1339. * {@inheritdoc}
  1340. */
  1341. public function getParentFieldDescription()
  1342. {
  1343. return $this->parentFieldDescription;
  1344. }
  1345. /**
  1346. * {@inheritdoc}
  1347. */
  1348. public function hasParentFieldDescription()
  1349. {
  1350. return $this->parentFieldDescription instanceof FieldDescriptionInterface;
  1351. }
  1352. /**
  1353. * {@inheritdoc}
  1354. */
  1355. public function setSubject($subject)
  1356. {
  1357. $this->subject = $subject;
  1358. }
  1359. /**
  1360. * {@inheritdoc}
  1361. */
  1362. public function getSubject()
  1363. {
  1364. if ($this->subject === null && $this->request) {
  1365. $id = $this->request->get($this->getIdParameter());
  1366. if (!preg_match('#^[0-9A-Fa-f]+$#', $id)) {
  1367. $this->subject = false;
  1368. } else {
  1369. $this->subject = $this->getModelManager()->find($this->class, $id);
  1370. }
  1371. }
  1372. return $this->subject;
  1373. }
  1374. /**
  1375. * {@inheritdoc}
  1376. */
  1377. public function hasSubject()
  1378. {
  1379. return $this->subject != null;
  1380. }
  1381. /**
  1382. * {@inheritdoc}
  1383. */
  1384. public function getFormFieldDescriptions()
  1385. {
  1386. $this->buildForm();
  1387. return $this->formFieldDescriptions;
  1388. }
  1389. /**
  1390. * {@inheritdoc}
  1391. */
  1392. public function getFormFieldDescription($name)
  1393. {
  1394. return $this->hasFormFieldDescription($name) ? $this->formFieldDescriptions[$name] : null;
  1395. }
  1396. /**
  1397. * Returns true if the admin has a FieldDescription with the given $name
  1398. *
  1399. * @param string $name
  1400. *
  1401. * @return bool
  1402. */
  1403. public function hasFormFieldDescription($name)
  1404. {
  1405. return array_key_exists($name, $this->formFieldDescriptions) ? true : false;
  1406. }
  1407. /**
  1408. * {@inheritdoc}
  1409. */
  1410. public function addFormFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1411. {
  1412. $this->formFieldDescriptions[$name] = $fieldDescription;
  1413. }
  1414. /**
  1415. * remove a FieldDescription
  1416. *
  1417. * @param string $name
  1418. *
  1419. * @return void
  1420. */
  1421. public function removeFormFieldDescription($name)
  1422. {
  1423. unset($this->formFieldDescriptions[$name]);
  1424. }
  1425. /**
  1426. * build and return the collection of form FieldDescription
  1427. *
  1428. * @return array collection of form FieldDescription
  1429. */
  1430. public function getShowFieldDescriptions()
  1431. {
  1432. $this->buildShow();
  1433. return $this->showFieldDescriptions;
  1434. }
  1435. /**
  1436. * Returns the form FieldDescription with the given $name
  1437. *
  1438. * @param string $name
  1439. *
  1440. * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
  1441. */
  1442. public function getShowFieldDescription($name)
  1443. {
  1444. $this->buildShow();
  1445. return $this->hasShowFieldDescription($name) ? $this->showFieldDescriptions[$name] : null;
  1446. }
  1447. /**
  1448. * {@inheritdoc}
  1449. */
  1450. public function hasShowFieldDescription($name)
  1451. {
  1452. return array_key_exists($name, $this->showFieldDescriptions);
  1453. }
  1454. /**
  1455. * {@inheritdoc}
  1456. */
  1457. public function addShowFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1458. {
  1459. $this->showFieldDescriptions[$name] = $fieldDescription;
  1460. }
  1461. /**
  1462. * {@inheritdoc}
  1463. */
  1464. public function removeShowFieldDescription($name)
  1465. {
  1466. unset($this->showFieldDescriptions[$name]);
  1467. }
  1468. /**
  1469. * {@inheritdoc}
  1470. */
  1471. public function getListFieldDescriptions()
  1472. {
  1473. $this->buildList();
  1474. return $this->listFieldDescriptions;
  1475. }
  1476. /**
  1477. * {@inheritdoc}
  1478. */
  1479. public function getListFieldDescription($name)
  1480. {
  1481. return $this->hasListFieldDescription($name) ? $this->listFieldDescriptions[$name] : null;
  1482. }
  1483. /**
  1484. * {@inheritdoc}
  1485. */
  1486. public function hasListFieldDescription($name)
  1487. {
  1488. $this->buildList();
  1489. return array_key_exists($name, $this->listFieldDescriptions) ? true : false;
  1490. }
  1491. /**
  1492. * {@inheritdoc}
  1493. */
  1494. public function addListFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1495. {
  1496. $this->listFieldDescriptions[$name] = $fieldDescription;
  1497. }
  1498. /**
  1499. * {@inheritdoc}
  1500. */
  1501. public function removeListFieldDescription($name)
  1502. {
  1503. unset($this->listFieldDescriptions[$name]);
  1504. }
  1505. /**
  1506. * Returns a filter FieldDescription
  1507. *
  1508. * @param string $name
  1509. *
  1510. * @return array|null
  1511. */
  1512. public function getFilterFieldDescription($name)
  1513. {
  1514. return $this->hasFilterFieldDescription($name) ? $this->filterFieldDescriptions[$name] : null;
  1515. }
  1516. /**
  1517. * {@inheritdoc}
  1518. */
  1519. public function hasFilterFieldDescription($name)
  1520. {
  1521. return array_key_exists($name, $this->filterFieldDescriptions) ? true : false;
  1522. }
  1523. /**
  1524. * {@inheritdoc}
  1525. */
  1526. public function addFilterFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1527. {
  1528. $this->filterFieldDescriptions[$name] = $fieldDescription;
  1529. }
  1530. /**
  1531. * {@inheritdoc}
  1532. */
  1533. public function removeFilterFieldDescription($name)
  1534. {
  1535. unset($this->filterFieldDescriptions[$name]);
  1536. }
  1537. /**
  1538. * {@inheritdoc}
  1539. */
  1540. public function getFilterFieldDescriptions()
  1541. {
  1542. $this->buildDatagrid();
  1543. return $this->filterFieldDescriptions;
  1544. }
  1545. /**
  1546. * {@inheritdoc}
  1547. */
  1548. public function addChild(AdminInterface $child)
  1549. {
  1550. $this->children[$child->getCode()] = $child;
  1551. $child->setBaseCodeRoute($this->getCode().'|'.$child->getCode());
  1552. $child->setParent($this);
  1553. }
  1554. /**
  1555. * {@inheritdoc}
  1556. */
  1557. public function hasChild($code)
  1558. {
  1559. return isset($this->children[$code]);
  1560. }
  1561. /**
  1562. * {@inheritdoc}
  1563. */
  1564. public function getChildren()
  1565. {
  1566. return $this->children;
  1567. }
  1568. /**
  1569. * {@inheritdoc}
  1570. */
  1571. public function getChild($code)
  1572. {
  1573. return $this->hasChild($code) ? $this->children[$code] : null;
  1574. }
  1575. /**
  1576. * {@inheritdoc}
  1577. */
  1578. public function setParent(AdminInterface $parent)
  1579. {
  1580. $this->parent = $parent;
  1581. }
  1582. /**
  1583. * {@inheritdoc}
  1584. */
  1585. public function getParent()
  1586. {
  1587. return $this->parent;
  1588. }
  1589. /**
  1590. * {@inheritdoc}
  1591. */
  1592. public function isChild()
  1593. {
  1594. return $this->parent instanceof AdminInterface;
  1595. }
  1596. /**
  1597. * Returns true if the admin has children, false otherwise
  1598. *
  1599. * @return bool if the admin has children
  1600. */
  1601. public function hasChildren()
  1602. {
  1603. return count($this->children) > 0;
  1604. }
  1605. /**
  1606. * {@inheritdoc}
  1607. */
  1608. public function setUniqid($uniqid)
  1609. {
  1610. $this->uniqid = $uniqid;
  1611. }
  1612. /**
  1613. * {@inheritdoc}
  1614. */
  1615. public function getUniqid()
  1616. {
  1617. if (!$this->uniqid) {
  1618. $this->uniqid = "s".uniqid();
  1619. }
  1620. return $this->uniqid;
  1621. }
  1622. /**
  1623. * Returns the classname label
  1624. *
  1625. * @return string the classname label
  1626. */
  1627. public function getClassnameLabel()
  1628. {
  1629. return $this->classnameLabel;
  1630. }
  1631. /**
  1632. * {@inheritdoc}
  1633. */
  1634. public function getPersistentParameters()
  1635. {
  1636. $parameters = array();
  1637. foreach ($this->getExtensions() as $extension) {
  1638. $params = $extension->getPersistentParameters($this);
  1639. if (!is_array($params)) {
  1640. throw new \RuntimeException(sprintf('The %s::getPersistentParameters must return an array', get_class($extension)));
  1641. }
  1642. $parameters = array_merge($parameters, $params);
  1643. }
  1644. return $parameters;
  1645. }
  1646. /**
  1647. * @param string $name
  1648. *
  1649. * @return null|mixed
  1650. */
  1651. public function getPersistentParameter($name)
  1652. {
  1653. $parameters = $this->getPersistentParameters();
  1654. return isset($parameters[$name]) ? $parameters[$name] : null;
  1655. }
  1656. /**
  1657. * {@inheritdoc}
  1658. */
  1659. public function getBreadcrumbs($action)
  1660. {
  1661. if ($this->isChild()) {
  1662. return $this->getParent()->getBreadcrumbs($action);
  1663. }
  1664. $menu = $this->buildBreadcrumbs($action);
  1665. do {
  1666. $breadcrumbs[] = $menu;
  1667. } while ($menu = $menu->getParent());
  1668. $breadcrumbs = array_reverse($breadcrumbs);
  1669. array_shift($breadcrumbs);
  1670. return $breadcrumbs;
  1671. }
  1672. /**
  1673. * Generates the breadcrumbs array
  1674. *
  1675. * Note: the method will be called by the top admin instance (parent => child)
  1676. *
  1677. * @param string $action
  1678. * @param \Knp\Menu\ItemInterface|null $menu
  1679. *
  1680. * @return array
  1681. */
  1682. public function buildBreadcrumbs($action, MenuItemInterface $menu = null)
  1683. {
  1684. if (isset($this->breadcrumbs[$action])) {
  1685. return $this->breadcrumbs[$action];
  1686. }
  1687. if (!$menu) {
  1688. $menu = $this->menuFactory->createItem('root');
  1689. $menu = $menu->addChild(
  1690. $this->trans($this->getLabelTranslatorStrategy()->getLabel('dashboard', 'breadcrumb', 'link'), array(), 'SonataAdminBundle'),
  1691. array('uri' => $this->routeGenerator->generate('sonata_admin_dashboard'))
  1692. );
  1693. }
  1694. $menu = $menu->addChild(
  1695. $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_list', $this->getClassnameLabel()), 'breadcrumb', 'link')),
  1696. array('uri' => $this->hasRoute('list') && $this->isGranted('LIST') ? $this->generateUrl('list') : null)
  1697. );
  1698. $childAdmin = $this->getCurrentChildAdmin();
  1699. if ($childAdmin) {
  1700. $id = $this->request->get($this->getIdParameter());
  1701. $menu = $menu->addChild(
  1702. $this->toString($this->getSubject()),
  1703. array('uri' => $this->hasRoute('edit') && $this->isGranted('EDIT') ? $this->generateUrl('edit', array('id' => $id)) : null)
  1704. );
  1705. return $childAdmin->buildBreadcrumbs($action, $menu);
  1706. } elseif ($this->isChild()) {
  1707. if ($action == 'list') {
  1708. $menu->setUri(false);
  1709. } elseif ($action != 'create' && $this->hasSubject()) {
  1710. $menu = $menu->addChild($this->toString($this->getSubject()));
  1711. } else {
  1712. $menu = $menu->addChild(
  1713. $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_%s', $this->getClassnameLabel(), $action), 'breadcrumb', 'link'))
  1714. );
  1715. }
  1716. } elseif ($action != 'list' && $this->hasSubject()) {
  1717. $menu = $menu->addChild($this->toString($this->getSubject()));
  1718. } elseif ($action != 'list') {
  1719. $menu = $menu->addChild(
  1720. $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_%s', $this->getClassnameLabel(), $action), 'breadcrumb', 'link'))
  1721. );
  1722. } else {
  1723. $menu->getBreadcrumbsArray();
  1724. }
  1725. return $this->breadcrumbs[$action] = $menu;
  1726. }
  1727. /**
  1728. * {@inheritdoc}
  1729. */
  1730. public function setCurrentChild($currentChild)
  1731. {
  1732. $this->currentChild = $currentChild;
  1733. }
  1734. /**
  1735. * {@inheritdoc}
  1736. */
  1737. public function getCurrentChild()
  1738. {
  1739. return $this->currentChild;
  1740. }
  1741. /**
  1742. * Returns the current child admin instance
  1743. *
  1744. * @return \Sonata\AdminBundle\Admin\AdminInterface|null the current child admin instance
  1745. */
  1746. public function getCurrentChildAdmin()
  1747. {
  1748. foreach ($this->children as $children) {
  1749. if ($children->getCurrentChild()) {
  1750. return $children;
  1751. }
  1752. }
  1753. return null;
  1754. }
  1755. /**
  1756. * {@inheritdoc}
  1757. */
  1758. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  1759. {
  1760. $domain = $domain ?: $this->translationDomain;
  1761. if (!$this->translator) {
  1762. return $id;
  1763. }
  1764. return $this->translator->trans($id, $parameters, $domain, $locale);
  1765. }
  1766. /**
  1767. * translate a message id
  1768. *
  1769. * @param string $id
  1770. * @param integer $count
  1771. * @param array $parameters
  1772. * @param null $domain
  1773. * @param null $locale
  1774. *
  1775. * @return string the translated string
  1776. */
  1777. public function transChoice($id, $count, array $parameters = array(), $domain = null, $locale = null)
  1778. {
  1779. $domain = $domain ?: $this->translationDomain;
  1780. if (!$this->translator) {
  1781. return $id;
  1782. }
  1783. return $this->translator->transChoice($id, $count, $parameters, $domain, $locale);
  1784. }
  1785. /**
  1786. * {@inheritdoc}
  1787. */
  1788. public function setTranslationDomain($translationDomain)
  1789. {
  1790. $this->translationDomain = $translationDomain;
  1791. }
  1792. /**
  1793. * {@inheritdoc}
  1794. */
  1795. public function getTranslationDomain()
  1796. {
  1797. return $this->translationDomain;
  1798. }
  1799. /**
  1800. * {@inheritdoc}
  1801. */
  1802. public function setTranslator(TranslatorInterface $translator)
  1803. {
  1804. $this->translator = $translator;
  1805. }
  1806. /**
  1807. * {@inheritdoc}
  1808. */
  1809. public function getTranslator()
  1810. {
  1811. return $this->translator;
  1812. }
  1813. /**
  1814. * {@inheritdoc}
  1815. */
  1816. public function getTranslationLabel($label, $context = '', $type = '')
  1817. {
  1818. return $this->getLabelTranslatorStrategy()->getLabel($label, $context, $type);
  1819. }
  1820. /**
  1821. * {@inheritdoc}
  1822. */
  1823. public function setRequest(Request $request)
  1824. {
  1825. $this->request = $request;
  1826. foreach ($this->getChildren() as $children) {
  1827. $children->setRequest($request);
  1828. }
  1829. }
  1830. /**
  1831. * {@inheritdoc}
  1832. */
  1833. public function getRequest()
  1834. {
  1835. if (!$this->request) {
  1836. throw new \RuntimeException('The Request object has not been set');
  1837. }
  1838. return $this->request;
  1839. }
  1840. /**
  1841. * {@inheritdoc}
  1842. */
  1843. public function hasRequest()
  1844. {
  1845. return $this->request !== null;
  1846. }
  1847. /**
  1848. * {@inheritdoc}
  1849. */
  1850. public function setFormContractor(FormContractorInterface $formBuilder)
  1851. {
  1852. $this->formContractor = $formBuilder;
  1853. }
  1854. /**
  1855. * @return \Sonata\AdminBundle\Builder\FormContractorInterface
  1856. */
  1857. public function getFormContractor()
  1858. {
  1859. return $this->formContractor;
  1860. }
  1861. /**
  1862. * {@inheritdoc}
  1863. */
  1864. public function setDatagridBuilder(DatagridBuilderInterface $datagridBuilder)
  1865. {
  1866. $this->datagridBuilder = $datagridBuilder;
  1867. }
  1868. /**
  1869. * {@inheritdoc}
  1870. */
  1871. public function getDatagridBuilder()
  1872. {
  1873. return $this->datagridBuilder;
  1874. }
  1875. /**
  1876. * {@inheritdoc}
  1877. */
  1878. public function setListBuilder(ListBuilderInterface $listBuilder)
  1879. {
  1880. $this->listBuilder = $listBuilder;
  1881. }
  1882. /**
  1883. * {@inheritdoc}
  1884. */
  1885. public function getListBuilder()
  1886. {
  1887. return $this->listBuilder;
  1888. }
  1889. /**
  1890. * @param \Sonata\AdminBundle\Builder\ShowBuilderInterface $showBuilder
  1891. *
  1892. * @return void
  1893. */
  1894. public function setShowBuilder(ShowBuilderInterface $showBuilder)
  1895. {
  1896. $this->showBuilder = $showBuilder;
  1897. }
  1898. /**
  1899. * @return \Sonata\AdminBundle\Builder\ShowBuilderInterface
  1900. */
  1901. public function getShowBuilder()
  1902. {
  1903. return $this->showBuilder;
  1904. }
  1905. /**
  1906. * {@inheritdoc}
  1907. */
  1908. public function setConfigurationPool(Pool $configurationPool)
  1909. {
  1910. $this->configurationPool = $configurationPool;
  1911. }
  1912. /**
  1913. * @return Pool
  1914. */
  1915. public function getConfigurationPool()
  1916. {
  1917. return $this->configurationPool;
  1918. }
  1919. /**
  1920. * {@inheritdoc}
  1921. */
  1922. public function setRouteGenerator(RouteGeneratorInterface $routeGenerator)
  1923. {
  1924. $this->routeGenerator = $routeGenerator;
  1925. }
  1926. /**
  1927. * @return \Sonata\AdminBundle\Route\RouteGeneratorInterface
  1928. */
  1929. public function getRouteGenerator()
  1930. {
  1931. return $this->routeGenerator;
  1932. }
  1933. /**
  1934. * {@inheritdoc}
  1935. */
  1936. public function getCode()
  1937. {
  1938. return $this->code;
  1939. }
  1940. /**
  1941. * @param string $baseCodeRoute
  1942. */
  1943. public function setBaseCodeRoute($baseCodeRoute)
  1944. {
  1945. $this->baseCodeRoute = $baseCodeRoute;
  1946. }
  1947. /**
  1948. * {@inheritdoc}
  1949. */
  1950. public function getBaseCodeRoute()
  1951. {
  1952. return $this->baseCodeRoute;
  1953. }
  1954. /**
  1955. * {@inheritdoc}
  1956. */
  1957. public function getModelManager()
  1958. {
  1959. return $this->modelManager;
  1960. }
  1961. /**
  1962. * @param \Sonata\AdminBundle\Model\ModelManagerInterface $modelManager
  1963. */
  1964. public function setModelManager(ModelManagerInterface $modelManager)
  1965. {
  1966. $this->modelManager = $modelManager;
  1967. }
  1968. /**
  1969. * {@inheritdoc}
  1970. */
  1971. public function getManagerType()
  1972. {
  1973. return $this->managerType;
  1974. }
  1975. /**
  1976. * @param string $type
  1977. */
  1978. public function setManagerType($type)
  1979. {
  1980. $this->managerType = $type;
  1981. }
  1982. /**
  1983. * {@inheritdoc}
  1984. */
  1985. public function getObjectIdentifier()
  1986. {
  1987. return $this->getCode();
  1988. }
  1989. /**
  1990. * Set the roles and permissions per role
  1991. *
  1992. * @param array $information
  1993. */
  1994. public function setSecurityInformation(array $information)
  1995. {
  1996. $this->securityInformation = $information;
  1997. }
  1998. /**
  1999. * {@inheritdoc}
  2000. */
  2001. public function getSecurityInformation()
  2002. {
  2003. return $this->securityInformation;
  2004. }
  2005. /**
  2006. * Return the list of permissions the user should have in order to display the admin
  2007. *
  2008. * @param string $context
  2009. *
  2010. * @return array
  2011. */
  2012. public function getPermissionsShow($context)
  2013. {
  2014. switch ($context) {
  2015. case self::CONTEXT_DASHBOARD:
  2016. case self::CONTEXT_MENU:
  2017. default:
  2018. return array('LIST');
  2019. }
  2020. }
  2021. /**
  2022. * {@inheritdoc}
  2023. */
  2024. public function showIn($context)
  2025. {
  2026. switch ($context) {
  2027. case self::CONTEXT_DASHBOARD:
  2028. case self::CONTEXT_MENU:
  2029. default:
  2030. return $this->isGranted($this->getPermissionsShow($context));
  2031. }
  2032. }
  2033. /**
  2034. * {@inheritdoc}
  2035. */
  2036. public function createObjectSecurity($object)
  2037. {
  2038. $this->getSecurityHandler()->createObjectSecurity($this, $object);
  2039. }
  2040. /**
  2041. * {@inheritdoc}
  2042. */
  2043. public function setSecurityHandler(SecurityHandlerInterface $securityHandler)
  2044. {
  2045. $this->securityHandler = $securityHandler;
  2046. }
  2047. /**
  2048. * {@inheritdoc}
  2049. */
  2050. public function getSecurityHandler()
  2051. {
  2052. return $this->securityHandler;
  2053. }
  2054. /**
  2055. * {@inheritdoc}
  2056. */
  2057. public function isGranted($name, $object = null)
  2058. {
  2059. return $this->securityHandler->isGranted($this, $name, $object ?: $this);
  2060. }
  2061. /**
  2062. * {@inheritdoc}
  2063. */
  2064. public function getUrlsafeIdentifier($entity)
  2065. {
  2066. return $this->getModelManager()->getUrlsafeIdentifier($entity);
  2067. }
  2068. /**
  2069. * {@inheritdoc}
  2070. */
  2071. public function getNormalizedIdentifier($entity)
  2072. {
  2073. return $this->getModelManager()->getNormalizedIdentifier($entity);
  2074. }
  2075. /**
  2076. * {@inheritdoc}
  2077. */
  2078. public function id($entity)
  2079. {
  2080. return $this->getNormalizedIdentifier($entity);
  2081. }
  2082. /**
  2083. * {@inheritdoc}
  2084. */
  2085. public function setValidator(ValidatorInterface $validator)
  2086. {
  2087. $this->validator = $validator;
  2088. }
  2089. /**
  2090. * {@inheritdoc}
  2091. */
  2092. public function getValidator()
  2093. {
  2094. return $this->validator;
  2095. }
  2096. /**
  2097. * {@inheritdoc}
  2098. */
  2099. public function getShow()
  2100. {
  2101. $this->buildShow();
  2102. return $this->show;
  2103. }
  2104. /**
  2105. * {@inheritdoc}
  2106. */
  2107. public function setFormTheme(array $formTheme)
  2108. {
  2109. $this->formTheme = $formTheme;
  2110. }
  2111. /**
  2112. * {@inheritdoc}
  2113. */
  2114. public function getFormTheme()
  2115. {
  2116. return $this->formTheme;
  2117. }
  2118. /**
  2119. * {@inheritdoc}
  2120. */
  2121. public function setFilterTheme(array $filterTheme)
  2122. {
  2123. $this->filterTheme = $filterTheme;
  2124. }
  2125. /**
  2126. * {@inheritdoc}
  2127. */
  2128. public function getFilterTheme()
  2129. {
  2130. return $this->filterTheme;
  2131. }
  2132. /**
  2133. * {@inheritdoc}
  2134. */
  2135. public function addExtension(AdminExtensionInterface $extension)
  2136. {
  2137. $this->extensions[] = $extension;
  2138. }
  2139. /**
  2140. * {@inheritdoc}
  2141. */
  2142. public function getExtensions()
  2143. {
  2144. return $this->extensions;
  2145. }
  2146. /**
  2147. * {@inheritdoc}
  2148. */
  2149. public function setMenuFactory(MenuFactoryInterface $menuFactory)
  2150. {
  2151. $this->menuFactory = $menuFactory;
  2152. }
  2153. /**
  2154. * {@inheritdoc}
  2155. */
  2156. public function getMenuFactory()
  2157. {
  2158. return $this->menuFactory;
  2159. }
  2160. /**
  2161. * {@inheritdoc}
  2162. */
  2163. public function setRouteBuilder(RouteBuilderInterface $routeBuilder)
  2164. {
  2165. $this->routeBuilder = $routeBuilder;
  2166. }
  2167. /**
  2168. * {@inheritdoc}
  2169. */
  2170. public function getRouteBuilder()
  2171. {
  2172. return $this->routeBuilder;
  2173. }
  2174. /**
  2175. * {@inheritdoc}
  2176. */
  2177. public function toString($object)
  2178. {
  2179. if (!is_object($object)) {
  2180. return '';
  2181. }
  2182. if (method_exists($object, '__toString') && null !== $object->__toString()) {
  2183. return (string) $object;
  2184. }
  2185. return sprintf("%s:%s", ClassUtils::getClass($object), spl_object_hash($object));
  2186. }
  2187. /**
  2188. * {@inheritdoc}
  2189. */
  2190. public function setLabelTranslatorStrategy(LabelTranslatorStrategyInterface $labelTranslatorStrategy)
  2191. {
  2192. $this->labelTranslatorStrategy = $labelTranslatorStrategy;
  2193. }
  2194. /**
  2195. * {@inheritdoc}
  2196. */
  2197. public function getLabelTranslatorStrategy()
  2198. {
  2199. return $this->labelTranslatorStrategy;
  2200. }
  2201. /**
  2202. * {@inheritdoc}
  2203. */
  2204. public function supportsPreviewMode()
  2205. {
  2206. return $this->supportsPreviewMode;
  2207. }
  2208. /**
  2209. * Set custom per page options
  2210. *
  2211. * @param array $options
  2212. */
  2213. public function setPerPageOptions(array $options)
  2214. {
  2215. $this->perPageOptions = $options;
  2216. }
  2217. /**
  2218. * Returns predefined per page options
  2219. *
  2220. * @return array
  2221. */
  2222. public function getPerPageOptions()
  2223. {
  2224. return $this->perPageOptions;
  2225. }
  2226. /**
  2227. * Returns true if the per page value is allowed, false otherwise
  2228. *
  2229. * @param int $perPage
  2230. *
  2231. * @return bool
  2232. */
  2233. public function determinedPerPageValue($perPage)
  2234. {
  2235. return in_array($perPage, $this->perPageOptions);
  2236. }
  2237. /**
  2238. * Predefine per page options
  2239. */
  2240. protected function predefinePerPageOptions()
  2241. {
  2242. array_unshift($this->perPageOptions, $this->maxPerPage);
  2243. $this->perPageOptions = array_unique($this->perPageOptions);
  2244. sort($this->perPageOptions);
  2245. }
  2246. /**
  2247. * {@inheritdoc}
  2248. */
  2249. public function isAclEnabled()
  2250. {
  2251. return $this->getSecurityHandler() instanceof AclSecurityHandlerInterface;
  2252. }
  2253. }