|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the Trash controller 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\REST\Server\Controller; |
|
12
|
|
|
|
|
13
|
|
|
use eZ\Publish\Core\REST\Server\Values; |
|
14
|
|
|
use eZ\Publish\Core\REST\Server\Controller as RestController; |
|
15
|
|
|
use eZ\Publish\API\Repository\TrashService; |
|
16
|
|
|
use eZ\Publish\API\Repository\LocationService; |
|
17
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query; |
|
18
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
|
19
|
|
|
use eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException; |
|
20
|
|
|
use InvalidArgumentException; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Trash controller. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Trash extends RestController |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Trash service. |
|
30
|
|
|
* |
|
31
|
|
|
* @var \eZ\Publish\API\Repository\TrashService |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $trashService; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Location service. |
|
37
|
|
|
* |
|
38
|
|
|
* @var \eZ\Publish\API\Repository\LocationService |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $locationService; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Construct controller. |
|
44
|
|
|
* |
|
45
|
|
|
* @param \eZ\Publish\API\Repository\TrashService $trashService |
|
46
|
|
|
* @param \eZ\Publish\API\Repository\LocationService $locationService |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(TrashService $trashService, LocationService $locationService) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->trashService = $trashService; |
|
51
|
|
|
$this->locationService = $locationService; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns a list of all trash items. |
|
56
|
|
|
* |
|
57
|
|
|
* @return \eZ\Publish\Core\REST\Server\Values\Trash |
|
58
|
|
|
*/ |
|
59
|
|
|
public function loadTrashItems(Request $request) |
|
60
|
|
|
{ |
|
61
|
|
|
$offset = $request->query->has('offset') ? (int)$request->query->get('offset') : 0; |
|
62
|
|
|
$limit = $request->query->has('limit') ? (int)$request->query->get('limit') : -1; |
|
63
|
|
|
|
|
64
|
|
|
$query = new Query(); |
|
65
|
|
|
$query->offset = $offset >= 0 ? $offset : null; |
|
66
|
|
|
$query->limit = $limit >= 0 ? $limit : null; |
|
67
|
|
|
|
|
68
|
|
|
$trashItems = array(); |
|
69
|
|
|
|
|
70
|
|
|
foreach ($this->trashService->findTrashItems($query)->items as $trashItem) { |
|
71
|
|
|
$trashItems[] = new Values\RestTrashItem( |
|
72
|
|
|
$trashItem, |
|
|
|
|
|
|
73
|
|
|
$this->locationService->getLocationChildCount($trashItem) |
|
|
|
|
|
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return new Values\Trash( |
|
78
|
|
|
$trashItems, |
|
79
|
|
|
$request->getPathInfo() |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns the trash item given by id. |
|
85
|
|
|
* |
|
86
|
|
|
* @param $trashItemId |
|
87
|
|
|
* |
|
88
|
|
|
* @return \eZ\Publish\Core\REST\Server\Values\RestTrashItem |
|
89
|
|
|
*/ |
|
90
|
|
|
public function loadTrashItem($trashItemId) |
|
91
|
|
|
{ |
|
92
|
|
|
return new Values\RestTrashItem( |
|
93
|
|
|
$trashItem = $this->trashService->loadTrashItem($trashItemId), |
|
94
|
|
|
$this->locationService->getLocationChildCount($trashItem) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Empties the trash. |
|
100
|
|
|
* |
|
101
|
|
|
* @return \eZ\Publish\Core\REST\Server\Values\NoContent |
|
102
|
|
|
*/ |
|
103
|
|
|
public function emptyTrash() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->trashService->emptyTrash(); |
|
106
|
|
|
|
|
107
|
|
|
return new Values\NoContent(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Deletes the given trash item. |
|
112
|
|
|
* |
|
113
|
|
|
* @param $trashItemId |
|
114
|
|
|
* |
|
115
|
|
|
* @return \eZ\Publish\Core\REST\Server\Values\NoContent |
|
116
|
|
|
*/ |
|
117
|
|
|
public function deleteTrashItem($trashItemId) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->trashService->deleteTrashItem( |
|
120
|
|
|
$this->trashService->loadTrashItem($trashItemId) |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
return new Values\NoContent(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Restores a trashItem. |
|
128
|
|
|
* |
|
129
|
|
|
* @param $trashItemId |
|
130
|
|
|
* |
|
131
|
|
|
* @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException |
|
132
|
|
|
* |
|
133
|
|
|
* @return \eZ\Publish\Core\REST\Server\Values\ResourceCreated |
|
134
|
|
|
*/ |
|
135
|
|
|
public function restoreTrashItem($trashItemId, Request $request) |
|
136
|
|
|
{ |
|
137
|
|
|
$requestDestination = null; |
|
138
|
|
|
try { |
|
139
|
|
|
$requestDestination = $request->headers->get('Destination'); |
|
140
|
|
|
} catch (InvalidArgumentException $e) { |
|
141
|
|
|
// No Destination header |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$parentLocation = null; |
|
145
|
|
|
if ($request->headers->has('Destination')) { |
|
146
|
|
|
$locationPathParts = explode( |
|
147
|
|
|
'/', |
|
148
|
|
|
$this->requestParser->parseHref($request->headers->get('Destination'), 'locationPath') |
|
|
|
|
|
|
149
|
|
|
); |
|
150
|
|
|
|
|
151
|
|
|
try { |
|
152
|
|
|
$parentLocation = $this->locationService->loadLocation(array_pop($locationPathParts)); |
|
153
|
|
|
} catch (NotFoundException $e) { |
|
154
|
|
|
throw new ForbiddenException($e->getMessage()); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$trashItem = $this->trashService->loadTrashItem($trashItemId); |
|
159
|
|
|
|
|
160
|
|
|
if ($requestDestination === null) { |
|
161
|
|
|
// If we're recovering under the original location |
|
162
|
|
|
// check if it exists, to return "403 Forbidden" in case it does not |
|
163
|
|
|
try { |
|
164
|
|
|
$this->locationService->loadLocation($trashItem->parentLocationId); |
|
|
|
|
|
|
165
|
|
|
} catch (NotFoundException $e) { |
|
166
|
|
|
throw new ForbiddenException($e->getMessage()); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$location = $this->trashService->recover($trashItem, $parentLocation); |
|
171
|
|
|
|
|
172
|
|
|
return new Values\ResourceCreated( |
|
173
|
|
|
$this->router->generate( |
|
174
|
|
|
'ezpublish_rest_loadLocation', |
|
175
|
|
|
array( |
|
176
|
|
|
'locationPath' => trim($location->pathString, '/'), |
|
177
|
|
|
) |
|
178
|
|
|
) |
|
179
|
|
|
); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.