forked from ezsystems/repository-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentFormProcessor.php
More file actions
284 lines (249 loc) · 12 KB
/
Copy pathContentFormProcessor.php
File metadata and controls
284 lines (249 loc) · 12 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
* This file is part of the eZ RepositoryForms package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\RepositoryForms\Form\Processor;
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\ContentStruct;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use EzSystems\RepositoryForms\Data\Content\ContentCreateData;
use EzSystems\RepositoryForms\Data\Content\ContentUpdateData;
use EzSystems\RepositoryForms\Data\NewnessCheckable;
use EzSystems\RepositoryForms\Event\FormActionEvent;
use EzSystems\RepositoryForms\Event\RepositoryFormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
/**
* Listens for and processes RepositoryForm events: publish, remove draft, save draft...
*/
class ContentFormProcessor implements EventSubscriberInterface
{
/** @var \eZ\Publish\API\Repository\ContentService */
private $contentService;
/** @var \eZ\Publish\API\Repository\LocationService */
private $locationService;
/** @var \Symfony\Component\Routing\RouterInterface */
private $router;
/**
* @param \eZ\Publish\API\Repository\ContentService $contentService
* @param \eZ\Publish\API\Repository\LocationService $locationService
* @param \Symfony\Component\Routing\RouterInterface $router
*/
public function __construct(
ContentService $contentService,
LocationService $locationService,
RouterInterface $router
) {
$this->contentService = $contentService;
$this->locationService = $locationService;
$this->router = $router;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
RepositoryFormEvents::CONTENT_PUBLISH => ['processPublish', 10],
RepositoryFormEvents::CONTENT_CANCEL => ['processCancel', 10],
RepositoryFormEvents::CONTENT_SAVE_DRAFT => ['processSaveDraft', 10],
RepositoryFormEvents::CONTENT_CREATE_DRAFT => ['processCreateDraft', 10],
];
}
/**
* @param \EzSystems\RepositoryForms\Event\FormActionEvent $event
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
* @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
*/
public function processSaveDraft(FormActionEvent $event)
{
/** @var \EzSystems\RepositoryForms\Data\Content\ContentCreateData|\EzSystems\RepositoryForms\Data\Content\ContentUpdateData $data */
$data = $event->getData();
$form = $event->getForm();
$formConfig = $form->getConfig();
$languageCode = $formConfig->getOption('languageCode');
$draft = $this->saveDraft($data, $languageCode);
$referrerLocation = $event->getOption('referrerLocation');
$contentLocation = $this->resolveLocation($draft, $referrerLocation, $data);
$event->setPayload('content', $draft);
$event->setPayload('is_new', $draft->contentInfo->isDraft());
$defaultUrl = $this->router->generate('ez_content_draft_edit', [
'contentId' => $draft->id,
'versionNo' => $draft->getVersionInfo()->versionNo,
'language' => $languageCode,
'locationId' => null !== $contentLocation ? $contentLocation->id : null,
]);
$event->setResponse(new RedirectResponse($formConfig->getAction() ?: $defaultUrl));
}
/**
* @param \EzSystems\RepositoryForms\Event\FormActionEvent $event
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
* @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
*/
public function processPublish(FormActionEvent $event)
{
/** @var \EzSystems\RepositoryForms\Data\Content\ContentCreateData|\EzSystems\RepositoryForms\Data\Content\ContentUpdateData $data */
$data = $event->getData();
$form = $event->getForm();
$draft = $this->saveDraft($data, $form->getConfig()->getOption('languageCode'));
$versionInfo = $draft->versionInfo;
$content = $this->contentService->publishVersion($versionInfo, [$versionInfo->initialLanguageCode]);
$event->setPayload('content', $content);
$event->setPayload('is_new', $draft->contentInfo->isDraft());
$redirectUrl = $form['redirectUrlAfterPublish']->getData() ?: $this->router->generate(
'ez_urlalias', [
'locationId' => $content->contentInfo->mainLocationId,
]
);
$event->setResponse(new RedirectResponse($redirectUrl));
}
/**
* @param \EzSystems\RepositoryForms\Event\FormActionEvent $event
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function processCancel(FormActionEvent $event)
{
/** @var \EzSystems\RepositoryForms\Data\Content\ContentCreateData|\EzSystems\RepositoryForms\Data\Content\ContentUpdateData $data */
$data = $event->getData();
if ($data->isNew()) {
$response = new RedirectResponse($this->router->generate(
'ez_urlalias',
['locationId' => $data->getLocationStructs()[0]->parentLocationId]
));
$event->setResponse($response);
return;
}
$content = $data->contentDraft;
$contentInfo = $content->contentInfo;
$versionInfo = $data->contentDraft->getVersionInfo();
$event->setPayload('content', $content);
// if there is only one version you have to remove whole content instead of a version itself
if (1 === count($this->contentService->loadVersions($contentInfo))) {
$parentLocation = $this->locationService->loadParentLocationsForDraftContent($versionInfo)[0];
$redirectionLocationId = $parentLocation->id;
$this->contentService->deleteContent($contentInfo);
} else {
$redirectionLocationId = $contentInfo->mainLocationId;
$this->contentService->deleteVersion($versionInfo);
}
$url = $this->router->generate(
'ez_urlalias',
['locationId' => $redirectionLocationId],
UrlGeneratorInterface::ABSOLUTE_URL
);
$event->setResponse(new RedirectResponse($url));
}
/**
* @param \EzSystems\RepositoryForms\Event\FormActionEvent $event
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function processCreateDraft(FormActionEvent $event)
{
/** @var $createContentDraft \EzSystems\RepositoryForms\Data\Content\CreateContentDraftData */
$createContentDraft = $event->getData();
$contentInfo = $this->contentService->loadContentInfo($createContentDraft->contentId);
$versionInfo = $this->contentService->loadVersionInfo($contentInfo, $createContentDraft->fromVersionNo);
$contentDraft = $this->contentService->createContentDraft($contentInfo, $versionInfo);
$referrerLocation = $event->getOption('referrerLocation');
$event->setPayload('content', $contentDraft);
$event->setPayload('is_new', $contentDraft->contentInfo->isDraft());
$contentEditUrl = $this->router->generate('ez_content_draft_edit', [
'contentId' => $contentDraft->id,
'versionNo' => $contentDraft->getVersionInfo()->versionNo,
'language' => $contentDraft->contentInfo->mainLanguageCode,
'locationId' => null !== $referrerLocation ? $referrerLocation->id : null,
]);
$event->setResponse(new RedirectResponse($contentEditUrl));
}
/**
* Saves content draft corresponding to $data.
* Depending on the nature of $data (create or update data), the draft will either be created or simply updated.
*
* @param ContentStruct|\EzSystems\RepositoryForms\Data\Content\ContentCreateData|\EzSystems\RepositoryForms\Data\Content\ContentUpdateData $data
* @param $languageCode
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
* @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
*/
private function saveDraft(ContentStruct $data, $languageCode)
{
$mainLanguageCode = $this->resolveMainLanguageCode($data);
foreach ($data->fieldsData as $fieldDefIdentifier => $fieldData) {
if ($mainLanguageCode != $languageCode && !$fieldData->fieldDefinition->isTranslatable) {
continue;
}
$data->setField($fieldDefIdentifier, $fieldData->value, $languageCode);
}
if ($data->isNew()) {
$contentDraft = $this->contentService->createContent($data, $data->getLocationStructs());
} else {
$contentDraft = $this->contentService->updateContent($data->contentDraft->getVersionInfo(), $data);
}
return $contentDraft;
}
/**
* @param \EzSystems\RepositoryForms\Data\Content\ContentUpdateData|\EzSystems\RepositoryForms\Data\Content\ContentCreateData $data
*
* @return string
*
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
*/
private function resolveMainLanguageCode($data): string
{
if (!$data instanceof ContentUpdateData && !$data instanceof ContentCreateData) {
throw new InvalidArgumentException(
'$data',
'expected type of ContentUpdateData or ContentCreateData'
);
}
return $data->isNew()
? $data->mainLanguageCode
: $data->contentDraft->getVersionInfo()->getContentInfo()->mainLanguageCode;
}
/**
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
* @param \eZ\Publish\API\Repository\Values\Content\Location|null $referrerLocation
* @param \EzSystems\RepositoryForms\Data\NewnessCheckable $data
*
* @return \eZ\Publish\API\Repository\Values\Content\Location|null
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
private function resolveLocation(Content $content, ?Location $referrerLocation, NewnessCheckable $data): ?Location
{
if ($data->isNew() || (!$content->contentInfo->published && null === $content->contentInfo->mainLocationId)) {
return null; // no location exists until new content is published
}
return $referrerLocation ?? $this->locationService->loadLocation($content->contentInfo->mainLocationId);
}
}