forked from ezsystems/ezpublish-legacy
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ezcontentobjecttreenodeoperations.php
136 lines (113 loc) · 5.04 KB
/
ezcontentobjecttreenodeoperations.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
<?php
/**
* File containing the eZContentObjectTreeNodeOperations class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
* @package kernel
*/
/*!
\class eZContentObjectTreeNodeOperations ezcontentobjecttreenodeoperations.php
\brief The class eZContentObjectTreeNodeOperations is a wrapper for node's
core-operations. It takes care about interface stuff.
Example: there is a 'move' core-operation that moves a node from one location
to another. But, for example, before and after moving we have to clear
view caches for old and new placements. Clearing of the cache is handled by
this class.
*/
class eZContentObjectTreeNodeOperations
{
/*!
\static
A wrapper for eZContentObjectTreeNode's 'move' operation.
It does:
- clears caches for old placement;
- performs actual move( calls eZContentObjectTreeNode->move() );
- updates subtree path;
- updates node's section;
- updates assignment( setting new 'parent_node' );
- clears caches for new placement;
\param $nodeID The id of a node to move.
\param $newParentNodeID The id of a new parent.
\return \c true if 'move' was done successfully, otherwise \c false;
*/
static function move( $nodeID, $newParentNodeID )
{
$result = false;
if ( !is_numeric( $nodeID ) || !is_numeric( $newParentNodeID ) )
return false;
$node = eZContentObjectTreeNode::fetch( $nodeID );
if ( !$node )
return false;
$object = $node->object();
if ( !$object )
return false;
$objectID = $object->attribute( 'id' );
$oldParentNode = $node->fetchParent();
$oldParentObject = $oldParentNode->object();
// clear user policy cache if this is a user object
if ( in_array( $object->attribute( 'contentclass_id' ), eZUser::contentClassIDs() ) )
{
eZUser::purgeUserCacheByUserId( $object->attribute( 'id' ) );
}
// clear cache for old placement.
// If child count exceeds threshold, do nothing here, and instead clear all view cache at the end.
$childCountInThresholdRange = eZContentCache::inCleanupThresholdRange( $node->childrenCount( false ) );
if ( $childCountInThresholdRange )
{
eZContentCacheManager::clearContentCacheIfNeeded( $objectID );
}
$db = eZDB::instance();
$db->begin();
$node->move( $newParentNodeID );
$newNode = eZContentObjectTreeNode::fetchNode( $objectID, $newParentNodeID );
if ( $newNode )
{
$newNode->updateSubTreePath( true, true );
if ( $newNode->attribute( 'main_node_id' ) == $newNode->attribute( 'node_id' ) )
{
// If the main node is moved we need to check if the section ID must change
$newParentNode = $newNode->fetchParent();
$newParentObject = $newParentNode->object();
if ( $object->attribute( 'section_id' ) != $newParentObject->attribute( 'section_id' ) )
{
eZContentObjectTreeNode::assignSectionToSubTree( $newNode->attribute( 'main_node_id' ),
$newParentObject->attribute( 'section_id' ),
$oldParentObject->attribute( 'section_id' ) );
}
}
// modify assignment
$curVersion = $object->attribute( 'current_version' );
$nodeAssignment = eZNodeAssignment::fetch( $objectID, $curVersion, $oldParentNode->attribute( 'node_id' ) );
if ( $nodeAssignment )
{
$nodeAssignment->setAttribute( 'parent_node', $newParentNodeID );
$nodeAssignment->setAttribute( 'op_code', eZNodeAssignment::OP_CODE_MOVE );
$nodeAssignment->store();
// update search index specifying we are doing a move operation
$nodeIDList = array( $nodeID );
eZSearch::removeNodeAssignment( $node->attribute( 'main_node_id' ), $newNode->attribute( 'main_node_id' ), $object->attribute( 'id' ), $nodeIDList );
eZSearch::addNodeAssignment( $newNode->attribute( 'main_node_id' ), $object->attribute( 'id' ), $nodeIDList, true );
}
$result = true;
}
else
{
eZDebug::writeError( "Node $nodeID was moved to $newParentNodeID but fetching the new node failed", __METHOD__ );
}
$db->commit();
// clear cache for new placement.
// If child count exceeds threshold, clear all view cache instead.
if ( $childCountInThresholdRange )
{
eZContentCacheManager::clearContentCacheIfNeeded( $objectID );
}
else
{
eZContentCacheManager::clearAllContentCache();
}
return $result;
}
}
?>