1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Http\Controllers\Admin; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Form\Factory\IFormFactory; |
8
|
|
|
use AbterPhp\Framework\Constant\Event; |
9
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
10
|
|
|
use AbterPhp\Framework\Events\FormReady; |
11
|
|
|
use AbterPhp\Framework\I18n\ITranslator; |
12
|
|
|
use AbterPhp\Framework\Orm\IGridRepo; |
13
|
|
|
use AbterPhp\Framework\Session\FlashService; |
14
|
|
|
use Casbin\Exceptions\CasbinException; |
15
|
|
|
use Opulence\Events\Dispatchers\IEventDispatcher; |
16
|
|
|
use Opulence\Http\Requests\RequestMethods; |
17
|
|
|
use Opulence\Http\Responses\Response; |
18
|
|
|
use Opulence\Orm\OrmException; |
19
|
|
|
use Opulence\Routing\Urls\URLException; |
20
|
|
|
use Opulence\Routing\Urls\UrlGenerator; |
21
|
|
|
use Opulence\Sessions\ISession; |
22
|
|
|
use Psr\Log\LoggerInterface; |
23
|
|
|
|
24
|
|
|
abstract class FormAbstract extends AdminAbstract |
25
|
|
|
{ |
26
|
|
|
use UrlTrait; |
27
|
|
|
use MessageTrait; |
28
|
|
|
|
29
|
|
|
const LOG_MSG_LOAD_FAILURE = 'Loading %1$s failed.'; |
30
|
|
|
|
31
|
|
|
const ENTITY_TITLE_SINGULAR = ''; |
32
|
|
|
|
33
|
|
|
const VIEW_FORM = 'contents/backend/form'; |
34
|
|
|
|
35
|
|
|
const VAR_ENTITY = 'entity'; |
36
|
|
|
const VAR_FORM = 'form'; |
37
|
|
|
|
38
|
|
|
const TITLE_NEW = 'framework:titleNew'; |
39
|
|
|
const TITLE_EDIT = 'framework:titleEdit'; |
40
|
|
|
|
41
|
|
|
const URL_NEW = '%s-new'; |
42
|
|
|
|
43
|
|
|
const RESOURCE_DEFAULT = '%s-form'; |
44
|
|
|
const RESOURCE_HEADER = '%s-header-form'; |
45
|
|
|
const RESOURCE_FOOTER = '%s-footer-form'; |
46
|
|
|
const RESOURCE_TYPE = 'form'; |
47
|
|
|
|
48
|
|
|
/** @var IGridRepo */ |
49
|
|
|
protected $repo; |
50
|
|
|
|
51
|
|
|
/** @var ISession */ |
52
|
|
|
protected $session; |
53
|
|
|
|
54
|
|
|
/** @var IFormFactory */ |
55
|
|
|
protected $formFactory; |
56
|
|
|
|
57
|
|
|
/** @var IEventDispatcher */ |
58
|
|
|
protected $eventDispatcher; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* FormAbstract constructor. |
62
|
|
|
* |
63
|
|
|
* @param FlashService $flashService |
64
|
|
|
* @param ITranslator $translator |
65
|
|
|
* @param UrlGenerator $urlGenerator |
66
|
|
|
* @param LoggerInterface $logger |
67
|
|
|
* @param IGridRepo $repo |
68
|
|
|
* @param ISession $session |
69
|
|
|
* @param IFormFactory $formFactory |
70
|
|
|
* @param IEventDispatcher $eventDispatcher |
71
|
|
|
*/ |
72
|
|
|
public function __construct( |
73
|
|
|
FlashService $flashService, |
74
|
|
|
ITranslator $translator, |
75
|
|
|
UrlGenerator $urlGenerator, |
76
|
|
|
LoggerInterface $logger, |
77
|
|
|
IGridRepo $repo, |
78
|
|
|
ISession $session, |
79
|
|
|
IFormFactory $formFactory, |
80
|
|
|
IEventDispatcher $eventDispatcher |
81
|
|
|
) { |
82
|
|
|
parent::__construct($flashService, $translator, $urlGenerator, $logger); |
83
|
|
|
|
84
|
|
|
$this->repo = $repo; |
85
|
|
|
$this->session = $session; |
86
|
|
|
$this->formFactory = $formFactory; |
87
|
|
|
$this->eventDispatcher = $eventDispatcher; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Response |
92
|
|
|
* @throws CasbinException |
93
|
|
|
* @throws URLException |
94
|
|
|
* @throws \Throwable |
95
|
|
|
*/ |
96
|
|
|
public function new(): Response |
97
|
|
|
{ |
98
|
|
|
$entity = $this->createEntity(''); |
99
|
|
|
|
100
|
|
|
$url = $this->urlGenerator->createFromName(sprintf(static::URL_NEW, static::ENTITY_PLURAL)); |
101
|
|
|
$title = $this->translator->translate(static::TITLE_NEW, static::ENTITY_TITLE_SINGULAR); |
102
|
|
|
$form = $this->formFactory->create($url, RequestMethods::POST, $this->getShowUrl(), $entity); |
103
|
|
|
|
104
|
|
|
$form->setTranslator($this->translator); |
105
|
|
|
|
106
|
|
|
$this->eventDispatcher->dispatch(Event::FORM_READY, new FormReady($form)); |
107
|
|
|
|
108
|
|
|
$this->view = $this->viewFactory->createView(static::VIEW_FORM); |
109
|
|
|
$this->view->setVar(static::VAR_ENTITY, $entity); |
110
|
|
|
$this->view->setVar(static::VAR_FORM, $form); |
111
|
|
|
|
112
|
|
|
$this->addCustomAssets($entity); |
113
|
|
|
|
114
|
|
|
return $this->createResponse($title); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $entityId |
119
|
|
|
* |
120
|
|
|
* @return Response |
121
|
|
|
* @throws CasbinException |
122
|
|
|
* @throws URLException |
123
|
|
|
* @throws \Throwable |
124
|
|
|
*/ |
125
|
|
|
public function edit(string $entityId): Response |
126
|
|
|
{ |
127
|
|
|
$entity = $this->retrieveEntity($entityId); |
128
|
|
|
|
129
|
|
|
$url = $this->getEditUrl($entityId); |
130
|
|
|
$title = $this->translator->translate(static::TITLE_EDIT, static::ENTITY_TITLE_SINGULAR, (string)$entity); |
131
|
|
|
$form = $this->formFactory->create($url, RequestMethods::PUT, $this->getShowUrl(), $entity); |
132
|
|
|
|
133
|
|
|
$this->eventDispatcher->dispatch(Event::FORM_READY, new FormReady($form)); |
134
|
|
|
|
135
|
|
|
$form->setTranslator($this->translator); |
136
|
|
|
|
137
|
|
|
$this->view = $this->viewFactory->createView(sprintf(static::VIEW_FORM, strtolower(static::ENTITY_SINGULAR))); |
138
|
|
|
$this->view->setVar(static::VAR_ENTITY, $entity); |
139
|
|
|
$this->view->setVar(static::VAR_FORM, $form); |
140
|
|
|
|
141
|
|
|
$this->addCustomAssets($entity); |
142
|
|
|
|
143
|
|
|
return $this->createResponse($title); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $entityId |
148
|
|
|
* |
149
|
|
|
* @return IStringerEntity |
150
|
|
|
*/ |
151
|
|
|
public function retrieveEntity(string $entityId): IStringerEntity |
152
|
|
|
{ |
153
|
|
|
/** @var FlashService $flashService */ |
154
|
|
|
$flashService = $this->flashService; |
155
|
|
|
|
156
|
|
|
try { |
157
|
|
|
/** @var IStringerEntity $entity */ |
158
|
|
|
$entity = $this->repo->getById($entityId); |
159
|
|
|
} catch (OrmException $e) { |
160
|
|
|
$errorMessage = $this->getMessage(static::ENTITY_LOAD_FAILURE); |
161
|
|
|
|
162
|
|
|
$flashService->mergeErrorMessages([$errorMessage]); |
163
|
|
|
|
164
|
|
|
$this->logger->info( |
165
|
|
|
sprintf(static::LOG_MSG_LOAD_FAILURE, static::ENTITY_SINGULAR), |
166
|
|
|
$this->getExceptionContext($e) |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
return $this->createEntity(''); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $entity; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $entityEntityId |
177
|
|
|
* |
178
|
|
|
* @return IStringerEntity |
179
|
|
|
*/ |
180
|
|
|
abstract protected function createEntity(string $entityEntityId): IStringerEntity; |
181
|
|
|
} |
182
|
|
|
|