AbstractAdmin.php 80 KB

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