Skip to content

Commit 3b66079

Browse files
authored
feat: Controller now use trait for better use in EasyAdmin (#176)
1 parent e4923b8 commit 3b66079

File tree

2 files changed

+125
-114
lines changed

2 files changed

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

src/Controller/EasyAdminController.php

Lines changed: 3 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -3,126 +3,15 @@
33
namespace AlterPHP\EasyAdminExtensionBundle\Controller;
44

55
use AlterPHP\EasyAdminExtensionBundle\Security\AdminAuthorizationChecker;
6+
use AlterPHP\EasyAdminExtensionBundle\Controller\AdminExtensionControllerTrait;
67
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;
118

129
class EasyAdminController extends BaseEasyAdminControler
1310
{
11+
use AdminExtensionControllerTrait;
12+
1413
public static function getSubscribedServices(): array
1514
{
1615
return \array_merge(parent::getSubscribedServices(), [AdminAuthorizationChecker::class]);
1716
}
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-
}
12817
}

0 commit comments

Comments
 (0)