1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\EzFormsBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use eZ\Bundle\EzPublishCoreBundle\Controller; |
7
|
|
|
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException; |
8
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\DataWrapper; |
9
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\Payload\InformationCollectionStruct; |
10
|
|
|
use eZ\Publish\API\Repository\ContentService; |
11
|
|
|
use eZ\Bundle\EzPublishCoreBundle\Controller; |
|
|
|
|
12
|
|
|
use eZ\Publish\API\Repository\LocationService; |
13
|
|
|
use eZ\Publish\API\Repository\Repository; |
14
|
|
|
use eZ\Publish\API\Repository\UserService; |
15
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\Type\CreateContentType; |
16
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\Type\CreateUserType; |
17
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\Type\InformationCollectionType; |
18
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\Type\UpdateContentType; |
19
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\Type\UpdateUserType; |
20
|
|
|
use RuntimeException; |
21
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
22
|
|
|
use Symfony\Component\Form\FormError; |
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
24
|
|
|
|
25
|
|
|
class DemoController extends Controller |
26
|
|
|
{ |
27
|
|
|
public function demoCreateContentAction(Request $request) |
28
|
|
|
{ |
29
|
|
|
/** @var Repository $repository */ |
30
|
|
|
$repository = $this->getRepository(); |
31
|
|
|
/** @var ContentService $contentService */ |
32
|
|
|
$contentService = $repository->getContentService(); |
33
|
|
|
/** @var LocationService $locationService */ |
34
|
|
|
$locationService = $repository->getLocationService(); |
35
|
|
|
// @todo for demo purpose, user should have necessary permissions by itself |
36
|
|
|
$repository->setCurrentUser( |
37
|
|
|
$repository->getUserService()->loadUserByLogin('admin') |
38
|
|
|
); |
39
|
|
|
$contentType = $repository->getContentTypeService() |
40
|
|
|
->loadContentTypeByIdentifier('test_type'); |
41
|
|
|
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB'); |
42
|
|
|
|
43
|
|
|
$data = new DataWrapper($contentCreateStruct, $contentCreateStruct->contentType); |
44
|
|
|
|
45
|
|
|
/** @var $formBuilder \Symfony\Component\Form\FormBuilderInterface */ |
46
|
|
|
$formBuilder = $this->container->get('form.factory') |
47
|
|
|
->createBuilder(CreateContentType::class, $data); |
48
|
|
|
|
49
|
|
|
$formBuilder->add('save', SubmitType::class, array('label' => 'Publish')); |
50
|
|
|
|
51
|
|
|
$form = $formBuilder->getForm(); |
52
|
|
|
$form->handleRequest($request); |
53
|
|
|
|
54
|
|
|
if ($form->isValid()) { |
55
|
|
|
$rootLocation = $locationService->loadLocation(2); |
56
|
|
|
|
57
|
|
|
try { |
58
|
|
|
$repository->beginTransaction(); |
59
|
|
|
|
60
|
|
|
$contentDraft = $contentService->createContent( |
61
|
|
|
$data->payload, |
62
|
|
|
array( |
63
|
|
|
$locationService->newLocationCreateStruct($rootLocation->id), |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$content = $contentService->publishVersion($contentDraft->versionInfo); |
68
|
|
|
|
69
|
|
|
$repository->commit(); |
70
|
|
|
} catch (Exception $e) { |
71
|
|
|
$repository->rollback(); |
72
|
|
|
// @todo do something else if needed |
73
|
|
|
throw $e; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->redirect( |
77
|
|
|
$this->generateUrl( |
78
|
|
|
$locationService->loadLocation( |
79
|
|
|
$content->contentInfo->mainLocationId |
80
|
|
|
) |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->render( |
86
|
|
|
'AcmeBundle::demo_create_content.html.twig', |
87
|
|
|
array( |
88
|
|
|
'form' => $form->createView(), |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function demoUpdateContentAction(Request $request) |
94
|
|
|
{ |
95
|
|
|
/** @var Repository $repository */ |
96
|
|
|
$repository = $this->getRepository(); |
97
|
|
|
/** @var ContentService $contentService */ |
98
|
|
|
$contentService = $repository->getContentService(); |
99
|
|
|
$content = $contentService->loadContent(137); |
100
|
|
|
$contentType = $repository->getContentTypeService() |
101
|
|
|
->loadContentType($content->contentInfo->contentTypeId); |
102
|
|
|
$contentUpdateStruct = $contentService->newContentUpdateStruct(); |
103
|
|
|
$contentUpdateStruct->initialLanguageCode = 'eng-GB'; |
104
|
|
|
|
105
|
|
|
$data = new DataWrapper($contentUpdateStruct, $contentType, $content); |
106
|
|
|
|
107
|
|
|
/** @var $formBuilder \Symfony\Component\Form\FormBuilderInterface */ |
108
|
|
|
$formBuilder = $this->container->get('form.factory') |
109
|
|
|
->createBuilder(UpdateContentType::class, $data); |
110
|
|
|
|
111
|
|
|
$formBuilder->add('save', SubmitType::class, array('label' => 'Update')); |
112
|
|
|
|
113
|
|
|
$form = $formBuilder->getForm(); |
114
|
|
|
$form->handleRequest($request); |
115
|
|
|
|
116
|
|
|
if ($form->isValid()) { |
117
|
|
|
try { |
118
|
|
|
$repository->beginTransaction(); |
119
|
|
|
|
120
|
|
|
$contentDraft = $contentService->createContentDraft($content->contentInfo); |
121
|
|
|
$contentDraft = $contentService->updateContent( |
122
|
|
|
$contentDraft->versionInfo, |
123
|
|
|
$data->payload |
124
|
|
|
); |
125
|
|
|
$content = $contentService->publishVersion($contentDraft->versionInfo); |
126
|
|
|
|
127
|
|
|
$repository->commit(); |
128
|
|
|
} catch (Exception $e) { |
129
|
|
|
$repository->rollback(); |
130
|
|
|
|
131
|
|
|
throw $e; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->redirect( |
135
|
|
|
$this->generateUrl( |
136
|
|
|
$this->getRepository()->getLocationService()->loadLocation( |
137
|
|
|
$content->contentInfo->mainLocationId |
138
|
|
|
) |
139
|
|
|
) |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $this->render( |
144
|
|
|
'AcmeBundle::demo_update_content.html.twig', |
145
|
|
|
array( |
146
|
|
|
'form' => $form->createView(), |
147
|
|
|
) |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function demoCreateUserAction(Request $request) |
152
|
|
|
{ |
153
|
|
|
/** @var Repository $repository */ |
154
|
|
|
$repository = $this->getRepository(); |
155
|
|
|
/** @var UserService $userService */ |
156
|
|
|
$userService = $repository->getUserService(); |
157
|
|
|
|
158
|
|
|
$contentType = $repository->getContentTypeService() |
159
|
|
|
->loadContentTypeByIdentifier('user'); |
160
|
|
|
$userCreateStruct = $userService->newUserCreateStruct( |
161
|
|
|
null, |
162
|
|
|
null, |
163
|
|
|
null, |
164
|
|
|
'eng-GB', |
165
|
|
|
$contentType |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
$userCreateStruct->enabled = false; |
169
|
|
|
|
170
|
|
|
$data = new DataWrapper($userCreateStruct, $userCreateStruct->contentType); |
171
|
|
|
|
172
|
|
|
/** @var $formBuilder \Symfony\Component\Form\FormBuilderInterface */ |
173
|
|
|
$formBuilder = $this->container->get('form.factory')->createBuilder(CreateUserType::class, $data); |
174
|
|
|
|
175
|
|
|
$formBuilder->add('save', SubmitType::class, array('label' => 'Publish')); |
176
|
|
|
|
177
|
|
|
$form = $formBuilder->getForm(); |
178
|
|
|
$form->handleRequest($request); |
179
|
|
|
|
180
|
|
|
if ($form->isValid()) { |
181
|
|
|
|
182
|
|
|
$userGroup = $userService->loadUserGroup(13); |
183
|
|
|
|
184
|
|
|
try { |
185
|
|
|
$user = $userService->createUser( |
186
|
|
|
$data->payload, |
187
|
|
|
array($userGroup) |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
// @todo send confirmation email and redirect to proper location (enter confirmation code or something) |
191
|
|
|
|
192
|
|
|
return $this->redirect( |
193
|
|
|
$this->generateUrl( |
194
|
|
|
$this->getRepository()->getLocationService()->loadLocation( |
195
|
|
|
$user->contentInfo->mainLocationId |
196
|
|
|
) |
197
|
|
|
) |
198
|
|
|
); |
199
|
|
|
} catch (InvalidArgumentException $e) { |
200
|
|
|
// There is no better way to do this ATM... |
201
|
|
|
$existingUsernameMessage = "Argument 'userCreateStruct' is invalid: User with provided login already exists"; |
202
|
|
|
if ($e->getMessage() === $existingUsernameMessage) { |
203
|
|
|
// Search for the first ezuser field type in content type |
204
|
|
|
foreach ($userCreateStruct->contentType->getFieldDefinitions() as $fieldDefinition) { |
205
|
|
|
if ($fieldDefinition->fieldTypeIdentifier === 'ezuser') { |
206
|
|
|
$userFieldDefinition = $fieldDefinition; |
207
|
|
|
break; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
// UserService validates for this, but it happens AFTER existing username validation |
212
|
|
|
if (!isset($userFieldDefinition)) { |
213
|
|
|
throw new RuntimeException("Could not find 'ezuser' field."); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$form->get($userFieldDefinition->identifier)->addError( |
217
|
|
|
new FormError('User with provided username already exists.') |
218
|
|
|
); |
219
|
|
|
} else { |
220
|
|
|
// @todo do something else if needed |
221
|
|
|
throw $e; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $this->render( |
227
|
|
|
'AcmeBundle::demo_create_user.html.twig', |
228
|
|
|
array( |
229
|
|
|
'form' => $form->createView(), |
230
|
|
|
) |
231
|
|
|
); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function demoUpdateUserAction(Request $request) |
235
|
|
|
{ |
236
|
|
|
/** @var Repository $repository */ |
237
|
|
|
$repository = $this->getRepository(); |
238
|
|
|
/** @var UserService $userService */ |
239
|
|
|
$userService = $repository->getUserService(); |
240
|
|
|
/** @var ContentService $contentService */ |
241
|
|
|
$contentService = $repository->getContentService(); |
242
|
|
|
|
243
|
|
|
$user = $userService->loadUser(142); |
244
|
|
|
$contentType = $repository->getContentTypeService()->loadContentTypeByIdentifier('user'); |
245
|
|
|
$contentUpdateStruct = $contentService->newContentUpdateStruct(); |
246
|
|
|
$contentUpdateStruct->initialLanguageCode = 'eng-GB'; |
247
|
|
|
$userUpdateStruct = $userService->newUserUpdateStruct(); |
248
|
|
|
$userUpdateStruct->contentUpdateStruct = $contentUpdateStruct; |
249
|
|
|
|
250
|
|
|
$data = new DataWrapper($userUpdateStruct, $contentType, $user); |
251
|
|
|
|
252
|
|
|
/** @var $formBuilder \Symfony\Component\Form\FormBuilderInterface */ |
253
|
|
|
$formBuilder = $this->container->get('form.factory')->createBuilder(UpdateUserType::class, $data); |
254
|
|
|
|
255
|
|
|
$formBuilder->add('save', SubmitType::class, array('label' => 'Update')); |
256
|
|
|
|
257
|
|
|
$form = $formBuilder->getForm(); |
258
|
|
|
$form->handleRequest($request); |
259
|
|
|
|
260
|
|
|
if ($form->isValid()) { |
261
|
|
|
$user = $userService->updateUser($user, $userUpdateStruct); |
262
|
|
|
|
263
|
|
|
return $this->redirect( |
264
|
|
|
$this->generateUrl( |
265
|
|
|
$this->getRepository()->getLocationService()->loadLocation( |
266
|
|
|
$user->contentInfo->mainLocationId |
267
|
|
|
) |
268
|
|
|
) |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $this->render( |
273
|
|
|
'AcmeBundle::demo_update_user.html.twig', |
274
|
|
|
array( |
275
|
|
|
'form' => $form->createView(), |
276
|
|
|
) |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function demoInformationCollectionAction(Request $request) |
281
|
|
|
{ |
282
|
|
|
$repository = $this->getRepository(); |
283
|
|
|
$contentService = $repository->getContentService(); |
284
|
|
|
// @todo for demo purpose, user should have necessary permissions by itself |
285
|
|
|
$repository->setCurrentUser( |
286
|
|
|
$repository->getUserService()->loadUserByLogin('admin') |
287
|
|
|
); |
288
|
|
|
|
289
|
|
|
$content = $contentService->loadContent(126); |
290
|
|
|
$contentTypeId = $content->versionInfo->contentInfo->contentTypeId; |
291
|
|
|
$contentType = $repository->getContentTypeService()->loadContentType($contentTypeId); |
292
|
|
|
|
293
|
|
|
$informationCollection = new InformationCollectionStruct(); |
294
|
|
|
|
295
|
|
|
$data = new DataWrapper($informationCollection, $contentType); |
296
|
|
|
|
297
|
|
|
// No method to create named builder in framework controller |
298
|
|
|
/** @var $formBuilder \Symfony\Component\Form\FormBuilderInterface */ |
299
|
|
|
$formBuilder = $this->container->get('form.factory')->createBuilder(InformationCollectionType::class, $data); |
300
|
|
|
// Adding controls as EzFormsBundle does not do that by itself |
301
|
|
|
$formBuilder->add('save', SubmitType::class, array('label' => 'Publish')); |
302
|
|
|
|
303
|
|
|
$form = $formBuilder->getForm(); |
304
|
|
|
$form->handleRequest($request); |
305
|
|
|
|
306
|
|
|
if ($form->isValid()) { |
307
|
|
|
/** @var InformationCollectionStruct $data */ |
308
|
|
|
$data = $form->getData()->payload; |
309
|
|
|
// save data to database |
310
|
|
|
// or something else |
311
|
|
|
// this is left for end developer |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
return $this->render( |
315
|
|
|
'NetgenEzFormsBundle::demo_form.html.twig', |
316
|
|
|
array( |
317
|
|
|
'form' => $form->createView(), |
318
|
|
|
) |
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|