Admin.php 59 KB

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