|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Netgen\Bundle\InformationCollectionBundle\DataCollector; |
|
6
|
|
|
|
|
7
|
|
|
use eZ\Publish\API\Repository\Repository; |
|
8
|
|
|
use eZ\Publish\API\Repository\Values\ContentType\ContentType; |
|
9
|
|
|
use eZ\Publish\Core\Helper\TranslationHelper; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
12
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
|
13
|
|
|
use Netgen\InformationCollection\Integration\RepositoryForms\InformationCollectionType; |
|
14
|
|
|
|
|
15
|
|
|
class InformationCollectionCollector extends DataCollector |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \eZ\Publish\API\Repository\Repository |
|
19
|
|
|
*/ |
|
20
|
|
|
private $repository; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \eZ\Publish\Core\Helper\TranslationHelper |
|
24
|
|
|
*/ |
|
25
|
|
|
private $translationHelper; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(Repository $repository, TranslationHelper $translationHelper) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->repository = $repository; |
|
30
|
|
|
$this->data = [ |
|
31
|
|
|
'count' => 0, |
|
32
|
|
|
'collections' => [], |
|
33
|
|
|
'content_type' => null, |
|
34
|
|
|
'content' => null, |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
$this->translationHelper = $translationHelper; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function collect(Request $request, Response $response, \Throwable $exception = null) |
|
41
|
|
|
{ |
|
42
|
|
|
if ($request->get(InformationCollectionType::FORM_BLOCK_PREFIX) !== null) { |
|
43
|
|
|
$this->mapCollectedData($request); |
|
44
|
|
|
|
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->data = []; |
|
49
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function reset(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->data = []; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getName(): string |
|
58
|
|
|
{ |
|
59
|
|
|
return 'netgen_information_collection_collector'; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getCollections(): array |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->data['collections'] ?? []; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getCollectionCount(): int |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->data['count'] ?? 0; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getContent(): string |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->data['content']; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getContentId(): int |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->data['content_id']; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getContentType(): string |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->data['content_type']; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getContentTypeId(): int |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->data['content_type_id']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getAdminSiteaccess(): string |
|
93
|
|
|
{ |
|
94
|
|
|
return 'admin'; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getContentTypeGroupId(): int |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->data['content_type_group_id']; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function mapCollectedData(Request $request): void |
|
103
|
|
|
{ |
|
104
|
|
|
$mapped = []; |
|
105
|
|
|
|
|
106
|
|
|
$data = $request->get('information_collection'); |
|
107
|
|
|
|
|
108
|
|
|
$contentId = $data['content_id']; |
|
109
|
|
|
$contentTypeId = intval($data['content_type_id']); |
|
110
|
|
|
/** @var ContentType $contentType */ |
|
111
|
|
|
$contentType = $this->repository->sudo( |
|
112
|
|
|
function(Repository $repository) use ($contentTypeId) { |
|
113
|
|
|
return $repository->getContentTypeService()->loadContentType($contentTypeId); |
|
114
|
|
|
} |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
$content = $this->repository->sudo( |
|
118
|
|
|
function(Repository $repository) use ($contentId) { |
|
119
|
|
|
return $repository->getContentService()->loadContent((int)$contentId); |
|
120
|
|
|
} |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
foreach ($data as $identifier => $datum) { |
|
124
|
|
|
if (is_array($datum) && array_key_exists('value', $datum)) { |
|
125
|
|
|
|
|
126
|
|
|
$fieldDefinition = $contentType->getFieldDefinition($identifier); |
|
127
|
|
|
|
|
128
|
|
|
$mapped['collections'][] = [ |
|
129
|
|
|
'identifier' => $identifier, |
|
130
|
|
|
'value' => $datum['value'], |
|
131
|
|
|
'name' => $this->translationHelper->getTranslatedByMethod($fieldDefinition, 'getName'), |
|
|
|
|
|
|
132
|
|
|
'type' => $fieldDefinition->fieldTypeIdentifier, |
|
133
|
|
|
]; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$mapped['content'] = $this->translationHelper->getTranslatedContentName($content); |
|
138
|
|
|
$mapped['content_id'] = $content->id; |
|
139
|
|
|
$mapped['content_type'] = $this->translationHelper->getTranslatedByMethod($contentType, 'getName'); |
|
140
|
|
|
$mapped['content_type_id'] = $contentType->id; |
|
141
|
|
|
$mapped['content_type_group_id'] = $contentType->getContentTypeGroups()[0]->id; |
|
142
|
|
|
$mapped['count'] = count($mapped['collections']); |
|
143
|
|
|
|
|
144
|
|
|
$this->data = $mapped; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: