1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Service; |
6
|
|
|
|
7
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
8
|
|
|
use eZ\Publish\API\Repository\Repository; |
9
|
|
|
use eZ\Publish\API\Repository\Values\User\UserReference; |
10
|
|
|
use Netgen\InformationCollection\API\Exception\PersistingFailedException; |
11
|
|
|
use Netgen\InformationCollection\API\Exception\StoringAttributeFailedException; |
12
|
|
|
use Netgen\InformationCollection\API\Exception\StoringCollectionFailedException; |
13
|
|
|
use Netgen\InformationCollection\API\Service\InformationCollection; |
14
|
|
|
use Netgen\InformationCollection\API\Value\Collection; |
15
|
|
|
use Netgen\InformationCollection\API\Value\CollectionCount; |
16
|
|
|
use Netgen\InformationCollection\API\Value\Collections; |
17
|
|
|
use Netgen\InformationCollection\API\Value\ContentsWithCollections; |
18
|
|
|
use Netgen\InformationCollection\API\Value\Filter\CollectionFields; |
19
|
|
|
use Netgen\InformationCollection\API\Value\Filter\CollectionId; |
20
|
|
|
use Netgen\InformationCollection\API\Value\Filter\Collections as FilterCollections; |
21
|
|
|
use Netgen\InformationCollection\API\Value\Filter\ContentId; |
22
|
|
|
use Netgen\InformationCollection\API\Value\Filter\Contents; |
23
|
|
|
use Netgen\InformationCollection\API\Value\Filter\Query; |
24
|
|
|
use Netgen\InformationCollection\API\Value\Filter\SearchCountQuery; |
25
|
|
|
use Netgen\InformationCollection\API\Value\Filter\SearchQuery; |
26
|
|
|
use Netgen\InformationCollection\API\Value\InformationCollectionStruct; |
27
|
|
|
use Netgen\InformationCollection\API\Value\ObjectCount; |
28
|
|
|
use Netgen\InformationCollection\API\Value\SearchCount; |
29
|
|
|
use Netgen\InformationCollection\Core\Factory\FieldDataFactory; |
30
|
|
|
use Netgen\InformationCollection\Core\Mapper\DomainObjectMapper; |
31
|
|
|
use Netgen\InformationCollection\Core\Persistence\Gateway\DoctrineDatabase; |
32
|
|
|
use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository; |
33
|
|
|
use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository; |
34
|
|
|
|
35
|
|
|
class InformationCollectionService implements InformationCollection |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository |
39
|
|
|
*/ |
40
|
|
|
protected $ezInfoCollectionRepository; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository |
44
|
|
|
*/ |
45
|
|
|
protected $ezInfoCollectionAttributeRepository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \eZ\Publish\API\Repository\Repository |
49
|
|
|
*/ |
50
|
|
|
protected $repository; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \Netgen\InformationCollection\Core\Persistence\Gateway\DoctrineDatabase |
54
|
|
|
*/ |
55
|
|
|
protected $gateway; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var \Netgen\InformationCollection\Core\Factory\FieldDataFactory |
59
|
|
|
*/ |
60
|
|
|
protected $fieldsFactory; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var \Netgen\InformationCollection\Core\Mapper\DomainObjectMapper |
64
|
|
|
*/ |
65
|
|
|
protected $objectMapper; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* InformationCollectionService constructor. |
69
|
|
|
* |
70
|
|
|
* @param \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository $ezInfoCollectionRepository |
71
|
|
|
* @param \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository $ezInfoCollectionAttributeRepository |
72
|
|
|
* @param \eZ\Publish\API\Repository\Repository $repository |
73
|
|
|
* @param \Netgen\InformationCollection\Core\Persistence\Gateway\DoctrineDatabase $gateway |
74
|
|
|
*/ |
75
|
|
|
public function __construct( |
76
|
|
|
EzInfoCollectionRepository $ezInfoCollectionRepository, |
77
|
|
|
EzInfoCollectionAttributeRepository $ezInfoCollectionAttributeRepository, |
78
|
|
|
Repository $repository, |
79
|
|
|
DoctrineDatabase $gateway, |
80
|
|
|
FieldDataFactory $factory, |
81
|
|
|
DomainObjectMapper $objectMapper |
82
|
|
|
) { |
83
|
|
|
$this->ezInfoCollectionRepository = $ezInfoCollectionRepository; |
84
|
|
|
$this->ezInfoCollectionAttributeRepository = $ezInfoCollectionAttributeRepository; |
85
|
|
|
$this->repository = $repository; |
86
|
|
|
$this->gateway = $gateway; |
87
|
|
|
$this->fieldsFactory = $factory; |
88
|
|
|
$this->objectMapper = $objectMapper; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function createCollection(InformationCollectionStruct $struct): void |
92
|
|
|
{ |
93
|
|
|
$contentType = $struct->getContentType(); |
94
|
|
|
$content = $struct->getContent(); |
95
|
|
|
|
96
|
|
|
$userReference = $this->repository |
97
|
|
|
->getPermissionResolver() |
98
|
|
|
->getCurrentUserReference(); |
99
|
|
|
|
100
|
|
|
$user = $this->getUser( |
101
|
|
|
$userReference->getUserId() |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$ezInfo = $this->ezInfoCollectionRepository |
105
|
|
|
->createNewFromValues($content, $user); |
|
|
|
|
106
|
|
|
|
107
|
|
|
try { |
108
|
|
|
$this->ezInfoCollectionRepository->save($ezInfo); |
109
|
|
|
} catch (StoringCollectionFailedException $e) { |
110
|
|
|
throw new PersistingFailedException('collection', $e->getMessage()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
foreach ($struct->getFieldsData() as $fieldDefIdentifier => $value) { |
114
|
|
|
if ($value === null) { |
115
|
|
|
continue; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$value = $this->fieldsFactory->getLegacyValue($value->value, $contentType->getFieldDefinition($fieldDefIdentifier)); |
119
|
|
|
$ezInfoAttribute = $this->ezInfoCollectionAttributeRepository |
120
|
|
|
->createNewFromValues($content, $ezInfo, $value, $fieldDefIdentifier); |
121
|
|
|
|
122
|
|
|
try { |
123
|
|
|
$this->ezInfoCollectionAttributeRepository->save($ezInfoAttribute); |
124
|
|
|
} catch (StoringAttributeFailedException $e) { |
125
|
|
|
throw new PersistingFailedException('attribute', $e->getMessage()); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getObjectsWithCollectionsCount(): ObjectCount |
131
|
|
|
{ |
132
|
|
|
return new ObjectCount( |
133
|
|
|
$this->gateway->getContentsWithCollectionsCount() |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function getObjectsWithCollections(Query $query): ContentsWithCollections |
141
|
|
|
{ |
142
|
|
|
$objects = $this->gateway->getObjectsWithCollections($query->getLimit(), $query->getOffset()); |
143
|
|
|
|
144
|
|
|
$contents = []; |
145
|
|
|
foreach ($objects as $object) { |
146
|
|
|
$contentId = (int) $object['content_id']; |
147
|
|
|
|
148
|
|
|
$firstCollection = $this->ezInfoCollectionRepository->findOneBy( |
149
|
|
|
[ |
150
|
|
|
'contentObjectId' => $contentId, |
151
|
|
|
], |
152
|
|
|
[ |
153
|
|
|
'created' => 'ASC', |
154
|
|
|
] |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$lastCollection = $this->ezInfoCollectionRepository->findOneBy( |
158
|
|
|
[ |
159
|
|
|
'contentObjectId' => $contentId, |
160
|
|
|
], |
161
|
|
|
[ |
162
|
|
|
'created' => 'DESC', |
163
|
|
|
] |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
$childCount = $this->ezInfoCollectionRepository->getChildrenCount($contentId); |
167
|
|
|
|
168
|
|
|
$contents[] = $this->objectMapper->mapContent($object, $firstCollection, $lastCollection, $childCount); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return new ContentsWithCollections($contents, count($contents)); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function getCollectionsCount(ContentId $contentId): CollectionCount |
175
|
|
|
{ |
176
|
|
|
return new CollectionCount( |
177
|
|
|
$this->ezInfoCollectionRepository->getChildrenCount($contentId->getContentId()) |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function getCollections(ContentId $contentId): Collections |
182
|
|
|
{ |
183
|
|
|
$collections = $this->ezInfoCollectionRepository->findBy( |
184
|
|
|
[ |
185
|
|
|
'contentObjectId' => $contentId->getContentId(), |
186
|
|
|
], |
187
|
|
|
[], |
188
|
|
|
$contentId->getLimit(), |
189
|
|
|
$contentId->getOffset() |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
$objects = []; |
193
|
|
|
foreach ($collections as $collection) { |
194
|
|
|
$objects[] = $this->loadCollection($collection->getId()); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return new Collections($objects, count($objects)); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function searchCount(SearchCountQuery $query): SearchCount |
201
|
|
|
{ |
202
|
|
|
$collections = $this->ezInfoCollectionAttributeRepository |
203
|
|
|
->search($query->getContentId(), $query->getSearchText()); |
204
|
|
|
|
205
|
|
|
// needs rewrite |
206
|
|
|
$collections = $this->ezInfoCollectionRepository->findBy( |
207
|
|
|
[ |
208
|
|
|
'id' => $collections, |
209
|
|
|
] |
210
|
|
|
); |
211
|
|
|
|
212
|
|
|
return new SearchCount(count($collections)); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritdoc} |
217
|
|
|
*/ |
218
|
|
|
public function search(SearchQuery $query): Collections |
219
|
|
|
{ |
220
|
|
|
$collections = $this->ezInfoCollectionAttributeRepository |
221
|
|
|
->search($query->getContentId(), $query->getSearchText()); |
222
|
|
|
|
223
|
|
|
$collections = $this->ezInfoCollectionRepository->findBy( |
224
|
|
|
[ |
225
|
|
|
'id' => $collections, |
226
|
|
|
], |
227
|
|
|
[], |
228
|
|
|
$query->getLimit(), |
229
|
|
|
$query->getOffset() |
230
|
|
|
); |
231
|
|
|
|
232
|
|
|
$objects = []; |
233
|
|
|
foreach ($collections as $collection) { |
234
|
|
|
$objects[] = $this->loadCollection($collection->getId()); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return new Collections($objects, count($objects)); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* {@inheritdoc} |
242
|
|
|
*/ |
243
|
|
|
public function getCollection(CollectionId $collectionId): Collection |
244
|
|
|
{ |
245
|
|
|
return $this->loadCollection($collectionId->getCollectionId()); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* {@inheritdoc} |
250
|
|
|
*/ |
251
|
|
|
public function deleteCollectionFields(CollectionFields $collectionFields): void |
252
|
|
|
{ |
253
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository |
254
|
|
|
->findBy( |
255
|
|
|
[ |
256
|
|
|
'contentObjectId' => $collectionFields->getContentId(), |
257
|
|
|
'informationCollectionId' => $collectionFields->getCollectionId(), |
258
|
|
|
'contentClassAttributeId' => $collectionFields->getFields(), |
259
|
|
|
] |
260
|
|
|
); |
261
|
|
|
|
262
|
|
|
$this->ezInfoCollectionAttributeRepository->remove($attributes); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* {@inheritdoc} |
267
|
|
|
*/ |
268
|
|
View Code Duplication |
public function deleteCollections(FilterCollections $collections): void |
|
|
|
|
269
|
|
|
{ |
270
|
|
|
$collections = $this->ezInfoCollectionRepository |
271
|
|
|
->findBy([ |
272
|
|
|
'contentObjectId' => $collections->getContentId(), |
273
|
|
|
'id' => $collections->getCollectionIds(), |
274
|
|
|
]); |
275
|
|
|
|
276
|
|
|
foreach ($collections as $collection) { |
277
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository->findBy(['informationCollectionId' => $collection->getId()]); |
278
|
|
|
$this->ezInfoCollectionAttributeRepository->remove($attributes); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$this->ezInfoCollectionRepository->remove($collections); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* {@inheritdoc} |
286
|
|
|
*/ |
287
|
|
View Code Duplication |
public function deleteCollectionByContent(Contents $contents): void |
|
|
|
|
288
|
|
|
{ |
289
|
|
|
$collections = $this->ezInfoCollectionRepository |
290
|
|
|
->findBy([ |
291
|
|
|
'contentObjectId' => $contents->getContents(), |
|
|
|
|
292
|
|
|
]); |
293
|
|
|
|
294
|
|
|
foreach ($collections as $collection) { |
295
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository->findBy(['informationCollectionId' => $collection->getId()]); |
296
|
|
|
$this->ezInfoCollectionAttributeRepository->remove($attributes); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$this->ezInfoCollectionRepository->remove($collections); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param UserReference $userReference |
|
|
|
|
304
|
|
|
* @param mixed $userId |
305
|
|
|
* |
306
|
|
|
* @return \eZ\Publish\API\Repository\Values\User\User |
307
|
|
|
*/ |
308
|
|
|
protected function getUser($userId) |
309
|
|
|
{ |
310
|
|
|
try { |
311
|
|
|
return $this->repository |
312
|
|
|
->getUserService() |
313
|
|
|
->loadUser($userId); |
314
|
|
|
} catch (NotFoundException $exception) { |
|
|
|
|
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @param int $collectionId |
320
|
|
|
* |
321
|
|
|
* @return \Netgen\InformationCollection\API\Value\Collection |
322
|
|
|
*/ |
323
|
|
|
protected function loadCollection($collectionId) |
324
|
|
|
{ |
325
|
|
|
$collection = $this->ezInfoCollectionRepository->findOneBy(['id' => $collectionId]); |
326
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository->findBy( |
327
|
|
|
[ |
328
|
|
|
'informationCollectionId' => $collectionId, |
329
|
|
|
] |
330
|
|
|
); |
331
|
|
|
|
332
|
|
|
return $this->objectMapper->mapCollection($collection, $attributes); |
|
|
|
|
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
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: