-
Notifications
You must be signed in to change notification settings - Fork 28
/
ContentEditController.php
204 lines (181 loc) · 7.64 KB
/
ContentEditController.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
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
<?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.
* @version //autogentag//
*/
namespace EzSystems\RepositoryFormsBundle\Controller;
use eZ\Bundle\EzPublishCoreBundle\Controller;
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
use eZ\Publish\Core\Base\Exceptions\BadStateException;
use EzSystems\RepositoryForms\Content\View\ContentCreateDraftView;
use EzSystems\RepositoryForms\Content\View\ContentEditView;
use EzSystems\RepositoryForms\Data\Content\CreateContentDraftData;
use EzSystems\RepositoryForms\Data\Mapper\ContentCreateMapper;
use EzSystems\RepositoryForms\Data\Mapper\ContentUpdateMapper;
use EzSystems\RepositoryForms\Form\ActionDispatcher\ActionDispatcherInterface;
use EzSystems\RepositoryForms\Form\Type\Content\ContentDraftCreateType;
use EzSystems\RepositoryForms\Form\Type\Content\ContentEditType;
use Symfony\Component\HttpFoundation\Request;
class ContentEditController extends Controller
{
/**
* @var ContentTypeService
*/
private $contentTypeService;
/**
* @var ContentService
*/
private $contentService;
/**
* @var LocationService
*/
private $locationService;
/**
* @var ActionDispatcherInterface
*/
private $contentActionDispatcher;
/**
* @var string
*
* @deprecated Deprecated since 1.10 and will be removed in 2.0. See setPagelayout().
*/
private $pagelayout;
public function __construct(
ContentTypeService $contentTypeService,
ContentService $contentService,
LocationService $locationService,
ActionDispatcherInterface $contentActionDispatcher
) {
$this->contentTypeService = $contentTypeService;
$this->locationService = $locationService;
$this->contentActionDispatcher = $contentActionDispatcher;
$this->contentService = $contentService;
}
/**
* Displays and processes a content creation form. Showing the form does not create a draft in the repository.
*
* @param int $contentTypeIdentifier ContentType id to create
* @param string $language Language code to create the content in (eng-GB, ger-DE, ...))
* @param int $parentLocationId Location the content should be a child of
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \EzSystems\RepositoryForms\Content\View\ContentEditView|\Symfony\Component\HttpFoundation\Response
*/
public function createWithoutDraftAction($contentTypeIdentifier, $language, $parentLocationId, Request $request)
{
$contentType = $this->contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier);
$data = (new ContentCreateMapper())->mapToFormData($contentType, [
'mainLanguageCode' => $language,
'parentLocation' => $this->locationService->newLocationCreateStruct($parentLocationId),
]);
$form = $this->createForm(ContentEditType::class, $data, ['languageCode' => $language]);
$form->handleRequest($request);
if ($form->isValid()) {
$this->contentActionDispatcher->dispatchFormAction($form, $data, $form->getClickedButton()->getName());
if ($response = $this->contentActionDispatcher->getResponse()) {
return $response;
}
}
return new ContentEditView(null, [
'form' => $form->createView(),
'languageCode' => $language,
]);
}
/**
* Displays a draft creation form that creates a content draft from an existing content.
*
* @param mixed $contentId
* @param int $fromVersionNo
* @param string $fromLanguage
* @param string $toLanguage
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \EzSystems\RepositoryForms\Content\View\ContentCreateDraftView|\Symfony\Component\HttpFoundation\Response
*/
public function createContentDraftAction($contentId, $fromVersionNo = null, $fromLanguage = null, $toLanguage = null, Request $request)
{
$createContentDraft = new CreateContentDraftData();
if ($contentId !== null) {
$createContentDraft->contentId = $contentId;
$contentInfo = $this->contentService->loadContentInfo($contentId);
$createContentDraft->fromVersionNo = $fromVersionNo ?: $contentInfo->currentVersionNo;
$createContentDraft->fromLanguage = $fromLanguage ?: $contentInfo->mainLanguageCode;
}
$form = $this->createForm(
ContentDraftCreateType::class,
$createContentDraft,
[
'action' => $this->generateUrl('ez_content_draft_create'),
]
);
$form->handleRequest($request);
if ($form->isValid()) {
$this->contentActionDispatcher->dispatchFormAction($form, $createContentDraft, $form->getClickedButton()->getName());
if ($response = $this->contentActionDispatcher->getResponse()) {
return $response;
}
}
return new ContentCreateDraftView(null, ['form' => $form->createView()]);
}
/**
* Shows a content draft editing form.
*
* @param int $contentId ContentType id to create
* @param int $versionNo Version number the version should be created from. Defaults to the currently published one.
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string $language Language code to create the version in (eng-GB, ger-DE, ...))
*
* @return \EzSystems\RepositoryForms\Content\View\ContentEditView|\Symfony\Component\HttpFoundation\Response
* @throws \eZ\Publish\Core\Base\Exceptions\BadStateException If the version isn't editable, or if there is no editable version.
*/
public function editContentDraftAction($contentId, $versionNo = null, Request $request, $language = null)
{
$draft = $this->contentService->loadContent($contentId, [$language], $versionNo);
if ($draft->getVersionInfo()->status !== VersionInfo::STATUS_DRAFT) {
throw new BadStateException('Version status', 'status is not draft');
}
$contentUpdate = (new ContentUpdateMapper())->mapToFormData(
$draft,
[
'languageCode' => $language,
'contentType' => $this->contentTypeService->loadContentType($draft->contentInfo->contentTypeId),
]
);
$form = $this->createForm(
ContentEditType::class,
$contentUpdate,
[
'languageCode' => $language,
'drafts_enabled' => true,
]
);
$form->handleRequest($request);
if ($form->isValid()) {
$this->contentActionDispatcher->dispatchFormAction($form, $contentUpdate, $form->getClickedButton()->getName());
if ($response = $this->contentActionDispatcher->getResponse()) {
return $response;
}
}
return new ContentEditView(null, [
'form' => $form->createView(),
'languageCode' => $language,
]);
}
/**
* @param string $pagelayout
* @return ContentEditController
*
* @deprecated Deprecated since 1.10 and will be removed in 2.0. Pagelayout is injected via ViewTemplatesListener.
*/
public function setPagelayout($pagelayout)
{
$this->pagelayout = $pagelayout;
return $this;
}
}