Admin.php 60 KB

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