|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Service; |
|
6
|
|
|
|
|
7
|
|
|
use eZ\Publish\API\Repository\Repository; |
|
8
|
|
|
use Netgen\Bundle\InformationCollectionBundle\API\Service\InformationCollection; |
|
9
|
|
|
use Netgen\Bundle\InformationCollectionBundle\API\Value\InformationCollection\Attribute; |
|
10
|
|
|
use Netgen\Bundle\InformationCollectionBundle\API\Value\InformationCollection\Collection; |
|
11
|
|
|
use Netgen\Bundle\InformationCollectionBundle\API\Value\InformationCollection\Collections; |
|
12
|
|
|
use Netgen\Bundle\InformationCollectionBundle\API\Value\InformationCollection\Content; |
|
13
|
|
|
use Netgen\Bundle\InformationCollectionBundle\API\Value\InformationCollection\ContentsWithCollections; |
|
14
|
|
|
use Netgen\Bundle\InformationCollectionBundle\API\Value\InformationCollection\Query; |
|
15
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Core\Persistence\Gateway\DoctrineDatabase; |
|
16
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Entity\EzInfoCollectionAttribute; |
|
17
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionAttributeRepository; |
|
18
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionRepository; |
|
19
|
|
|
|
|
20
|
|
|
class InformationCollectionService implements InformationCollection |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var \Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionRepository |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $ezInfoCollectionRepository; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionAttributeRepository |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $ezInfoCollectionAttributeRepository; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var \eZ\Publish\API\Repository\Repository |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $repository; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var \eZ\Publish\API\Repository\ContentService |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $contentService; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var \eZ\Publish\API\Repository\ContentTypeService |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $contentTypeService; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var \Netgen\Bundle\InformationCollectionBundle\Core\Persistence\Gateway\DoctrineDatabase |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $gateway; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* InformationCollectionService constructor. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionRepository $ezInfoCollectionRepository |
|
56
|
|
|
* @param \Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionAttributeRepository $ezInfoCollectionAttributeRepository |
|
57
|
|
|
* @param \eZ\Publish\API\Repository\Repository $repository |
|
58
|
|
|
* @param \Netgen\Bundle\InformationCollectionBundle\Core\Persistence\Gateway\DoctrineDatabase $gateway |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct( |
|
61
|
|
|
EzInfoCollectionRepository $ezInfoCollectionRepository, |
|
62
|
|
|
EzInfoCollectionAttributeRepository $ezInfoCollectionAttributeRepository, |
|
63
|
|
|
Repository $repository, |
|
64
|
|
|
DoctrineDatabase $gateway |
|
65
|
|
|
) { |
|
66
|
|
|
$this->ezInfoCollectionRepository = $ezInfoCollectionRepository; |
|
67
|
|
|
$this->ezInfoCollectionAttributeRepository = $ezInfoCollectionAttributeRepository; |
|
68
|
|
|
$this->repository = $repository; |
|
69
|
|
|
$this->contentService = $repository->getContentService(); |
|
70
|
|
|
$this->contentTypeService = $repository->getContentTypeService(); |
|
71
|
|
|
$this->gateway = $gateway; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getObjectsWithCollections(Query $query) |
|
78
|
|
|
{ |
|
79
|
|
|
if ($query->limit === Query::COUNT_QUERY) { |
|
80
|
|
|
return new ContentsWithCollections([ |
|
81
|
|
|
'count' => $this->gateway->getContentsWithCollectionsCount(), |
|
82
|
|
|
]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$objects = $this->gateway->getObjectsWithCollections($query->limit, $query->offset); |
|
86
|
|
|
|
|
87
|
|
|
$contents = []; |
|
88
|
|
|
foreach ($objects as $object) { |
|
89
|
|
|
$content = $this->contentService->loadContent((int) $object['content_id']); |
|
90
|
|
|
|
|
91
|
|
|
$firstCollection = $this->ezInfoCollectionRepository->findOneBy( |
|
92
|
|
|
[ |
|
93
|
|
|
'contentObjectId' => $content->id, |
|
94
|
|
|
], |
|
95
|
|
|
[ |
|
96
|
|
|
'created' => 'ASC', |
|
97
|
|
|
] |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$lastCollection = $this->ezInfoCollectionRepository->findOneBy( |
|
101
|
|
|
[ |
|
102
|
|
|
'contentObjectId' => $content->id, |
|
103
|
|
|
], |
|
104
|
|
|
[ |
|
105
|
|
|
'created' => 'DESC', |
|
106
|
|
|
] |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
$contents[] = new Content( |
|
110
|
|
|
[ |
|
111
|
|
|
'content' => $content, |
|
112
|
|
|
'contentType' => $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId), |
|
113
|
|
|
'firstCollection' => $firstCollection, |
|
114
|
|
|
'lastCollection' => $lastCollection, |
|
115
|
|
|
'count' => $this->ezInfoCollectionRepository->getChildrenCount($content->id), |
|
116
|
|
|
'hasLocation' => empty($object['main_node_id']) ? false : true, |
|
117
|
|
|
] |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return new ContentsWithCollections( |
|
122
|
|
|
[ |
|
123
|
|
|
'contents' => $contents, |
|
124
|
|
|
'count' => count($contents), |
|
125
|
|
|
] |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function getCollections(Query $query) |
|
130
|
|
|
{ |
|
131
|
|
|
if ($query->limit === Query::COUNT_QUERY) { |
|
132
|
|
|
return new Collections([ |
|
133
|
|
|
'count' => $this->ezInfoCollectionRepository->getChildrenCount($query->contentId), |
|
134
|
|
|
'collections' => [], |
|
135
|
|
|
]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$collections = $this->ezInfoCollectionRepository->findBy( |
|
139
|
|
|
[ |
|
140
|
|
|
'contentObjectId' => $query->contentId, |
|
141
|
|
|
], |
|
142
|
|
|
[], |
|
143
|
|
|
$query->limit, |
|
144
|
|
|
$query->offset |
|
145
|
|
|
); |
|
146
|
|
|
|
|
147
|
|
|
$objects = []; |
|
148
|
|
|
foreach ($collections as $collection) { |
|
149
|
|
|
$objects[] = $this->loadCollection($collection->getId()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return new Collections( |
|
153
|
|
|
[ |
|
154
|
|
|
'collections' => $objects, |
|
155
|
|
|
'count' => count($objects), |
|
156
|
|
|
] |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* {@inheritdoc} |
|
162
|
|
|
*/ |
|
163
|
|
|
public function search(Query $query) |
|
164
|
|
|
{ |
|
165
|
|
|
if ($query->limit === Query::COUNT_QUERY) { |
|
166
|
|
|
$collections = $this->ezInfoCollectionAttributeRepository->search($query->contentId, $query->searchText); |
|
167
|
|
|
|
|
168
|
|
|
// needs rewrite |
|
169
|
|
|
$collections = $this->ezInfoCollectionRepository->findBy( |
|
170
|
|
|
[ |
|
171
|
|
|
'id' => $collections, |
|
172
|
|
|
] |
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
|
|
return new Collections([ |
|
176
|
|
|
'count' => count($collections), |
|
177
|
|
|
'collections' => [], |
|
178
|
|
|
]); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$collections = $this->ezInfoCollectionAttributeRepository->search($query->contentId, $query->searchText); |
|
182
|
|
|
|
|
183
|
|
|
$collections = $this->ezInfoCollectionRepository->findBy( |
|
184
|
|
|
[ |
|
185
|
|
|
'id' => $collections, |
|
186
|
|
|
], |
|
187
|
|
|
[], |
|
188
|
|
|
$query->limit, |
|
189
|
|
|
$query->offset |
|
190
|
|
|
); |
|
191
|
|
|
|
|
192
|
|
|
$objects = []; |
|
193
|
|
|
foreach ($collections as $collection) { |
|
194
|
|
|
$objects[] = $this->loadCollection($collection->getId()); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return new Collections( |
|
198
|
|
|
[ |
|
199
|
|
|
'collections' => $objects, |
|
200
|
|
|
'count' => count($objects), |
|
201
|
|
|
] |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* {@inheritdoc} |
|
207
|
|
|
*/ |
|
208
|
|
|
public function getCollection(Query $query) |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->loadCollection($query->collectionId); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* {@inheritdoc} |
|
215
|
|
|
*/ |
|
216
|
|
|
public function deleteCollectionFields(Query $query) |
|
217
|
|
|
{ |
|
218
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository |
|
219
|
|
|
->findBy( |
|
220
|
|
|
[ |
|
221
|
|
|
'contentObjectId' => $query->contentId, |
|
222
|
|
|
'informationCollectionId' => $query->collectionId, |
|
223
|
|
|
'contentClassAttributeId' => $query->fields, |
|
224
|
|
|
]); |
|
225
|
|
|
|
|
226
|
|
|
$this->ezInfoCollectionAttributeRepository->remove($attributes); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* {@inheritdoc} |
|
231
|
|
|
*/ |
|
232
|
|
View Code Duplication |
public function deleteCollections(Query $query) |
|
|
|
|
|
|
233
|
|
|
{ |
|
234
|
|
|
$collections = $this->ezInfoCollectionRepository |
|
235
|
|
|
->findBy([ |
|
236
|
|
|
'contentObjectId' => $query->contentId, |
|
237
|
|
|
'id' => $query->collections, |
|
238
|
|
|
]); |
|
239
|
|
|
|
|
240
|
|
|
foreach ($collections as $collection) { |
|
241
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository->findBy(['informationCollectionId' => $collection->getId()]); |
|
242
|
|
|
$this->ezInfoCollectionAttributeRepository->remove($attributes); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
$this->ezInfoCollectionRepository->remove($collections); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* {@inheritdoc} |
|
250
|
|
|
*/ |
|
251
|
|
View Code Duplication |
public function deleteCollectionByContent(Query $query) |
|
|
|
|
|
|
252
|
|
|
{ |
|
253
|
|
|
$collections = $this->ezInfoCollectionRepository |
|
254
|
|
|
->findBy([ |
|
255
|
|
|
'contentObjectId' => $query->contents, |
|
256
|
|
|
]); |
|
257
|
|
|
|
|
258
|
|
|
foreach ($collections as $collection) { |
|
259
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository->findBy(['informationCollectionId' => $collection->getId()]); |
|
260
|
|
|
$this->ezInfoCollectionAttributeRepository->remove($attributes); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
$this->ezInfoCollectionRepository->remove($collections); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* @param int $userId |
|
268
|
|
|
* |
|
269
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
|
270
|
|
|
* |
|
271
|
|
|
* @return \eZ\Publish\API\Repository\Values\User\User |
|
272
|
|
|
*/ |
|
273
|
|
|
protected function getUser($userId) |
|
274
|
|
|
{ |
|
275
|
|
|
return $this->repository->getUserService()->loadUser($userId); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @param int $collectionId |
|
280
|
|
|
* |
|
281
|
|
|
* @return \Netgen\Bundle\InformationCollectionBundle\API\Value\InformationCollection\Collection |
|
282
|
|
|
*/ |
|
283
|
|
|
protected function loadCollection($collectionId) |
|
284
|
|
|
{ |
|
285
|
|
|
$collection = $this->ezInfoCollectionRepository->findOneBy(['id' => $collectionId]); |
|
286
|
|
|
|
|
287
|
|
|
$content = $this->contentService->loadContent($collection->getContentObjectId()); |
|
288
|
|
|
|
|
289
|
|
|
$contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId); |
|
290
|
|
|
$definitionsById = $contentType->fieldDefinitionsById; |
|
|
|
|
|
|
291
|
|
|
|
|
292
|
|
|
$collections = $this->ezInfoCollectionAttributeRepository->findBy( |
|
293
|
|
|
[ |
|
294
|
|
|
'informationCollectionId' => $collectionId, |
|
295
|
|
|
] |
|
296
|
|
|
); |
|
297
|
|
|
|
|
298
|
|
|
$attributes = []; |
|
299
|
|
|
/** @var EzInfoCollectionAttribute $coll */ |
|
300
|
|
|
foreach ($collections as $coll) { |
|
301
|
|
|
if (empty($definitionsById[$coll->getContentClassAttributeId()])) { |
|
302
|
|
|
continue; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
$attributes[] = new Attribute([ |
|
306
|
|
|
'entity' => $coll, |
|
307
|
|
|
'field' => $definitionsById[$coll->getContentClassAttributeId()], |
|
308
|
|
|
]); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
return new Collection([ |
|
312
|
|
|
'entity' => $collection, |
|
313
|
|
|
'attributes' => $attributes, |
|
314
|
|
|
'user' => $this->getUser($collection->getCreatorId()), |
|
315
|
|
|
'content' => $content, |
|
316
|
|
|
]); |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.