eZ Publish  [4.0]
eznodeassignment.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZNodeAssignment class
00004 //
00005 // Created on: <02-οΛΤ-2002 15:58:10 sp>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 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 eznodeassignment.php
00032 */
00033 
00034 /*!
00035   \class eZNodeAssignment eznodeassignment.php
00036   \brief The class eZNodeAssignment does
00037 
00038 */
00039 require_once( "lib/ezutils/classes/ezdebug.php" );
00040 //include_once( "kernel/classes/ezpersistentobject.php" );
00041 
00042 class eZNodeAssignment extends eZPersistentObject
00043 {
00044     // Bit 0 is used to mark if the action is to be performed or not
00045     // A value of 0 means ignore and 1 means execute
00046     const OP_CODE_NOP =          0;
00047     const OP_CODE_EXECUTE =      1;
00048     // Create the node at specified location
00049     const OP_CODE_CREATE_NOP =   2;
00050     const OP_CODE_CREATE =       3;
00051     // Move the node to new location
00052     const OP_CODE_MOVE_NOP =     4;
00053     const OP_CODE_MOVE =         5;
00054     // Remove existing node
00055     const OP_CODE_REMOVE_NOP =   6;
00056     const OP_CODE_REMOVE =       7;
00057     // Set (update/create) values for node
00058     const OP_CODE_SET_NOP =      8;
00059     const OP_CODE_SET =          9;
00060 
00061     /*!
00062      Constructor
00063     */
00064     function eZNodeAssignment( $row )
00065     {
00066         $this->TempNode = null;
00067         $this->Name = false;
00068         $this->eZPersistentObject( $row );
00069     }
00070 
00071     static function definition()
00072     {
00073         return array( 'fields' => array( 'id' => array( 'name' => 'ID',
00074                                                         'datatype' => 'integer',
00075                                                         'default' => 0,
00076                                                         'required' => true ),
00077                                          'remote_id' => array( 'name' => 'RemoteID',
00078                                                                'datatype' => 'integer',
00079                                                                'default' => 0,
00080                                                                'required' => true ),
00081                                          'contentobject_id' => array( 'name' => 'ContentobjectID',
00082                                                                       'datatype' => 'integer',
00083                                                                       'default' => 0,
00084                                                                       'required' => true,
00085                                                                       'foreign_class' => 'eZContentObject',
00086                                                                       'foreign_attribute' => 'id',
00087                                                                       'multiplicity' => '1..*' ),
00088                                          'contentobject_version' => array( 'name' => 'ContentObjectVersion',
00089                                                                            'datatype' => 'integer',
00090                                                                            'default' => 0,
00091                                                                            'required' => true ),
00092                                          'parent_node' => array( 'name' => 'ParentNode',
00093                                                                  'datatype' => 'integer',
00094                                                                  'default' => 0,
00095                                                                  'required' => true,
00096                                                                  'foreign_class' => 'eZContentObjectTreeNode',
00097                                                                  'foreign_attribute' => 'node_id',
00098                                                                  'multiplicity' => '1..*' ),
00099                                          'sort_field' => array( 'name' => 'SortField',
00100                                                                 'datatype' => 'integer',
00101                                                                 'default' => 1,
00102                                                                 'required' => true ),
00103                                          'sort_order' => array( 'name' => 'SortOrder',
00104                                                                 'datatype' => 'integer',
00105                                                                 'default' => 1,
00106                                                                 'required' => true ),
00107                                          'is_main' => array( 'name' => 'Main',
00108                                                              'datatype' => 'integer',
00109                                                              'default' => 0,
00110                                                              'required' => true ),
00111                                          'from_node_id' => array( 'name' => 'FromNodeID',
00112                                                                   'datatype' => 'integer',
00113                                                                   'default' => 0,
00114                                                                   'required' => true,
00115                                                                   'foreign_class' => 'eZContentObjectTreeNode',
00116                                                                   'foreign_attribute' => 'node_id',
00117                                                                   'multiplicity' => '1..*' ),
00118                                          'parent_remote_id' => array( 'name' => 'ParentRemoteID',
00119                                                                       'datatype' => 'string',
00120                                                                       'default' => '',
00121                                                                       'required' => false ),
00122                                          'op_code' => array( 'name' => 'OpCode',
00123                                                              'datatype' => 'int',
00124                                                              'default' => 0, // eZNodeAssignment::OP_CODE_NOP
00125                                                              'required' => true ) ),
00126                       'keys' => array( 'id' ),
00127                       "function_attributes" => array( "parent_node_obj"      => "getParentNode",
00128                                                       "parent_contentobject" => "getParentObject",
00129                                                       "node"                 => "fetchNode",
00130                                                       'is_nop_operation'     => 'isNopOperation',
00131                                                       'is_create_operation'  => 'isCreateOperation',
00132                                                       'is_move_operation'    => 'isMoveOperation',
00133                                                       'is_remove_operation'  => 'isRemoveOperation',
00134                                                       'is_set_operation'     => 'isSetOperation',
00135                                                       'temp_node'            => 'tempNode' ),
00136                       "increment_key" => "id",
00137                       'class_name' => 'eZNodeAssignment',
00138                       'name' => 'eznode_assignment' );
00139     }
00140 
00141     function tempNode()
00142     {
00143         if ( $this->TempNode == null )
00144         {
00145             $this->TempNode = eZContentObjectTreeNode::create( $this->attribute( 'parent_node' ),
00146                                                                $this->attribute( 'contentobject_id' ),
00147                                                                $this->attribute( 'contentobject_version' ),
00148                                                                $this->attribute( 'sort_field' ),
00149                                                                $this->attribute( 'sort_order' ) );
00150             $this->TempNode->setName( $this->Name );
00151         }
00152         return $this->TempNode;
00153     }
00154 
00155     function setName( $name )
00156     {
00157         return $this->Name = $name;
00158     }
00159 
00160     function name()
00161     {
00162         return $this->Name;
00163     }
00164 
00165     /*!
00166      * Returns true if the assignment is a nop (no operation) operation.
00167      * \return bool
00168      */
00169     function isNopOperation()
00170     {
00171         return ( $this->OpCode & 1 ) == eZNodeAssignment::OP_CODE_NOP;
00172     }
00173 
00174     /*!
00175      * Returns true if the assignment is a create operation.
00176      * \return bool
00177      */
00178     function isCreateOperation()
00179     {
00180         return $this->OpCode == eZNodeAssignment::OP_CODE_CREATE;
00181     }
00182 
00183     /*!
00184      * Returns true if the assignment is a move operation.
00185      * \return bool
00186      */
00187     function isMoveOperation()
00188     {
00189         return $this->OpCode == eZNodeAssignment::OP_CODE_MOVE;
00190     }
00191 
00192     /*!
00193      * Returns true if the assignment is a remove operation.
00194      * \return bool
00195      */
00196     function isRemoveOperation()
00197     {
00198         return $this->OpCode == eZNodeAssignment::OP_CODE_REMOVE;
00199     }
00200 
00201     /*!
00202      * Returns true if the assignment is a set (update/create) operation.
00203      * \return bool
00204      */
00205     function isSetOperation()
00206     {
00207         return $this->OpCode == eZNodeAssignment::OP_CODE_SET;
00208     }
00209 
00210     static function create( $parameters = array() )
00211     {
00212         if ( !isset( $parameters['contentobject_id'] ) )
00213         {
00214             eZDebug::writeError( $parameters, "Cannot create node assignment without contentobject_id" );
00215             $retValue = null;
00216             return $retValue;
00217         }
00218         if ( !isset( $parameters['contentobject_version'] ) )
00219         {
00220             $parameters['contentobject_version'] = 1;
00221         }
00222         if ( !isset( $parameters['remote_id'] ) )
00223         {
00224             $parameters['remote_id'] = 0;
00225         }
00226         if ( !isset( $parameters['parent_node'] ) )
00227         {
00228             $parameters['parent_node'] = 2;
00229         }
00230         if ( !isset( $parameters['is_main'] ) )
00231         {
00232             $parameters['is_main'] = 0;
00233         }
00234         if ( !isset( $parameters['sort_field'] ) )
00235         {
00236             $parameters['sort_field'] = 2; // Published
00237         }
00238         if ( !isset( $parameters['sort_order'] ) )
00239         {
00240             $parameters['sort_order'] = 0; // Desc
00241         }
00242         if ( !isset( $parameters['from_node_id'] ) )
00243         {
00244             $parameters['from_node_id'] = 0;
00245         }
00246         if ( !isset( $parameters['parent_remote_id'] ) )
00247         {
00248             $parameters['parent_remote_id'] = '';
00249         }
00250         if ( !isset( $parameters['op_code'] ) )
00251         {
00252             // The default value for new node-assigments is to create nodes from them.
00253             $parameters['op_code'] = eZNodeAssignment::OP_CODE_CREATE;
00254         }
00255 
00256         return new eZNodeAssignment( $parameters );
00257     }
00258 
00259     /*!
00260      Marks the specified nodeassignment to remove its node. It uses
00261      \a parentNodeID and \a contentObjectID if they are given,
00262      if they are \c false it will mark the current node assignment.
00263 
00264      \param $parentNodeID The ID of the parent node
00265      \param $contentObjectID The ID of the object
00266      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00267      the calls within a db transaction; thus within db->begin and db->commit.
00268      */
00269     function remove( $parentNodeID = false, $contentObjectID = false )
00270     {
00271         $db = eZDB::instance();
00272         if ( $parentNodeID == false and $contentObjectID == false )
00273         {
00274             $nodeAssignment = $this;
00275         }
00276         else
00277         {
00278             $parentNodeID =(int) $parentNodeID;
00279             $contentObjectID =(int) $contentObjectID;
00280             $cond = array( 'parent_node' => $parentNodeID,
00281                            'contentobject_id' => $contentObjectID );
00282             $nodeAssignment = eZPersistentObject::fetchObject( eZNodeAssignment::definition(),
00283                                                                null,
00284                                                                $cond,
00285                                                                true );
00286         }
00287         $nodeAssignment->setAttribute( "op_code", eZNodeAssignment::OP_CODE_REMOVE );
00288         $nodeAssignment->store();
00289     }
00290 
00291     /*!
00292      \static
00293      Marks the node assignment with the ID \a $assignmentID to remove its node.
00294 
00295      \param $assignmentID Either an ID or an array with IDs.
00296      \return \c true if it were able to remove the assignments, \c false if something failed.
00297      \note If \a $assignmentID is an empty array it immediately returns \c false.
00298      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00299      the calls within a db transaction; thus within db->begin and db->commit.
00300     */
00301     static function removeByID( $assignmentID )
00302     {
00303         $db = eZDB::instance();
00304         if ( is_array( $assignmentID ) )
00305         {
00306             if ( count( $assignmentID ) == 0 )
00307             {
00308                 return false;
00309             }
00310             $sql = "UPDATE eznode_assignment SET op_code = " . eZNodeAssignment::OP_CODE_REMOVE . ", is_main = 0 WHERE id IN ( ";
00311             $i = 0;
00312             foreach ( $assignmentID as $id )
00313             {
00314                 if ( $i > 0 )
00315                     $sql .= ", ";
00316                 $sql .= (int)$id;
00317                 ++$i;
00318             }
00319             $sql .= ' )';
00320         }
00321         else
00322         {
00323             $sql = "UPDATE eznode_assignment SET op_code = " . eZNodeAssignment::OP_CODE_REMOVE . ", is_main = 0 WHERE id=" . (int)$assignmentID;
00324         }
00325         $db->query( $sql );
00326         return true;
00327     }
00328 
00329     /*!
00330      Delete specified nodeassignment if \a parentNodeID and \a contentObjectID are given,
00331      if they are \c false it will remove the current node assignment.
00332 
00333      \param $parentNodeID The ID of the parent node
00334      \param $contentObjectID The ID of the object
00335      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00336      the calls within a db transaction; thus within db->begin and db->commit.
00337      */
00338     function purge( $parentNodeID = false, $contentObjectID = false )
00339     {
00340         $db = eZDB::instance();
00341         if ( $parentNodeID == false and $contentObjectID == false )
00342         {
00343             $nodeAssignmentID = $this->attribute( 'id' );
00344             $sqlQuery = "DELETE FROM eznode_assignment WHERE id='$nodeAssignmentID'";
00345             $db->query( $sqlQuery );
00346         }
00347         else
00348         {
00349             $parentNodeID =(int) $parentNodeID;
00350             $contentObjectID =(int) $contentObjectID;
00351             $sqlQuery = "DELETE FROM eznode_assignment WHERE parent_node='$parentNodeID' AND contentobject_id='$contentObjectID'";
00352             $db->query( $sqlQuery );
00353         }
00354     }
00355 
00356     /*!
00357      \static
00358      Delelet the node assignment with the ID \a $assignmentID.
00359 
00360      \param $assignmentID Either an ID or an array with IDs.
00361      \return \c true if it were able to remove the assignments, \c false if something failed.
00362      \note If \a $assignmentID is an empty array it immediately returns \c false.
00363      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00364      the calls within a db transaction; thus within db->begin and db->commit.
00365     */
00366     static function purgeByID( $assignmentID )
00367     {
00368         $db = eZDB::instance();
00369         if ( is_array( $assignmentID ) )
00370         {
00371             if ( count( $assignmentID ) == 0 )
00372             {
00373                 return false;
00374             }
00375             $sql = "DELETE FROM eznode_assignment WHERE id IN ( ";
00376             $i = 0;
00377             foreach ( $assignmentID as $id )
00378             {
00379                 if ( $i > 0 )
00380                     $sql .= ", ";
00381                 $sql .= (int)$id;
00382                 ++$i;
00383             }
00384             $sql .= ' )';
00385         }
00386         else
00387         {
00388             $sql = "DELETE FROM eznode_assignment WHERE id=" . (int)$assignmentID;
00389         }
00390         $db->query( $sql );
00391         return true;
00392     }
00393 
00394     static function fetchForObject( $contentObjectID, $version = 1, $main = 0, $asObject = true )
00395     {
00396         $cond = array( 'contentobject_id' => $contentObjectID,
00397                        'contentobject_version' => $version );
00398         if( $main > 0 )
00399         {
00400             $cond['is_main'] = 1;
00401         }
00402         $objectList = eZPersistentObject::fetchObjectList( eZNodeAssignment::definition(),
00403                                                             null,
00404                                                             $cond,
00405                                                             null,
00406                                                             null,
00407                                                             $asObject );
00408         return $objectList;
00409     }
00410 
00411     static function fetch( $contentObjectID, $version = 1, $parentNode = 0 ,$asObject = true )
00412     {
00413         $cond = array( 'contentobject_id' => $contentObjectID,
00414                        'contentobject_version' => $version,
00415                        'parent_node' => $parentNode );
00416         return eZPersistentObject::fetchObject( eZNodeAssignment::definition(),
00417                                                 null,
00418                                                 $cond,
00419                                                 $asObject );
00420     }
00421 
00422     /*!
00423      Finds the node for the current assignemnt if it exists and returns it.
00424      \return An eZContentObjectTreeNode object or \c null if no node was found.
00425       \sa eZContentObjectTreeNode::fetchNode
00426     */
00427     function fetchNode()
00428     {
00429         return eZContentObjectTreeNode::fetchNode( $this->ContentobjectID, $this->ParentNode );
00430     }
00431 
00432     /*!
00433      Fetches the node assignment which has id \a $id and returns it.
00434      \sa fetchListByID
00435     */
00436     static function fetchByID( $id ,$asObject = true )
00437     {
00438         $cond = array( 'id' => $id );
00439         return eZPersistentObject::fetchObject( eZNodeAssignment::definition(),
00440                                                 null, $cond,
00441                                                 $asObject );
00442     }
00443 
00444     /*!
00445      Fetches all node assignments which is mentioned in array \a $ID and returns it.
00446      \sa fetchByID
00447     */
00448     static function fetchListByID( $idList ,$asObject = true )
00449     {
00450         $cond = array( 'id' => array( $idList ) );
00451         return eZPersistentObject::fetchObjectList( eZNodeAssignment::definition(),
00452                                                     null, $cond, null, null,
00453                                                     $asObject );
00454     }
00455 
00456     function cloneNodeAssignment( $nextVersionNumber = 1, $contentObjectID = false )
00457     {
00458         $assignmentRow = array( 'contentobject_id' => $this->attribute( 'contentobject_id' ),
00459                                 'contentobject_version' => $nextVersionNumber,
00460                                 'remote_id' => $this->attribute( 'remote_id' ),
00461                                 'parent_node' => $this->attribute( 'parent_node' ),
00462                                 'sort_field' => $this->attribute( 'sort_field' ),
00463                                 'sort_order' => $this->attribute( 'sort_order' ),
00464                                 'is_main' => $this->attribute( 'is_main' ),
00465                                 'parent_remote_id' => $this->attribute( 'parent_remote_id' ) );
00466         if ( $contentObjectID !== false )
00467             $assignmentRow['contentobject_id'] = $contentObjectID;
00468         return eZNodeAssignment::create( $assignmentRow );
00469     }
00470 
00471     function getParentNode()
00472     {
00473         return eZContentObjectTreeNode::fetch( $this->attribute( 'parent_node' ) );
00474     }
00475 
00476     /*!
00477      \return The contentobject which the parent node points to.
00478     */
00479     function getParentObject( )
00480     {
00481         return eZContentObject::fetchByNodeID( $this->attribute( 'parent_node' ) );
00482     }
00483 
00484     /*!
00485     \static
00486     Chooses and sets new main assignment for the specified object, in case if there's main assignment already.
00487     \return false if there is already main assignment, true on success.
00488      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00489      the calls within a db transaction; thus within db->begin and db->commit.
00490     */
00491     static function setNewMainAssignment( $objectID, $version )
00492     {
00493 
00494         $assignments = eZNodeAssignment::fetchForObject( $objectID, $version );
00495 
00496         if ( count( $assignments ) == 0 )
00497             return true;
00498 
00499         // check: if there is already main assignment for the object then we should do nothing
00500         // BTW choose first nonremoving assignment as new main assignment
00501         $newMainAssignment = null;
00502         foreach ( $assignments as $key => $assignment )
00503         {
00504             if ( $assignment->attribute( 'op_code' ) != eZNodeAssignment::OP_CODE_REMOVE )
00505             {
00506                 if ( $newMainAssignment === null )
00507                 {
00508                     $newMainAssignment = $assignment;
00509                 }
00510                 if ( $assignment->attribute( 'is_main' ) )
00511                 {
00512                     return false;
00513                 }
00514             }
00515         }
00516 
00517         $db = eZDB::instance();
00518 
00519         if ( $newMainAssignment === null )
00520         {
00521             $db->query( "UPDATE eznode_assignment SET is_main=0 WHERE contentobject_id=$objectID AND contentobject_version=$version" );
00522             return false;
00523         }
00524 
00525         $parentMainNodeID = $newMainAssignment->attribute( 'parent_node' );
00526 
00527         $db->begin();
00528         $db->query( "UPDATE eznode_assignment SET is_main=1 WHERE contentobject_id=$objectID AND contentobject_version=$version AND parent_node=$parentMainNodeID" );
00529         $db->query( "UPDATE eznode_assignment SET is_main=0 WHERE contentobject_id=$objectID AND contentobject_version=$version AND parent_node<>$parentMainNodeID" );
00530         $db->commit();
00531 
00532         return true;
00533     }
00534 
00535     /// \privatesection
00536     public $ID;
00537     /// Used for giving unique values to an assignment which can later be checked.
00538     /// This is often used in templates to provide limited choices for assignments.
00539     public $RemoteID;
00540     public $ParentRemoteID;
00541     public $ContentobjectID;
00542     public $ContentObjectVersion;
00543     public $ParentNode;
00544     public $SortField;
00545     public $SortOrder;
00546     public $Main;
00547     public $FromNodeID;
00548 }
00549 
00550 ?>