forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectStateHandler.php
488 lines (429 loc) · 15.3 KB
/
ObjectStateHandler.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
/**
* File containing the Object State InMemory Handler class
*
* @copyright Copyright (C) 1999-2013 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\ObjectState\Handler as ObjectStateHandlerInterface;
use eZ\Publish\SPI\Persistence\Content\ObjectState;
use eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct;
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
use eZ\Publish\Core\Base\Exceptions\NotFoundException as NotFound;
/**
* The Object State Handler class provides managing of object states and groups
*/
class ObjectStateHandler implements ObjectStateHandlerInterface
{
/**
* @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;
}
/**
* Creates a new object state group
*
* @param \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct $input
*
* @return \eZ\Publish\SPI\Persistence\Content\ObjectState\Group
*/
public function createGroup( InputStruct $input )
{
return $this->backend->create( 'Content\\ObjectState\\Group', $this->getInputData( $input ) );
}
/**
* Loads a object state group
*
* @param mixed $groupId
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the group was not found
*
* @return \eZ\Publish\SPI\Persistence\Content\ObjectState\Group
*/
public function loadGroup( $groupId )
{
return $this->backend->load( 'Content\\ObjectState\\Group', $groupId );
}
/**
* Loads a object state group by identifier
*
* @param string $identifier
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the group was not found
*
* @return \eZ\Publish\SPI\Persistence\Content\ObjectState\Group
*/
public function loadGroupByIdentifier( $identifier )
{
$objectStateGroups = $this->backend->find( 'Content\\ObjectState\\Group', array( 'identifier' => $identifier ) );
if ( empty( $objectStateGroups ) )
{
throw new NotFound( "Content\\ObjectState\\Group", array( "identifier" => $identifier ) );
}
return reset( $objectStateGroups );
}
/**
* Loads all object state groups
*
* @param int $offset
* @param int $limit
*
* @return \eZ\Publish\SPI\Persistence\Content\ObjectState\Group[]
*/
public function loadAllGroups( $offset = 0, $limit = -1 )
{
$objectStateGroups = array();
foreach ( $this->backend->find( 'Content\\ObjectState\\Group', array() ) as $objectStateGroup )
{
$objectStateGroups[] = $objectStateGroup;
}
return array_slice( $objectStateGroups, $offset, $limit >= 0 ? $limit : null );
}
/**
* This method returns the ordered list of object states of a group
*
* @param mixed $groupId
*
* @return \eZ\Publish\SPI\Persistence\Content\ObjectState[]
*/
public function loadObjectStates( $groupId )
{
$objectStates = $this->backend->find( 'Content\\ObjectState', array( 'groupId' => $groupId ) );
usort(
$objectStates,
function ( ObjectState $first, ObjectState $second )
{
if ( $first->priority == $second->priority )
return 0;
return $first->priority > $second->priority ? 1 : -1;
}
);
return $objectStates;
}
/**
* Updates an object state group
*
* @param mixed $groupId
* @param \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct $input
*
* @return \eZ\Publish\SPI\Persistence\Content\ObjectState\Group
*/
public function updateGroup( $groupId, InputStruct $input )
{
$this->backend->update( 'Content\\ObjectState\\Group', $groupId, $this->getInputData( $input ) );
return $this->loadGroup( $groupId );
}
/**
* Deletes a object state group including all states and links to content
*
* @param mixed $groupId
*/
public function deleteGroup( $groupId )
{
$this->backend->deleteByMatch( 'Content\\ObjectState', array( 'groupId' => $groupId ) );
$this->backend->delete( 'Content\\ObjectState\\Group', $groupId );
}
/**
* Creates a new object state in the given group.
* The new state gets the last priority.
* Note: in current kernel: If it is the first state all content objects will
* set to this state.
*
* @param mixed $groupId
* @param \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct $input
*
* @return \eZ\Publish\SPI\Persistence\Content\ObjectState
*/
public function create( $groupId, InputStruct $input )
{
$inputData = $this->getInputData( $input );
$newPriority = 0;
$objectStates = $this->loadObjectStates( $groupId );
if ( !empty( $objectStates ) )
{
$newPriority = $objectStates[count( $objectStates ) - 1]->priority + 1;
}
$inputData["groupId"] = (int)$groupId;
$inputData["priority"] = $newPriority;
$createdState = $this->backend->create( 'Content\\ObjectState', $inputData );
if ( $newPriority == 0 )
{
$allContentInfos = $this->backend->find( "Content\\ContentInfo" );
$allContentIds = array_map(
function ( ContentInfo $contentInfo )
{
return $contentInfo->id;
},
$allContentInfos
);
$this->backend->update( "Content\\ObjectState", $createdState->id, array( "_contentId" => $allContentIds ) );
}
return $createdState;
}
/**
* Loads an object state
*
* @param mixed $stateId
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the state was not found
*
* @return \eZ\Publish\SPI\Persistence\Content\ObjectState
*/
public function load( $stateId )
{
return $this->backend->load( 'Content\\ObjectState', $stateId );
}
/**
* Loads an object state by identifier and group it belongs to
*
* @param string $identifier
* @param mixed $groupId
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the state was not found
*
* @return \eZ\Publish\SPI\Persistence\Content\ObjectState
*/
public function loadByIdentifier( $identifier, $groupId )
{
$objectStates = $this->backend->find(
'Content\\ObjectState',
array(
'identifier' => $identifier,
'groupId' => $groupId
)
);
if ( empty( $objectStates ) )
{
throw new NotFound( "Content\\ObjectState", array( "identifier" => $identifier, "groupId" => $groupId ) );
}
return reset( $objectStates );
}
/**
* Updates an object state
*
* @param mixed $stateId
* @param \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct $input
*
* @return \eZ\Publish\SPI\Persistence\Content\ObjectState
*/
public function update( $stateId, InputStruct $input )
{
$this->backend->update( 'Content\\ObjectState', $stateId, $this->getInputData( $input ) );
return $this->load( $stateId );
}
/**
* Changes the priority of the state
*
* @param mixed $stateId
* @param int $priority
*/
public function setPriority( $stateId, $priority )
{
$objectState = $this->load( $stateId );
$groupStates = $this->loadObjectStates( $objectState->groupId );
$priorityList = array();
foreach ( $groupStates as $index => $groupState )
{
$priorityList[$groupState->id] = $index;
}
$priorityList[$objectState->id] = (int)$priority;
asort( $priorityList );
foreach ( array_keys( $priorityList ) as $objectStatePriority => $objectStateId )
{
$this->backend->update( 'Content\\ObjectState', $objectStateId, array( "priority" => $objectStatePriority ) );
}
}
/**
* Deletes a object state. The state of the content objects is reset to the
* first object state in the group.
*
* @param mixed $stateId
*/
public function delete( $stateId )
{
// We need to load the object state as we need $groupId
$objectState = $this->load( $stateId );
// Find all content for the current $stateId
$contentList = $this->getObjectStateContentList( $stateId );
// Delete the state
$this->backend->delete( 'Content\\ObjectState', $stateId );
// Update the priorities of the group states if there are any more states in the group
$groupStates = $this->loadObjectStates( $objectState->groupId );
if ( empty( $groupStates ) )
return;
$priority = 0;
foreach ( $groupStates as $groupState )
{
$this->backend->update( 'Content\\ObjectState', $groupState->id, array( "priority" => $priority ) );
$priority++;
}
// Now reassign content from old state to the first state in the group
$firstObjectState = current( $this->backend->find( "Content\\ObjectState", array( "priority" => 0 ) ) );
$existingContent = $this->getObjectStateContentList( $firstObjectState->id ) + $contentList;
$existingContent = array_values( array_unique( $existingContent ) );
$this->backend->update( 'Content\\ObjectState', $firstObjectState->id, array( "_contentId" => $existingContent ) );
}
/**
* Sets the object-state of a state group to $stateId for the given content.
*
* @param mixed $contentId
* @param mixed $groupId
* @param mixed $stateId
*
* @return boolean
*/
public function setContentState( $contentId, $groupId, $stateId )
{
$groupStateIds = $this->getGroupStateList( $groupId );
if ( empty( $groupStateIds ) || !in_array( $stateId, $groupStateIds ) )
return false;
$contentToStateMap = $this->getContentToStateMap();
if ( isset( $contentToStateMap[(int)$contentId] ) )
{
// If the content was in one of the group states,
// find all content for the old state and update the old state with excluded $contentId
$existingStateIds = array_values( array_intersect( $groupStateIds, $contentToStateMap[(int)$contentId] ) );
if ( !empty( $existingStateIds ) )
{
$this->backend->update(
"Content\\ObjectState",
$existingStateIds[0],
array(
"_contentId" => array_values(
array_diff( $this->getObjectStateContentList( $existingStateIds[0] ), array( $contentId ) )
)
)
);
}
}
// Find all content for the new state and update the new state with added $contentId
$newStateContentList = $this->getObjectStateContentList( $stateId );
$newStateContentList[] = $contentId;
$this->backend->update( "Content\\ObjectState", $stateId, array( "_contentId" => $newStateContentList ) );
return true;
}
/**
* Gets the object-state of object identified by $contentId.
*
* The $state is the id of the state within one group.
*
* @param mixed $contentId
* @param mixed $stateGroupId
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if no state is found
*
* @return \eZ\Publish\SPI\Persistence\Content\ObjectState
*/
public function getContentState( $contentId, $stateGroupId )
{
$groupStateIds = $this->getGroupStateList( $stateGroupId );
if ( empty( $groupStateIds ) )
throw new NotFound( "Content\\ObjectState", array( "groupId" => $stateGroupId ) );
$contentId = (int)$contentId;
$contentToStateMap = $this->getContentToStateMap();
if ( !isset( $contentToStateMap[$contentId] ) )
throw new NotFound( "Content\\ObjectState", array( "groupId" => $stateGroupId ) );
$foundStates = array_values( array_intersect( $groupStateIds, $contentToStateMap[$contentId] ) );
if ( empty( $foundStates ) )
throw new NotFound( "Content\\ObjectState", array( "groupId" => $stateGroupId ) );
return $this->load( $foundStates[0] );
}
/**
* Returns the number of objects which are in this state
*
* @param mixed $stateId
*
* @return int
*/
public function getContentCount( $stateId )
{
return count( $this->getObjectStateContentList( $stateId ) );
}
/**
* Converts InputStruct to array and adds missing languageCodes array into it
*
* @param \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct $input
*
* @return array
*/
protected function getInputData( InputStruct $input )
{
$inputData = (array)$input;
$inputData["languageCodes"] = array_keys( $input->name );
return $inputData;
}
/**
* Gets a mapping array of all content and states they belong to
*
* This method serves as a hack because InMemory storage is unable to
* store M:N relations between content and object states as there's no
* value object for the link
*
* @return array
*/
protected function getContentToStateMap()
{
$contentToStateMap = array();
$contentInfoArray = $this->backend->find( "Content\\ContentInfo" );
foreach ( $contentInfoArray as $contentInfo )
{
$objectStates = $this->backend->find( "Content\\ObjectState", array( "_contentId" => $contentInfo->id ) );
foreach ( $objectStates as $objectState )
{
$contentToStateMap[$contentInfo->id][] = $objectState->id;
}
}
return $contentToStateMap;
}
/**
* Returns all content IDs that belong to $stateId
*
* @param int $stateId
*
* @return array
*/
protected function getObjectStateContentList( $stateId )
{
$contentList = array();
foreach ( $this->getContentToStateMap() as $contentId => $stateList )
{
if ( in_array( $stateId, $stateList ) )
$contentList[] = $contentId;
}
return $contentList;
}
/**
* Returns all state IDs that belong to $groupId
*
* @param int $groupId
*
* @return array
*/
protected function getGroupStateList( $groupId )
{
$groupStates = $this->loadObjectStates( $groupId );
return array_map(
function ( ObjectState $objectState )
{
return $objectState->id;
},
$groupStates
);
}
}