1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location; |
6
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\ContentCollection; |
7
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\LocationCollection; |
8
|
|
|
use Kaliop\eZMigrationBundle\Core\Matcher\ContentMatcher; |
9
|
|
|
use Kaliop\eZMigrationBundle\Core\Matcher\LocationMatcher; |
10
|
|
|
use Kaliop\eZMigrationBundle\Core\Helper\SortConverter; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Handles location migrations. |
14
|
|
|
*/ |
15
|
|
|
class LocationManager extends RepositoryExecutor |
16
|
|
|
{ |
17
|
|
|
protected $supportedStepTypes = array('location'); |
18
|
|
|
protected $supportedActions = array('create', 'load', 'update', 'delete', 'trash'); |
19
|
|
|
|
20
|
|
|
protected $contentMatcher; |
21
|
|
|
protected $locationMatcher; |
22
|
|
|
protected $sortConverter; |
23
|
|
|
|
24
|
|
|
public function __construct(ContentMatcher $contentMatcher, LocationMatcher $locationMatcher, SortConverter $sortConverter) |
25
|
|
|
{ |
26
|
|
|
$this->contentMatcher = $contentMatcher; |
27
|
|
|
$this->locationMatcher = $locationMatcher; |
28
|
|
|
$this->sortConverter = $sortConverter; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Method to handle the create operation of the migration instructions |
33
|
|
|
*/ |
34
|
|
|
protected function create($step) |
35
|
|
|
{ |
36
|
|
|
$locationService = $this->repository->getLocationService(); |
37
|
|
|
|
38
|
|
|
if (!isset($step->dsl['parent_location']) && !isset($step->dsl['parent_location_id'])) { |
39
|
|
|
throw new \Exception('Missing parent location id. This is required to create the new location.'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// support legacy tag: parent_location_id |
43
|
|
|
if (!isset($step->dsl['parent_location']) && isset($step->dsl['parent_location_id'])) { |
44
|
|
|
$parentLocationIds = $step->dsl['parent_location_id']; |
45
|
|
|
} else { |
46
|
|
|
$parentLocationIds = $step->dsl['parent_location']; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!is_array($parentLocationIds)) { |
50
|
|
|
$parentLocationIds = array($parentLocationIds); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (isset($step->dsl['is_main']) && count($parentLocationIds) > 1) { |
54
|
|
|
throw new \Exception('Can not set more than one new location as main.'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// resolve references and remote ids |
58
|
|
|
foreach ($parentLocationIds as $id => $parentLocationId) { |
59
|
|
|
$parentLocationId = $this->referenceResolver->resolveReference($parentLocationId); |
60
|
|
|
$parentLocationIds[$id] = $this->matchLocationByKey($parentLocationId)->id; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$contentCollection = $this->matchContents('create', $step); |
64
|
|
|
|
65
|
|
|
$locations = array(); |
66
|
|
|
foreach ($contentCollection as $content) { |
|
|
|
|
67
|
|
|
$contentInfo = $content->contentInfo; |
68
|
|
|
|
69
|
|
|
foreach ($parentLocationIds as $parentLocationId) { |
70
|
|
|
$locationCreateStruct = $locationService->newLocationCreateStruct($parentLocationId); |
71
|
|
|
|
72
|
|
|
if (isset($step->dsl['is_hidden'])) { |
73
|
|
|
$locationCreateStruct->hidden = $step->dsl['is_hidden']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (isset($step->dsl['priority'])) { |
77
|
|
|
$locationCreateStruct->priority = $step->dsl['priority']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (isset($step->dsl['sort_order'])) { |
81
|
|
|
$locationCreateStruct->sortOrder = $this->getSortOrder($step->dsl['sort_order']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (isset($step->dsl['sort_field'])) { |
85
|
|
|
$locationCreateStruct->sortField = $this->getSortField($step->dsl['sort_field']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$location = $locationService->createLocation($contentInfo, $locationCreateStruct); |
89
|
|
|
|
90
|
|
|
if (isset($step->dsl['is_main'])) { |
91
|
|
|
$this->setMainLocation($location); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$locations[] = $location; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$locationCollection = new LocationCollection($locations); |
99
|
|
|
|
100
|
|
|
$this->setReferences($locationCollection, $step); |
101
|
|
|
|
102
|
|
|
return $locationCollection; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function load($step) |
106
|
|
|
{ |
107
|
|
|
$locationCollection = $this->matchLocations('load', $step); |
108
|
|
|
|
109
|
|
|
$this->setReferences($locationCollection, $step); |
|
|
|
|
110
|
|
|
|
111
|
|
|
return $locationCollection; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Updates information for a location like priority, sort field and sort order. |
116
|
|
|
* Updates the visibility of the location when needed. |
117
|
|
|
* Can move a location and its children to a new parent location or swap two locations. |
118
|
|
|
*/ |
119
|
|
|
protected function update($step) |
120
|
|
|
{ |
121
|
|
|
$locationService = $this->repository->getLocationService(); |
122
|
|
|
|
123
|
|
|
$locationCollection = $this->matchLocations('update', $step); |
124
|
|
|
|
125
|
|
|
if (count($locationCollection) > 1 && isset($step->dsl['references'])) { |
126
|
|
|
throw new \Exception("Can not execute Location update because multiple locations match, and a references section is specified in the dsl. References can be set when only 1 location matches"); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (count($locationCollection) > 1 && isset($step->dsl['swap_with_location'])) { |
130
|
|
|
throw new \Exception("Can not execute Location update because multiple locations match, and a swap_with_location is specified in the dsl."); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// support legacy tag: parent_location_id |
134
|
|
|
if (isset($step->dsl['swap_with_location']) && (isset($step->dsl['parent_location']) || isset($step->dsl['parent_location_id']))) { |
135
|
|
|
throw new \Exception('Cannot move location to a new parent and swap location with another location at the same time.'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
foreach ($locationCollection as $key => $location) { |
|
|
|
|
139
|
|
|
|
140
|
|
|
if (isset($step->dsl['priority']) |
141
|
|
|
|| isset($step->dsl['sort_field']) |
142
|
|
|
|| isset($step->dsl['sort_order']) |
143
|
|
|
|| isset($step->dsl['remote_id']) |
144
|
|
|
) { |
145
|
|
|
$locationUpdateStruct = $locationService->newLocationUpdateStruct(); |
146
|
|
|
|
147
|
|
|
if (isset($step->dsl['priority'])) { |
148
|
|
|
$locationUpdateStruct->priority = $step->dsl['priority']; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (isset($step->dsl['sort_field'])) { |
152
|
|
|
$locationUpdateStruct->sortField = $this->getSortField($step->dsl['sort_field'], $location->sortField); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (isset($step->dsl['sort_order'])) { |
156
|
|
|
$locationUpdateStruct->sortOrder = $this->getSortOrder($step->dsl['sort_order'], $location->sortOrder); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (isset($step->dsl['remote_id'])) { |
160
|
|
|
$locationUpdateStruct->remoteId = $step->dsl['remote_id']; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$location = $locationService->updateLocation($location, $locationUpdateStruct); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// Check if visibility needs to be updated |
167
|
|
|
if (isset($step->dsl['is_hidden'])) { |
168
|
|
|
if ($step->dsl['is_hidden']) { |
169
|
|
|
$location = $locationService->hideLocation($location); |
170
|
|
|
} else { |
171
|
|
|
$location = $locationService->unhideLocation($location); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// Move or swap location |
176
|
|
|
if (isset($step->dsl['parent_location']) || isset($step->dsl['parent_location_id'])) { |
177
|
|
|
// Move the location and all its children to a new parent |
178
|
|
|
$parentLocationId = isset($step->dsl['parent_location']) ? $step->dsl['parent_location'] : $step->dsl['parent_location_id']; |
179
|
|
|
$parentLocationId = $this->referenceResolver->resolveReference($parentLocationId); |
180
|
|
|
|
181
|
|
|
$newParentLocation = $locationService->loadLocation($parentLocationId); |
182
|
|
|
|
183
|
|
|
$locationService->moveSubtree($location, $newParentLocation); |
184
|
|
|
} elseif (isset($step->dsl['swap_with_location'])) { |
185
|
|
|
// Swap locations |
186
|
|
|
$swapLocationId = $step->dsl['swap_with_location']; |
187
|
|
|
$swapLocationId = $this->referenceResolver->resolveReference($swapLocationId); |
188
|
|
|
|
189
|
|
|
$locationToSwap = $this->matchLocationByKey($swapLocationId); |
190
|
|
|
|
191
|
|
|
$locationService->swapLocation($location, $locationToSwap); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// make the location the main one |
195
|
|
|
if (isset($step->dsl['is_main'])) { |
196
|
|
|
$this->setMainLocation($location); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$locationCollection[$key] = $location; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$this->setReferences($locationCollection, $step); |
|
|
|
|
203
|
|
|
|
204
|
|
|
return $locationCollection; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Delete locations |
209
|
|
|
*/ |
210
|
|
View Code Duplication |
protected function delete($step) |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
$locationCollection = $this->matchLocations('delete', $step); |
213
|
|
|
|
214
|
|
|
$this->setReferences($locationCollection, $step); |
|
|
|
|
215
|
|
|
|
216
|
|
|
$locationService = $this->repository->getLocationService(); |
217
|
|
|
|
218
|
|
|
foreach ($locationCollection as $location) { |
|
|
|
|
219
|
|
|
$locationService->deleteLocation($location); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $locationCollection; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Delete locations sending them to the trash |
227
|
|
|
*/ |
228
|
|
View Code Duplication |
protected function trash($step) |
|
|
|
|
229
|
|
|
{ |
230
|
|
|
$locationCollection = $this->matchLocations('delete', $step); |
231
|
|
|
|
232
|
|
|
$this->setReferences($locationCollection, $step); |
|
|
|
|
233
|
|
|
|
234
|
|
|
$trashService = $this->repository->getTrashService(); |
235
|
|
|
|
236
|
|
|
foreach ($locationCollection as $location) { |
|
|
|
|
237
|
|
|
$trashService->trash($location); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $locationCollection; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param string $action |
245
|
|
|
* @return LocationCollection |
246
|
|
|
* @throws \Exception |
247
|
|
|
*/ |
248
|
|
View Code Duplication |
protected function matchLocations($action, $step) |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
if (!isset($step->dsl['location_id']) && !isset($step->dsl['match'])) { |
251
|
|
|
throw new \Exception("The id or a match condition is required to $action a location"); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
// Backwards compat |
255
|
|
|
if (isset($step->dsl['match'])) { |
256
|
|
|
$match = $step->dsl['match']; |
257
|
|
|
} else { |
258
|
|
|
$match = array('location_id' => $step->dsl['location_id']); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
// convert the references passed in the match |
262
|
|
|
$match = $this->resolveReferencesRecursively($match); |
263
|
|
|
|
264
|
|
|
return $this->locationMatcher->match($match); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Sets references to object attributes |
269
|
|
|
* |
270
|
|
|
* The Location Manager currently supports setting references to location id. |
271
|
|
|
* |
272
|
|
|
* @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute. |
273
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location|LocationCollection $location |
274
|
|
|
* @return boolean |
275
|
|
|
*/ |
276
|
|
View Code Duplication |
protected function setReferences($location, $step) |
|
|
|
|
277
|
|
|
{ |
278
|
|
|
if (!array_key_exists('references', $step->dsl)) { |
279
|
|
|
return false; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$references = $this->setReferencesCommon($location, $step->dsl['references']); |
283
|
|
|
$location = $this->insureSingleEntity($location, $references); |
|
|
|
|
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param Location $location |
288
|
|
|
* @param array $references the definitions of the references to set |
289
|
|
|
* @throws \InvalidArgumentException When trying to assign a reference to an unsupported attribute |
290
|
|
|
* @return array key: the reference names, values: the reference values |
291
|
|
|
*/ |
292
|
|
View Code Duplication |
protected function getReferencesValues(Location $location, array $references) |
|
|
|
|
293
|
|
|
{ |
294
|
|
|
$refs = array(); |
295
|
|
|
foreach ($references as $reference) { |
296
|
|
|
switch ($reference['attribute']) { |
297
|
|
|
case 'location_id': |
298
|
|
|
case 'id': |
299
|
|
|
$value = $location->id; |
300
|
|
|
break; |
301
|
|
|
case 'remote_id': |
302
|
|
|
case 'location_remote_id': |
303
|
|
|
$value = $location->remoteId; |
304
|
|
|
break; |
305
|
|
|
case 'always_available': |
306
|
|
|
$value = $location->contentInfo->alwaysAvailable; |
307
|
|
|
break; |
308
|
|
|
case 'content_id': |
309
|
|
|
$value = $location->contentId; |
310
|
|
|
break; |
311
|
|
|
case 'content_type_id': |
312
|
|
|
$value = $location->contentInfo->contentTypeId; |
313
|
|
|
break; |
314
|
|
|
case 'content_type_identifier': |
315
|
|
|
$contentTypeService = $this->repository->getContentTypeService(); |
316
|
|
|
$value = $contentTypeService->loadContentType($location->contentInfo->contentTypeId)->identifier; |
317
|
|
|
break; |
318
|
|
|
case 'current_version': |
319
|
|
|
case 'current_version_no': |
320
|
|
|
$value = $location->contentInfo->currentVersionNo; |
321
|
|
|
break; |
322
|
|
|
case 'depth': |
323
|
|
|
$value = $location->depth; |
324
|
|
|
break; |
325
|
|
|
case 'is_hidden': |
326
|
|
|
$value = $location->hidden; |
327
|
|
|
break; |
328
|
|
|
case 'main_location_id': |
329
|
|
|
$value = $location->contentInfo->mainLocationId; |
330
|
|
|
break; |
331
|
|
|
case 'main_language_code': |
332
|
|
|
$value = $location->contentInfo->mainLanguageCode; |
333
|
|
|
break; |
334
|
|
|
case 'modification_date': |
335
|
|
|
$value = $location->contentInfo->modificationDate->getTimestamp(); |
336
|
|
|
break; |
337
|
|
|
case 'name': |
338
|
|
|
$value = $location->contentInfo->name; |
339
|
|
|
break; |
340
|
|
|
case 'owner_id': |
341
|
|
|
$value = $location->contentInfo->ownerId; |
342
|
|
|
break; |
343
|
|
|
case 'parent_location_id': |
344
|
|
|
$value = $location->parentLocationId; |
345
|
|
|
break; |
346
|
|
|
case 'path': |
347
|
|
|
$value = $location->pathString; |
348
|
|
|
break; |
349
|
|
|
case 'priority': |
350
|
|
|
$value = $location->priority; |
351
|
|
|
break; |
352
|
|
|
case 'publication_date': |
353
|
|
|
$value = $location->contentInfo->publishedDate->getTimestamp(); |
354
|
|
|
break; |
355
|
|
|
case 'section_id': |
356
|
|
|
$value = $location->contentInfo->sectionId; |
357
|
|
|
break; |
358
|
|
|
case 'section_identifier': |
359
|
|
|
$sectionService = $this->repository->getSectionService(); |
360
|
|
|
$value = $sectionService->loadSection($location->contentInfo->sectionId)->identifier; |
361
|
|
|
break; |
362
|
|
|
case 'sort_field': |
363
|
|
|
$value = $this->sortConverter->sortField2Hash($location->sortField); |
364
|
|
|
break; |
365
|
|
|
case 'sort_order': |
366
|
|
|
$value = $this->sortConverter->sortOrder2Hash($location->sortOrder); |
367
|
|
|
break; |
368
|
|
|
default: |
369
|
|
|
throw new \InvalidArgumentException('Location Manager does not support setting references for attribute ' . $reference['attribute']); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
$refs[$reference['identifier']] = $value; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
return $refs; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* @param int|string|array $locationKey |
380
|
|
|
* @return Location |
381
|
|
|
*/ |
382
|
|
|
public function matchLocationByKey($locationKey) |
383
|
|
|
{ |
384
|
|
|
return $this->locationMatcher->matchOneByKey($locationKey); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* NB: weirdly enough, it returns contents, not locations |
389
|
|
|
* |
390
|
|
|
* @param string $action |
391
|
|
|
* @return ContentCollection |
392
|
|
|
* @throws \Exception |
393
|
|
|
*/ |
394
|
|
|
protected function matchContents($action, $step) |
395
|
|
|
{ |
396
|
|
|
if (!isset($step->dsl['object_id']) && !isset($step->dsl['remote_id']) && !isset($step->dsl['match'])) { |
397
|
|
|
throw new \Exception("The ID or remote ID of an object or a Match Condition is required to $action a new location."); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
// Backwards compat |
401
|
|
|
if (!isset($step->dsl['match'])) { |
402
|
|
|
if (isset($step->dsl['object_id'])) { |
403
|
|
|
$step->dsl['match'] = array('content_id' => $step->dsl['object_id']); |
404
|
|
|
} elseif (isset($step->dsl['remote_id'])) { |
405
|
|
|
$step->dsl['match'] = array('content_remote_id' => $step->dsl['remote_id']); |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
$match = $step->dsl['match']; |
410
|
|
|
|
411
|
|
|
// convert the references passed in the match |
412
|
|
|
foreach ($match as $condition => $values) { |
413
|
|
|
if (is_array($values)) { |
414
|
|
|
foreach ($values as $position => $value) { |
415
|
|
|
$match[$condition][$position] = $this->referenceResolver->resolveReference($value); |
416
|
|
|
} |
417
|
|
|
} else { |
418
|
|
|
$match[$condition] = $this->referenceResolver->resolveReference($values); |
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
return $this->contentMatcher->matchContent($match); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
protected function setMainLocation(Location $location) |
426
|
|
|
{ |
427
|
|
|
$contentService = $this->repository->getContentService(); |
428
|
|
|
$contentMetaDataUpdateStruct = $contentService->newContentMetadataUpdateStruct(); |
429
|
|
|
$contentMetaDataUpdateStruct->mainLocationId = $location->id; |
430
|
|
|
$contentService->updateContentMetadata($location->contentInfo, $contentMetaDataUpdateStruct); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* @param $newValue |
435
|
|
|
* @param null $currentValue |
436
|
|
|
* @return int|null |
437
|
|
|
* |
438
|
|
|
* * @todo make protected |
439
|
|
|
*/ |
440
|
|
|
public function getSortField($newValue, $currentValue = null) |
441
|
|
|
{ |
442
|
|
|
$sortField = $currentValue; |
443
|
|
|
|
444
|
|
|
if ($newValue !== null) { |
445
|
|
|
$sortField = $this->sortConverter->hash2SortField($newValue); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
return $sortField; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Get the sort order based on the current value and the value in the DSL definition. |
453
|
|
|
* |
454
|
|
|
* @see \eZ\Publish\API\Repository\Values\Content\Location::SORT_ORDER_* |
455
|
|
|
* |
456
|
|
|
* @param int $newValue |
457
|
|
|
* @param int $currentValue |
458
|
|
|
* @return int|null |
459
|
|
|
* |
460
|
|
|
* @todo make protected |
461
|
|
|
*/ |
462
|
|
|
public function getSortOrder($newValue, $currentValue = null) |
463
|
|
|
{ |
464
|
|
|
$sortOrder = $currentValue; |
465
|
|
|
|
466
|
|
|
if ($newValue !== null) { |
467
|
|
|
$sortOrder = $this->sortConverter->hash2SortOrder($newValue); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
return $sortOrder; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
} |
474
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.