1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the eZ\Publish\Core\Repository\LocationService class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @version //autogentag// |
10
|
|
|
*/ |
11
|
|
|
namespace eZ\Publish\Core\Repository; |
12
|
|
|
|
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct; |
14
|
|
|
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct; |
15
|
|
|
use eZ\Publish\API\Repository\Values\Content\ContentInfo; |
16
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location as APILocation; |
17
|
|
|
use eZ\Publish\API\Repository\Values\Content\LocationList; |
18
|
|
|
use eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct; |
19
|
|
|
use eZ\Publish\API\Repository\LocationService as LocationServiceInterface; |
20
|
|
|
use eZ\Publish\API\Repository\Repository as RepositoryInterface; |
21
|
|
|
use eZ\Publish\SPI\Persistence\Handler; |
22
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query; |
23
|
|
|
use eZ\Publish\API\Repository\Values\Content\LocationQuery; |
24
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion; |
25
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalAnd as CriterionLogicalAnd; |
26
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalNot as CriterionLogicalNot; |
27
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Subtree as CriterionSubtree; |
28
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\SortClause; |
29
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException; |
30
|
|
|
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue; |
31
|
|
|
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; |
32
|
|
|
use eZ\Publish\Core\Base\Exceptions\BadStateException; |
33
|
|
|
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException; |
34
|
|
|
use Exception; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Location service, used for complex subtree operations. |
38
|
|
|
* |
39
|
|
|
* @example Examples/location.php |
40
|
|
|
*/ |
41
|
|
|
class LocationService implements LocationServiceInterface |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var \eZ\Publish\Core\Repository\Repository |
45
|
|
|
*/ |
46
|
|
|
protected $repository; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \eZ\Publish\SPI\Persistence\Handler |
50
|
|
|
*/ |
51
|
|
|
protected $persistenceHandler; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $settings; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var \eZ\Publish\Core\Repository\Helper\DomainMapper |
60
|
|
|
*/ |
61
|
|
|
protected $domainMapper; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var \eZ\Publish\Core\Repository\Helper\NameSchemaService |
65
|
|
|
*/ |
66
|
|
|
protected $nameSchemaService; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var \eZ\Publish\Core\Repository\PermissionsCriterionHandler |
70
|
|
|
*/ |
71
|
|
|
protected $permissionsCriterionHandler; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Setups service with reference to repository object that created it & corresponding handler. |
75
|
|
|
* |
76
|
|
|
* @param \eZ\Publish\API\Repository\Repository $repository |
77
|
|
|
* @param \eZ\Publish\SPI\Persistence\Handler $handler |
78
|
|
|
* @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper |
79
|
|
|
* @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
80
|
|
|
* @param \eZ\Publish\Core\Repository\PermissionsCriterionHandler $permissionsCriterionHandler |
81
|
|
|
* @param array $settings |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function __construct( |
84
|
|
|
RepositoryInterface $repository, |
85
|
|
|
Handler $handler, |
86
|
|
|
Helper\DomainMapper $domainMapper, |
87
|
|
|
Helper\NameSchemaService $nameSchemaService, |
88
|
|
|
PermissionsCriterionHandler $permissionsCriterionHandler, |
89
|
|
|
array $settings = array() |
90
|
|
|
) { |
91
|
|
|
$this->repository = $repository; |
|
|
|
|
92
|
|
|
$this->persistenceHandler = $handler; |
93
|
|
|
$this->domainMapper = $domainMapper; |
94
|
|
|
$this->nameSchemaService = $nameSchemaService; |
95
|
|
|
// Union makes sure default settings are ignored if provided in argument |
96
|
|
|
$this->settings = $settings + array( |
97
|
|
|
//'defaultSetting' => array(), |
98
|
|
|
); |
99
|
|
|
$this->permissionsCriterionHandler = $permissionsCriterionHandler; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Copies the subtree starting from $subtree as a new subtree of $targetLocation. |
104
|
|
|
* |
105
|
|
|
* Only the items on which the user has read access are copied. |
106
|
|
|
* |
107
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed copy the subtree to the given parent location |
108
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree |
109
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the target location is a sub location of the given location |
110
|
|
|
* |
111
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $subtree - the subtree denoted by the location to copy |
112
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $targetParentLocation - the target parent location for the copy operation |
113
|
|
|
* |
114
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location The newly created location of the copied subtree |
115
|
|
|
*/ |
116
|
|
|
public function copySubtree(APILocation $subtree, APILocation $targetParentLocation) |
117
|
|
|
{ |
118
|
|
|
$loadedSubtree = $this->loadLocation($subtree->id); |
119
|
|
|
$loadedTargetLocation = $this->loadLocation($targetParentLocation->id); |
120
|
|
|
|
121
|
|
|
if (stripos($loadedTargetLocation->pathString, $loadedSubtree->pathString) !== false) { |
|
|
|
|
122
|
|
|
throw new InvalidArgumentException('targetParentLocation', 'target parent location is a sub location of the given subtree'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// check create permission on target |
126
|
|
|
if (!$this->repository->canUser('content', 'create', $loadedSubtree->getContentInfo(), $loadedTargetLocation)) { |
|
|
|
|
127
|
|
|
throw new UnauthorizedException('content', 'create'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** Check read access to whole source subtree |
131
|
|
|
* @var bool|\eZ\Publish\API\Repository\Values\Content\Query\Criterion |
132
|
|
|
*/ |
133
|
|
|
$contentReadCriterion = $this->permissionsCriterionHandler->getPermissionsCriterion(); |
134
|
|
View Code Duplication |
if ($contentReadCriterion === false) { |
|
|
|
|
135
|
|
|
throw new UnauthorizedException('content', 'read'); |
136
|
|
|
} elseif ($contentReadCriterion !== true) { |
137
|
|
|
// Query if there are any content in subtree current user don't have access to |
138
|
|
|
$query = new Query( |
139
|
|
|
array( |
140
|
|
|
'limit' => 0, |
141
|
|
|
'filter' => new CriterionLogicalAnd( |
142
|
|
|
array( |
143
|
|
|
new CriterionSubtree($loadedSubtree->pathString), |
|
|
|
|
144
|
|
|
new CriterionLogicalNot($contentReadCriterion), |
|
|
|
|
145
|
|
|
) |
146
|
|
|
), |
147
|
|
|
) |
148
|
|
|
); |
149
|
|
|
$result = $this->repository->getSearchService()->findContent($query, array(), false); |
150
|
|
|
if ($result->totalCount > 0) { |
151
|
|
|
throw new UnauthorizedException('content', 'read'); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$this->repository->beginTransaction(); |
156
|
|
|
try { |
157
|
|
|
$newLocation = $this->persistenceHandler->locationHandler()->copySubtree( |
158
|
|
|
$loadedSubtree->id, |
|
|
|
|
159
|
|
|
$loadedTargetLocation->id |
|
|
|
|
160
|
|
|
); |
161
|
|
|
|
162
|
|
|
$content = $this->repository->getContentService()->loadContent($newLocation->contentId); |
163
|
|
|
$urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content); |
164
|
|
View Code Duplication |
foreach ($urlAliasNames as $languageCode => $name) { |
|
|
|
|
165
|
|
|
$this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
166
|
|
|
$newLocation->id, |
167
|
|
|
$loadedTargetLocation->id, |
|
|
|
|
168
|
|
|
$name, |
169
|
|
|
$languageCode, |
170
|
|
|
$content->contentInfo->alwaysAvailable |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$this->persistenceHandler->urlAliasHandler()->locationCopied( |
175
|
|
|
$loadedSubtree->id, |
|
|
|
|
176
|
|
|
$newLocation->id, |
177
|
|
|
$loadedTargetLocation->id |
|
|
|
|
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
$this->repository->commit(); |
181
|
|
|
} catch (Exception $e) { |
182
|
|
|
$this->repository->rollback(); |
183
|
|
|
throw $e; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $this->domainMapper->buildLocationDomainObject($newLocation); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Loads a location object from its $locationId. |
191
|
|
|
* |
192
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location |
193
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found |
194
|
|
|
* |
195
|
|
|
* @param mixed $locationId |
196
|
|
|
* |
197
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location |
198
|
|
|
*/ |
199
|
|
|
public function loadLocation($locationId) |
200
|
|
|
{ |
201
|
|
|
$spiLocation = $this->persistenceHandler->locationHandler()->load($locationId); |
202
|
|
|
$location = $this->domainMapper->buildLocationDomainObject($spiLocation); |
203
|
|
|
if (!$this->repository->canUser('content', 'read', $location->getContentInfo(), $location)) { |
|
|
|
|
204
|
|
|
throw new UnauthorizedException('content', 'read'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $location; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Loads a location object from its $remoteId. |
212
|
|
|
* |
213
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location |
214
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If more than one location with same remote ID was found |
215
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found |
216
|
|
|
* |
217
|
|
|
* @param string $remoteId |
218
|
|
|
* |
219
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location |
220
|
|
|
*/ |
221
|
|
|
public function loadLocationByRemoteId($remoteId) |
222
|
|
|
{ |
223
|
|
|
if (!is_string($remoteId)) { |
224
|
|
|
throw new InvalidArgumentValue('remoteId', $remoteId); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$spiLocation = $this->persistenceHandler->locationHandler()->loadByRemoteId($remoteId); |
228
|
|
|
$location = $this->domainMapper->buildLocationDomainObject($spiLocation); |
229
|
|
|
if (!$this->repository->canUser('content', 'read', $location->getContentInfo(), $location)) { |
|
|
|
|
230
|
|
|
throw new UnauthorizedException('content', 'read'); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $location; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Loads the locations for the given content object. |
238
|
|
|
* |
239
|
|
|
* If a $rootLocation is given, only locations that belong to this location are returned. |
240
|
|
|
* The location list is also filtered by permissions on reading locations. |
241
|
|
|
* |
242
|
|
|
* @todo permissions check is missing |
243
|
|
|
* |
244
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if there is no published version yet |
245
|
|
|
* |
246
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
247
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $rootLocation |
248
|
|
|
* |
249
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location[] An array of {@link Location} |
250
|
|
|
*/ |
251
|
|
|
public function loadLocations(ContentInfo $contentInfo, APILocation $rootLocation = null) |
252
|
|
|
{ |
253
|
|
|
if (!$contentInfo->published) { |
254
|
|
|
throw new BadStateException('$contentInfo', 'ContentInfo has no published versions'); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$spiLocations = $this->persistenceHandler->locationHandler()->loadLocationsByContent( |
258
|
|
|
$contentInfo->id, |
259
|
|
|
$rootLocation !== null ? $rootLocation->id : null |
260
|
|
|
); |
261
|
|
|
|
262
|
|
|
$locations = array(); |
263
|
|
|
foreach ($spiLocations as $spiLocation) { |
264
|
|
|
$locations[] = $this->domainMapper->buildLocationDomainObject($spiLocation); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return $locations; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Loads children which are readable by the current user of a location object sorted by sortField and sortOrder. |
272
|
|
|
* |
273
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
274
|
|
|
* @param int $offset the start offset for paging |
275
|
|
|
* @param int $limit the number of locations returned |
276
|
|
|
* |
277
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\LocationList |
278
|
|
|
*/ |
279
|
|
|
public function loadLocationChildren(APILocation $location, $offset = 0, $limit = 25) |
280
|
|
|
{ |
281
|
|
|
if (!$this->domainMapper->isValidLocationSortField($location->sortField)) { |
282
|
|
|
throw new InvalidArgumentValue('sortField', $location->sortField, 'Location'); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
if (!$this->domainMapper->isValidLocationSortOrder($location->sortOrder)) { |
286
|
|
|
throw new InvalidArgumentValue('sortOrder', $location->sortOrder, 'Location'); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
if (!is_int($offset)) { |
290
|
|
|
throw new InvalidArgumentValue('offset', $offset); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
if (!is_int($limit)) { |
294
|
|
|
throw new InvalidArgumentValue('limit', $limit); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
$childLocations = array(); |
298
|
|
|
$searchResult = $this->searchChildrenLocations( |
299
|
|
|
$location->id, |
300
|
|
|
$location->sortField, |
301
|
|
|
$location->sortOrder, |
302
|
|
|
$offset, |
303
|
|
|
$limit |
304
|
|
|
); |
305
|
|
|
foreach ($searchResult->searchHits as $searchHit) { |
306
|
|
|
$childLocations[] = $searchHit->valueObject; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return new LocationList( |
310
|
|
|
array( |
311
|
|
|
'locations' => $childLocations, |
312
|
|
|
'totalCount' => $searchResult->totalCount, |
313
|
|
|
) |
314
|
|
|
); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Returns the number of children which are readable by the current user of a location object. |
319
|
|
|
* |
320
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
321
|
|
|
* |
322
|
|
|
* @return int |
323
|
|
|
*/ |
324
|
|
|
public function getLocationChildCount(APILocation $location) |
325
|
|
|
{ |
326
|
|
|
$searchResult = $this->searchChildrenLocations( |
327
|
|
|
$location->id, |
328
|
|
|
$location->sortField, |
329
|
|
|
$location->sortOrder, |
330
|
|
|
0, |
331
|
|
|
0 |
332
|
|
|
); |
333
|
|
|
|
334
|
|
|
return $searchResult->totalCount; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Searches children locations of the provided parent location id. |
339
|
|
|
* |
340
|
|
|
* @param mixed $parentLocationId |
341
|
|
|
* @param int $sortField |
342
|
|
|
* @param int $sortOrder |
343
|
|
|
* @param int $offset |
344
|
|
|
* @param int $limit |
345
|
|
|
* |
346
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
347
|
|
|
*/ |
348
|
|
|
protected function searchChildrenLocations( |
349
|
|
|
$parentLocationId, |
350
|
|
|
$sortField = null, |
351
|
|
|
$sortOrder = APILocation::SORT_ORDER_ASC, |
352
|
|
|
$offset = 0, |
353
|
|
|
$limit = -1 |
354
|
|
|
) { |
355
|
|
|
$query = new LocationQuery( |
356
|
|
|
array( |
357
|
|
|
'filter' => new Criterion\ParentLocationId($parentLocationId), |
358
|
|
|
'offset' => $offset >= 0 ? (int)$offset : 0, |
359
|
|
|
'limit' => $limit >= 0 ? (int)$limit : null, |
360
|
|
|
) |
361
|
|
|
); |
362
|
|
|
|
363
|
|
|
if ($sortField !== null) { |
364
|
|
|
$query->sortClauses = array($this->getSortClauseBySortField($sortField, $sortOrder)); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
return $this->repository->getSearchService()->findLocations($query); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Creates the new $location in the content repository for the given content. |
372
|
|
|
* |
373
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to create this location |
374
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the content is already below the specified parent |
375
|
|
|
* or the parent is a sub location of the location of the content |
376
|
|
|
* or if set the remoteId exists already |
377
|
|
|
* |
378
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
379
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct |
380
|
|
|
* |
381
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location the newly created Location |
382
|
|
|
*/ |
383
|
|
|
public function createLocation(ContentInfo $contentInfo, LocationCreateStruct $locationCreateStruct) |
384
|
|
|
{ |
385
|
|
|
$content = $this->repository->getContentService()->loadContent($contentInfo->id); |
386
|
|
|
$parentLocation = $this->loadLocation($locationCreateStruct->parentLocationId); |
387
|
|
|
|
388
|
|
|
if (!$this->repository->canUser('content', 'create', $content->contentInfo, $parentLocation)) { |
|
|
|
|
389
|
|
|
throw new UnauthorizedException('content', 'create'); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
// Check if the parent is a sub location of one of the existing content locations (this also solves the |
393
|
|
|
// situation where parent location actually one of the content locations), |
394
|
|
|
// or if the content already has location below given location create struct parent |
395
|
|
|
$existingContentLocations = $this->loadLocations($content->contentInfo); |
396
|
|
|
if (!empty($existingContentLocations)) { |
397
|
|
|
foreach ($existingContentLocations as $existingContentLocation) { |
398
|
|
|
if (stripos($parentLocation->pathString, $existingContentLocation->pathString) !== false) { |
|
|
|
|
399
|
|
|
throw new InvalidArgumentException( |
400
|
|
|
'$locationCreateStruct', |
401
|
|
|
'Specified parent is a sub location of one of the existing content locations.' |
402
|
|
|
); |
403
|
|
|
} |
404
|
|
|
if ($parentLocation->id == $existingContentLocation->parentLocationId) { |
|
|
|
|
405
|
|
|
throw new InvalidArgumentException( |
406
|
|
|
'$locationCreateStruct', |
407
|
|
|
'Content is already below the specified parent.' |
408
|
|
|
); |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
$spiLocationCreateStruct = $this->domainMapper->buildSPILocationCreateStruct( |
414
|
|
|
$locationCreateStruct, |
415
|
|
|
$parentLocation, |
416
|
|
|
$content->contentInfo->mainLocationId !== null ? $content->contentInfo->mainLocationId : true, |
417
|
|
|
$content->contentInfo->id, |
418
|
|
|
$content->contentInfo->currentVersionNo |
419
|
|
|
); |
420
|
|
|
|
421
|
|
|
$this->repository->beginTransaction(); |
422
|
|
|
try { |
423
|
|
|
$newLocation = $this->persistenceHandler->locationHandler()->create($spiLocationCreateStruct); |
424
|
|
|
|
425
|
|
|
$urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content); |
426
|
|
View Code Duplication |
foreach ($urlAliasNames as $languageCode => $name) { |
|
|
|
|
427
|
|
|
$this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
428
|
|
|
$newLocation->id, |
429
|
|
|
$newLocation->parentId, |
430
|
|
|
$name, |
431
|
|
|
$languageCode, |
432
|
|
|
$content->contentInfo->alwaysAvailable, |
433
|
|
|
// @todo: this is legacy storage specific for updating ezcontentobject_tree.path_identification_string, to be removed |
434
|
|
|
$languageCode === $content->contentInfo->mainLanguageCode |
435
|
|
|
); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
$this->repository->commit(); |
439
|
|
|
} catch (Exception $e) { |
440
|
|
|
$this->repository->rollback(); |
441
|
|
|
throw $e; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
return $this->domainMapper->buildLocationDomainObject($newLocation); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Updates $location in the content repository. |
449
|
|
|
* |
450
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to update this location |
451
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if if set the remoteId exists already |
452
|
|
|
* |
453
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
454
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct $locationUpdateStruct |
455
|
|
|
* |
456
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location the updated Location |
457
|
|
|
*/ |
458
|
|
|
public function updateLocation(APILocation $location, LocationUpdateStruct $locationUpdateStruct) |
459
|
|
|
{ |
460
|
|
|
if ($locationUpdateStruct->priority !== null && !is_int($locationUpdateStruct->priority)) { |
461
|
|
|
throw new InvalidArgumentValue('priority', $locationUpdateStruct->priority, 'LocationUpdateStruct'); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
if ($locationUpdateStruct->remoteId !== null && (!is_string($locationUpdateStruct->remoteId) || empty($locationUpdateStruct->remoteId))) { |
465
|
|
|
throw new InvalidArgumentValue('remoteId', $locationUpdateStruct->remoteId, 'LocationUpdateStruct'); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
if ($locationUpdateStruct->sortField !== null && !$this->domainMapper->isValidLocationSortField($locationUpdateStruct->sortField)) { |
469
|
|
|
throw new InvalidArgumentValue('sortField', $locationUpdateStruct->sortField, 'LocationUpdateStruct'); |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
if ($locationUpdateStruct->sortOrder !== null && !$this->domainMapper->isValidLocationSortOrder($locationUpdateStruct->sortOrder)) { |
473
|
|
|
throw new InvalidArgumentValue('sortOrder', $locationUpdateStruct->sortOrder, 'LocationUpdateStruct'); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
$loadedLocation = $this->loadLocation($location->id); |
477
|
|
|
|
478
|
|
|
if ($locationUpdateStruct->remoteId !== null) { |
479
|
|
|
try { |
480
|
|
|
$existingLocation = $this->loadLocationByRemoteId($locationUpdateStruct->remoteId); |
481
|
|
|
if ($existingLocation !== null && $existingLocation->id !== $loadedLocation->id) { |
|
|
|
|
482
|
|
|
throw new InvalidArgumentException('locationUpdateStruct', 'location with provided remote ID already exists'); |
483
|
|
|
} |
484
|
|
|
} catch (APINotFoundException $e) { |
|
|
|
|
485
|
|
|
} |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
if (!$this->repository->canUser('content', 'edit', $loadedLocation->getContentInfo(), $loadedLocation)) { |
|
|
|
|
489
|
|
|
throw new UnauthorizedException('content', 'edit'); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
$updateStruct = new UpdateStruct(); |
493
|
|
|
$updateStruct->priority = $locationUpdateStruct->priority !== null ? $locationUpdateStruct->priority : $loadedLocation->priority; |
|
|
|
|
494
|
|
|
$updateStruct->remoteId = $locationUpdateStruct->remoteId !== null ? trim($locationUpdateStruct->remoteId) : $loadedLocation->remoteId; |
|
|
|
|
495
|
|
|
$updateStruct->sortField = $locationUpdateStruct->sortField !== null ? $locationUpdateStruct->sortField : $loadedLocation->sortField; |
|
|
|
|
496
|
|
|
$updateStruct->sortOrder = $locationUpdateStruct->sortOrder !== null ? $locationUpdateStruct->sortOrder : $loadedLocation->sortOrder; |
|
|
|
|
497
|
|
|
|
498
|
|
|
$this->repository->beginTransaction(); |
499
|
|
|
try { |
500
|
|
|
$this->persistenceHandler->locationHandler()->update($updateStruct, $loadedLocation->id); |
|
|
|
|
501
|
|
|
$this->repository->commit(); |
502
|
|
|
} catch (Exception $e) { |
503
|
|
|
$this->repository->rollback(); |
504
|
|
|
throw $e; |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
return $this->loadLocation($loadedLocation->id); |
|
|
|
|
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* Swaps the contents held by $location1 and $location2. |
512
|
|
|
* |
513
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to swap content |
514
|
|
|
* |
515
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location1 |
516
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location2 |
517
|
|
|
*/ |
518
|
|
|
public function swapLocation(APILocation $location1, APILocation $location2) |
519
|
|
|
{ |
520
|
|
|
$loadedLocation1 = $this->loadLocation($location1->id); |
521
|
|
|
$loadedLocation2 = $this->loadLocation($location2->id); |
522
|
|
|
|
523
|
|
|
if (!$this->repository->canUser('content', 'edit', $loadedLocation1->getContentInfo(), $loadedLocation1)) { |
|
|
|
|
524
|
|
|
throw new UnauthorizedException('content', 'edit'); |
525
|
|
|
} |
526
|
|
|
if (!$this->repository->canUser('content', 'edit', $loadedLocation2->getContentInfo(), $loadedLocation2)) { |
|
|
|
|
527
|
|
|
throw new UnauthorizedException('content', 'edit'); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
$this->repository->beginTransaction(); |
531
|
|
|
try { |
532
|
|
|
$this->persistenceHandler->locationHandler()->swap($loadedLocation1->id, $loadedLocation2->id); |
|
|
|
|
533
|
|
|
$this->persistenceHandler->urlAliasHandler()->locationSwapped( |
534
|
|
|
$location1->id, |
535
|
|
|
$location1->parentLocationId, |
536
|
|
|
$location2->id, |
537
|
|
|
$location2->parentLocationId |
538
|
|
|
); |
539
|
|
|
$this->repository->commit(); |
540
|
|
|
} catch (Exception $e) { |
541
|
|
|
$this->repository->rollback(); |
542
|
|
|
throw $e; |
543
|
|
|
} |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* Hides the $location and marks invisible all descendants of $location. |
548
|
|
|
* |
549
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to hide this location |
550
|
|
|
* |
551
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
552
|
|
|
* |
553
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value |
554
|
|
|
*/ |
555
|
|
View Code Duplication |
public function hideLocation(APILocation $location) |
556
|
|
|
{ |
557
|
|
|
if (!$this->repository->canUser('content', 'hide', $location->getContentInfo(), $location)) { |
|
|
|
|
558
|
|
|
throw new UnauthorizedException('content', 'hide'); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
$this->repository->beginTransaction(); |
562
|
|
|
try { |
563
|
|
|
$this->persistenceHandler->locationHandler()->hide($location->id); |
564
|
|
|
$this->repository->commit(); |
565
|
|
|
} catch (Exception $e) { |
566
|
|
|
$this->repository->rollback(); |
567
|
|
|
throw $e; |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
return $this->loadLocation($location->id); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* Unhides the $location. |
575
|
|
|
* |
576
|
|
|
* This method and marks visible all descendants of $locations |
577
|
|
|
* until a hidden location is found. |
578
|
|
|
* |
579
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to unhide this location |
580
|
|
|
* |
581
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
582
|
|
|
* |
583
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value |
584
|
|
|
*/ |
585
|
|
View Code Duplication |
public function unhideLocation(APILocation $location) |
586
|
|
|
{ |
587
|
|
|
if (!$this->repository->canUser('content', 'hide', $location->getContentInfo(), $location)) { |
|
|
|
|
588
|
|
|
throw new UnauthorizedException('content', 'hide'); |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
$this->repository->beginTransaction(); |
592
|
|
|
try { |
593
|
|
|
$this->persistenceHandler->locationHandler()->unHide($location->id); |
594
|
|
|
$this->repository->commit(); |
595
|
|
|
} catch (Exception $e) { |
596
|
|
|
$this->repository->rollback(); |
597
|
|
|
throw $e; |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
return $this->loadLocation($location->id); |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
/** |
604
|
|
|
* Moves the subtree to $newParentLocation. |
605
|
|
|
* |
606
|
|
|
* If a user has the permission to move the location to a target location |
607
|
|
|
* he can do it regardless of an existing descendant on which the user has no permission. |
608
|
|
|
* |
609
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to move this location to the target |
610
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree |
611
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the new parent is in a subtree of the location |
612
|
|
|
* |
613
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
614
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
615
|
|
|
*/ |
616
|
|
|
public function moveSubtree(APILocation $location, APILocation $newParentLocation) |
617
|
|
|
{ |
618
|
|
|
$location = $this->loadLocation($location->id); |
619
|
|
|
$newParentLocation = $this->loadLocation($newParentLocation->id); |
620
|
|
|
|
621
|
|
|
// check create permission on target location |
622
|
|
|
if (!$this->repository->canUser('content', 'create', $location->getContentInfo(), $newParentLocation)) { |
|
|
|
|
623
|
|
|
throw new UnauthorizedException('content', 'create'); |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
/** Check read access to whole source subtree |
627
|
|
|
* @var bool|\eZ\Publish\API\Repository\Values\Content\Query\Criterion |
628
|
|
|
*/ |
629
|
|
|
$contentReadCriterion = $this->permissionsCriterionHandler->getPermissionsCriterion(); |
630
|
|
View Code Duplication |
if ($contentReadCriterion === false) { |
|
|
|
|
631
|
|
|
throw new UnauthorizedException('content', 'read'); |
632
|
|
|
} elseif ($contentReadCriterion !== true) { |
633
|
|
|
// Query if there are any content in subtree current user don't have access to |
634
|
|
|
$query = new Query( |
635
|
|
|
array( |
636
|
|
|
'limit' => 0, |
637
|
|
|
'filter' => new CriterionLogicalAnd( |
638
|
|
|
array( |
639
|
|
|
new CriterionSubtree($location->pathString), |
|
|
|
|
640
|
|
|
new CriterionLogicalNot($contentReadCriterion), |
|
|
|
|
641
|
|
|
) |
642
|
|
|
), |
643
|
|
|
) |
644
|
|
|
); |
645
|
|
|
$result = $this->repository->getSearchService()->findContent($query, array(), false); |
646
|
|
|
if ($result->totalCount > 0) { |
647
|
|
|
throw new UnauthorizedException('content', 'read'); |
648
|
|
|
} |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
if (strpos($newParentLocation->pathString, $location->pathString) === 0) { |
|
|
|
|
652
|
|
|
throw new InvalidArgumentException( |
653
|
|
|
'$newParentLocation', |
654
|
|
|
'new parent location is in a subtree of the given $location' |
655
|
|
|
); |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
$this->repository->beginTransaction(); |
659
|
|
|
try { |
660
|
|
|
$this->persistenceHandler->locationHandler()->move($location->id, $newParentLocation->id); |
|
|
|
|
661
|
|
|
|
662
|
|
|
$content = $this->repository->getContentService()->loadContent($location->contentId); |
663
|
|
|
$urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content); |
664
|
|
View Code Duplication |
foreach ($urlAliasNames as $languageCode => $name) { |
|
|
|
|
665
|
|
|
$this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
666
|
|
|
$location->id, |
|
|
|
|
667
|
|
|
$newParentLocation->id, |
|
|
|
|
668
|
|
|
$name, |
669
|
|
|
$languageCode, |
670
|
|
|
$content->contentInfo->alwaysAvailable |
671
|
|
|
); |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
$this->persistenceHandler->urlAliasHandler()->locationMoved( |
675
|
|
|
$location->id, |
|
|
|
|
676
|
|
|
$location->parentLocationId, |
|
|
|
|
677
|
|
|
$newParentLocation->id |
|
|
|
|
678
|
|
|
); |
679
|
|
|
|
680
|
|
|
$this->repository->commit(); |
681
|
|
|
} catch (Exception $e) { |
682
|
|
|
$this->repository->rollback(); |
683
|
|
|
throw $e; |
684
|
|
|
} |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* Deletes $location and all its descendants. |
689
|
|
|
* |
690
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to delete this location or a descendant |
691
|
|
|
* |
692
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
693
|
|
|
*/ |
694
|
|
|
public function deleteLocation(APILocation $location) |
695
|
|
|
{ |
696
|
|
|
$location = $this->loadLocation($location->id); |
697
|
|
|
|
698
|
|
|
if (!$this->repository->canUser('content', 'manage_locations', $location->getContentInfo())) { |
|
|
|
|
699
|
|
|
throw new UnauthorizedException('content', 'manage_locations'); |
700
|
|
|
} |
701
|
|
|
if (!$this->repository->canUser('content', 'remove', $location->getContentInfo(), $location)) { |
|
|
|
|
702
|
|
|
throw new UnauthorizedException('content', 'remove'); |
703
|
|
|
} |
704
|
|
|
|
705
|
|
|
/** Check remove access to descendants |
706
|
|
|
* @var bool|\eZ\Publish\API\Repository\Values\Content\Query\Criterion |
707
|
|
|
*/ |
708
|
|
|
$contentReadCriterion = $this->permissionsCriterionHandler->getPermissionsCriterion('content', 'remove'); |
709
|
|
View Code Duplication |
if ($contentReadCriterion === false) { |
|
|
|
|
710
|
|
|
throw new UnauthorizedException('content', 'remove'); |
711
|
|
|
} elseif ($contentReadCriterion !== true) { |
712
|
|
|
// Query if there are any content in subtree current user don't have access to |
713
|
|
|
$query = new Query( |
714
|
|
|
array( |
715
|
|
|
'limit' => 0, |
716
|
|
|
'filter' => new CriterionLogicalAnd( |
717
|
|
|
array( |
718
|
|
|
new CriterionSubtree($location->pathString), |
|
|
|
|
719
|
|
|
new CriterionLogicalNot($contentReadCriterion), |
|
|
|
|
720
|
|
|
) |
721
|
|
|
), |
722
|
|
|
) |
723
|
|
|
); |
724
|
|
|
$result = $this->repository->getSearchService()->findContent($query, array(), false); |
725
|
|
|
if ($result->totalCount > 0) { |
726
|
|
|
throw new UnauthorizedException('content', 'remove'); |
727
|
|
|
} |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
$this->repository->beginTransaction(); |
731
|
|
|
try { |
732
|
|
|
$this->persistenceHandler->locationHandler()->removeSubtree($location->id); |
|
|
|
|
733
|
|
|
$this->persistenceHandler->urlAliasHandler()->locationDeleted($location->id); |
|
|
|
|
734
|
|
|
$this->repository->commit(); |
735
|
|
|
} catch (Exception $e) { |
736
|
|
|
$this->repository->rollback(); |
737
|
|
|
throw $e; |
738
|
|
|
} |
739
|
|
|
} |
740
|
|
|
|
741
|
|
|
/** |
742
|
|
|
* Instantiates a new location create class. |
743
|
|
|
* |
744
|
|
|
* @param mixed $parentLocationId the parent under which the new location should be created |
745
|
|
|
* |
746
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct |
747
|
|
|
*/ |
748
|
|
|
public function newLocationCreateStruct($parentLocationId) |
749
|
|
|
{ |
750
|
|
|
return new LocationCreateStruct( |
751
|
|
|
array( |
752
|
|
|
'parentLocationId' => $parentLocationId, |
753
|
|
|
) |
754
|
|
|
); |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
/** |
758
|
|
|
* Instantiates a new location update class. |
759
|
|
|
* |
760
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct |
761
|
|
|
*/ |
762
|
|
|
public function newLocationUpdateStruct() |
763
|
|
|
{ |
764
|
|
|
return new LocationUpdateStruct(); |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
/** |
768
|
|
|
* Instantiates a correct sort clause object based on provided location sort field and sort order. |
769
|
|
|
* |
770
|
|
|
* @param int $sortField |
771
|
|
|
* @param int $sortOrder |
772
|
|
|
* |
773
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Query\SortClause |
774
|
|
|
*/ |
775
|
|
|
protected function getSortClauseBySortField($sortField, $sortOrder = APILocation::SORT_ORDER_ASC) |
776
|
|
|
{ |
777
|
|
|
$sortOrder = $sortOrder == APILocation::SORT_ORDER_DESC ? Query::SORT_DESC : Query::SORT_ASC; |
778
|
|
|
switch ($sortField) { |
779
|
|
|
case APILocation::SORT_FIELD_PATH: |
780
|
|
|
return new SortClause\Location\Path($sortOrder); |
781
|
|
|
|
782
|
|
|
case APILocation::SORT_FIELD_PUBLISHED: |
783
|
|
|
return new SortClause\DatePublished($sortOrder); |
784
|
|
|
|
785
|
|
|
case APILocation::SORT_FIELD_MODIFIED: |
786
|
|
|
return new SortClause\DateModified($sortOrder); |
787
|
|
|
|
788
|
|
|
case APILocation::SORT_FIELD_SECTION: |
789
|
|
|
return new SortClause\SectionIdentifier($sortOrder); |
790
|
|
|
|
791
|
|
|
case APILocation::SORT_FIELD_DEPTH: |
792
|
|
|
return new SortClause\Location\Depth($sortOrder); |
793
|
|
|
|
794
|
|
|
//@todo: sort clause not yet implemented |
795
|
|
|
// case APILocation::SORT_FIELD_CLASS_IDENTIFIER: |
796
|
|
|
|
797
|
|
|
//@todo: sort clause not yet implemented |
798
|
|
|
// case APILocation::SORT_FIELD_CLASS_NAME: |
799
|
|
|
|
800
|
|
|
case APILocation::SORT_FIELD_PRIORITY: |
801
|
|
|
return new SortClause\Location\Priority($sortOrder); |
802
|
|
|
|
803
|
|
|
case APILocation::SORT_FIELD_NAME: |
804
|
|
|
return new SortClause\ContentName($sortOrder); |
805
|
|
|
|
806
|
|
|
//@todo: sort clause not yet implemented |
807
|
|
|
// case APILocation::SORT_FIELD_MODIFIED_SUBNODE: |
808
|
|
|
|
809
|
|
|
case APILocation::SORT_FIELD_NODE_ID: |
810
|
|
|
return new SortClause\Location\Id($sortOrder); |
811
|
|
|
|
812
|
|
|
case APILocation::SORT_FIELD_CONTENTOBJECT_ID: |
813
|
|
|
return new SortClause\ContentId($sortOrder); |
814
|
|
|
|
815
|
|
|
default: |
816
|
|
|
return new SortClause\Location\Path($sortOrder); |
817
|
|
|
} |
818
|
|
|
} |
819
|
|
|
} |
820
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.