Admin.php 60 KB

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