1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\InformationCollectionBundle\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
6
|
|
|
use eZ\Publish\Core\MVC\Symfony\Security\Authorization\Attribute; |
7
|
|
|
use eZ\Bundle\EzPublishCoreBundle\Controller; |
|
|
|
|
8
|
|
|
use eZ\Publish\API\Repository\ContentService; |
9
|
|
|
use eZ\Publish\Core\MVC\ConfigResolverInterface; |
10
|
|
|
use Netgen\InformationCollection\API\Persistence\Anonymizer\Anonymizer; |
11
|
|
|
use Netgen\InformationCollection\API\Service\InformationCollection; |
12
|
|
|
use Netgen\InformationCollection\API\Value\Collection; |
13
|
|
|
use Netgen\InformationCollection\API\Value\Filter\CollectionFields; |
14
|
|
|
use Netgen\InformationCollection\API\Value\Filter\Collections; |
15
|
|
|
use Netgen\InformationCollection\API\Value\Filter\ContentId; |
16
|
|
|
use Netgen\InformationCollection\API\Value\Filter\Contents; |
17
|
|
|
use Netgen\InformationCollection\API\Value\Filter\Query; |
18
|
|
|
use Netgen\InformationCollection\API\Value\Filter\SearchQuery; |
19
|
|
|
use Netgen\InformationCollection\Core\Pagination\InformationCollectionCollectionListAdapter; |
20
|
|
|
use Netgen\InformationCollection\Core\Pagination\InformationCollectionCollectionListSearchAdapter; |
21
|
|
|
use Netgen\InformationCollection\Core\Pagination\InformationCollectionContentsAdapter; |
22
|
|
|
use Pagerfanta\Adapter\AdapterInterface; |
23
|
|
|
use Pagerfanta\Pagerfanta; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
26
|
|
|
|
27
|
|
|
class AdminController extends Controller |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var \Netgen\InformationCollection\API\Service\InformationCollection |
31
|
|
|
*/ |
32
|
|
|
protected $service; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \eZ\Publish\API\Repository\ContentService |
36
|
|
|
*/ |
37
|
|
|
protected $contentService; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \eZ\Publish\Core\MVC\ConfigResolverInterface |
41
|
|
|
*/ |
42
|
|
|
protected $configResolver; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var \Netgen\InformationCollection\API\Persistence\Anonymizer\Anonymizer |
46
|
|
|
*/ |
47
|
|
|
protected $anonymizer; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var \Symfony\Contracts\Translation\TranslatorInterface |
51
|
|
|
*/ |
52
|
|
|
private $translator; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* AdminController constructor. |
56
|
|
|
* |
57
|
|
|
* @param \Netgen\InformationCollection\API\Service\InformationCollection $service |
58
|
|
|
* @param \Netgen\InformationCollection\API\Persistence\Anonymizer\Anonymizer $anonymizer |
59
|
|
|
* @param \eZ\Publish\API\Repository\ContentService $contentService |
60
|
|
|
* @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver |
61
|
|
|
* @param \Symfony\Contracts\Translation\TranslatorInterface $translator |
62
|
|
|
*/ |
63
|
|
|
public function __construct( |
64
|
|
|
InformationCollection $service, |
65
|
|
|
Anonymizer $anonymizer, |
66
|
|
|
ContentService $contentService, |
67
|
|
|
ConfigResolverInterface $configResolver, |
68
|
|
|
TranslatorInterface $translator |
69
|
|
|
) |
70
|
|
|
{ |
71
|
|
|
$this->service = $service; |
72
|
|
|
$this->contentService = $contentService; |
73
|
|
|
$this->configResolver = $configResolver; |
74
|
|
|
$this->anonymizer = $anonymizer; |
75
|
|
|
$this->translator = $translator; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Displays overview page |
80
|
|
|
* |
81
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
82
|
|
|
* |
83
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function overviewAction(Request $request) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$this->checkReadPermissions(); |
88
|
|
|
|
89
|
|
|
$adapter = new InformationCollectionContentsAdapter($this->service, Query::countQuery()); |
90
|
|
|
$pager = $this->getPager($adapter, (int) $request->query->get('page')); |
91
|
|
|
|
92
|
|
|
return $this->render("@NetgenInformationCollection/admin/overview.html.twig", ['objects' => $pager]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Displays list of collection for selected Content |
97
|
|
|
* |
98
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
99
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Content $content |
100
|
|
|
* |
101
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function collectionListAction(Request $request, Content $content) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$this->checkReadPermissions(); |
106
|
|
|
|
107
|
|
|
$adapter = new InformationCollectionCollectionListAdapter($this->service, ContentId::withContentId($content->id)); |
108
|
|
|
$pager = $this->getPager($adapter, (int)$request->query->get('page')); |
109
|
|
|
|
110
|
|
|
return $this->render("@NetgenInformationCollection/admin/collection_list.html.twig", [ |
111
|
|
|
'objects' => $pager, |
112
|
|
|
'content' => $content, |
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Handles collection search |
118
|
|
|
* |
119
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
120
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Content $content |
121
|
|
|
* |
122
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
123
|
|
|
*/ |
124
|
|
|
public function searchAction(Request $request, Content $content) |
125
|
|
|
{ |
126
|
|
|
$this->checkReadPermissions(); |
127
|
|
|
|
128
|
|
|
$query = SearchQuery::withContentAndSearchText($content->id, $request->query->get('searchText')); |
129
|
|
|
|
130
|
|
|
$adapter = new InformationCollectionCollectionListSearchAdapter($this->service, $query); |
131
|
|
|
$pager = $this->getPager($adapter, (int)$request->query->get('page')); |
132
|
|
|
|
133
|
|
|
return $this->render("@NetgenInformationCollection/admin/collection_list.html.twig", |
134
|
|
|
[ |
135
|
|
|
'objects' => $pager, |
136
|
|
|
'content' => $content, |
137
|
|
|
] |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Displays individual collection details |
143
|
|
|
* |
144
|
|
|
* @param \Netgen\InformationCollection\API\Value\Collection $collection |
145
|
|
|
* |
146
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
147
|
|
|
*/ |
148
|
|
|
public function viewAction(Collection $collection) |
149
|
|
|
{ |
150
|
|
|
$this->checkReadPermissions(); |
151
|
|
|
|
152
|
|
|
return $this->render("@NetgenInformationCollection/admin/view.html.twig", [ |
153
|
|
|
'collection' => $collection, |
154
|
|
|
'content' => $collection->getContent(), |
155
|
|
|
]); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Handles actions performed on overview page |
160
|
|
|
* |
161
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
162
|
|
|
* |
163
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
164
|
|
|
*/ |
165
|
|
|
public function handleContentsAction(Request $request) |
166
|
|
|
{ |
167
|
|
|
$this->checkReadPermissions(); |
168
|
|
|
|
169
|
|
|
$contents = $request->request->get('ContentId', []); |
|
|
|
|
170
|
|
|
$count = count($contents); |
171
|
|
|
|
172
|
|
|
if (empty($contents)) { |
173
|
|
|
$this->addFlashMessage('errors', 'contents_not_selected'); |
174
|
|
|
|
175
|
|
|
return $this->redirectToRoute('netgen_information_collection.route.admin.overview'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if ($request->request->has('DeleteCollectionByContentAction')) { |
179
|
|
|
|
180
|
|
|
$this->checkDeletePermissions(); |
181
|
|
|
|
182
|
|
|
$query = new Contents($contents); |
183
|
|
|
|
184
|
|
|
$this->service->deleteCollectionByContent($query); |
185
|
|
|
|
186
|
|
|
$this->addFlashMessage('success', 'content_removed', $count); |
187
|
|
|
|
188
|
|
|
return $this->redirectToRoute('netgen_information_collection.route.admin.overview'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->addFlashMessage('error', 'something_went_wrong'); |
192
|
|
|
|
193
|
|
|
return $this->redirectToRoute('netgen_information_collection.route.admin.overview'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Handles actions performed on collection list page |
198
|
|
|
* |
199
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
200
|
|
|
* |
201
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
202
|
|
|
*/ |
203
|
|
|
public function handleCollectionListAction(Request $request) |
204
|
|
|
{ |
205
|
|
|
$this->checkReadPermissions(); |
206
|
|
|
|
207
|
|
|
$contentId = $request->request->get('ContentId'); |
208
|
|
|
$collections = $request->request->get('CollectionId', []); |
|
|
|
|
209
|
|
|
$count = count($collections); |
210
|
|
|
|
211
|
|
|
if (empty($collections)) { |
212
|
|
|
$this->addFlashMessage('errors', 'collections_not_selected'); |
213
|
|
|
|
214
|
|
|
return $this->redirectToRoute('netgen_information_collection.route.admin.collection_list', ['contentId' => $contentId]); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if ($request->request->has('DeleteCollectionAction')) { |
218
|
|
|
|
219
|
|
|
$this->checkDeletePermissions(); |
220
|
|
|
|
221
|
|
|
$query = new Collections($contentId, $collections); |
222
|
|
|
|
223
|
|
|
$this->service->deleteCollections($query); |
224
|
|
|
|
225
|
|
|
$this->addFlashMessage('success', 'collection_removed', $count); |
226
|
|
|
|
227
|
|
|
return $this->redirectToRoute('netgen_information_collection.route.admin.collection_list', ['contentId' => $contentId]); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if ($request->request->has('AnonymizeCollectionAction')) { |
231
|
|
|
|
232
|
|
|
$this->checkAnonymizePermissions(); |
233
|
|
|
|
234
|
|
|
foreach ($collections as $collection) { |
235
|
|
|
$this->anonymizer->anonymizeCollection($collection); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$this->addFlashMessage('success', 'collection_anonymized', $count); |
239
|
|
|
|
240
|
|
|
return $this->redirectToRoute('netgen_information_collection.route.admin.collection_list', ['contentId' => $contentId]); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$this->addFlashMessage('error', 'something_went_wrong'); |
244
|
|
|
|
245
|
|
|
return $this->redirectToRoute('netgen_information_collection.route.admin.collection_list', ['contentId' => $contentId]); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Handles action on collection details page |
250
|
|
|
* |
251
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
252
|
|
|
* |
253
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
254
|
|
|
*/ |
255
|
|
|
public function handleCollectionAction(Request $request) |
256
|
|
|
{ |
257
|
|
|
$this->checkReadPermissions(); |
258
|
|
|
|
259
|
|
|
$collectionId = $request->request->get('CollectionId'); |
260
|
|
|
$contentId = $request->request->get('ContentId'); |
261
|
|
|
$fields = $request->request->get('FieldId', []); |
|
|
|
|
262
|
|
|
$count = count($fields); |
263
|
|
|
|
264
|
|
|
if ( |
265
|
|
|
($request->request->has('AnonymizeFieldAction') || $request->request->has('DeleteFieldAction')) |
266
|
|
|
&& empty($fields) |
267
|
|
|
) { |
268
|
|
|
$this->addFlashMessage('errors', 'fields_not_selected'); |
269
|
|
|
|
270
|
|
|
return $this->redirectToRoute('netgen_information_collection.route.admin.view', ['collectionId' => $collectionId]); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
View Code Duplication |
if ($request->request->has('DeleteFieldAction')) { |
|
|
|
|
274
|
|
|
|
275
|
|
|
$this->checkDeletePermissions(); |
276
|
|
|
|
277
|
|
|
$query = new CollectionFields($contentId, $collectionId, $fields); |
278
|
|
|
|
279
|
|
|
$this->service->deleteCollectionFields($query); |
280
|
|
|
|
281
|
|
|
$this->addFlashMessage('success', 'field_removed', $count); |
282
|
|
|
|
283
|
|
|
return $this->redirectToRoute('netgen_information_collection.route.admin.view', ['collectionId' => $collectionId]); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
View Code Duplication |
if ($request->request->has('AnonymizeFieldAction')) { |
|
|
|
|
287
|
|
|
|
288
|
|
|
$this->checkAnonymizePermissions(); |
289
|
|
|
|
290
|
|
|
$this->anonymizer->anonymizeCollection($collectionId, $fields); |
291
|
|
|
|
292
|
|
|
$this->addFlashMessage('success', 'field_anonymized', $count); |
293
|
|
|
|
294
|
|
|
return $this->redirectToRoute('netgen_information_collection.route.admin.view', ['collectionId' => $collectionId]); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
View Code Duplication |
if ($request->request->has('DeleteCollectionAction')) { |
|
|
|
|
298
|
|
|
|
299
|
|
|
$this->checkDeletePermissions(); |
300
|
|
|
|
301
|
|
|
$query = new Collections($contentId, [$collectionId]); |
302
|
|
|
$this->service->deleteCollections($query); |
303
|
|
|
|
304
|
|
|
$this->addFlashMessage("success", "collection_removed"); |
305
|
|
|
|
306
|
|
|
return $this->redirectToRoute('netgen_information_collection.route.admin.collection_list', ['contentId' => $contentId]); |
307
|
|
|
|
308
|
|
|
} |
309
|
|
|
|
310
|
|
View Code Duplication |
if ($request->request->has('AnonymizeCollectionAction')) { |
|
|
|
|
311
|
|
|
|
312
|
|
|
$this->checkAnonymizePermissions(); |
313
|
|
|
|
314
|
|
|
$this->anonymizer->anonymizeCollection($collectionId); |
315
|
|
|
|
316
|
|
|
$this->addFlashMessage("success", "collection_anonymized"); |
317
|
|
|
|
318
|
|
|
return $this->redirectToRoute('netgen_information_collection.route.admin.view', ['collectionId' => $collectionId]); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
$this->addFlashMessage('error', 'something_went_wrong'); |
322
|
|
|
|
323
|
|
|
return $this->redirectToRoute('netgen_information_collection.route.admin.view', ['collectionId' => $collectionId]); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Adds a flash message with specified parameters. |
328
|
|
|
* |
329
|
|
|
* @param string $messageType |
330
|
|
|
* @param string $message |
331
|
|
|
* @param int $count |
332
|
|
|
* @param array $parameters |
333
|
|
|
*/ |
334
|
|
|
protected function addFlashMessage(string $messageType, string $message, int $count = 1, array $parameters = array()) |
335
|
|
|
{ |
336
|
|
|
$parameters = array_merge($parameters, ['count' => $count]); |
337
|
|
|
|
338
|
|
|
$this->addFlash( |
339
|
|
|
'netgen_information_collection.' . $messageType, |
340
|
|
|
$this->translator->trans( |
341
|
|
|
$messageType . '.' . $message, |
342
|
|
|
$parameters, |
343
|
|
|
'netgen_information_collection_flash' |
344
|
|
|
) |
345
|
|
|
); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Returns configured instance of Pagerfanta |
350
|
|
|
* |
351
|
|
|
* @param \Pagerfanta\Adapter\AdapterInterface $adapter |
352
|
|
|
* @param int $currentPage |
353
|
|
|
* |
354
|
|
|
* @return \Pagerfanta\Pagerfanta |
355
|
|
|
*/ |
356
|
|
|
protected function getPager(AdapterInterface $adapter, int $currentPage): Pagerfanta |
357
|
|
|
{ |
358
|
|
|
$currentPage = (int) $currentPage; |
359
|
|
|
$pager = new Pagerfanta($adapter); |
360
|
|
|
$pager->setNormalizeOutOfRangePages(true); |
361
|
|
|
$pager->setMaxPerPage( |
362
|
|
|
$this->configResolver->getParameter('admin.max_per_page', 'netgen_information_collection') |
363
|
|
|
); |
364
|
|
|
$pager->setCurrentPage($currentPage > 0 ? $currentPage : 1); |
365
|
|
|
|
366
|
|
|
return $pager; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
protected function checkReadPermissions(): void |
370
|
|
|
{ |
371
|
|
|
$attribute = new Attribute('infocollector', 'read'); |
372
|
|
|
$this->denyAccessUnlessGranted($attribute); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
protected function checkDeletePermissions(): void |
376
|
|
|
{ |
377
|
|
|
$attribute = new Attribute('infocollector', 'delete'); |
378
|
|
|
$this->denyAccessUnlessGranted($attribute); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
protected function checkAnonymizePermissions(): void |
382
|
|
|
{ |
383
|
|
|
$attribute = new Attribute('infocollector', 'anonymize'); |
384
|
|
|
$this->denyAccessUnlessGranted($attribute); |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: