Admin.php 60 KB

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