|
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
|
|
|
|
|
10
|
|
|
class LocationManager extends RepositoryExecutor |
|
11
|
|
|
{ |
|
12
|
|
|
protected $supportedStepTypes = array('location'); |
|
13
|
|
|
|
|
14
|
|
|
protected $contentMatcher; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(ContentMatcher $contentMatcher) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->contentMatcher = $contentMatcher; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Method to handle the create operation of the migration instructions |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function create() |
|
25
|
|
|
{ |
|
26
|
|
|
$locationService = $this->repository->getLocationService(); |
|
27
|
|
|
|
|
28
|
|
|
if (!isset($this->dsl['parent_location_id'])) { |
|
29
|
|
|
throw new \Exception('Missing parent location id. This is required to create the new location.'); |
|
30
|
|
|
} |
|
31
|
|
|
if (!is_array($this->dsl['parent_location_id'])) { |
|
32
|
|
|
$this->dsl['parent_location_id'] = array($this->dsl['parent_location_id']); |
|
33
|
|
|
} |
|
34
|
|
|
foreach ($this->dsl['parent_location_id'] as $id => $parentLocationId) { |
|
35
|
|
|
if ($this->referenceResolver->isReference($parentLocationId)) { |
|
36
|
|
|
$this->dsl['parent_location_id'][$id] = $this->referenceResolver->getReferenceValue($parentLocationId); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$contentCollection = $this->matchContents('create'); |
|
41
|
|
|
|
|
42
|
|
|
$locations = null; |
|
43
|
|
|
foreach ($contentCollection as $content) { |
|
|
|
|
|
|
44
|
|
|
$contentInfo = $content->contentInfo; |
|
45
|
|
|
|
|
46
|
|
|
foreach ($this->dsl['parent_location_id'] as $parentLocationId) { |
|
|
|
|
|
|
47
|
|
|
$locationCreateStruct = $locationService->newLocationCreateStruct($parentLocationId); |
|
48
|
|
|
|
|
49
|
|
|
$locationCreateStruct->hidden = isset($this->dsl['is_hidden']) ?: false; |
|
50
|
|
|
|
|
51
|
|
|
if (isset($this->dsl['priority'])) { |
|
52
|
|
|
$locationCreateStruct->priority = $this->dsl['priority']; |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$locationCreateStruct->sortOrder = $this->getSortOrder(); |
|
56
|
|
|
$locationCreateStruct->sortField = $this->getSortField(); |
|
57
|
|
|
|
|
58
|
|
|
$locations[] = $locationService->createLocation($contentInfo, $locationCreateStruct); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->setReferences(new LocationCollection($locations)); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Updates basic information for a location like priority, sort field and sort order. |
|
67
|
|
|
* Updates the visibility of the location when needed. |
|
68
|
|
|
* Can move a location and its children to a new parent location or swap two locations. |
|
69
|
|
|
* |
|
70
|
|
|
* @todo add support for flexible matchers |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function update() |
|
73
|
|
|
{ |
|
74
|
|
|
$locationService = $this->repository->getLocationService(); |
|
75
|
|
|
|
|
76
|
|
|
if (!isset($this->dsl['location_id'])) { |
|
77
|
|
|
throw new \Exception('No location set for update.'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (isset($this->dsl['swap_with_location']) && isset($this->dsl['patent_location_id'])) { |
|
81
|
|
|
throw new \Exception('Cannot move location to a new parent and swap location with another location at the same time.'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$locationId = $this->dsl['location_id']; |
|
85
|
|
|
if ($this->referenceResolver->isReference($locationId)) { |
|
86
|
|
|
$locationId = $this->referenceResolver->getReferenceValue($locationId); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$location = $locationService->loadLocation($locationId); |
|
90
|
|
|
|
|
91
|
|
|
if (array_key_exists('priority', $this->dsl) |
|
92
|
|
|
|| array_key_exists('sort_field', $this->dsl) |
|
93
|
|
|
|| array_key_exists('sort_order', $this->dsl) |
|
94
|
|
|
|| array_key_exists('remote_id', $this->dsl) |
|
95
|
|
|
) { |
|
96
|
|
|
$locationUpdateStruct = $locationService->newLocationUpdateStruct(); |
|
97
|
|
|
|
|
98
|
|
|
if (isset($this->dsl['priority'])) { |
|
99
|
|
|
$locationUpdateStruct->priority = $this->dsl['priority']; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (isset($this->dsl['sort_field'])) { |
|
103
|
|
|
$locationUpdateStruct->sortField = $this->getSortField($location->sortField); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (isset($this->dsl['sort_order'])) { |
|
107
|
|
|
$locationUpdateStruct->sortOrder = $this->getSortOrder($location->sortOrder); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if (isset($this->dsl['remote_id'])) { |
|
111
|
|
|
$locationUpdateStruct->remoteId = $this->dsl['remote_id']; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$locationService->updateLocation($location, $locationUpdateStruct); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// Check if visibility needs to be updated |
|
118
|
|
|
if (isset($this->dsl['is_hidden'])) { |
|
119
|
|
|
if ($this->dsl['is_hidden']) { |
|
120
|
|
|
$locationService->hideLocation($location); |
|
121
|
|
|
} else { |
|
122
|
|
|
$locationService->unhideLocation($location); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Move or swap location |
|
127
|
|
|
if (isset($this->dsl['parent_location_id'])) { |
|
128
|
|
|
// Move the location and all it's children to a new parent |
|
129
|
|
|
$parentLocationId = $this->dsl['parent_location_id']; |
|
130
|
|
|
if ($this->referenceResolver->isReference($parentLocationId)) { |
|
131
|
|
|
$parentLocationId = $this->referenceResolver->getReferenceValue($parentLocationId); |
|
132
|
|
|
} |
|
133
|
|
|
$newParentLocation = $locationService->loadLocation($parentLocationId); |
|
134
|
|
|
$locationService->moveSubtree($location, $newParentLocation); |
|
135
|
|
View Code Duplication |
} elseif (isset($this->dsl['swap_with_location'])) { |
|
|
|
|
|
|
136
|
|
|
//Swap locations |
|
137
|
|
|
$swapLocationId = $this->dsl['swap_with_location']; |
|
138
|
|
|
if ($this->referenceResolver->isReference($swapLocationId)) { |
|
139
|
|
|
$swapLocationId = $this->referenceResolver->getReferenceValue($swapLocationId); |
|
140
|
|
|
} |
|
141
|
|
|
$locationToSwap = $locationService->loadLocation($swapLocationId); |
|
142
|
|
|
|
|
143
|
|
|
$locationService->swapLocation($location, $locationToSwap); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$this->setReferences($location); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Delete locations identified by their ids. |
|
151
|
|
|
* |
|
152
|
|
|
* @todo add support for flexible matchers |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function delete() |
|
155
|
|
|
{ |
|
156
|
|
|
$locationService = $this->repository->getLocationService(); |
|
157
|
|
|
|
|
158
|
|
|
if (!isset($this->dsl['location_id'])) { |
|
159
|
|
|
throw new \Exception('No location provided for deletion'); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
if (!is_array($this->dsl['location_id'])) { |
|
163
|
|
|
$this->dsl['location_id'] = array($this->dsl['location_id']); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
foreach ($this->dsl['location_id'] as $locationId) { |
|
167
|
|
|
$location = $locationService->loadLocation($locationId); |
|
168
|
|
|
$locationService->deleteLocation($location); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* NB: weirdly enough, it returns contents, not locations |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $action |
|
176
|
|
|
* @return ContentCollection |
|
177
|
|
|
* @throws \Exception |
|
178
|
|
|
*/ |
|
179
|
|
View Code Duplication |
protected function matchContents($action) |
|
|
|
|
|
|
180
|
|
|
{ |
|
181
|
|
|
if (!isset($this->dsl['object_id']) && !isset($this->dsl['remote_id']) && !isset($this->dsl['match'])) { |
|
182
|
|
|
throw new \Exception("The ID or remote ID of an object or a Match Condition is required to $action a new location."); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
// Backwards compat |
|
186
|
|
|
if (!isset($this->dsl['match'])) { |
|
187
|
|
|
if (isset($this->dsl['object_id'])) { |
|
188
|
|
|
$this->dsl['match'] = array('content_id' => $this->dsl['object_id']); |
|
189
|
|
|
} elseif (isset($this->dsl['remote_id'])) { |
|
190
|
|
|
$this->dsl['match'] = array('content_remote_id' => $this->dsl['remote_id']); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$match = $this->dsl['match']; |
|
195
|
|
|
|
|
196
|
|
|
// convert the references passed in the match |
|
197
|
|
|
foreach ($match as $condition => $values) { |
|
198
|
|
|
if (is_array($values)) { |
|
199
|
|
|
foreach ($values as $position => $value) { |
|
200
|
|
|
if ($this->referenceResolver->isReference($value)) { |
|
201
|
|
|
$match[$condition][$position] = $this->referenceResolver->getReferenceValue($value); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
} else { |
|
205
|
|
|
if ($this->referenceResolver->isReference($values)) { |
|
206
|
|
|
$match[$condition] = $this->referenceResolver->getReferenceValue($values); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
return $this->contentMatcher->matchContent($match); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
protected function getSortField($currentValue = null) |
|
215
|
|
|
{ |
|
216
|
|
|
$sortField = Location::SORT_FIELD_PUBLISHED; |
|
217
|
|
|
|
|
218
|
|
|
if (!is_null($currentValue)) { |
|
219
|
|
|
$sortField = $currentValue; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
if (isset($this->dsl['sort_field'])) { |
|
223
|
|
|
$sortFieldId = "SORT_FIELD_" . strtoupper($this->dsl['sort_field']); |
|
224
|
|
|
|
|
225
|
|
|
$ref = new \ReflectionClass('eZ\Publish\API\Repository\Values\Content\Location'); |
|
226
|
|
|
|
|
227
|
|
|
$sortField = $ref->getConstant($sortFieldId); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return $sortField; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Get the sort order based on the current value and the value in the DSL definition. |
|
235
|
|
|
* |
|
236
|
|
|
* If no current value is set and there is no value in the DSL it will default to Location::SORT_ORDER_ASC |
|
237
|
|
|
* |
|
238
|
|
|
* @see \eZ\Publish\API\Repository\Values\Content\Location::SORT_ORDER_* |
|
239
|
|
|
* |
|
240
|
|
|
* @param int $currentValue |
|
241
|
|
|
* @return int |
|
242
|
|
|
*/ |
|
243
|
|
|
protected function getSortOrder($currentValue = null) |
|
244
|
|
|
{ |
|
245
|
|
|
$sortOrder = Location::SORT_ORDER_ASC; |
|
246
|
|
|
if (!is_null($currentValue)) { |
|
247
|
|
|
$sortOrder = $currentValue; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
if (isset($this->dsl['sort_order'])) { |
|
251
|
|
|
if (strtoupper($this->dsl['sort_order']) === 'ASC') { |
|
252
|
|
|
$sortOrder = Location::SORT_ORDER_ASC; |
|
253
|
|
|
} else { |
|
254
|
|
|
$sortOrder = Location::SORT_ORDER_DESC; |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
return $sortOrder; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Sets references to object attributes |
|
263
|
|
|
* |
|
264
|
|
|
* The Location Manager currently supports setting references to location id. |
|
265
|
|
|
* |
|
266
|
|
|
* @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute. |
|
267
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location|LocationCollection $location |
|
268
|
|
|
* @return boolean |
|
269
|
|
|
*/ |
|
270
|
|
|
protected function setReferences($location) |
|
271
|
|
|
{ |
|
272
|
|
|
if (!array_key_exists('references', $this->dsl)) { |
|
273
|
|
|
return false; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
if ($location instanceof LocationCollection) { |
|
277
|
|
|
if (count($location) > 1) { |
|
278
|
|
|
throw new \InvalidArgumentException('Location Manager does not support setting references for creating/updating of multiple locations'); |
|
279
|
|
|
} |
|
280
|
|
|
$location = reset($location); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
foreach ($this->dsl['references'] as $reference) { |
|
284
|
|
|
switch ($reference['attribute']) { |
|
285
|
|
|
case 'location_id': |
|
286
|
|
|
case 'id': |
|
287
|
|
|
$value = $location->id; |
|
288
|
|
|
break; |
|
289
|
|
|
default: |
|
290
|
|
|
throw new \InvalidArgumentException('Location Manager does not support setting references for attribute ' . $reference['attribute']); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
$this->referenceResolver->addReference($reference['identifier'], $value); |
|
|
|
|
|
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
return true; |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
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.