AbstractAdmin.php 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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 Doctrine\Common\Util\ClassUtils;
  12. use Knp\Menu\FactoryInterface as MenuFactoryInterface;
  13. use Knp\Menu\ItemInterface;
  14. use Knp\Menu\ItemInterface as MenuItemInterface;
  15. use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
  16. use Sonata\AdminBundle\Builder\FormContractorInterface;
  17. use Sonata\AdminBundle\Builder\ListBuilderInterface;
  18. use Sonata\AdminBundle\Builder\RouteBuilderInterface;
  19. use Sonata\AdminBundle\Builder\ShowBuilderInterface;
  20. use Sonata\AdminBundle\Datagrid\DatagridInterface;
  21. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  22. use Sonata\AdminBundle\Datagrid\ListMapper;
  23. use Sonata\AdminBundle\Datagrid\Pager;
  24. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  25. use Sonata\AdminBundle\Form\FormMapper;
  26. use Sonata\AdminBundle\Model\ModelManagerInterface;
  27. use Sonata\AdminBundle\Route\RouteCollection;
  28. use Sonata\AdminBundle\Route\RouteGeneratorInterface;
  29. use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
  30. use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
  31. use Sonata\AdminBundle\Show\ShowMapper;
  32. use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
  33. use Sonata\CoreBundle\Model\Metadata;
  34. use Sonata\CoreBundle\Validator\Constraints\InlineConstraint;
  35. use Sonata\CoreBundle\Validator\ErrorElement;
  36. use Symfony\Component\Form\Form;
  37. use Symfony\Component\Form\FormBuilderInterface;
  38. use Symfony\Component\HttpFoundation\Request;
  39. use Symfony\Component\PropertyAccess\PropertyPath;
  40. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  41. use Symfony\Component\Security\Acl\Model\DomainObjectInterface;
  42. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  43. use Symfony\Component\Translation\TranslatorInterface;
  44. use Symfony\Component\Validator\Validator\ValidatorInterface;
  45. use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
  46. /**
  47. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  48. */
  49. abstract class AbstractAdmin implements AdminInterface, DomainObjectInterface
  50. {
  51. const CONTEXT_MENU = 'menu';
  52. const CONTEXT_DASHBOARD = 'dashboard';
  53. const CLASS_REGEX =
  54. '@
  55. (?:([A-Za-z0-9]*)\\\)? # vendor name / app name
  56. (Bundle\\\)? # optional bundle directory
  57. ([A-Za-z0-9]+?)(?:Bundle)?\\\ # bundle name, with optional suffix
  58. (
  59. Entity|Document|Model|PHPCR|CouchDocument|Phpcr|
  60. Doctrine\\\Orm|Doctrine\\\Phpcr|Doctrine\\\MongoDB|Doctrine\\\CouchDB
  61. )\\\(.*)@x';
  62. const MOSAIC_ICON_CLASS = 'fa fa-th-large fa-fw';
  63. /**
  64. * The list FieldDescription constructed from the configureListField method.
  65. *
  66. * @var array
  67. */
  68. protected $listFieldDescriptions = array();
  69. /**
  70. * The show FieldDescription constructed from the configureShowFields method.
  71. *
  72. * @var array
  73. */
  74. protected $showFieldDescriptions = array();
  75. /**
  76. * The list FieldDescription constructed from the configureFormField method.
  77. *
  78. * @var array
  79. */
  80. protected $formFieldDescriptions = array();
  81. /**
  82. * The filter FieldDescription constructed from the configureFilterField method.
  83. *
  84. * @var array
  85. */
  86. protected $filterFieldDescriptions = array();
  87. /**
  88. * The number of result to display in the list.
  89. *
  90. * @var int
  91. */
  92. protected $maxPerPage = 32;
  93. /**
  94. * The maximum number of page numbers to display in the list.
  95. *
  96. * @var int
  97. */
  98. protected $maxPageLinks = 25;
  99. /**
  100. * The base route name used to generate the routing information.
  101. *
  102. * @var string
  103. */
  104. protected $baseRouteName;
  105. /**
  106. * The base route pattern used to generate the routing information.
  107. *
  108. * @var string
  109. */
  110. protected $baseRoutePattern;
  111. /**
  112. * The base name controller used to generate the routing information.
  113. *
  114. * @var string
  115. */
  116. protected $baseControllerName;
  117. /**
  118. * The label class name (used in the title/breadcrumb ...).
  119. *
  120. * @var string
  121. */
  122. protected $classnameLabel;
  123. /**
  124. * The translation domain to be used to translate messages.
  125. *
  126. * @var string
  127. */
  128. protected $translationDomain = 'messages';
  129. /**
  130. * Options to set to the form (ie, validation_groups).
  131. *
  132. * @var array
  133. */
  134. protected $formOptions = array();
  135. /**
  136. * Default values to the datagrid.
  137. *
  138. * @var array
  139. */
  140. protected $datagridValues = array(
  141. '_page' => 1,
  142. '_per_page' => 32,
  143. );
  144. /**
  145. * Predefined per page options.
  146. *
  147. * @var array
  148. */
  149. protected $perPageOptions = array(16, 32, 64, 128, 192);
  150. /**
  151. * Pager type.
  152. *
  153. * @var string
  154. */
  155. protected $pagerType = Pager::TYPE_DEFAULT;
  156. /**
  157. * The code related to the admin.
  158. *
  159. * @var string
  160. */
  161. protected $code;
  162. /**
  163. * The label.
  164. *
  165. * @var string
  166. */
  167. protected $label;
  168. /**
  169. * Whether or not to persist the filters in the session.
  170. *
  171. * @var bool
  172. */
  173. protected $persistFilters = false;
  174. /**
  175. * Array of routes related to this admin.
  176. *
  177. * @var RouteCollection
  178. */
  179. protected $routes;
  180. /**
  181. * The subject only set in edit/update/create mode.
  182. *
  183. * @var object
  184. */
  185. protected $subject;
  186. /**
  187. * Define a Collection of child admin, ie /admin/order/{id}/order-element/{childId}.
  188. *
  189. * @var array
  190. */
  191. protected $children = array();
  192. /**
  193. * Reference the parent collection.
  194. *
  195. * @var AdminInterface|null
  196. */
  197. protected $parent = null;
  198. /**
  199. * The base code route refer to the prefix used to generate the route name.
  200. *
  201. * @var string
  202. */
  203. protected $baseCodeRoute = '';
  204. /**
  205. * The related field reflection, ie if OrderElement is linked to Order,
  206. * then the $parentReflectionProperty must be the ReflectionProperty of
  207. * the order (OrderElement::$order).
  208. *
  209. * @var \ReflectionProperty
  210. */
  211. protected $parentAssociationMapping = null;
  212. /**
  213. * Reference the parent FieldDescription related to this admin
  214. * only set for FieldDescription which is associated to an Sub Admin instance.
  215. *
  216. * @var FieldDescriptionInterface
  217. */
  218. protected $parentFieldDescription;
  219. /**
  220. * If true then the current admin is part of the nested admin set (from the url).
  221. *
  222. * @var bool
  223. */
  224. protected $currentChild = false;
  225. /**
  226. * The uniqid is used to avoid clashing with 2 admin related to the code
  227. * ie: a Block linked to a Block.
  228. *
  229. * @var string
  230. */
  231. protected $uniqid;
  232. /**
  233. * The Entity or Document manager.
  234. *
  235. * @var ModelManagerInterface
  236. */
  237. protected $modelManager;
  238. /**
  239. * The current request object.
  240. *
  241. * @var \Symfony\Component\HttpFoundation\Request
  242. */
  243. protected $request;
  244. /**
  245. * The translator component.
  246. *
  247. * NEXT_MAJOR: remove this property
  248. *
  249. * @var \Symfony\Component\Translation\TranslatorInterface
  250. *
  251. * @deprecated since 3.9, to be removed with 4.0
  252. */
  253. protected $translator;
  254. /**
  255. * The related form contractor.
  256. *
  257. * @var FormContractorInterface
  258. */
  259. protected $formContractor;
  260. /**
  261. * The related list builder.
  262. *
  263. * @var ListBuilderInterface
  264. */
  265. protected $listBuilder;
  266. /**
  267. * The related view builder.
  268. *
  269. * @var ShowBuilderInterface
  270. */
  271. protected $showBuilder;
  272. /**
  273. * The related datagrid builder.
  274. *
  275. * @var DatagridBuilderInterface
  276. */
  277. protected $datagridBuilder;
  278. /**
  279. * @var RouteBuilderInterface
  280. */
  281. protected $routeBuilder;
  282. /**
  283. * The datagrid instance.
  284. *
  285. * @var \Sonata\AdminBundle\Datagrid\DatagridInterface
  286. */
  287. protected $datagrid;
  288. /**
  289. * The router instance.
  290. *
  291. * @var RouteGeneratorInterface
  292. */
  293. protected $routeGenerator;
  294. /**
  295. * The generated breadcrumbs.
  296. *
  297. * NEXT_MAJOR : remove this property
  298. *
  299. * @var array
  300. */
  301. protected $breadcrumbs = array();
  302. /**
  303. * @var SecurityHandlerInterface
  304. */
  305. protected $securityHandler = null;
  306. /**
  307. * @var ValidatorInterface|LegacyValidatorInterface
  308. */
  309. protected $validator = null;
  310. /**
  311. * The configuration pool.
  312. *
  313. * @var Pool
  314. */
  315. protected $configurationPool;
  316. /**
  317. * @var MenuItemInterface
  318. */
  319. protected $menu;
  320. /**
  321. * @var MenuFactoryInterface
  322. */
  323. protected $menuFactory;
  324. /**
  325. * @var array
  326. */
  327. protected $loaded = array(
  328. 'view_fields' => false,
  329. 'view_groups' => false,
  330. 'routes' => false,
  331. 'tab_menu' => false,
  332. );
  333. /**
  334. * @var array
  335. */
  336. protected $formTheme = array();
  337. /**
  338. * @var array
  339. */
  340. protected $filterTheme = array();
  341. /**
  342. * @var array
  343. */
  344. protected $templates = array();
  345. /**
  346. * @var AdminExtensionInterface[]
  347. */
  348. protected $extensions = array();
  349. /**
  350. * @var LabelTranslatorStrategyInterface
  351. */
  352. protected $labelTranslatorStrategy;
  353. /**
  354. * Setting to true will enable preview mode for
  355. * the entity and show a preview button in the
  356. * edit/create forms.
  357. *
  358. * @var bool
  359. */
  360. protected $supportsPreviewMode = false;
  361. /**
  362. * Roles and permissions per role.
  363. *
  364. * @var array [role] => array([permission], [permission])
  365. */
  366. protected $securityInformation = array();
  367. protected $cacheIsGranted = array();
  368. /**
  369. * Action list for the search result.
  370. *
  371. * @var string[]
  372. */
  373. protected $searchResultActions = array('edit', 'show');
  374. protected $listModes = array(
  375. 'list' => array(
  376. 'class' => 'fa fa-list fa-fw',
  377. ),
  378. 'mosaic' => array(
  379. 'class' => self::MOSAIC_ICON_CLASS,
  380. ),
  381. );
  382. /**
  383. * The Access mapping.
  384. *
  385. * @var array
  386. */
  387. protected $accessMapping = array();
  388. /**
  389. * The class name managed by the admin class.
  390. *
  391. * @var string
  392. */
  393. private $class;
  394. /**
  395. * The subclasses supported by the admin class.
  396. *
  397. * @var array
  398. */
  399. private $subClasses = array();
  400. /**
  401. * The list collection.
  402. *
  403. * @var array
  404. */
  405. private $list;
  406. /**
  407. * @var FieldDescriptionCollection
  408. */
  409. private $show;
  410. /**
  411. * @var Form
  412. */
  413. private $form;
  414. /**
  415. * @var DatagridInterface
  416. */
  417. private $filter;
  418. /**
  419. * The cached base route name.
  420. *
  421. * @var string
  422. */
  423. private $cachedBaseRouteName;
  424. /**
  425. * The cached base route pattern.
  426. *
  427. * @var string
  428. */
  429. private $cachedBaseRoutePattern;
  430. /**
  431. * The form group disposition.
  432. *
  433. * @var array|bool
  434. */
  435. private $formGroups = false;
  436. /**
  437. * The form tabs disposition.
  438. *
  439. * @var array|bool
  440. */
  441. private $formTabs = false;
  442. /**
  443. * The view group disposition.
  444. *
  445. * @var array|bool
  446. */
  447. private $showGroups = false;
  448. /**
  449. * The view tab disposition.
  450. *
  451. * @var array|bool
  452. */
  453. private $showTabs = false;
  454. /**
  455. * The manager type to use for the admin.
  456. *
  457. * @var string
  458. */
  459. private $managerType;
  460. /**
  461. * The breadcrumbsBuilder component.
  462. *
  463. * @var BreadcrumbsBuilderInterface
  464. */
  465. private $breadcrumbsBuilder;
  466. /**
  467. * @param string $code
  468. * @param string $class
  469. * @param string $baseControllerName
  470. */
  471. public function __construct($code, $class, $baseControllerName)
  472. {
  473. $this->code = $code;
  474. $this->class = $class;
  475. $this->baseControllerName = $baseControllerName;
  476. $this->predefinePerPageOptions();
  477. $this->datagridValues['_per_page'] = $this->maxPerPage;
  478. }
  479. /**
  480. * {@inheritdoc}
  481. */
  482. public function getExportFormats()
  483. {
  484. return array(
  485. 'json', 'xml', 'csv', 'xls',
  486. );
  487. }
  488. /**
  489. * @return array
  490. */
  491. public function getExportFields()
  492. {
  493. $fields = $this->getModelManager()->getExportFields($this->getClass());
  494. foreach ($this->getExtensions() as $extension) {
  495. if (method_exists($extension, 'configureExportFields')) {
  496. $fields = $extension->configureExportFields($this, $fields);
  497. }
  498. }
  499. return $fields;
  500. }
  501. /**
  502. * {@inheritdoc}
  503. */
  504. public function getDataSourceIterator()
  505. {
  506. $datagrid = $this->getDatagrid();
  507. $datagrid->buildPager();
  508. $fields = array();
  509. foreach ($this->getExportFields() as $key => $field) {
  510. $label = $this->getTranslationLabel($field, 'export', 'label');
  511. $transLabel = $this->trans($label);
  512. // NEXT_MAJOR: Remove this hack, because all field labels will be translated with the major release
  513. // No translation key exists
  514. if ($transLabel == $label) {
  515. $fields[$key] = $field;
  516. } else {
  517. $fields[$transLabel] = $field;
  518. }
  519. }
  520. return $this->getModelManager()->getDataSourceIterator($datagrid, $fields);
  521. }
  522. /**
  523. * {@inheritdoc}
  524. */
  525. public function validate(ErrorElement $errorElement, $object)
  526. {
  527. }
  528. /**
  529. * define custom variable.
  530. */
  531. public function initialize()
  532. {
  533. if (!$this->classnameLabel) {
  534. $this->classnameLabel = substr($this->getClass(), strrpos($this->getClass(), '\\') + 1);
  535. }
  536. $this->baseCodeRoute = $this->getCode();
  537. $this->configure();
  538. }
  539. /**
  540. * {@inheritdoc}
  541. */
  542. public function configure()
  543. {
  544. }
  545. /**
  546. * {@inheritdoc}
  547. */
  548. public function update($object)
  549. {
  550. $this->preUpdate($object);
  551. foreach ($this->extensions as $extension) {
  552. $extension->preUpdate($this, $object);
  553. }
  554. $result = $this->getModelManager()->update($object);
  555. // BC compatibility
  556. if (null !== $result) {
  557. $object = $result;
  558. }
  559. $this->postUpdate($object);
  560. foreach ($this->extensions as $extension) {
  561. $extension->postUpdate($this, $object);
  562. }
  563. return $object;
  564. }
  565. /**
  566. * {@inheritdoc}
  567. */
  568. public function create($object)
  569. {
  570. $this->prePersist($object);
  571. foreach ($this->extensions as $extension) {
  572. $extension->prePersist($this, $object);
  573. }
  574. $result = $this->getModelManager()->create($object);
  575. // BC compatibility
  576. if (null !== $result) {
  577. $object = $result;
  578. }
  579. $this->postPersist($object);
  580. foreach ($this->extensions as $extension) {
  581. $extension->postPersist($this, $object);
  582. }
  583. $this->createObjectSecurity($object);
  584. return $object;
  585. }
  586. /**
  587. * {@inheritdoc}
  588. */
  589. public function delete($object)
  590. {
  591. $this->preRemove($object);
  592. foreach ($this->extensions as $extension) {
  593. $extension->preRemove($this, $object);
  594. }
  595. $this->getSecurityHandler()->deleteObjectSecurity($this, $object);
  596. $this->getModelManager()->delete($object);
  597. $this->postRemove($object);
  598. foreach ($this->extensions as $extension) {
  599. $extension->postRemove($this, $object);
  600. }
  601. }
  602. /**
  603. * {@inheritdoc}
  604. */
  605. public function preValidate($object)
  606. {
  607. }
  608. /**
  609. * {@inheritdoc}
  610. */
  611. public function preUpdate($object)
  612. {
  613. }
  614. /**
  615. * {@inheritdoc}
  616. */
  617. public function postUpdate($object)
  618. {
  619. }
  620. /**
  621. * {@inheritdoc}
  622. */
  623. public function prePersist($object)
  624. {
  625. }
  626. /**
  627. * {@inheritdoc}
  628. */
  629. public function postPersist($object)
  630. {
  631. }
  632. /**
  633. * {@inheritdoc}
  634. */
  635. public function preRemove($object)
  636. {
  637. }
  638. /**
  639. * {@inheritdoc}
  640. */
  641. public function postRemove($object)
  642. {
  643. }
  644. /**
  645. * {@inheritdoc}
  646. */
  647. public function preBatchAction($actionName, ProxyQueryInterface $query, array &$idx, $allElements)
  648. {
  649. }
  650. /**
  651. * {@inheritdoc}
  652. */
  653. public function getFilterParameters()
  654. {
  655. $parameters = array();
  656. // build the values array
  657. if ($this->hasRequest()) {
  658. $filters = $this->request->query->get('filter', array());
  659. // if persisting filters, save filters to session, or pull them out of session if no new filters set
  660. if ($this->persistFilters) {
  661. if ($filters == array() && $this->request->query->get('filters') != 'reset') {
  662. $filters = $this->request->getSession()->get($this->getCode().'.filter.parameters', array());
  663. } else {
  664. $this->request->getSession()->set($this->getCode().'.filter.parameters', $filters);
  665. }
  666. }
  667. $parameters = array_merge(
  668. $this->getModelManager()->getDefaultSortValues($this->getClass()),
  669. $this->datagridValues,
  670. $this->getDefaultFilterValues(),
  671. $filters
  672. );
  673. if (!$this->determinedPerPageValue($parameters['_per_page'])) {
  674. $parameters['_per_page'] = $this->maxPerPage;
  675. }
  676. // always force the parent value
  677. if ($this->isChild() && $this->getParentAssociationMapping()) {
  678. $name = str_replace('.', '__', $this->getParentAssociationMapping());
  679. $parameters[$name] = array('value' => $this->request->get($this->getParent()->getIdParameter()));
  680. }
  681. }
  682. return $parameters;
  683. }
  684. /**
  685. * {@inheritdoc}
  686. */
  687. public function buildDatagrid()
  688. {
  689. if ($this->datagrid) {
  690. return;
  691. }
  692. $filterParameters = $this->getFilterParameters();
  693. // transform _sort_by from a string to a FieldDescriptionInterface for the datagrid.
  694. if (isset($filterParameters['_sort_by']) && is_string($filterParameters['_sort_by'])) {
  695. if ($this->hasListFieldDescription($filterParameters['_sort_by'])) {
  696. $filterParameters['_sort_by'] = $this->getListFieldDescription($filterParameters['_sort_by']);
  697. } else {
  698. $filterParameters['_sort_by'] = $this->getModelManager()->getNewFieldDescriptionInstance(
  699. $this->getClass(),
  700. $filterParameters['_sort_by'],
  701. array()
  702. );
  703. $this->getListBuilder()->buildField(null, $filterParameters['_sort_by'], $this);
  704. }
  705. }
  706. // initialize the datagrid
  707. $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $filterParameters);
  708. $this->datagrid->getPager()->setMaxPageLinks($this->maxPageLinks);
  709. $mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);
  710. // build the datagrid filter
  711. $this->configureDatagridFilters($mapper);
  712. // ok, try to limit to add parent filter
  713. if ($this->isChild() && $this->getParentAssociationMapping() && !$mapper->has($this->getParentAssociationMapping())) {
  714. $mapper->add($this->getParentAssociationMapping(), null, array(
  715. 'show_filter' => false,
  716. 'label' => false,
  717. 'field_type' => 'sonata_type_model_hidden',
  718. 'field_options' => array(
  719. 'model_manager' => $this->getModelManager(),
  720. ),
  721. 'operator_type' => 'hidden',
  722. ), null, null, array(
  723. 'admin_code' => $this->getParent()->getCode(),
  724. ));
  725. }
  726. foreach ($this->getExtensions() as $extension) {
  727. $extension->configureDatagridFilters($mapper);
  728. }
  729. }
  730. /**
  731. * Returns the name of the parent related field, so the field can be use to set the default
  732. * value (ie the parent object) or to filter the object.
  733. *
  734. * @return string the name of the parent related field
  735. */
  736. public function getParentAssociationMapping()
  737. {
  738. return $this->parentAssociationMapping;
  739. }
  740. /**
  741. * Returns the baseRoutePattern used to generate the routing information.
  742. *
  743. * @throws \RuntimeException
  744. *
  745. * @return string the baseRoutePattern used to generate the routing information
  746. */
  747. public function getBaseRoutePattern()
  748. {
  749. if (null !== $this->cachedBaseRoutePattern) {
  750. return $this->cachedBaseRoutePattern;
  751. }
  752. if ($this->isChild()) { // the admin class is a child, prefix it with the parent route pattern
  753. if (!$this->baseRoutePattern) {
  754. preg_match(self::CLASS_REGEX, $this->class, $matches);
  755. if (!$matches) {
  756. throw new \RuntimeException(sprintf('Please define a default `baseRoutePattern` value for the admin class `%s`', get_class($this)));
  757. }
  758. }
  759. $this->cachedBaseRoutePattern = sprintf('%s/{id}/%s',
  760. $this->getParent()->getBaseRoutePattern(),
  761. $this->baseRoutePattern ?: $this->urlize($matches[5], '-')
  762. );
  763. } elseif ($this->baseRoutePattern) {
  764. $this->cachedBaseRoutePattern = $this->baseRoutePattern;
  765. } else {
  766. preg_match(self::CLASS_REGEX, $this->class, $matches);
  767. if (!$matches) {
  768. throw new \RuntimeException(sprintf('Please define a default `baseRoutePattern` value for the admin class `%s`', get_class($this)));
  769. }
  770. $this->cachedBaseRoutePattern = sprintf('/%s%s/%s',
  771. empty($matches[1]) ? '' : $this->urlize($matches[1], '-').'/',
  772. $this->urlize($matches[3], '-'),
  773. $this->urlize($matches[5], '-')
  774. );
  775. }
  776. return $this->cachedBaseRoutePattern;
  777. }
  778. /**
  779. * Returns the baseRouteName used to generate the routing information.
  780. *
  781. * @throws \RuntimeException
  782. *
  783. * @return string the baseRouteName used to generate the routing information
  784. */
  785. public function getBaseRouteName()
  786. {
  787. if (null !== $this->cachedBaseRouteName) {
  788. return $this->cachedBaseRouteName;
  789. }
  790. if ($this->isChild()) { // the admin class is a child, prefix it with the parent route name
  791. if (!$this->baseRouteName) {
  792. preg_match(self::CLASS_REGEX, $this->class, $matches);
  793. if (!$matches) {
  794. throw new \RuntimeException(sprintf('Cannot automatically determine base route name, please define a default `baseRouteName` value for the admin class `%s`', get_class($this)));
  795. }
  796. }
  797. $this->cachedBaseRouteName = sprintf('%s_%s',
  798. $this->getParent()->getBaseRouteName(),
  799. $this->baseRouteName ?: $this->urlize($matches[5])
  800. );
  801. } elseif ($this->baseRouteName) {
  802. $this->cachedBaseRouteName = $this->baseRouteName;
  803. } else {
  804. preg_match(self::CLASS_REGEX, $this->class, $matches);
  805. if (!$matches) {
  806. throw new \RuntimeException(sprintf('Cannot automatically determine base route name, please define a default `baseRouteName` value for the admin class `%s`', get_class($this)));
  807. }
  808. $this->cachedBaseRouteName = sprintf('admin_%s%s_%s',
  809. empty($matches[1]) ? '' : $this->urlize($matches[1]).'_',
  810. $this->urlize($matches[3]),
  811. $this->urlize($matches[5])
  812. );
  813. }
  814. return $this->cachedBaseRouteName;
  815. }
  816. /**
  817. * urlize the given word.
  818. *
  819. * @param string $word
  820. * @param string $sep the separator
  821. *
  822. * @return string
  823. */
  824. public function urlize($word, $sep = '_')
  825. {
  826. return strtolower(preg_replace('/[^a-z0-9_]/i', $sep.'$1', $word));
  827. }
  828. /**
  829. * {@inheritdoc}
  830. */
  831. public function getClass()
  832. {
  833. // see https://github.com/sonata-project/SonataCoreBundle/commit/247eeb0a7ca7211142e101754769d70bc402a5b4
  834. if ($this->hasSubject() && is_object($this->getSubject())) {
  835. return ClassUtils::getClass($this->getSubject());
  836. }
  837. if (!$this->hasActiveSubClass()) {
  838. if (count($this->getSubClasses()) > 0) {
  839. $subject = $this->getSubject();
  840. if ($subject && is_object($subject)) {
  841. return ClassUtils::getClass($subject);
  842. }
  843. }
  844. return $this->class;
  845. }
  846. if ($this->getParentFieldDescription() && $this->hasActiveSubClass()) {
  847. throw new \RuntimeException('Feature not implemented: an embedded admin cannot have subclass');
  848. }
  849. $subClass = $this->getRequest()->query->get('subclass');
  850. return $this->getSubClass($subClass);
  851. }
  852. /**
  853. * {@inheritdoc}
  854. */
  855. public function getSubClasses()
  856. {
  857. return $this->subClasses;
  858. }
  859. /**
  860. * {@inheritdoc}
  861. */
  862. public function addSubClass($subClass)
  863. {
  864. if (!in_array($subClass, $this->subClasses)) {
  865. $this->subClasses[] = $subClass;
  866. }
  867. }
  868. /**
  869. * {@inheritdoc}
  870. */
  871. public function setSubClasses(array $subClasses)
  872. {
  873. $this->subClasses = $subClasses;
  874. }
  875. /**
  876. * {@inheritdoc}
  877. */
  878. public function hasSubClass($name)
  879. {
  880. return isset($this->subClasses[$name]);
  881. }
  882. /**
  883. * {@inheritdoc}
  884. */
  885. public function hasActiveSubClass()
  886. {
  887. if (count($this->subClasses) > 0 && $this->request) {
  888. return null !== $this->getRequest()->query->get('subclass');
  889. }
  890. return false;
  891. }
  892. /**
  893. * {@inheritdoc}
  894. */
  895. public function getActiveSubClass()
  896. {
  897. if (!$this->hasActiveSubClass()) {
  898. return;
  899. }
  900. return $this->getClass();
  901. }
  902. /**
  903. * {@inheritdoc}
  904. */
  905. public function getActiveSubclassCode()
  906. {
  907. if (!$this->hasActiveSubClass()) {
  908. return;
  909. }
  910. $subClass = $this->getRequest()->query->get('subclass');
  911. if (!$this->hasSubClass($subClass)) {
  912. return;
  913. }
  914. return $subClass;
  915. }
  916. /**
  917. * {@inheritdoc}
  918. */
  919. public function getBatchActions()
  920. {
  921. $actions = array();
  922. if ($this->hasRoute('delete') && $this->isGranted('DELETE')) {
  923. $actions['delete'] = array(
  924. 'label' => 'action_delete',
  925. 'translation_domain' => 'SonataAdminBundle',
  926. 'ask_confirmation' => true, // by default always true
  927. );
  928. }
  929. $actions = $this->configureBatchActions($actions);
  930. foreach ($this->getExtensions() as $extension) {
  931. // TODO: remove method check in next major release
  932. if (method_exists($extension, 'configureBatchActions')) {
  933. $actions = $extension->configureBatchActions($this, $actions);
  934. }
  935. }
  936. return $actions;
  937. }
  938. /**
  939. * {@inheritdoc}
  940. */
  941. public function getRoutes()
  942. {
  943. $this->buildRoutes();
  944. return $this->routes;
  945. }
  946. /**
  947. * {@inheritdoc}
  948. */
  949. public function getRouterIdParameter()
  950. {
  951. return $this->isChild() ? '{childId}' : '{id}';
  952. }
  953. /**
  954. * {@inheritdoc}
  955. */
  956. public function getIdParameter()
  957. {
  958. return $this->isChild() ? 'childId' : 'id';
  959. }
  960. /**
  961. * {@inheritdoc}
  962. */
  963. public function hasRoute($name)
  964. {
  965. if (!$this->routeGenerator) {
  966. throw new \RuntimeException('RouteGenerator cannot be null');
  967. }
  968. return $this->routeGenerator->hasAdminRoute($this, $name);
  969. }
  970. /**
  971. * {@inheritdoc}
  972. */
  973. public function isCurrentRoute($name, $adminCode = null)
  974. {
  975. if (!$this->hasRequest()) {
  976. return false;
  977. }
  978. $request = $this->getRequest();
  979. $route = $request->get('_route');
  980. if ($adminCode) {
  981. $admin = $this->getConfigurationPool()->getAdminByAdminCode($adminCode);
  982. } else {
  983. $admin = $this;
  984. }
  985. if (!$admin) {
  986. return false;
  987. }
  988. return ($admin->getBaseRouteName().'_'.$name) == $route;
  989. }
  990. /**
  991. * {@inheritdoc}
  992. */
  993. public function generateObjectUrl($name, $object, array $parameters = array(), $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
  994. {
  995. $parameters['id'] = $this->getUrlsafeIdentifier($object);
  996. return $this->generateUrl($name, $parameters, $absolute);
  997. }
  998. /**
  999. * {@inheritdoc}
  1000. */
  1001. public function generateUrl($name, array $parameters = array(), $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
  1002. {
  1003. return $this->routeGenerator->generateUrl($this, $name, $parameters, $absolute);
  1004. }
  1005. /**
  1006. * {@inheritdoc}
  1007. */
  1008. public function generateMenuUrl($name, array $parameters = array(), $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
  1009. {
  1010. return $this->routeGenerator->generateMenuUrl($this, $name, $parameters, $absolute);
  1011. }
  1012. /**
  1013. * @param array $templates
  1014. */
  1015. public function setTemplates(array $templates)
  1016. {
  1017. $this->templates = $templates;
  1018. }
  1019. /**
  1020. * @param string $name
  1021. * @param string $template
  1022. */
  1023. public function setTemplate($name, $template)
  1024. {
  1025. $this->templates[$name] = $template;
  1026. }
  1027. /**
  1028. * @return array
  1029. */
  1030. public function getTemplates()
  1031. {
  1032. return $this->templates;
  1033. }
  1034. /**
  1035. * {@inheritdoc}
  1036. */
  1037. public function getTemplate($name)
  1038. {
  1039. if (isset($this->templates[$name])) {
  1040. return $this->templates[$name];
  1041. }
  1042. }
  1043. /**
  1044. * {@inheritdoc}
  1045. */
  1046. public function getNewInstance()
  1047. {
  1048. $object = $this->getModelManager()->getModelInstance($this->getClass());
  1049. foreach ($this->getExtensions() as $extension) {
  1050. $extension->alterNewInstance($this, $object);
  1051. }
  1052. return $object;
  1053. }
  1054. /**
  1055. * {@inheritdoc}
  1056. */
  1057. public function getFormBuilder()
  1058. {
  1059. $this->formOptions['data_class'] = $this->getClass();
  1060. $formBuilder = $this->getFormContractor()->getFormBuilder(
  1061. $this->getUniqid(),
  1062. $this->formOptions
  1063. );
  1064. $this->defineFormBuilder($formBuilder);
  1065. return $formBuilder;
  1066. }
  1067. /**
  1068. * This method is being called by the main admin class and the child class,
  1069. * the getFormBuilder is only call by the main admin class.
  1070. *
  1071. * @param FormBuilderInterface $formBuilder
  1072. */
  1073. public function defineFormBuilder(FormBuilderInterface $formBuilder)
  1074. {
  1075. $mapper = new FormMapper($this->getFormContractor(), $formBuilder, $this);
  1076. $this->configureFormFields($mapper);
  1077. foreach ($this->getExtensions() as $extension) {
  1078. $extension->configureFormFields($mapper);
  1079. }
  1080. $this->attachInlineValidator();
  1081. }
  1082. /**
  1083. * {@inheritdoc}
  1084. */
  1085. public function attachAdminClass(FieldDescriptionInterface $fieldDescription)
  1086. {
  1087. $pool = $this->getConfigurationPool();
  1088. $adminCode = $fieldDescription->getOption('admin_code');
  1089. if ($adminCode !== null) {
  1090. $admin = $pool->getAdminByAdminCode($adminCode);
  1091. } else {
  1092. $admin = $pool->getAdminByClass($fieldDescription->getTargetEntity());
  1093. }
  1094. if (!$admin) {
  1095. return;
  1096. }
  1097. if ($this->hasRequest()) {
  1098. $admin->setRequest($this->getRequest());
  1099. }
  1100. $fieldDescription->setAssociationAdmin($admin);
  1101. }
  1102. /**
  1103. * {@inheritdoc}
  1104. */
  1105. public function getObject($id)
  1106. {
  1107. $object = $this->getModelManager()->find($this->getClass(), $id);
  1108. foreach ($this->getExtensions() as $extension) {
  1109. $extension->alterObject($this, $object);
  1110. }
  1111. return $object;
  1112. }
  1113. /**
  1114. * {@inheritdoc}
  1115. */
  1116. public function getForm()
  1117. {
  1118. $this->buildForm();
  1119. return $this->form;
  1120. }
  1121. /**
  1122. * {@inheritdoc}
  1123. */
  1124. public function getList()
  1125. {
  1126. $this->buildList();
  1127. return $this->list;
  1128. }
  1129. /**
  1130. * {@inheritdoc}
  1131. */
  1132. public function createQuery($context = 'list')
  1133. {
  1134. if (func_num_args() > 0) {
  1135. @trigger_error(
  1136. 'The $context argument of '.__METHOD__.' is deprecated since 3.3, to be removed in 4.0.',
  1137. E_USER_DEPRECATED
  1138. );
  1139. }
  1140. $query = $this->getModelManager()->createQuery($this->class);
  1141. foreach ($this->extensions as $extension) {
  1142. $extension->configureQuery($this, $query, $context);
  1143. }
  1144. return $query;
  1145. }
  1146. /**
  1147. * {@inheritdoc}
  1148. */
  1149. public function getDatagrid()
  1150. {
  1151. $this->buildDatagrid();
  1152. return $this->datagrid;
  1153. }
  1154. /**
  1155. * {@inheritdoc}
  1156. */
  1157. public function buildTabMenu($action, AdminInterface $childAdmin = null)
  1158. {
  1159. if ($this->loaded['tab_menu']) {
  1160. return;
  1161. }
  1162. $this->loaded['tab_menu'] = true;
  1163. $menu = $this->menuFactory->createItem('root');
  1164. $menu->setChildrenAttribute('class', 'nav navbar-nav');
  1165. $menu->setExtra('translation_domain', $this->translationDomain);
  1166. // Prevents BC break with KnpMenuBundle v1.x
  1167. if (method_exists($menu, 'setCurrentUri')) {
  1168. $menu->setCurrentUri($this->getRequest()->getBaseUrl().$this->getRequest()->getPathInfo());
  1169. }
  1170. $this->configureTabMenu($menu, $action, $childAdmin);
  1171. foreach ($this->getExtensions() as $extension) {
  1172. $extension->configureTabMenu($this, $menu, $action, $childAdmin);
  1173. }
  1174. $this->menu = $menu;
  1175. }
  1176. /**
  1177. * {@inheritdoc}
  1178. */
  1179. public function buildSideMenu($action, AdminInterface $childAdmin = null)
  1180. {
  1181. return $this->buildTabMenu($action, $childAdmin);
  1182. }
  1183. /**
  1184. * @param string $action
  1185. * @param AdminInterface $childAdmin
  1186. *
  1187. * @return ItemInterface
  1188. */
  1189. public function getSideMenu($action, AdminInterface $childAdmin = null)
  1190. {
  1191. if ($this->isChild()) {
  1192. return $this->getParent()->getSideMenu($action, $this);
  1193. }
  1194. $this->buildSideMenu($action, $childAdmin);
  1195. return $this->menu;
  1196. }
  1197. /**
  1198. * Returns the root code.
  1199. *
  1200. * @return string the root code
  1201. */
  1202. public function getRootCode()
  1203. {
  1204. return $this->getRoot()->getCode();
  1205. }
  1206. /**
  1207. * Returns the master admin.
  1208. *
  1209. * @return Admin the root admin class
  1210. */
  1211. public function getRoot()
  1212. {
  1213. $parentFieldDescription = $this->getParentFieldDescription();
  1214. if (!$parentFieldDescription) {
  1215. return $this;
  1216. }
  1217. return $parentFieldDescription->getAdmin()->getRoot();
  1218. }
  1219. /**
  1220. * {@inheritdoc}
  1221. */
  1222. public function setBaseControllerName($baseControllerName)
  1223. {
  1224. $this->baseControllerName = $baseControllerName;
  1225. }
  1226. /**
  1227. * {@inheritdoc}
  1228. */
  1229. public function getBaseControllerName()
  1230. {
  1231. return $this->baseControllerName;
  1232. }
  1233. /**
  1234. * @param string $label
  1235. */
  1236. public function setLabel($label)
  1237. {
  1238. $this->label = $label;
  1239. }
  1240. /**
  1241. * {@inheritdoc}
  1242. */
  1243. public function getLabel()
  1244. {
  1245. return $this->label;
  1246. }
  1247. /**
  1248. * @param bool $persist
  1249. */
  1250. public function setPersistFilters($persist)
  1251. {
  1252. $this->persistFilters = $persist;
  1253. }
  1254. /**
  1255. * @param int $maxPerPage
  1256. */
  1257. public function setMaxPerPage($maxPerPage)
  1258. {
  1259. $this->maxPerPage = $maxPerPage;
  1260. }
  1261. /**
  1262. * @return int
  1263. */
  1264. public function getMaxPerPage()
  1265. {
  1266. return $this->maxPerPage;
  1267. }
  1268. /**
  1269. * @param int $maxPageLinks
  1270. */
  1271. public function setMaxPageLinks($maxPageLinks)
  1272. {
  1273. $this->maxPageLinks = $maxPageLinks;
  1274. }
  1275. /**
  1276. * @return int
  1277. */
  1278. public function getMaxPageLinks()
  1279. {
  1280. return $this->maxPageLinks;
  1281. }
  1282. /**
  1283. * {@inheritdoc}
  1284. */
  1285. public function getFormGroups()
  1286. {
  1287. return $this->formGroups;
  1288. }
  1289. /**
  1290. * {@inheritdoc}
  1291. */
  1292. public function setFormGroups(array $formGroups)
  1293. {
  1294. $this->formGroups = $formGroups;
  1295. }
  1296. /**
  1297. * {@inheritdoc}
  1298. */
  1299. public function removeFieldFromFormGroup($key)
  1300. {
  1301. foreach ($this->formGroups as $name => $formGroup) {
  1302. unset($this->formGroups[$name]['fields'][$key]);
  1303. if (empty($this->formGroups[$name]['fields'])) {
  1304. unset($this->formGroups[$name]);
  1305. }
  1306. }
  1307. }
  1308. /**
  1309. * @param array $group
  1310. * @param array $keys
  1311. */
  1312. public function reorderFormGroup($group, array $keys)
  1313. {
  1314. $formGroups = $this->getFormGroups();
  1315. $formGroups[$group]['fields'] = array_merge(array_flip($keys), $formGroups[$group]['fields']);
  1316. $this->setFormGroups($formGroups);
  1317. }
  1318. /**
  1319. * {@inheritdoc}
  1320. */
  1321. public function getFormTabs()
  1322. {
  1323. return $this->formTabs;
  1324. }
  1325. /**
  1326. * {@inheritdoc}
  1327. */
  1328. public function setFormTabs(array $formTabs)
  1329. {
  1330. $this->formTabs = $formTabs;
  1331. }
  1332. /**
  1333. * {@inheritdoc}
  1334. */
  1335. public function getShowTabs()
  1336. {
  1337. return $this->showTabs;
  1338. }
  1339. /**
  1340. * {@inheritdoc}
  1341. */
  1342. public function setShowTabs(array $showTabs)
  1343. {
  1344. $this->showTabs = $showTabs;
  1345. }
  1346. /**
  1347. * {@inheritdoc}
  1348. */
  1349. public function getShowGroups()
  1350. {
  1351. return $this->showGroups;
  1352. }
  1353. /**
  1354. * {@inheritdoc}
  1355. */
  1356. public function setShowGroups(array $showGroups)
  1357. {
  1358. $this->showGroups = $showGroups;
  1359. }
  1360. /**
  1361. * {@inheritdoc}
  1362. */
  1363. public function reorderShowGroup($group, array $keys)
  1364. {
  1365. $showGroups = $this->getShowGroups();
  1366. $showGroups[$group]['fields'] = array_merge(array_flip($keys), $showGroups[$group]['fields']);
  1367. $this->setShowGroups($showGroups);
  1368. }
  1369. /**
  1370. * {@inheritdoc}
  1371. */
  1372. public function setParentFieldDescription(FieldDescriptionInterface $parentFieldDescription)
  1373. {
  1374. $this->parentFieldDescription = $parentFieldDescription;
  1375. }
  1376. /**
  1377. * {@inheritdoc}
  1378. */
  1379. public function getParentFieldDescription()
  1380. {
  1381. return $this->parentFieldDescription;
  1382. }
  1383. /**
  1384. * {@inheritdoc}
  1385. */
  1386. public function hasParentFieldDescription()
  1387. {
  1388. return $this->parentFieldDescription instanceof FieldDescriptionInterface;
  1389. }
  1390. /**
  1391. * {@inheritdoc}
  1392. */
  1393. public function setSubject($subject)
  1394. {
  1395. if (is_object($subject) && !is_a($subject, $this->class, true)) {
  1396. $message = <<<'EOT'
  1397. You are trying to set entity an instance of "%s",
  1398. which is not the one registered with this admin class ("%s").
  1399. This is deprecated since 3.5 and will no longer be supported in 4.0.
  1400. EOT;
  1401. @trigger_error(
  1402. sprintf($message, get_class($subject), $this->class),
  1403. E_USER_DEPRECATED
  1404. ); // NEXT_MAJOR : throw an exception instead
  1405. }
  1406. $this->subject = $subject;
  1407. }
  1408. /**
  1409. * {@inheritdoc}
  1410. */
  1411. public function getSubject()
  1412. {
  1413. if ($this->subject === null && $this->request) {
  1414. $id = $this->request->get($this->getIdParameter());
  1415. $this->subject = $this->getModelManager()->find($this->class, $id);
  1416. }
  1417. return $this->subject;
  1418. }
  1419. /**
  1420. * {@inheritdoc}
  1421. */
  1422. public function hasSubject()
  1423. {
  1424. return $this->subject != null;
  1425. }
  1426. /**
  1427. * {@inheritdoc}
  1428. */
  1429. public function getFormFieldDescriptions()
  1430. {
  1431. $this->buildForm();
  1432. return $this->formFieldDescriptions;
  1433. }
  1434. /**
  1435. * {@inheritdoc}
  1436. */
  1437. public function getFormFieldDescription($name)
  1438. {
  1439. return $this->hasFormFieldDescription($name) ? $this->formFieldDescriptions[$name] : null;
  1440. }
  1441. /**
  1442. * Returns true if the admin has a FieldDescription with the given $name.
  1443. *
  1444. * @param string $name
  1445. *
  1446. * @return bool
  1447. */
  1448. public function hasFormFieldDescription($name)
  1449. {
  1450. return array_key_exists($name, $this->formFieldDescriptions) ? true : false;
  1451. }
  1452. /**
  1453. * {@inheritdoc}
  1454. */
  1455. public function addFormFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1456. {
  1457. $this->formFieldDescriptions[$name] = $fieldDescription;
  1458. }
  1459. /**
  1460. * remove a FieldDescription.
  1461. *
  1462. * @param string $name
  1463. */
  1464. public function removeFormFieldDescription($name)
  1465. {
  1466. unset($this->formFieldDescriptions[$name]);
  1467. }
  1468. /**
  1469. * build and return the collection of form FieldDescription.
  1470. *
  1471. * @return array collection of form FieldDescription
  1472. */
  1473. public function getShowFieldDescriptions()
  1474. {
  1475. $this->buildShow();
  1476. return $this->showFieldDescriptions;
  1477. }
  1478. /**
  1479. * Returns the form FieldDescription with the given $name.
  1480. *
  1481. * @param string $name
  1482. *
  1483. * @return FieldDescriptionInterface
  1484. */
  1485. public function getShowFieldDescription($name)
  1486. {
  1487. $this->buildShow();
  1488. return $this->hasShowFieldDescription($name) ? $this->showFieldDescriptions[$name] : null;
  1489. }
  1490. /**
  1491. * {@inheritdoc}
  1492. */
  1493. public function hasShowFieldDescription($name)
  1494. {
  1495. return array_key_exists($name, $this->showFieldDescriptions);
  1496. }
  1497. /**
  1498. * {@inheritdoc}
  1499. */
  1500. public function addShowFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1501. {
  1502. $this->showFieldDescriptions[$name] = $fieldDescription;
  1503. }
  1504. /**
  1505. * {@inheritdoc}
  1506. */
  1507. public function removeShowFieldDescription($name)
  1508. {
  1509. unset($this->showFieldDescriptions[$name]);
  1510. }
  1511. /**
  1512. * {@inheritdoc}
  1513. */
  1514. public function getListFieldDescriptions()
  1515. {
  1516. $this->buildList();
  1517. return $this->listFieldDescriptions;
  1518. }
  1519. /**
  1520. * {@inheritdoc}
  1521. */
  1522. public function getListFieldDescription($name)
  1523. {
  1524. return $this->hasListFieldDescription($name) ? $this->listFieldDescriptions[$name] : null;
  1525. }
  1526. /**
  1527. * {@inheritdoc}
  1528. */
  1529. public function hasListFieldDescription($name)
  1530. {
  1531. $this->buildList();
  1532. return array_key_exists($name, $this->listFieldDescriptions) ? true : false;
  1533. }
  1534. /**
  1535. * {@inheritdoc}
  1536. */
  1537. public function addListFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1538. {
  1539. $this->listFieldDescriptions[$name] = $fieldDescription;
  1540. }
  1541. /**
  1542. * {@inheritdoc}
  1543. */
  1544. public function removeListFieldDescription($name)
  1545. {
  1546. unset($this->listFieldDescriptions[$name]);
  1547. }
  1548. /**
  1549. * {@inheritdoc}
  1550. */
  1551. public function getFilterFieldDescription($name)
  1552. {
  1553. return $this->hasFilterFieldDescription($name) ? $this->filterFieldDescriptions[$name] : null;
  1554. }
  1555. /**
  1556. * {@inheritdoc}
  1557. */
  1558. public function hasFilterFieldDescription($name)
  1559. {
  1560. return array_key_exists($name, $this->filterFieldDescriptions) ? true : false;
  1561. }
  1562. /**
  1563. * {@inheritdoc}
  1564. */
  1565. public function addFilterFieldDescription($name, FieldDescriptionInterface $fieldDescription)
  1566. {
  1567. $this->filterFieldDescriptions[$name] = $fieldDescription;
  1568. }
  1569. /**
  1570. * {@inheritdoc}
  1571. */
  1572. public function removeFilterFieldDescription($name)
  1573. {
  1574. unset($this->filterFieldDescriptions[$name]);
  1575. }
  1576. /**
  1577. * {@inheritdoc}
  1578. */
  1579. public function getFilterFieldDescriptions()
  1580. {
  1581. $this->buildDatagrid();
  1582. return $this->filterFieldDescriptions;
  1583. }
  1584. /**
  1585. * {@inheritdoc}
  1586. */
  1587. public function addChild(AdminInterface $child)
  1588. {
  1589. $this->children[$child->getCode()] = $child;
  1590. $child->setBaseCodeRoute($this->getCode().'|'.$child->getCode());
  1591. $child->setParent($this);
  1592. }
  1593. /**
  1594. * {@inheritdoc}
  1595. */
  1596. public function hasChild($code)
  1597. {
  1598. return isset($this->children[$code]);
  1599. }
  1600. /**
  1601. * {@inheritdoc}
  1602. */
  1603. public function getChildren()
  1604. {
  1605. return $this->children;
  1606. }
  1607. /**
  1608. * {@inheritdoc}
  1609. */
  1610. public function getChild($code)
  1611. {
  1612. return $this->hasChild($code) ? $this->children[$code] : null;
  1613. }
  1614. /**
  1615. * {@inheritdoc}
  1616. */
  1617. public function setParent(AdminInterface $parent)
  1618. {
  1619. $this->parent = $parent;
  1620. }
  1621. /**
  1622. * {@inheritdoc}
  1623. */
  1624. public function getParent()
  1625. {
  1626. return $this->parent;
  1627. }
  1628. /**
  1629. * {@inheritdoc}
  1630. */
  1631. public function isChild()
  1632. {
  1633. return $this->parent instanceof AdminInterface;
  1634. }
  1635. /**
  1636. * Returns true if the admin has children, false otherwise.
  1637. *
  1638. * @return bool if the admin has children
  1639. */
  1640. public function hasChildren()
  1641. {
  1642. return count($this->children) > 0;
  1643. }
  1644. /**
  1645. * {@inheritdoc}
  1646. */
  1647. public function setUniqid($uniqid)
  1648. {
  1649. $this->uniqid = $uniqid;
  1650. }
  1651. /**
  1652. * {@inheritdoc}
  1653. */
  1654. public function getUniqid()
  1655. {
  1656. if (!$this->uniqid) {
  1657. $this->uniqid = 's'.uniqid();
  1658. }
  1659. return $this->uniqid;
  1660. }
  1661. /**
  1662. * Returns the classname label.
  1663. *
  1664. * @return string the classname label
  1665. */
  1666. public function getClassnameLabel()
  1667. {
  1668. return $this->classnameLabel;
  1669. }
  1670. /**
  1671. * {@inheritdoc}
  1672. */
  1673. public function getPersistentParameters()
  1674. {
  1675. $parameters = array();
  1676. foreach ($this->getExtensions() as $extension) {
  1677. $params = $extension->getPersistentParameters($this);
  1678. if (!is_array($params)) {
  1679. throw new \RuntimeException(sprintf('The %s::getPersistentParameters must return an array', get_class($extension)));
  1680. }
  1681. $parameters = array_merge($parameters, $params);
  1682. }
  1683. return $parameters;
  1684. }
  1685. /**
  1686. * @param string $name
  1687. *
  1688. * @return null|mixed
  1689. */
  1690. public function getPersistentParameter($name)
  1691. {
  1692. $parameters = $this->getPersistentParameters();
  1693. return isset($parameters[$name]) ? $parameters[$name] : null;
  1694. }
  1695. /**
  1696. * {@inheritdoc}
  1697. */
  1698. public function getBreadcrumbs($action)
  1699. {
  1700. @trigger_error(
  1701. 'The '.__METHOD__.' method is deprecated since version 3.2 and will be removed in 4.0.'.
  1702. ' Use Sonata\AdminBundle\Admin\BreadcrumbsBuilder::getBreadcrumbs instead.',
  1703. E_USER_DEPRECATED
  1704. );
  1705. return $this->getBreadcrumbsBuilder()->getBreadcrumbs($this, $action);
  1706. }
  1707. /**
  1708. * Generates the breadcrumbs array.
  1709. *
  1710. * Note: the method will be called by the top admin instance (parent => child)
  1711. *
  1712. * @param string $action
  1713. * @param ItemInterface|null $menu
  1714. *
  1715. * @return array
  1716. */
  1717. public function buildBreadcrumbs($action, MenuItemInterface $menu = null)
  1718. {
  1719. @trigger_error(
  1720. 'The '.__METHOD__.' method is deprecated since version 3.2 and will be removed in 4.0.',
  1721. E_USER_DEPRECATED
  1722. );
  1723. if (isset($this->breadcrumbs[$action])) {
  1724. return $this->breadcrumbs[$action];
  1725. }
  1726. return $this->breadcrumbs[$action] = $this->getBreadcrumbsBuilder()
  1727. ->buildBreadcrumbs($this, $action, $menu);
  1728. }
  1729. /**
  1730. * NEXT_MAJOR : remove this method.
  1731. *
  1732. * @return BreadcrumbsBuilderInterface
  1733. */
  1734. final public function getBreadcrumbsBuilder()
  1735. {
  1736. @trigger_error(
  1737. 'The '.__METHOD__.' method is deprecated since version 3.2 and will be removed in 4.0.'.
  1738. ' Use the sonata.admin.breadcrumbs_builder service instead.',
  1739. E_USER_DEPRECATED
  1740. );
  1741. if ($this->breadcrumbsBuilder === null) {
  1742. $this->breadcrumbsBuilder = new BreadcrumbsBuilder($this->getConfigurationPool()->getContainer()->getParameter('sonata.admin.configuration.breadcrumbs'));
  1743. }
  1744. return $this->breadcrumbsBuilder;
  1745. }
  1746. /**
  1747. * NEXT_MAJOR : remove this method.
  1748. *
  1749. * @param BreadcrumbsBuilderInterface
  1750. *
  1751. * @return AbstractAdmin
  1752. */
  1753. final public function setBreadcrumbsBuilder(BreadcrumbsBuilderInterface $value)
  1754. {
  1755. @trigger_error(
  1756. 'The '.__METHOD__.' method is deprecated since version 3.2 and will be removed in 4.0.'.
  1757. ' Use the sonata.admin.breadcrumbs_builder service instead.',
  1758. E_USER_DEPRECATED
  1759. );
  1760. $this->breadcrumbsBuilder = $value;
  1761. return $this;
  1762. }
  1763. /**
  1764. * {@inheritdoc}
  1765. */
  1766. public function setCurrentChild($currentChild)
  1767. {
  1768. $this->currentChild = $currentChild;
  1769. }
  1770. /**
  1771. * {@inheritdoc}
  1772. */
  1773. public function getCurrentChild()
  1774. {
  1775. return $this->currentChild;
  1776. }
  1777. /**
  1778. * Returns the current child admin instance.
  1779. *
  1780. * @return AdminInterface|null the current child admin instance
  1781. */
  1782. public function getCurrentChildAdmin()
  1783. {
  1784. foreach ($this->children as $children) {
  1785. if ($children->getCurrentChild()) {
  1786. return $children;
  1787. }
  1788. }
  1789. return;
  1790. }
  1791. /**
  1792. * {@inheritdoc}
  1793. */
  1794. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  1795. {
  1796. @trigger_error(
  1797. 'The '.__METHOD__.' method is deprecated since version 3.9 and will be removed in 4.0.',
  1798. E_USER_DEPRECATED
  1799. );
  1800. $domain = $domain ?: $this->getTranslationDomain();
  1801. return $this->translator->trans($id, $parameters, $domain, $locale);
  1802. }
  1803. /**
  1804. * Translate a message id.
  1805. *
  1806. * NEXT_MAJOR: remove this method
  1807. *
  1808. * @param string $id
  1809. * @param int $count
  1810. * @param array $parameters
  1811. * @param string|null $domain
  1812. * @param string|null $locale
  1813. *
  1814. * @return string the translated string
  1815. *
  1816. * @deprecated since 3.9, to be removed with 4.0
  1817. */
  1818. public function transChoice($id, $count, array $parameters = array(), $domain = null, $locale = null)
  1819. {
  1820. @trigger_error(
  1821. 'The '.__METHOD__.' method is deprecated since version 3.9 and will be removed in 4.0.',
  1822. E_USER_DEPRECATED
  1823. );
  1824. $domain = $domain ?: $this->getTranslationDomain();
  1825. return $this->translator->transChoice($id, $count, $parameters, $domain, $locale);
  1826. }
  1827. /**
  1828. * {@inheritdoc}
  1829. */
  1830. public function setTranslationDomain($translationDomain)
  1831. {
  1832. $this->translationDomain = $translationDomain;
  1833. }
  1834. /**
  1835. * {@inheritdoc}
  1836. */
  1837. public function getTranslationDomain()
  1838. {
  1839. return $this->translationDomain;
  1840. }
  1841. /**
  1842. * {@inheritdoc}
  1843. *
  1844. * NEXT_MAJOR: remove this method
  1845. *
  1846. * @deprecated since 3.9, to be removed with 4.0
  1847. */
  1848. public function setTranslator(TranslatorInterface $translator, $deprecation = true)
  1849. {
  1850. if ($deprecation) {
  1851. @trigger_error(
  1852. 'The '.__METHOD__.' method is deprecated since version 3.9 and will be removed in 4.0.',
  1853. E_USER_DEPRECATED
  1854. );
  1855. }
  1856. $this->translator = $translator;
  1857. }
  1858. /**
  1859. * {@inheritdoc}
  1860. *
  1861. * NEXT_MAJOR: remove this method
  1862. *
  1863. * @deprecated since 3.9, to be removed with 4.0
  1864. */
  1865. public function getTranslator()
  1866. {
  1867. @trigger_error(
  1868. 'The '.__METHOD__.' method is deprecated since version 3.9 and will be removed in 4.0.',
  1869. E_USER_DEPRECATED
  1870. );
  1871. return $this->translator;
  1872. }
  1873. /**
  1874. * {@inheritdoc}
  1875. */
  1876. public function getTranslationLabel($label, $context = '', $type = '')
  1877. {
  1878. return $this->getLabelTranslatorStrategy()->getLabel($label, $context, $type);
  1879. }
  1880. /**
  1881. * {@inheritdoc}
  1882. */
  1883. public function setRequest(Request $request)
  1884. {
  1885. $this->request = $request;
  1886. foreach ($this->getChildren() as $children) {
  1887. $children->setRequest($request);
  1888. }
  1889. }
  1890. /**
  1891. * {@inheritdoc}
  1892. */
  1893. public function getRequest()
  1894. {
  1895. if (!$this->request) {
  1896. throw new \RuntimeException('The Request object has not been set');
  1897. }
  1898. return $this->request;
  1899. }
  1900. /**
  1901. * {@inheritdoc}
  1902. */
  1903. public function hasRequest()
  1904. {
  1905. return $this->request !== null;
  1906. }
  1907. /**
  1908. * {@inheritdoc}
  1909. */
  1910. public function setFormContractor(FormContractorInterface $formBuilder)
  1911. {
  1912. $this->formContractor = $formBuilder;
  1913. }
  1914. /**
  1915. * @return FormContractorInterface
  1916. */
  1917. public function getFormContractor()
  1918. {
  1919. return $this->formContractor;
  1920. }
  1921. /**
  1922. * {@inheritdoc}
  1923. */
  1924. public function setDatagridBuilder(DatagridBuilderInterface $datagridBuilder)
  1925. {
  1926. $this->datagridBuilder = $datagridBuilder;
  1927. }
  1928. /**
  1929. * {@inheritdoc}
  1930. */
  1931. public function getDatagridBuilder()
  1932. {
  1933. return $this->datagridBuilder;
  1934. }
  1935. /**
  1936. * {@inheritdoc}
  1937. */
  1938. public function setListBuilder(ListBuilderInterface $listBuilder)
  1939. {
  1940. $this->listBuilder = $listBuilder;
  1941. }
  1942. /**
  1943. * {@inheritdoc}
  1944. */
  1945. public function getListBuilder()
  1946. {
  1947. return $this->listBuilder;
  1948. }
  1949. /**
  1950. * @param ShowBuilderInterface $showBuilder
  1951. */
  1952. public function setShowBuilder(ShowBuilderInterface $showBuilder)
  1953. {
  1954. $this->showBuilder = $showBuilder;
  1955. }
  1956. /**
  1957. * @return ShowBuilderInterface
  1958. */
  1959. public function getShowBuilder()
  1960. {
  1961. return $this->showBuilder;
  1962. }
  1963. /**
  1964. * {@inheritdoc}
  1965. */
  1966. public function setConfigurationPool(Pool $configurationPool)
  1967. {
  1968. $this->configurationPool = $configurationPool;
  1969. }
  1970. /**
  1971. * @return Pool
  1972. */
  1973. public function getConfigurationPool()
  1974. {
  1975. return $this->configurationPool;
  1976. }
  1977. /**
  1978. * {@inheritdoc}
  1979. */
  1980. public function setRouteGenerator(RouteGeneratorInterface $routeGenerator)
  1981. {
  1982. $this->routeGenerator = $routeGenerator;
  1983. }
  1984. /**
  1985. * @return RouteGeneratorInterface
  1986. */
  1987. public function getRouteGenerator()
  1988. {
  1989. return $this->routeGenerator;
  1990. }
  1991. /**
  1992. * {@inheritdoc}
  1993. */
  1994. public function getCode()
  1995. {
  1996. return $this->code;
  1997. }
  1998. /**
  1999. * @param string $baseCodeRoute
  2000. */
  2001. public function setBaseCodeRoute($baseCodeRoute)
  2002. {
  2003. $this->baseCodeRoute = $baseCodeRoute;
  2004. }
  2005. /**
  2006. * {@inheritdoc}
  2007. */
  2008. public function getBaseCodeRoute()
  2009. {
  2010. return $this->baseCodeRoute;
  2011. }
  2012. /**
  2013. * {@inheritdoc}
  2014. */
  2015. public function getModelManager()
  2016. {
  2017. return $this->modelManager;
  2018. }
  2019. /**
  2020. * @param ModelManagerInterface $modelManager
  2021. */
  2022. public function setModelManager(ModelManagerInterface $modelManager)
  2023. {
  2024. $this->modelManager = $modelManager;
  2025. }
  2026. /**
  2027. * {@inheritdoc}
  2028. */
  2029. public function getManagerType()
  2030. {
  2031. return $this->managerType;
  2032. }
  2033. /**
  2034. * @param string $type
  2035. */
  2036. public function setManagerType($type)
  2037. {
  2038. $this->managerType = $type;
  2039. }
  2040. /**
  2041. * {@inheritdoc}
  2042. */
  2043. public function getObjectIdentifier()
  2044. {
  2045. return $this->getCode();
  2046. }
  2047. /**
  2048. * Set the roles and permissions per role.
  2049. *
  2050. * @param array $information
  2051. */
  2052. public function setSecurityInformation(array $information)
  2053. {
  2054. $this->securityInformation = $information;
  2055. }
  2056. /**
  2057. * {@inheritdoc}
  2058. */
  2059. public function getSecurityInformation()
  2060. {
  2061. return $this->securityInformation;
  2062. }
  2063. /**
  2064. * Return the list of permissions the user should have in order to display the admin.
  2065. *
  2066. * @param string $context
  2067. *
  2068. * @return array
  2069. */
  2070. public function getPermissionsShow($context)
  2071. {
  2072. switch ($context) {
  2073. case self::CONTEXT_DASHBOARD:
  2074. case self::CONTEXT_MENU:
  2075. default:
  2076. return array('LIST');
  2077. }
  2078. }
  2079. /**
  2080. * {@inheritdoc}
  2081. */
  2082. public function showIn($context)
  2083. {
  2084. switch ($context) {
  2085. case self::CONTEXT_DASHBOARD:
  2086. case self::CONTEXT_MENU:
  2087. default:
  2088. return $this->isGranted($this->getPermissionsShow($context));
  2089. }
  2090. }
  2091. /**
  2092. * {@inheritdoc}
  2093. */
  2094. public function createObjectSecurity($object)
  2095. {
  2096. $this->getSecurityHandler()->createObjectSecurity($this, $object);
  2097. }
  2098. /**
  2099. * {@inheritdoc}
  2100. */
  2101. public function setSecurityHandler(SecurityHandlerInterface $securityHandler)
  2102. {
  2103. $this->securityHandler = $securityHandler;
  2104. }
  2105. /**
  2106. * {@inheritdoc}
  2107. */
  2108. public function getSecurityHandler()
  2109. {
  2110. return $this->securityHandler;
  2111. }
  2112. /**
  2113. * {@inheritdoc}
  2114. */
  2115. public function isGranted($name, $object = null)
  2116. {
  2117. $key = md5(json_encode($name).($object ? '/'.spl_object_hash($object) : ''));
  2118. if (!array_key_exists($key, $this->cacheIsGranted)) {
  2119. $this->cacheIsGranted[$key] = $this->securityHandler->isGranted($this, $name, $object ?: $this);
  2120. }
  2121. return $this->cacheIsGranted[$key];
  2122. }
  2123. /**
  2124. * {@inheritdoc}
  2125. */
  2126. public function getUrlsafeIdentifier($entity)
  2127. {
  2128. return $this->getModelManager()->getUrlsafeIdentifier($entity);
  2129. }
  2130. /**
  2131. * {@inheritdoc}
  2132. */
  2133. public function getNormalizedIdentifier($entity)
  2134. {
  2135. return $this->getModelManager()->getNormalizedIdentifier($entity);
  2136. }
  2137. /**
  2138. * {@inheritdoc}
  2139. */
  2140. public function id($entity)
  2141. {
  2142. return $this->getNormalizedIdentifier($entity);
  2143. }
  2144. /**
  2145. * {@inheritdoc}
  2146. */
  2147. public function setValidator($validator)
  2148. {
  2149. // TODO: Remove it when bumping requirements to SF 2.5+
  2150. if (!$validator instanceof ValidatorInterface && !$validator instanceof LegacyValidatorInterface) {
  2151. throw new \InvalidArgumentException('Argument 1 must be an instance of Symfony\Component\Validator\Validator\ValidatorInterface or Symfony\Component\Validator\ValidatorInterface');
  2152. }
  2153. $this->validator = $validator;
  2154. }
  2155. /**
  2156. * {@inheritdoc}
  2157. */
  2158. public function getValidator()
  2159. {
  2160. return $this->validator;
  2161. }
  2162. /**
  2163. * {@inheritdoc}
  2164. */
  2165. public function getShow()
  2166. {
  2167. $this->buildShow();
  2168. return $this->show;
  2169. }
  2170. /**
  2171. * {@inheritdoc}
  2172. */
  2173. public function setFormTheme(array $formTheme)
  2174. {
  2175. $this->formTheme = $formTheme;
  2176. }
  2177. /**
  2178. * {@inheritdoc}
  2179. */
  2180. public function getFormTheme()
  2181. {
  2182. return $this->formTheme;
  2183. }
  2184. /**
  2185. * {@inheritdoc}
  2186. */
  2187. public function setFilterTheme(array $filterTheme)
  2188. {
  2189. $this->filterTheme = $filterTheme;
  2190. }
  2191. /**
  2192. * {@inheritdoc}
  2193. */
  2194. public function getFilterTheme()
  2195. {
  2196. return $this->filterTheme;
  2197. }
  2198. /**
  2199. * {@inheritdoc}
  2200. */
  2201. public function addExtension(AdminExtensionInterface $extension)
  2202. {
  2203. $this->extensions[] = $extension;
  2204. }
  2205. /**
  2206. * {@inheritdoc}
  2207. */
  2208. public function getExtensions()
  2209. {
  2210. return $this->extensions;
  2211. }
  2212. /**
  2213. * {@inheritdoc}
  2214. */
  2215. public function setMenuFactory(MenuFactoryInterface $menuFactory)
  2216. {
  2217. $this->menuFactory = $menuFactory;
  2218. }
  2219. /**
  2220. * {@inheritdoc}
  2221. */
  2222. public function getMenuFactory()
  2223. {
  2224. return $this->menuFactory;
  2225. }
  2226. /**
  2227. * {@inheritdoc}
  2228. */
  2229. public function setRouteBuilder(RouteBuilderInterface $routeBuilder)
  2230. {
  2231. $this->routeBuilder = $routeBuilder;
  2232. }
  2233. /**
  2234. * {@inheritdoc}
  2235. */
  2236. public function getRouteBuilder()
  2237. {
  2238. return $this->routeBuilder;
  2239. }
  2240. /**
  2241. * {@inheritdoc}
  2242. */
  2243. public function toString($object)
  2244. {
  2245. if (!is_object($object)) {
  2246. return '';
  2247. }
  2248. if (method_exists($object, '__toString') && null !== $object->__toString()) {
  2249. return (string) $object;
  2250. }
  2251. return sprintf('%s:%s', ClassUtils::getClass($object), spl_object_hash($object));
  2252. }
  2253. /**
  2254. * {@inheritdoc}
  2255. */
  2256. public function setLabelTranslatorStrategy(LabelTranslatorStrategyInterface $labelTranslatorStrategy)
  2257. {
  2258. $this->labelTranslatorStrategy = $labelTranslatorStrategy;
  2259. }
  2260. /**
  2261. * {@inheritdoc}
  2262. */
  2263. public function getLabelTranslatorStrategy()
  2264. {
  2265. return $this->labelTranslatorStrategy;
  2266. }
  2267. /**
  2268. * {@inheritdoc}
  2269. */
  2270. public function supportsPreviewMode()
  2271. {
  2272. return $this->supportsPreviewMode;
  2273. }
  2274. /**
  2275. * Set custom per page options.
  2276. *
  2277. * @param array $options
  2278. */
  2279. public function setPerPageOptions(array $options)
  2280. {
  2281. $this->perPageOptions = $options;
  2282. }
  2283. /**
  2284. * Returns predefined per page options.
  2285. *
  2286. * @return array
  2287. */
  2288. public function getPerPageOptions()
  2289. {
  2290. return $this->perPageOptions;
  2291. }
  2292. /**
  2293. * Set pager type.
  2294. *
  2295. * @param string $pagerType
  2296. */
  2297. public function setPagerType($pagerType)
  2298. {
  2299. $this->pagerType = $pagerType;
  2300. }
  2301. /**
  2302. * Get pager type.
  2303. *
  2304. * @return string
  2305. */
  2306. public function getPagerType()
  2307. {
  2308. return $this->pagerType;
  2309. }
  2310. /**
  2311. * Returns true if the per page value is allowed, false otherwise.
  2312. *
  2313. * @param int $perPage
  2314. *
  2315. * @return bool
  2316. */
  2317. public function determinedPerPageValue($perPage)
  2318. {
  2319. return in_array($perPage, $this->perPageOptions);
  2320. }
  2321. /**
  2322. * {@inheritdoc}
  2323. */
  2324. public function isAclEnabled()
  2325. {
  2326. return $this->getSecurityHandler() instanceof AclSecurityHandlerInterface;
  2327. }
  2328. /**
  2329. * {@inheritdoc}
  2330. */
  2331. public function getObjectMetadata($object)
  2332. {
  2333. return new Metadata($this->toString($object));
  2334. }
  2335. /**
  2336. * {@inheritdoc}
  2337. */
  2338. public function getListModes()
  2339. {
  2340. return $this->listModes;
  2341. }
  2342. /**
  2343. * {@inheritdoc}
  2344. */
  2345. public function setListMode($mode)
  2346. {
  2347. if (!$this->hasRequest()) {
  2348. throw new \RuntimeException(sprintf('No request attached to the current admin: %s', $this->getCode()));
  2349. }
  2350. $this->getRequest()->getSession()->set(sprintf('%s.list_mode', $this->getCode()), $mode);
  2351. }
  2352. /**
  2353. * {@inheritdoc}
  2354. */
  2355. public function getListMode()
  2356. {
  2357. if (!$this->hasRequest()) {
  2358. return 'list';
  2359. }
  2360. return $this->getRequest()->getSession()->get(sprintf('%s.list_mode', $this->getCode()), 'list');
  2361. }
  2362. /**
  2363. * {@inheritdoc}
  2364. */
  2365. public function getAccessMapping()
  2366. {
  2367. return $this->accessMapping;
  2368. }
  2369. /**
  2370. * {@inheritdoc}
  2371. */
  2372. public function checkAccess($action, $object = null)
  2373. {
  2374. $access = $this->getAccess();
  2375. if (!array_key_exists($action, $access)) {
  2376. throw new \InvalidArgumentException(sprintf('Action "%s" could not be found in access mapping. Please make sure your action is defined into your admin class accessMapping property.', $action));
  2377. }
  2378. if (!is_array($access[$action])) {
  2379. $access[$action] = array($access[$action]);
  2380. }
  2381. foreach ($access[$action] as $role) {
  2382. if (false === $this->isGranted($role, $object)) {
  2383. throw new AccessDeniedException(sprintf('Access Denied to the action %s and role %s', $action, $role));
  2384. }
  2385. }
  2386. }
  2387. /**
  2388. * Hook to handle access authorization, without throw Exception.
  2389. *
  2390. * @param string $action
  2391. * @param object $object
  2392. *
  2393. * @return bool
  2394. */
  2395. public function hasAccess($action, $object = null)
  2396. {
  2397. $access = $this->getAccess();
  2398. if (!array_key_exists($action, $access)) {
  2399. return false;
  2400. }
  2401. if (!is_array($access[$action])) {
  2402. $access[$action] = array($access[$action]);
  2403. }
  2404. foreach ($access[$action] as $role) {
  2405. if (false === $this->isGranted($role, $object)) {
  2406. return false;
  2407. }
  2408. }
  2409. return true;
  2410. }
  2411. /**
  2412. * {@inheritdoc}
  2413. */
  2414. public function configureActionButtons($action, $object = null)
  2415. {
  2416. $list = array();
  2417. if (in_array($action, array('tree', 'show', 'edit', 'delete', 'list', 'batch'))
  2418. && $this->hasAccess('create')
  2419. && $this->hasRoute('create')
  2420. ) {
  2421. $list['create'] = array(
  2422. 'template' => $this->getTemplate('button_create'),
  2423. );
  2424. }
  2425. if (in_array($action, array('show', 'delete', 'acl', 'history'))
  2426. && $this->canAccessObject('edit', $object)
  2427. && $this->hasRoute('edit')
  2428. ) {
  2429. $list['edit'] = array(
  2430. 'template' => $this->getTemplate('button_edit'),
  2431. );
  2432. }
  2433. if (in_array($action, array('show', 'edit', 'acl'))
  2434. && $this->canAccessObject('history', $object)
  2435. && $this->hasRoute('history')
  2436. ) {
  2437. $list['history'] = array(
  2438. 'template' => $this->getTemplate('button_history'),
  2439. );
  2440. }
  2441. if (in_array($action, array('edit', 'history'))
  2442. && $this->isAclEnabled()
  2443. && $this->canAccessObject('acl', $object)
  2444. && $this->hasRoute('acl')
  2445. ) {
  2446. $list['acl'] = array(
  2447. 'template' => $this->getTemplate('button_acl'),
  2448. );
  2449. }
  2450. if (in_array($action, array('edit', 'history', 'acl'))
  2451. && $this->canAccessObject('show', $object)
  2452. && count($this->getShow()) > 0
  2453. && $this->hasRoute('show')
  2454. ) {
  2455. $list['show'] = array(
  2456. 'template' => $this->getTemplate('button_show'),
  2457. );
  2458. }
  2459. if (in_array($action, array('show', 'edit', 'delete', 'acl', 'batch'))
  2460. && $this->hasAccess('list')
  2461. && $this->hasRoute('list')
  2462. ) {
  2463. $list['list'] = array(
  2464. 'template' => $this->getTemplate('button_list'),
  2465. );
  2466. }
  2467. return $list;
  2468. }
  2469. /**
  2470. * @param string $action
  2471. * @param mixed $object
  2472. *
  2473. * @return array
  2474. */
  2475. public function getActionButtons($action, $object = null)
  2476. {
  2477. $list = $this->configureActionButtons($action, $object);
  2478. foreach ($this->getExtensions() as $extension) {
  2479. // TODO: remove method check in next major release
  2480. if (method_exists($extension, 'configureActionButtons')) {
  2481. $list = $extension->configureActionButtons($this, $list, $action, $object);
  2482. }
  2483. }
  2484. return $list;
  2485. }
  2486. /**
  2487. * Get the list of actions that can be accessed directly from the dashboard.
  2488. *
  2489. * @return array
  2490. */
  2491. public function getDashboardActions()
  2492. {
  2493. $actions = array();
  2494. if ($this->hasRoute('create') && $this->isGranted('CREATE')) {
  2495. $actions['create'] = array(
  2496. 'label' => 'link_add',
  2497. 'translation_domain' => 'SonataAdminBundle',
  2498. 'template' => $this->getTemplate('action_create'),
  2499. 'url' => $this->generateUrl('create'),
  2500. 'icon' => 'plus-circle',
  2501. );
  2502. }
  2503. if ($this->hasRoute('list') && $this->isGranted('LIST')) {
  2504. $actions['list'] = array(
  2505. 'label' => 'link_list',
  2506. 'translation_domain' => 'SonataAdminBundle',
  2507. 'url' => $this->generateUrl('list'),
  2508. 'icon' => 'list',
  2509. );
  2510. }
  2511. return $actions;
  2512. }
  2513. /**
  2514. * Setting to true will enable mosaic button for the admin screen.
  2515. * Setting to false will hide mosaic button for the admin screen.
  2516. *
  2517. * @param bool $isShown
  2518. */
  2519. final public function showMosaicButton($isShown)
  2520. {
  2521. if ($isShown) {
  2522. $this->listModes['mosaic'] = array('class' => self::MOSAIC_ICON_CLASS);
  2523. } else {
  2524. unset($this->listModes['mosaic']);
  2525. }
  2526. }
  2527. /**
  2528. * @param FormMapper $form
  2529. */
  2530. final public function getSearchResultLink($object)
  2531. {
  2532. foreach ($this->searchResultActions as $action) {
  2533. if ($this->hasRoute($action) && $this->hasAccess($action, $object)) {
  2534. return $this->generateObjectUrl($action, $object);
  2535. }
  2536. }
  2537. return;
  2538. }
  2539. /**
  2540. * Checks if a filter type is set to a default value.
  2541. *
  2542. * @param string $name
  2543. *
  2544. * @return bool
  2545. */
  2546. final public function isDefaultFilter($name)
  2547. {
  2548. $filter = $this->getFilterParameters();
  2549. $default = $this->getDefaultFilterValues();
  2550. if (!array_key_exists($name, $filter) || !array_key_exists($name, $default)) {
  2551. return false;
  2552. }
  2553. return $filter[$name] == $default[$name];
  2554. }
  2555. /**
  2556. * Check object existence and access, without throw Exception.
  2557. *
  2558. * @param string $action
  2559. * @param object $object
  2560. *
  2561. * @return bool
  2562. */
  2563. public function canAccessObject($action, $object)
  2564. {
  2565. return $object && $this->id($object) && $this->hasAccess($action, $object);
  2566. }
  2567. /**
  2568. * Returns a list of default filters.
  2569. *
  2570. * @return array
  2571. */
  2572. final protected function getDefaultFilterValues()
  2573. {
  2574. $defaultFilterValues = array();
  2575. $this->configureDefaultFilterValues($defaultFilterValues);
  2576. foreach ($this->getExtensions() as $extension) {
  2577. // NEXT_MAJOR: remove method check in next major release
  2578. if (method_exists($extension, 'configureDefaultFilterValues')) {
  2579. $extension->configureDefaultFilterValues($this, $defaultFilterValues);
  2580. }
  2581. }
  2582. return $defaultFilterValues;
  2583. }
  2584. /**
  2585. * {@inheritdoc}
  2586. */
  2587. protected function configureFormFields(FormMapper $form)
  2588. {
  2589. }
  2590. /**
  2591. * @param ListMapper $list
  2592. */
  2593. protected function configureListFields(ListMapper $list)
  2594. {
  2595. }
  2596. /**
  2597. * @param DatagridMapper $filter
  2598. */
  2599. protected function configureDatagridFilters(DatagridMapper $filter)
  2600. {
  2601. }
  2602. /**
  2603. * @param ShowMapper $show
  2604. */
  2605. protected function configureShowFields(ShowMapper $show)
  2606. {
  2607. }
  2608. /**
  2609. * @param RouteCollection $collection
  2610. */
  2611. protected function configureRoutes(RouteCollection $collection)
  2612. {
  2613. }
  2614. /**
  2615. * Allows you to customize batch actions.
  2616. *
  2617. * @param array $actions List of actions
  2618. *
  2619. * @return array
  2620. */
  2621. protected function configureBatchActions($actions)
  2622. {
  2623. return $actions;
  2624. }
  2625. /**
  2626. * NEXT_MAJOR: remove this method.
  2627. *
  2628. * @param MenuItemInterface $menu
  2629. * @param $action
  2630. * @param AdminInterface $childAdmin
  2631. *
  2632. * @return mixed
  2633. *
  2634. * @deprecated Use configureTabMenu instead
  2635. */
  2636. protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
  2637. {
  2638. }
  2639. /**
  2640. * Configures the tab menu in your admin.
  2641. *
  2642. * @param MenuItemInterface $menu
  2643. * @param string $action
  2644. * @param AdminInterface $childAdmin
  2645. *
  2646. * @return mixed
  2647. */
  2648. protected function configureTabMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
  2649. {
  2650. // Use configureSideMenu not to mess with previous overrides
  2651. // TODO remove once deprecation period is over
  2652. $this->configureSideMenu($menu, $action, $childAdmin);
  2653. }
  2654. /**
  2655. * build the view FieldDescription array.
  2656. */
  2657. protected function buildShow()
  2658. {
  2659. if ($this->show) {
  2660. return;
  2661. }
  2662. $this->show = new FieldDescriptionCollection();
  2663. $mapper = new ShowMapper($this->showBuilder, $this->show, $this);
  2664. $this->configureShowFields($mapper);
  2665. foreach ($this->getExtensions() as $extension) {
  2666. $extension->configureShowFields($mapper);
  2667. }
  2668. }
  2669. /**
  2670. * build the list FieldDescription array.
  2671. */
  2672. protected function buildList()
  2673. {
  2674. if ($this->list) {
  2675. return;
  2676. }
  2677. $this->list = $this->getListBuilder()->getBaseList();
  2678. $mapper = new ListMapper($this->getListBuilder(), $this->list, $this);
  2679. if (count($this->getBatchActions()) > 0) {
  2680. $fieldDescription = $this->getModelManager()->getNewFieldDescriptionInstance($this->getClass(), 'batch', array(
  2681. 'label' => 'batch',
  2682. 'code' => '_batch',
  2683. 'sortable' => false,
  2684. 'virtual_field' => true,
  2685. ));
  2686. $fieldDescription->setAdmin($this);
  2687. $fieldDescription->setTemplate($this->getTemplate('batch'));
  2688. $mapper->add($fieldDescription, 'batch');
  2689. }
  2690. $this->configureListFields($mapper);
  2691. foreach ($this->getExtensions() as $extension) {
  2692. $extension->configureListFields($mapper);
  2693. }
  2694. if ($this->hasRequest() && $this->getRequest()->isXmlHttpRequest()) {
  2695. $fieldDescription = $this->getModelManager()->getNewFieldDescriptionInstance($this->getClass(), 'select', array(
  2696. 'label' => false,
  2697. 'code' => '_select',
  2698. 'sortable' => false,
  2699. 'virtual_field' => false,
  2700. ));
  2701. $fieldDescription->setAdmin($this);
  2702. $fieldDescription->setTemplate($this->getTemplate('select'));
  2703. $mapper->add($fieldDescription, 'select');
  2704. }
  2705. }
  2706. /**
  2707. * Build the form FieldDescription collection.
  2708. */
  2709. protected function buildForm()
  2710. {
  2711. if ($this->form) {
  2712. return;
  2713. }
  2714. // append parent object if any
  2715. // todo : clean the way the Admin class can retrieve set the object
  2716. if ($this->isChild() && $this->getParentAssociationMapping()) {
  2717. $parent = $this->getParent()->getObject($this->request->get($this->getParent()->getIdParameter()));
  2718. $propertyAccessor = $this->getConfigurationPool()->getPropertyAccessor();
  2719. $propertyPath = new PropertyPath($this->getParentAssociationMapping());
  2720. $object = $this->getSubject();
  2721. $value = $propertyAccessor->getValue($object, $propertyPath);
  2722. if (is_array($value) || ($value instanceof \Traversable && $value instanceof \ArrayAccess)) {
  2723. $value[] = $parent;
  2724. $propertyAccessor->setValue($object, $propertyPath, $value);
  2725. } else {
  2726. $propertyAccessor->setValue($object, $propertyPath, $parent);
  2727. }
  2728. }
  2729. $this->form = $this->getFormBuilder()->getForm();
  2730. }
  2731. /**
  2732. * Gets the subclass corresponding to the given name.
  2733. *
  2734. * @param string $name The name of the sub class
  2735. *
  2736. * @return string the subclass
  2737. */
  2738. protected function getSubClass($name)
  2739. {
  2740. if ($this->hasSubClass($name)) {
  2741. return $this->subClasses[$name];
  2742. }
  2743. throw new \RuntimeException(sprintf('Unable to find the subclass `%s` for admin `%s`', $name, get_class($this)));
  2744. }
  2745. /**
  2746. * Attach the inline validator to the model metadata, this must be done once per admin.
  2747. */
  2748. protected function attachInlineValidator()
  2749. {
  2750. $admin = $this;
  2751. // add the custom inline validation option
  2752. // TODO: Remove conditional method when bumping requirements to SF 2.5+
  2753. if (method_exists($this->validator, 'getMetadataFor')) {
  2754. $metadata = $this->validator->getMetadataFor($this->getClass());
  2755. } else {
  2756. $metadata = $this->validator->getMetadataFactory()->getMetadataFor($this->getClass());
  2757. }
  2758. $metadata->addConstraint(new InlineConstraint(array(
  2759. 'service' => $this,
  2760. 'method' => function (ErrorElement $errorElement, $object) use ($admin) {
  2761. /* @var \Sonata\AdminBundle\Admin\AdminInterface $admin */
  2762. // This avoid the main validation to be cascaded to children
  2763. // The problem occurs when a model Page has a collection of Page as property
  2764. if ($admin->hasSubject() && spl_object_hash($object) !== spl_object_hash($admin->getSubject())) {
  2765. return;
  2766. }
  2767. $admin->validate($errorElement, $object);
  2768. foreach ($admin->getExtensions() as $extension) {
  2769. $extension->validate($admin, $errorElement, $object);
  2770. }
  2771. },
  2772. 'serializingWarning' => true,
  2773. )));
  2774. }
  2775. /**
  2776. * Predefine per page options.
  2777. */
  2778. protected function predefinePerPageOptions()
  2779. {
  2780. array_unshift($this->perPageOptions, $this->maxPerPage);
  2781. $this->perPageOptions = array_unique($this->perPageOptions);
  2782. sort($this->perPageOptions);
  2783. }
  2784. /**
  2785. * Return list routes with permissions name.
  2786. *
  2787. * @return array
  2788. */
  2789. protected function getAccess()
  2790. {
  2791. $access = array_merge(array(
  2792. 'acl' => 'MASTER',
  2793. 'export' => 'EXPORT',
  2794. 'historyCompareRevisions' => 'EDIT',
  2795. 'historyViewRevision' => 'EDIT',
  2796. 'history' => 'EDIT',
  2797. 'edit' => 'EDIT',
  2798. 'show' => 'VIEW',
  2799. 'create' => 'CREATE',
  2800. 'delete' => 'DELETE',
  2801. 'batchDelete' => 'DELETE',
  2802. 'list' => 'LIST',
  2803. ), $this->getAccessMapping());
  2804. foreach ($this->extensions as $extension) {
  2805. // TODO: remove method check in next major release
  2806. if (method_exists($extension, 'getAccessMapping')) {
  2807. $access = array_merge($access, $extension->getAccessMapping($this));
  2808. }
  2809. }
  2810. return $access;
  2811. }
  2812. /**
  2813. * Returns a list of default filters.
  2814. *
  2815. * @param array $filterValues
  2816. */
  2817. protected function configureDefaultFilterValues(array &$filterValues)
  2818. {
  2819. }
  2820. /**
  2821. * Build all the related urls to the current admin.
  2822. */
  2823. private function buildRoutes()
  2824. {
  2825. if ($this->loaded['routes']) {
  2826. return;
  2827. }
  2828. $this->loaded['routes'] = true;
  2829. $this->routes = new RouteCollection(
  2830. $this->getBaseCodeRoute(),
  2831. $this->getBaseRouteName(),
  2832. $this->getBaseRoutePattern(),
  2833. $this->getBaseControllerName()
  2834. );
  2835. $this->routeBuilder->build($this, $this->routes);
  2836. $this->configureRoutes($this->routes);
  2837. foreach ($this->getExtensions() as $extension) {
  2838. $extension->configureRoutes($this, $this->routes);
  2839. }
  2840. }
  2841. }