eZ Publish  [4.1]
ezcontentobjecttreenodeoperations.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZContentObjectTreeNodeOperations class
00004 //
00005 // Created on: <12-Sep-2005 12:02:22 dl>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.1.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2009 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file
00032 */
00033 
00034 /*!
00035   \class eZContentObjectTreeNodeOperations ezcontentobjecttreenodeoperations.php
00036   \brief The class eZContentObjectTreeNodeOperations is a wrapper for node's
00037   core-operations. It takes care about interface stuff.
00038   Example: there is a 'move' core-operation that moves a node from one location
00039   to another. But, for example, before and after moving we have to clear
00040   view caches for old and new placements. Clearing of the cache is handled by
00041   this class.
00042 */
00043 
00044 class eZContentObjectTreeNodeOperations
00045 {
00046     /*!
00047      Constructor
00048     */
00049     function eZContentObjectTreeNodeOperations()
00050     {
00051     }
00052 
00053     /*!
00054      \static
00055      A wrapper for eZContentObjectTreeNode's 'move' operation.
00056      It does:
00057       - clears caches for old placement;
00058       - performs actual move( calls eZContentObjectTreeNode->move() );
00059       - updates subtree path;
00060       - updates node's section;
00061       - updates assignment( setting new 'parent_node' );
00062       - clears caches for new placement;
00063 
00064      \param $nodeID The id of a node to move.
00065      \param $newParentNodeID The id of a new parent.
00066      \return \c true if 'move' was done successfully, otherwise \c false;
00067     */
00068     static function move( $nodeID, $newParentNodeID )
00069     {
00070         $result = false;
00071 
00072         if ( !is_numeric( $nodeID ) || !is_numeric( $newParentNodeID ) )
00073             return false;
00074 
00075         $node = eZContentObjectTreeNode::fetch( $nodeID );
00076         if ( !$node )
00077             return false;
00078 
00079         $object = $node->object();
00080         if ( !$object )
00081             return false;
00082 
00083         $objectID = $object->attribute( 'id' );
00084         $oldParentNode = $node->fetchParent();
00085         $oldParentObject = $oldParentNode->object();
00086 
00087         // clear user policy cache if this is a user object
00088         if ( in_array( $object->attribute( 'contentclass_id' ), eZUser::contentClassIDs() ) )
00089         {
00090             eZUser::cleanupCache();
00091         }
00092 
00093         // clear cache for old placement.
00094         eZContentCacheManager::clearContentCacheIfNeeded( $objectID );
00095 
00096         $db = eZDB::instance();
00097         $db->begin();
00098 
00099         $node->move( $newParentNodeID );
00100 
00101         $newNode = eZContentObjectTreeNode::fetchNode( $objectID, $newParentNodeID );
00102 
00103         if ( $newNode )
00104         {
00105             $newNode->updateSubTreePath( true, true );
00106             if ( $newNode->attribute( 'main_node_id' ) == $newNode->attribute( 'node_id' ) )
00107             {
00108                 // If the main node is moved we need to check if the section ID must change
00109                 $newParentNode = $newNode->fetchParent();
00110                 $newParentObject = $newParentNode->object();
00111                 if ( $object->attribute( 'section_id' ) != $newParentObject->attribute( 'section_id' ) )
00112                 {
00113 
00114                     eZContentObjectTreeNode::assignSectionToSubTree( $newNode->attribute( 'main_node_id' ),
00115                                                                      $newParentObject->attribute( 'section_id' ),
00116                                                                      $oldParentObject->attribute( 'section_id' ) );
00117                 }
00118             }
00119 
00120             // modify assignment
00121             $curVersion     = $object->attribute( 'current_version' );
00122             $nodeAssignment = eZNodeAssignment::fetch( $objectID, $curVersion, $oldParentNode->attribute( 'node_id' ) );
00123 
00124             if ( $nodeAssignment )
00125             {
00126                 $nodeAssignment->setAttribute( 'parent_node', $newParentNodeID );
00127                 $nodeAssignment->setAttribute( 'op_code', eZNodeAssignment::OP_CODE_MOVE );
00128                 $nodeAssignment->store();
00129             }
00130 
00131             $result = true;
00132         }
00133         else
00134         {
00135             eZDebug::writeError( "Node $nodeID was moved to $newParentNodeID but fetching the new node failed" );
00136         }
00137 
00138         $db->commit();
00139 
00140         // clear cache for new placement.
00141         eZContentCacheManager::clearContentCacheIfNeeded( $objectID );
00142 
00143         return $result;
00144     }
00145 }
00146 
00147 
00148 ?>