|
3 | 3 | namespace AlterPHP\EasyAdminExtensionBundle\Controller; |
4 | 4 |
|
5 | 5 | use AlterPHP\EasyAdminExtensionBundle\Security\AdminAuthorizationChecker; |
| 6 | +use AlterPHP\EasyAdminExtensionBundle\Controller\AdminExtensionControllerTrait; |
6 | 7 | use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController as BaseEasyAdminControler; |
7 | | -use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents; |
8 | | -use League\Uri\Modifiers\RemoveQueryParams; |
9 | | -use League\Uri\Schemes\Http; |
10 | | -use Symfony\Component\HttpFoundation\JsonResponse; |
11 | 8 |
|
12 | 9 | class EasyAdminController extends BaseEasyAdminControler |
13 | 10 | { |
| 11 | + use AdminExtensionControllerTrait; |
| 12 | + |
14 | 13 | public static function getSubscribedServices(): array |
15 | 14 | { |
16 | 15 | return \array_merge(parent::getSubscribedServices(), [AdminAuthorizationChecker::class]); |
17 | 16 | } |
18 | | - |
19 | | - protected function embeddedListAction() |
20 | | - { |
21 | | - $this->dispatch(EasyAdminEvents::PRE_LIST); |
22 | | - |
23 | | - $maxResults = (int) $this->request->query->get('max-results', $this->config['list']['max_results']); |
24 | | - |
25 | | - $paginator = $this->findAll($this->entity['class'], $this->request->query->get('page', 1), $maxResults, $this->request->query->get('sortField'), $this->request->query->get('sortDirection'), $this->entity['list']['dql_filter']); |
26 | | - |
27 | | - $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]); |
28 | | - |
29 | | - // Filter displaid columns |
30 | | - $hiddenFields = $this->request->query->get('hidden-fields', []); |
31 | | - $fields = \array_filter( |
32 | | - $this->entity['list']['fields'], |
33 | | - function ($name) use ($hiddenFields) { |
34 | | - return !\in_array($name, $hiddenFields); |
35 | | - }, |
36 | | - ARRAY_FILTER_USE_KEY |
37 | | - ); |
38 | | - |
39 | | - // Removes existing referer |
40 | | - $baseMasterRequestUri = !$this->request->isXmlHttpRequest() |
41 | | - ? $this->get('request_stack')->getMasterRequest()->getUri() |
42 | | - : $this->request->headers->get('referer'); |
43 | | - $baseMasterRequestUri = Http::createFromString($baseMasterRequestUri); |
44 | | - $removeRefererModifier = new RemoveQueryParams(['referer']); |
45 | | - $masterRequestUri = $removeRefererModifier->process($baseMasterRequestUri); |
46 | | - |
47 | | - $requestParameters = $this->request->query->all(); |
48 | | - $requestParameters['referer'] = (string) $masterRequestUri; |
49 | | - |
50 | | - $viewVars = [ |
51 | | - 'paginator' => $paginator, |
52 | | - 'fields' => $fields, |
53 | | - '_request_parameters' => $requestParameters, |
54 | | - ]; |
55 | | - |
56 | | - return $this->executeDynamicMethod( |
57 | | - 'render<EntityName>Template', |
58 | | - ['embeddedList', $this->entity['embeddedList']['template'], $viewVars] |
59 | | - ); |
60 | | - } |
61 | | - |
62 | | - /** |
63 | | - * {@inheritdoc} |
64 | | - * |
65 | | - * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException |
66 | | - */ |
67 | | - protected function isActionAllowed($actionName) |
68 | | - { |
69 | | - switch ($actionName) { |
70 | | - // autocomplete action is mapped to list action for access permissions |
71 | | - case 'autocomplete': |
72 | | - // filters (EasyAdmin new list filters) action is mapped to list action for access permissions |
73 | | - case 'filters': |
74 | | - // embeddedList action is mapped to list action for access permissions |
75 | | - case 'embeddedList': |
76 | | - $actionName = 'list'; |
77 | | - break; |
78 | | - // newAjax action is mapped to new action for access permissions |
79 | | - case 'newAjax': |
80 | | - $actionName = 'new'; |
81 | | - break; |
82 | | - default: |
83 | | - break; |
84 | | - } |
85 | | - |
86 | | - // Get item for edit/show or custom actions => security voters may apply |
87 | | - $easyadmin = $this->request->attributes->get('easyadmin'); |
88 | | - $subject = $easyadmin['item'] ?? null; |
89 | | - $this->get(AdminAuthorizationChecker::class)->checksUserAccess($this->entity, $actionName, $subject); |
90 | | - |
91 | | - return parent::isActionAllowed($actionName); |
92 | | - } |
93 | | - |
94 | | - /** |
95 | | - * The method that is executed when the user performs a 'new ajax' action on an entity. |
96 | | - * |
97 | | - * @return JsonResponse |
98 | | - */ |
99 | | - protected function newAjaxAction() |
100 | | - { |
101 | | - $this->dispatch(EasyAdminEvents::PRE_NEW); |
102 | | - |
103 | | - $entity = $this->executeDynamicMethod('createNew<EntityName>Entity'); |
104 | | - $easyadmin = \array_merge($this->request->attributes->get('easyadmin'), ['item' => $entity]); |
105 | | - $this->request->attributes->set('easyadmin', $easyadmin); |
106 | | - |
107 | | - $fields = $this->entity['new']['fields']; |
108 | | - $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]); |
109 | | - $newForm->handleRequest($this->request); |
110 | | - if ($newForm->isSubmitted() && $newForm->isValid()) { |
111 | | - $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]); |
112 | | - $this->executeDynamicMethod('persist<EntityName>Entity', [$entity]); |
113 | | - $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]); |
114 | | - |
115 | | - return new JsonResponse(['option' => ['id' => $entity->getId(), 'text' => (string) $entity]]); |
116 | | - } |
117 | | - |
118 | | - $this->dispatch(EasyAdminEvents::POST_NEW, ['entity_fields' => $fields, 'form' => $newForm, 'entity' => $entity]); |
119 | | - |
120 | | - $parameters = ['form' => $newForm->createView(), 'entity_fields' => $fields, 'entity' => $entity]; |
121 | | - $templatePath = '@EasyAdminExtension/default/new_ajax.html.twig'; |
122 | | - if (isset($this->entity['templates']['new_ajax'])) { |
123 | | - $templatePath = $this->entity['templates']['new_ajax']; |
124 | | - } |
125 | | - |
126 | | - return new JsonResponse(['html' => $this->renderView($templatePath, $parameters)]); |
127 | | - } |
128 | 17 | } |
0 commit comments