eZ Publish  [trunk]
ezcontentobjecttreenodeoperations.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZContentObjectTreeNodeOperations class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /*!
00012   \class eZContentObjectTreeNodeOperations ezcontentobjecttreenodeoperations.php
00013   \brief The class eZContentObjectTreeNodeOperations is a wrapper for node's
00014   core-operations. It takes care about interface stuff.
00015   Example: there is a 'move' core-operation that moves a node from one location
00016   to another. But, for example, before and after moving we have to clear
00017   view caches for old and new placements. Clearing of the cache is handled by
00018   this class.
00019 */
00020 
00021 class eZContentObjectTreeNodeOperations
00022 {
00023     /*!
00024      Constructor
00025     */
00026     function eZContentObjectTreeNodeOperations()
00027     {
00028     }
00029 
00030     /*!
00031      \static
00032      A wrapper for eZContentObjectTreeNode's 'move' operation.
00033      It does:
00034       - clears caches for old placement;
00035       - performs actual move( calls eZContentObjectTreeNode->move() );
00036       - updates subtree path;
00037       - updates node's section;
00038       - updates assignment( setting new 'parent_node' );
00039       - clears caches for new placement;
00040 
00041      \param $nodeID The id of a node to move.
00042      \param $newParentNodeID The id of a new parent.
00043      \return \c true if 'move' was done successfully, otherwise \c false;
00044     */
00045     static function move( $nodeID, $newParentNodeID )
00046     {
00047         $result = false;
00048 
00049         if ( !is_numeric( $nodeID ) || !is_numeric( $newParentNodeID ) )
00050             return false;
00051 
00052         $node = eZContentObjectTreeNode::fetch( $nodeID );
00053         if ( !$node )
00054             return false;
00055 
00056         $object = $node->object();
00057         if ( !$object )
00058             return false;
00059 
00060         $objectID = $object->attribute( 'id' );
00061         $oldParentNode = $node->fetchParent();
00062         $oldParentObject = $oldParentNode->object();
00063 
00064         // clear user policy cache if this is a user object
00065         if ( in_array( $object->attribute( 'contentclass_id' ), eZUser::contentClassIDs() ) )
00066         {
00067             eZUser::purgeUserCacheByUserId( $object->attribute( 'id' ) );
00068         }
00069 
00070         // clear cache for old placement.
00071         eZContentCacheManager::clearContentCacheIfNeeded( $objectID );
00072 
00073         $db = eZDB::instance();
00074         $db->begin();
00075 
00076         $node->move( $newParentNodeID );
00077 
00078         $newNode = eZContentObjectTreeNode::fetchNode( $objectID, $newParentNodeID );
00079 
00080         if ( $newNode )
00081         {
00082             $newNode->updateSubTreePath( true, true );
00083             if ( $newNode->attribute( 'main_node_id' ) == $newNode->attribute( 'node_id' ) )
00084             {
00085                 // If the main node is moved we need to check if the section ID must change
00086                 $newParentNode = $newNode->fetchParent();
00087                 $newParentObject = $newParentNode->object();
00088                 if ( $object->attribute( 'section_id' ) != $newParentObject->attribute( 'section_id' ) )
00089                 {
00090 
00091                     eZContentObjectTreeNode::assignSectionToSubTree( $newNode->attribute( 'main_node_id' ),
00092                                                                      $newParentObject->attribute( 'section_id' ),
00093                                                                      $oldParentObject->attribute( 'section_id' ) );
00094                 }
00095             }
00096 
00097             // modify assignment
00098             $curVersion     = $object->attribute( 'current_version' );
00099             $nodeAssignment = eZNodeAssignment::fetch( $objectID, $curVersion, $oldParentNode->attribute( 'node_id' ) );
00100 
00101             if ( $nodeAssignment )
00102             {
00103                 $nodeAssignment->setAttribute( 'parent_node', $newParentNodeID );
00104                 $nodeAssignment->setAttribute( 'op_code', eZNodeAssignment::OP_CODE_MOVE );
00105                 $nodeAssignment->store();
00106 
00107                 // update search index
00108                 $nodeIDList = array( $nodeID );
00109                 eZSearch::removeNodeAssignment( $node->attribute( 'main_node_id' ), $newNode->attribute( 'main_node_id' ), $object->attribute( 'id' ), $nodeIDList );
00110                 eZSearch::addNodeAssignment( $newNode->attribute( 'main_node_id' ), $object->attribute( 'id' ), $nodeIDList );
00111             }
00112 
00113             $result = true;
00114         }
00115         else
00116         {
00117             eZDebug::writeError( "Node $nodeID was moved to $newParentNodeID but fetching the new node failed" );
00118         }
00119 
00120         $db->commit();
00121 
00122         // clear cache for new placement.
00123         eZContentCacheManager::clearContentCacheIfNeeded( $objectID );
00124 
00125         return $result;
00126     }
00127 }
00128 
00129 
00130 ?>