AbstractAdmin.php 78 KB

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