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\ContentId; |
21
|
|
|
use Netgen\InformationCollection\API\Value\Filter\Contents; |
22
|
|
|
use Netgen\InformationCollection\API\Value\Filter\Query; |
23
|
|
|
use Netgen\InformationCollection\API\Value\Filter\SearchCountQuery; |
24
|
|
|
use Netgen\InformationCollection\API\Value\Filter\SearchQuery; |
25
|
|
|
use Netgen\InformationCollection\API\Value\Filter\Collections as FilterCollections; |
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\EzInfoCollectionRepository; |
33
|
|
|
use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository; |
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 $factory |
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
|
|
|
|
114
|
|
|
foreach ($struct->getFieldsData() as $fieldDefIdentifier => $value) { |
115
|
|
|
if ($value === null) { |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$value = $this->fieldsFactory->getLegacyValue($value->value, $contentType->getFieldDefinition($fieldDefIdentifier)); |
120
|
|
|
$ezInfoAttribute = $this->ezInfoCollectionAttributeRepository |
121
|
|
|
->createNewFromValues($content, $ezInfo, $value, $fieldDefIdentifier); |
122
|
|
|
|
123
|
|
|
try { |
124
|
|
|
$this->ezInfoCollectionAttributeRepository->save($ezInfoAttribute); |
125
|
|
|
} catch (StoringAttributeFailedException $e) { |
126
|
|
|
throw new PersistingFailedException('attribute', $e->getMessage()); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
public function getObjectsWithCollectionsCount(): ObjectCount |
133
|
|
|
{ |
134
|
|
|
return new ObjectCount( |
135
|
|
|
$this->gateway->getContentsWithCollectionsCount() |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public function getObjectsWithCollections(Query $query): ContentsWithCollections |
143
|
|
|
{ |
144
|
|
|
$objects = $this->gateway->getObjectsWithCollections($query->getLimit(), $query->getOffset()); |
145
|
|
|
|
146
|
|
|
$contents = []; |
147
|
|
|
foreach ($objects as $object) { |
148
|
|
|
$contentId = (int) $object['content_id']; |
149
|
|
|
|
150
|
|
|
$firstCollection = $this->ezInfoCollectionRepository->findOneBy( |
151
|
|
|
[ |
152
|
|
|
'contentObjectId' => $contentId, |
153
|
|
|
], |
154
|
|
|
[ |
155
|
|
|
'created' => 'ASC', |
156
|
|
|
] |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
$lastCollection = $this->ezInfoCollectionRepository->findOneBy( |
160
|
|
|
[ |
161
|
|
|
'contentObjectId' => $contentId, |
162
|
|
|
], |
163
|
|
|
[ |
164
|
|
|
'created' => 'DESC', |
165
|
|
|
] |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
$childCount = $this->ezInfoCollectionRepository->getChildrenCount($contentId); |
169
|
|
|
|
170
|
|
|
$contents[] = $this->objectMapper->mapContent($object, $firstCollection, $lastCollection, $childCount); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return new ContentsWithCollections($contents, count($contents)); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function getCollectionsCount(ContentId $contentId): CollectionCount |
177
|
|
|
{ |
178
|
|
|
return new CollectionCount( |
179
|
|
|
$this->ezInfoCollectionRepository->getChildrenCount($contentId->getContentId()) |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function getCollections(ContentId $contentId): Collections |
184
|
|
|
{ |
185
|
|
|
$collections = $this->ezInfoCollectionRepository->findBy( |
186
|
|
|
[ |
187
|
|
|
'contentObjectId' => $contentId->getContentId(), |
188
|
|
|
], |
189
|
|
|
[], |
190
|
|
|
$contentId->getLimit(), |
191
|
|
|
$contentId->getOffset() |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
$objects = []; |
195
|
|
|
foreach ($collections as $collection) { |
196
|
|
|
$objects[] = $this->loadCollection($collection->getId()); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return new Collections($objects, count($objects)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function searchCount(SearchCountQuery $query): SearchCount |
203
|
|
|
{ |
204
|
|
|
$collections = $this->ezInfoCollectionAttributeRepository |
205
|
|
|
->search($query->getContentId(), $query->getSearchText()); |
206
|
|
|
|
207
|
|
|
// needs rewrite |
208
|
|
|
$collections = $this->ezInfoCollectionRepository->findBy( |
209
|
|
|
[ |
210
|
|
|
'id' => $collections, |
211
|
|
|
] |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
return new SearchCount(count($collections)); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
public function search(SearchQuery $query): Collections |
221
|
|
|
{ |
222
|
|
|
$collections = $this->ezInfoCollectionAttributeRepository |
223
|
|
|
->search($query->getContentId(), $query->getSearchText()); |
224
|
|
|
|
225
|
|
|
$collections = $this->ezInfoCollectionRepository->findBy( |
226
|
|
|
[ |
227
|
|
|
'id' => $collections, |
228
|
|
|
], |
229
|
|
|
[], |
230
|
|
|
$query->getLimit(), |
231
|
|
|
$query->getOffset() |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
$objects = []; |
235
|
|
|
foreach ($collections as $collection) { |
236
|
|
|
$objects[] = $this->loadCollection($collection->getId()); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return new Collections($objects, count($objects)); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* {@inheritdoc} |
244
|
|
|
*/ |
245
|
|
|
public function getCollection(CollectionId $collectionId): Collection |
246
|
|
|
{ |
247
|
|
|
return $this->loadCollection($collectionId->getCollectionId()); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* {@inheritdoc} |
252
|
|
|
*/ |
253
|
|
|
public function deleteCollectionFields(CollectionFields $collectionFields): void |
254
|
|
|
{ |
255
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository |
256
|
|
|
->findBy( |
257
|
|
|
[ |
258
|
|
|
'contentObjectId' => $collectionFields->getContentId(), |
259
|
|
|
'informationCollectionId' => $collectionFields->getCollectionId(), |
260
|
|
|
'contentClassAttributeId' => $collectionFields->getFields(), |
261
|
|
|
]); |
262
|
|
|
|
263
|
|
|
$this->ezInfoCollectionAttributeRepository->remove($attributes); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
|
View Code Duplication |
public function deleteCollections(FilterCollections $collections): void |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
$collections = $this->ezInfoCollectionRepository |
272
|
|
|
->findBy([ |
273
|
|
|
'contentObjectId' => $collections->getContentId(), |
274
|
|
|
'id' => $collections->getCollectionIds(), |
275
|
|
|
]); |
276
|
|
|
|
277
|
|
|
foreach ($collections as $collection) { |
278
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository->findBy(['informationCollectionId' => $collection->getId()]); |
279
|
|
|
$this->ezInfoCollectionAttributeRepository->remove($attributes); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$this->ezInfoCollectionRepository->remove($collections); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* {@inheritdoc} |
287
|
|
|
*/ |
288
|
|
View Code Duplication |
public function deleteCollectionByContent(Contents $contents): void |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
$collections = $this->ezInfoCollectionRepository |
291
|
|
|
->findBy([ |
292
|
|
|
'contentObjectId' => $contents->getContents(), |
|
|
|
|
293
|
|
|
]); |
294
|
|
|
|
295
|
|
|
foreach ($collections as $collection) { |
296
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository->findBy(['informationCollectionId' => $collection->getId()]); |
297
|
|
|
$this->ezInfoCollectionAttributeRepository->remove($attributes); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$this->ezInfoCollectionRepository->remove($collections); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param UserReference $userReference |
|
|
|
|
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
|
|
|
|
315
|
|
|
} catch (NotFoundException $exception) { |
|
|
|
|
316
|
|
|
|
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @param int $collectionId |
323
|
|
|
* |
324
|
|
|
* @return \Netgen\InformationCollection\API\Value\Collection |
325
|
|
|
*/ |
326
|
|
|
protected function loadCollection($collectionId) |
327
|
|
|
{ |
328
|
|
|
$collection = $this->ezInfoCollectionRepository->findOneBy(['id' => $collectionId]); |
329
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository->findBy( |
330
|
|
|
[ |
331
|
|
|
'informationCollectionId' => $collectionId, |
332
|
|
|
] |
333
|
|
|
); |
334
|
|
|
|
335
|
|
|
return $this->objectMapper->mapCollection($collection, $attributes); |
|
|
|
|
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|
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: