Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Core/REST/Server/Controller/Location.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\REST\Server\Controller;
8
9 use eZ\Publish\API\Repository\URLAliasService;
10 use eZ\Publish\Core\REST\Common\Message;
11 use eZ\Publish\Core\REST\Common\Exceptions;
12 use eZ\Publish\Core\REST\Server\Values;
13 use eZ\Publish\Core\REST\Server\Controller as RestController;
14 use eZ\Publish\API\Repository\LocationService;
15 use eZ\Publish\API\Repository\ContentService;
16 use eZ\Publish\API\Repository\TrashService;
17 use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
18 use eZ\Publish\Core\REST\Server\Exceptions\BadRequestException;
19 use eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException;
20 use Symfony\Component\HttpFoundation\Request;
21
25 class Location extends RestController
26 {
32 protected $locationService;
33
39 protected $contentService;
40
46 protected $trashService;
47
53 protected $urlAliasService;
54
63 public function __construct(
64 LocationService $locationService,
65 ContentService $contentService,
66 TrashService $trashService,
67 URLAliasService $urlAliasService
68 ) {
69 $this->locationService = $locationService;
70 $this->contentService = $contentService;
71 $this->trashService = $trashService;
72 $this->urlAliasService = $urlAliasService;
73 }
74
82 public function redirectLocation(Request $request)
83 {
84 if ($request->query->has('id')) {
85 $location = $this->locationService->loadLocation($request->query->get('id'));
86 } elseif ($request->query->has('remoteId')) {
87 $location = $this->locationService->loadLocationByRemoteId($request->query->get('remoteId'));
88 } elseif ($request->query->has('urlAlias')) {
89 $urlAlias = $this->urlAliasService->lookup($request->query->get('urlAlias'));
90 $location = $this->locationService->loadLocation($urlAlias->destination);
91 } else {
92 throw new BadRequestException("At least one of 'id', 'remoteId' or 'urlAlias' parameters is required.");
93 }
94
95 return new Values\TemporaryRedirect(
96 $this->router->generate(
97 'ezpublish_rest_loadLocation',
98 [
99 'locationPath' => trim($location->pathString, '/'),
100 ]
101 )
102 );
103 }
104
114 public function createLocation($contentId, Request $request)
115 {
116 $locationCreateStruct = $this->inputDispatcher->parse(
117 new Message(
118 ['Content-Type' => $request->headers->get('Content-Type')],
119 $request->getContent()
120 )
121 );
122
123 $contentInfo = $this->contentService->loadContentInfo($contentId);
124
125 try {
126 $createdLocation = $this->locationService->createLocation($contentInfo, $locationCreateStruct);
127 } catch (InvalidArgumentException $e) {
128 throw new ForbiddenException($e->getMessage());
129 }
130
131 return new Values\CreatedLocation(['restLocation' => new Values\RestLocation($createdLocation, 0)]);
132 }
133
141 public function loadLocation($locationPath)
142 {
143 $location = $this->locationService->loadLocation(
144 $this->extractLocationIdFromPath($locationPath)
145 );
146
147 if (trim($location->pathString, '/') != $locationPath) {
148 throw new Exceptions\NotFoundException(
149 "Could not find location with path string $locationPath"
150 );
151 }
152
153 return new Values\CachedValue(
154 new Values\RestLocation(
155 $location,
156 $this->locationService->getLocationChildCount($location)
157 ),
158 ['locationId' => $location->id]
159 );
160 }
161
169 public function deleteSubtree($locationPath)
170 {
171 $location = $this->locationService->loadLocation(
172 $this->extractLocationIdFromPath($locationPath)
173 );
174 $this->locationService->deleteLocation($location);
175
176 return new Values\NoContent();
177 }
178
186 public function copySubtree($locationPath, Request $request)
187 {
188 $location = $this->locationService->loadLocation(
189 $this->extractLocationIdFromPath($locationPath)
190 );
191
192 $destinationLocation = $this->locationService->loadLocation(
193 $this->extractLocationIdFromPath(
194 $this->requestParser->parseHref(
195 $request->headers->get('Destination'),
196 'locationPath'
197 )
198 )
199 );
200
201 $newLocation = $this->locationService->copySubtree($location, $destinationLocation);
202
203 return new Values\ResourceCreated(
204 $this->router->generate(
205 'ezpublish_rest_loadLocation',
206 [
207 'locationPath' => trim($newLocation->pathString, '/'),
208 ]
209 )
210 );
211 }
212
222 public function moveSubtree($locationPath, Request $request)
223 {
224 $locationToMove = $this->locationService->loadLocation(
225 $this->extractLocationIdFromPath($locationPath)
226 );
227
228 $destinationLocationId = null;
229 $destinationHref = $request->headers->get('Destination');
230 try {
231 // First check to see if the destination is for moving within another subtree
232 $destinationLocationId = $this->extractLocationIdFromPath(
233 $this->requestParser->parseHref($destinationHref, 'locationPath')
234 );
235
236 // We're moving the subtree
237 $destinationLocation = $this->locationService->loadLocation($destinationLocationId);
238 $this->locationService->moveSubtree($locationToMove, $destinationLocation);
239
240 // Reload the location to get the new position is subtree
241 $locationToMove = $this->locationService->loadLocation($locationToMove->id);
242
243 return new Values\ResourceCreated(
244 $this->router->generate(
245 'ezpublish_rest_loadLocation',
246 [
247 'locationPath' => trim($locationToMove->pathString, '/'),
248 ]
249 )
250 );
251 } catch (Exceptions\InvalidArgumentException $e) {
252 // If parsing of destination fails, let's try to see if destination is trash
253 try {
254 $route = $this->requestParser->parse($destinationHref);
255 if (!isset($route['_route']) || $route['_route'] !== 'ezpublish_rest_loadTrashItems') {
256 throw new Exceptions\InvalidArgumentException('');
257 }
258 // Trash the subtree
259 $trashItem = $this->trashService->trash($locationToMove);
260
261 if (isset($trashItem)) {
262 return new Values\ResourceCreated(
263 $this->router->generate(
264 'ezpublish_rest_loadTrashItem',
265 ['trashItemId' => $trashItem->id]
266 )
267 );
268 } else {
269 // Only a location has been trashed and not the object
270 return new Values\NoContent();
271 }
272 } catch (Exceptions\InvalidArgumentException $e) {
273 // If that fails, the Destination header is not formatted right
274 // so just throw the BadRequestException
275 throw new BadRequestException("{$destinationHref} is not an acceptable destination");
276 }
277 }
278 }
279
287 public function swapLocation($locationPath, Request $request)
288 {
289 $locationId = $this->extractLocationIdFromPath($locationPath);
290 $location = $this->locationService->loadLocation($locationId);
291
292 $destinationLocation = $this->locationService->loadLocation(
293 $this->extractLocationIdFromPath(
294 $this->requestParser->parseHref(
295 $request->headers->get('Destination'),
296 'locationPath'
297 )
298 )
299 );
300
301 $this->locationService->swapLocation($location, $destinationLocation);
302
303 return new Values\NoContent();
304 }
305
313 public function loadLocationByRemoteId(Request $request)
314 {
315 return new Values\LocationList(
316 [
317 new Values\RestLocation(
318 $location = $this->locationService->loadLocationByRemoteId(
319 $request->query->get('remoteId')
320 ),
321 $this->locationService->getLocationChildCount($location)
322 ),
323 ],
324 $request->getPathInfo()
325 );
326 }
327
335 public function loadLocationsForContent($contentId, Request $request)
336 {
337 $restLocations = [];
338 $contentInfo = $this->contentService->loadContentInfo($contentId);
339 foreach ($this->locationService->loadLocations($contentInfo) as $location) {
340 $restLocations[] = new Values\RestLocation(
341 $location,
342 // @todo Remove, and make optional in VO. Not needed for a location list.
343 $this->locationService->getLocationChildCount($location)
344 );
345 }
346
347 return new Values\CachedValue(
348 new Values\LocationList($restLocations, $request->getPathInfo()),
349 ['locationId' => $contentInfo->mainLocationId]
350 );
351 }
352
360 public function loadLocationChildren($locationPath, Request $request)
361 {
362 $offset = $request->query->has('offset') ? (int)$request->query->get('offset') : 0;
363 $limit = $request->query->has('limit') ? (int)$request->query->get('limit') : 10;
364
365 $restLocations = [];
366 $locationId = $this->extractLocationIdFromPath($locationPath);
367 $children = $this->locationService->loadLocationChildren(
368 $this->locationService->loadLocation($locationId),
369 $offset >= 0 ? $offset : 0,
370 $limit >= 0 ? $limit : 25
371 )->locations;
372 foreach ($children as $location) {
373 $restLocations[] = new Values\RestLocation(
374 $location,
375 $this->locationService->getLocationChildCount($location)
376 );
377 }
378
379 return new Values\CachedValue(
380 new Values\LocationList($restLocations, $request->getPathInfo()),
381 ['locationId' => $locationId]
382 );
383 }
384
392 private function extractLocationIdFromPath($path)
393 {
394 $pathParts = explode('/', $path);
395
396 return array_pop($pathParts);
397 }
398
406 public function updateLocation($locationPath, Request $request)
407 {
408 $locationUpdate = $this->inputDispatcher->parse(
409 new Message(
410 ['Content-Type' => $request->headers->get('Content-Type')],
411 $request->getContent()
412 )
413 );
414
415 $location = $this->locationService->loadLocation($this->extractLocationIdFromPath($locationPath));
416
417 // First handle hiding/unhiding so that updating location afterwards
418 // will return updated location with hidden/visible status correctly updated
419 // Exact check for true/false is needed as null signals that no hiding/unhiding
420 // is to be performed
421 if ($locationUpdate->hidden === true) {
422 $this->locationService->hideLocation($location);
423 } elseif ($locationUpdate->hidden === false) {
424 $this->locationService->unhideLocation($location);
425 }
426
427 return new Values\RestLocation(
428 $location = $this->locationService->updateLocation($location, $locationUpdate->locationUpdateStruct),
429 $this->locationService->getLocationChildCount($location)
430 );
431 }
432 }
eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
This exception is thrown if a service method is called with an illegal or non appropriate value.
Definition: API/Repository/Exceptions/InvalidArgumentException.php:13
eZ\Publish\API\Repository\Values\Content\Location
Definition: API/Repository/Values/Content/Location.php:32
eZ\Publish\Core\Base\Exceptions\ForbiddenException
Forbidden Exception implementation.
Definition: Core/Base/Exceptions/ForbiddenException.php:17
eZ\Publish\Core\REST\Common\Message
Simple response struct.
Definition: Core/REST/Common/Message.php:13
eZ\Publish\Core\REST\Server\Controller\Location\loadLocationByRemoteId
loadLocationByRemoteId(Request $request)
Loads a location by remote ID.
Definition: Core/REST/Server/Controller/Location.php:313
eZ\Publish\Core\REST\Server\Controller\Location\swapLocation
swapLocation($locationPath, Request $request)
Swaps a location with another one.
Definition: Core/REST/Server/Controller/Location.php:287
eZ\Publish\Core\REST\Server\Controller\Location\loadLocationsForContent
loadLocationsForContent($contentId, Request $request)
Loads all locations for content object.
Definition: Core/REST/Server/Controller/Location.php:335
eZ\Publish\Core\REST\Server\Controller\Location\$urlAliasService
$urlAliasService
Definition: Core/REST/Server/Controller/Location.php:53
eZ\Publish\Core\REST\Server\Controller\Location\updateLocation
updateLocation($locationPath, Request $request)
Updates a location.
Definition: Core/REST/Server/Controller/Location.php:406
eZ\Publish\Core\REST\Server\Controller\Location\extractLocationIdFromPath
extractLocationIdFromPath($path)
Extracts and returns an item id from a path, e.g.
Definition: Core/REST/Server/Controller/Location.php:392
eZ\Publish\Core\REST\Server\Controller\Location\createLocation
createLocation($contentId, Request $request)
Creates a new location for object with id $contentId.
Definition: Core/REST/Server/Controller/Location.php:114
eZ\Publish\Core\REST\Server\Controller\Location\loadLocation
loadLocation($locationPath)
Loads a location.
Definition: Core/REST/Server/Controller/Location.php:141
eZ\Publish\Core\REST\Server\Controller\Location\$trashService
$trashService
Definition: Core/REST/Server/Controller/Location.php:46
eZ\Publish\Core\REST\Server\Controller\Location\__construct
__construct(LocationService $locationService, ContentService $contentService, TrashService $trashService, URLAliasService $urlAliasService)
Construct controller.
Definition: Core/REST/Server/Controller/Location.php:63
eZ\Publish\Core\REST\Server\Controller\Location\$contentService
$contentService
Definition: Core/REST/Server/Controller/Location.php:39
eZ\Publish\Core\REST\Server\Controller\Location\deleteSubtree
deleteSubtree($locationPath)
Deletes a location.
Definition: Core/REST/Server/Controller/Location.php:169
eZ\Publish\Core\REST\Server\Controller\Location\copySubtree
copySubtree($locationPath, Request $request)
Copies a subtree to a new destination.
Definition: Core/REST/Server/Controller/Location.php:186
eZ\Publish\Core\REST\Server\Controller\Location\moveSubtree
moveSubtree($locationPath, Request $request)
Moves a subtree to a new location.
Definition: Core/REST/Server/Controller/Location.php:222
eZ\Publish\Core\REST\Server\Controller\Location\$locationService
$locationService
Definition: Core/REST/Server/Controller/Location.php:32
eZ\Publish\Core\REST\Server\Controller\Location\loadLocationChildren
loadLocationChildren($locationPath, Request $request)
Loads child locations of a location.
Definition: Core/REST/Server/Controller/Location.php:360
eZ\Publish\Core\REST\Server\Controller\Location\redirectLocation
redirectLocation(Request $request)
Loads the location for a given ID (x)or remote ID.
Definition: Core/REST/Server/Controller/Location.php:82
eZ\Publish\Core\REST\Server\Exceptions\BadRequestException
Exception thrown if the request is not formatted correctly.
Definition: Exceptions/BadRequestException.php:15
eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException
Exception thrown if the request is forbidden.
Definition: Core/REST/Server/Exceptions/ForbiddenException.php:15
eZ\Publish\Core\REST\Server\Values\CachedValue
Definition: Values/CachedValue.php:13
eZ\Publish\Core\REST\Server\Values\CreatedLocation
Struct representing a freshly created location.
Definition: Values/CreatedLocation.php:15
eZ\Publish\Core\REST\Server\Values\LocationList
Location list view model.
Definition: Core/REST/Server/Values/LocationList.php:15
eZ\Publish\Core\REST\Server\Values\NoContent
Definition: Values/NoContent.php:12
eZ\Publish\Core\REST\Server\Values\ResourceCreated
Definition: Values/ResourceCreated.php:12
eZ\Publish\Core\REST\Server\Values\RestLocation
RestLocation view model.
Definition: Values/RestLocation.php:16
eZ\Publish\Core\REST\Server\Values\TemporaryRedirect
Definition: Values/TemporaryRedirect.php:12
eZ\Publish\API\Repository\ContentService
Definition: API/Repository/ContentService.php:27
eZ\Publish\API\Repository\LocationService
Definition: API/Repository/LocationService.php:21
eZ\Publish\API\Repository\TrashService
Trash service, used for managing trashed content.
Definition: API/Repository/TrashService.php:17
eZ\Publish\API\Repository\URLAliasService
Definition: API/Repository/URLAliasService.php:17
eZ\Publish\Core\REST\Common\Exceptions
Definition: Core/REST/Common/Exceptions/ForbiddenException.php:7
eZ\Publish\Core\REST\Server\Controller
Definition: BinaryContent.php:7
eZ\Publish\Core\REST\Server\Values
Definition: Core/REST/Server/Values/BookmarkList.php:9
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.