forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrashHandler.php
174 lines (155 loc) · 5.56 KB
/
TrashHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* File containing the TrashHandler implementation
*
* @copyright Copyright (C) 1999-2014 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
*/
namespace eZ\Publish\Core\Persistence\InMemory;
use eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler as TrashHandlerInterface;
use eZ\Publish\SPI\Persistence\Content\Location\CreateStruct;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler
*/
class TrashHandler implements TrashHandlerInterface
{
/**
* @var Handler
*/
protected $handler;
/**
* @var Backend
*/
protected $backend;
/**
* Setups current handler instance with reference to Handler object that created it.
*
* @param Handler $handler
* @param Backend $backend The storage engine backend
*/
public function __construct( Handler $handler, Backend $backend )
{
$this->handler = $handler;
$this->backend = $backend;
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler
*/
public function loadTrashItem( $id )
{
return $this->backend->load( 'Content\\Location\\Trashed', $id );
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler
* @todo Handle field types actions
*/
public function trashSubtree( $locationId )
{
$location = $this->handler->locationHandler()->load( $locationId );
$subtreeLocations = $this->backend->find(
'Content\\Location',
array( 'pathString' => $location->pathString . '%' )
);
$isLocationRemoved = false;
$parentLocationId = null;
$subtreeLocations[] = $location;
foreach ( $subtreeLocations as $location )
{
if ( $location->id == $locationId )
{
$parentLocationId = $location->parentId;
}
if ( count( $this->backend->find( 'Content\\Location', array( 'contentId' => $location->contentId ) ) ) == 1 )
{
$this->backend->delete( 'Content\\Location', $location->id );
$this->backend->create( 'Content\\Location\\Trashed', (array)$location, false );
}
else
{
if ( $location->id == $locationId )
{
$isLocationRemoved = true;
}
$this->backend->delete( 'Content\\Location', $location->id );
$remainingLocations = $this->backend->find( 'Content\\Location', array( 'contentId' => $location->contentId ) );
$this->backend->update(
'Content\\ContentInfo',
$location->contentId,
array( 'mainLocationId' => $remainingLocations[0]->id )
);
}
}
return $isLocationRemoved ? null : $this->loadTrashItem( $locationId );
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler
* @todo Handle field types actions
*/
public function recover( $trashedId, $newParentId )
{
$trashedLocation = $this->loadTrashItem( $trashedId );
$newParent = $this->handler->locationHandler()->load( $newParentId );
// Restore location under $newParent
$struct = new CreateStruct;
foreach ( $struct as $property => $value )
{
if ( isset( $trashedLocation->$property ) )
{
$struct->$property = $trashedLocation->$property;
}
}
$struct->parentId = $newParent->id;
$recoveredLocation = $this->handler->locationHandler()->create( $struct )->id;
$this->backend->delete( 'Content\\Location\\Trashed', $trashedId );
return $recoveredLocation;
}
/**
* Limited implementation (no criterion/sort support).
* Will return all trashed locations, regardless criterion filter or sort clauses provided.
* Offset/Limit is however supported
*
* @see eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler
*/
public function findTrashItems( Criterion $criterion = null, $offset = 0, $limit = null, array $sort = null )
{
return array_slice(
$this->backend->find( 'Content\\Location\\Trashed' ),
$offset,
$limit
);
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler
*/
public function emptyTrash()
{
$trashedIds = array();
$contentIds = array();
foreach ( $this->backend->find( 'Content\\Location\\Trashed' ) as $trashed )
{
$trashedIds[] = $trashed->id;
$contentIds[] = $trashed->contentId;
}
if ( !empty( $trashedIds ) )
{
// Remove associated content for trashed locations
foreach ( $contentIds as $contentId )
{
$this->handler->contentHandler()->deleteContent( $contentId );
}
// Remove trashed locations
$this->backend->deleteByMatch( 'Content\\Location\\Trashed', array( 'id' => $trashedIds ) );
}
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler
*/
public function deleteTrashItem( $trashedId )
{
$vo = $this->loadTrashItem( $trashedId );
$this->handler->contentHandler()->deleteContent( $vo->contentId );
$this->backend->delete( 'Content\\Location\\Trashed', $trashedId );
}
}