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