eZ Publish  [trunk]
node_edit.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00004  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00005  * @version //autogentag//
00006  * @package kernel
00007  */
00008 
00009 function checkNodeAssignments( $module, $class, $object, $version, $contentObjectAttributes, $editVersion, $editLanguage, $FromLanguage, &$validation )
00010 {
00011     $http = eZHTTPTool::instance();
00012 
00013     // If the object has been previously published we do not allow node assignment operations
00014     if ( $object->attribute( 'status' ) != eZContentObject::STATUS_DRAFT )
00015     {
00016         if ( !$module->isCurrentAction( 'AddPrimaryNodeAssignment' ) )
00017         {
00018             return;
00019         }
00020     }
00021 
00022     // If node assignment handling is diabled we return immedieately
00023     $useNodeAssigments = true;
00024     if ( $http->hasPostVariable( 'UseNodeAssigments' ) )
00025         $useNodeAssigments = (bool)$http->postVariable( 'UseNodeAssigments' );
00026 
00027     if ( !$useNodeAssigments )
00028         return;
00029 
00030     $ObjectID = $object->attribute( 'id' );
00031     // Assign to nodes
00032     if ( $module->isCurrentAction( 'AddNodeAssignment' ) ||
00033          $module->isCurrentAction( 'AddPrimaryNodeAssignment' ) )
00034     {
00035         $selectedNodeIDArray = eZContentBrowse::result( 'AddNodeAssignment' );
00036         $assignedNodes = $version->nodeAssignments();
00037         $assignedIDArray = array();
00038         $setMainNode = false;
00039         $hasMainNode = false;
00040         foreach ( $assignedNodes as $assignedNode )
00041         {
00042             $assignedNodeID = $assignedNode->attribute( 'parent_node' );
00043             if ( $assignedNode->attribute( 'is_main' ) )
00044                 $hasMainNode = true;
00045             $assignedIDArray[] = $assignedNodeID;
00046         }
00047         if ( !$hasMainNode )
00048             $setMainNode = true;
00049 
00050         // prevent PHP warning
00051         if ( !isset( $selectedNodeIDArray ) || !is_array( $selectedNodeIDArray ) )
00052              $selectedNodeIDArray = array();
00053 
00054         foreach ( $selectedNodeIDArray as $nodeID )
00055         {
00056             if ( !in_array( $nodeID, $assignedIDArray ) )
00057             {
00058                 $isPermitted = true;
00059                 // Check access
00060                 $newNode = eZContentObjectTreeNode::fetch( $nodeID );
00061                 $newNodeObject = $newNode->attribute( 'object' );
00062 
00063                 $canCreate = $newNodeObject->checkAccess( 'create', $class->attribute( 'id' ), $newNodeObject->attribute( 'contentclass_id' ) ) == 1;
00064                 if ( !$canCreate )
00065                     $isPermitted = false;
00066                 else
00067                 {
00068                     $canCreateClassList = $newNodeObject->attribute( 'can_create_class_list' );
00069                     $objectClassID = $object->attribute( 'contentclass_id' );
00070                     $canCreateClassIDList = array();
00071                     foreach ( array_keys( $canCreateClassList ) as $key )
00072                     {
00073                         $canCreateClassIDList[] = $canCreateClassList[$key]['id'];
00074                     }
00075                     if ( !in_array( $objectClassID, $canCreateClassIDList ) )
00076                         $isPermitted = false;
00077                 }
00078                 if ( !$isPermitted )
00079                 {
00080                     eZDebug::writeError( $newNode->pathWithNames(), "You are not allowed to place this object under:" );
00081                     $validation[ 'placement' ][] = array( 'text' => ezpI18n::tr( 'kernel/content', 'You are not allowed to place this object under: %1', null, array( $newNode->pathWithNames() ) ) );
00082                     $validation[ 'processed' ] = true;
00083                     // Error message.
00084                 }
00085                 else
00086                 {
00087                     $isMain = 0;
00088                     if ( $setMainNode )
00089                         $isMain = 1;
00090                     $setMainNode = false;
00091                     $db = eZDB::instance();
00092                     $db->begin();
00093                     $version->assignToNode( $nodeID, $isMain );
00094                     $db->commit();
00095                 }
00096             }
00097         }
00098     }
00099 }
00100 
00101 function checkNodeMovements( $module, $class, $object, $version, $contentObjectAttributes, $editVersion, $editLanguage, $FromLanguage, &$validation )
00102 {
00103     $http = eZHTTPTool::instance();
00104 
00105     // If the object has been previously published we do not allow node assignment operations
00106     if ( $object->attribute( 'status' ) != eZContentObject::STATUS_DRAFT )
00107     {
00108         return;
00109     }
00110 
00111     // If node assignment handling is diabled we return immedieately
00112     $useNodeAssigments = true;
00113     if ( $http->hasPostVariable( 'UseNodeAssigments' ) )
00114         $useNodeAssigments = (bool)$http->postVariable( 'UseNodeAssigments' );
00115 
00116     if ( !$useNodeAssigments )
00117         return;
00118 
00119     $ObjectID = $object->attribute( 'id' );
00120     // Move to another node
00121     if ( $module->isCurrentAction( 'MoveNodeAssignment' ) )
00122     {
00123         $selectedNodeIDArray = eZContentBrowse::result( 'MoveNodeAssignment' );
00124         $fromNodeID = $http->postVariable( "FromNodeID" );
00125         $oldAssignmentParentID = $http->postVariable( 'OldAssignmentParentID' );
00126 
00127         if ( $selectedNodeIDArray != null )
00128         {
00129             $assignedNodes = $version->nodeAssignments();
00130             $assignedIDArray = array();
00131             foreach ( $assignedNodes as  $assignedNode )
00132             {
00133                 $assignedNodeID = $assignedNode->attribute( 'parent_node' );
00134                 $assignedIDArray[] = $assignedNodeID;
00135             }
00136             foreach ( $selectedNodeIDArray as $nodeID )
00137             {
00138                 if ( !in_array( $nodeID, $assignedIDArray ) )
00139                 {
00140                     $isPermitted = true;
00141                     // Check access
00142                     $newNode = eZContentObjectTreeNode::fetch( $nodeID );
00143                     $newNodeObject = $newNode->attribute( 'object' );
00144 
00145                     $canCreate = $newNodeObject->checkAccess( 'create', $class->attribute( 'id' ), $newNodeObject->attribute( 'contentclass_id' ) ) == 1;
00146                     eZDebug::writeDebug( $canCreate,"can create");
00147                     if ( !$canCreate )
00148                     {
00149                         $isPermitted = false;
00150                     }
00151                     else
00152                     {
00153                         $canCreateClassList = $newNodeObject->attribute( 'can_create_class_list' );
00154                         $canCreateClassIDList = array();
00155                         foreach ( array_keys( $canCreateClassList ) as $key )
00156                         {
00157                             $canCreateClassIDList[] = $canCreateClassList[$key]['id'];
00158                         }
00159                         $objectClassID = $object->attribute( 'contentclass_id' );
00160                         if ( !in_array( $objectClassID, $canCreateClassIDList ) )
00161                              $isPermitted = false;
00162                     }
00163                     if ( !$isPermitted )
00164                     {
00165                         eZDebug::writeError( $newNode->pathWithNames(), "You are not allowed to place this object under:" );
00166                         $validation[ 'placement' ][] = array( 'text' => ezpI18n::tr( 'kernel/content', "You are not allowed to place this object under: %1", null, array( $newNode->pathWithNames() ) ) );
00167                         $validation[ 'processed' ] = true;
00168                         // Error message.
00169                     }
00170                     else
00171                     {
00172                         $oldAssignment = eZPersistentObject::fetchObject( eZNodeAssignment::definition(),
00173                                                                            null,
00174                                                                            array( 'contentobject_id' => $object->attribute( 'id' ),
00175                                                                                   'parent_node' => $oldAssignmentParentID,
00176                                                                                   'contentobject_version' => $version->attribute( 'version' )
00177                                                                                   ),
00178                                                                            true );
00179 
00180                         $realNode = eZContentObjectTreeNode::fetchNode( $version->attribute( 'contentobject_id' ), $oldAssignment->attribute( 'parent_node' ) );
00181 
00182                         $db = eZDB::instance();
00183                         $db->begin();
00184                         // No longer remove then add assignment, instead change the existing one
00185                         if ( $realNode  === null )
00186                         {
00187                             $fromNodeID = 0;
00188                         }
00189                         if ( $oldAssignment->attribute( 'is_main' ) == '1' )
00190                         {
00191                             $oldAssignment->setAttribute( 'parent_node', $nodeID );
00192                             $oldAssignment->setAttribute( 'is_main', 1 );
00193                             $oldAssignment->setAttribute( 'from_node_id', $fromNodeID );
00194 //                            $version->assignToNode( $nodeID, 1, $fromNodeID, $oldAssignment->attribute( 'sort_field' ), $oldAssignment->attribute( 'sort_order' ) );
00195                         }
00196                         else
00197                         {
00198                             $oldAssignment->setAttribute( 'parent_node', $nodeID );
00199                             $oldAssignment->setAttribute( 'is_main', 0 );
00200                             $oldAssignment->setAttribute( 'from_node_id', $fromNodeID );
00201 //                            $version->assignToNode( $nodeID, 0, $fromNodeID, $oldAssignment->attribute( 'sort_field' ), $oldAssignment->attribute( 'sort_order' ) );
00202                         }
00203                         $oldAssignment->setAttribute( 'op_code', eZNodeAssignment::OP_CODE_MOVE );
00204                         $oldAssignment->store();
00205                         //$version->removeAssignment( $oldAssignmentParentID );
00206                         $db->commit();
00207                     }
00208                 }
00209             }
00210         }
00211     }
00212 }
00213 
00214 function storeNodeAssignments( $module, $class, $object, $version, $contentObjectAttributes, $editVersion, $editLanguage )
00215 {
00216     $http = eZHTTPTool::instance();
00217 
00218     // If the object has been previously published we do not allow node assignment operations
00219     if ( $object->attribute( 'status' ) != eZContentObject::STATUS_DRAFT )
00220     {
00221         return;
00222     }
00223 
00224     // If node assignment handling is diabled we return immedieately
00225     $useNodeAssigments = true;
00226     if ( $http->hasPostVariable( 'UseNodeAssigments' ) )
00227         $useNodeAssigments = (bool)$http->postVariable( 'UseNodeAssigments' );
00228 
00229     if ( !$useNodeAssigments )
00230         return;
00231 
00232     $setPlacementNodeIDArray = array();
00233     if ( $http->hasPostVariable( 'SetPlacementNodeIDArray' ) )
00234         $setPlacementNodeIDArray = $http->postVariable( 'SetPlacementNodeIDArray' );
00235 
00236     // We will quit if some important POST variables are missing
00237     if ( !$http->hasPostVariable( 'MainNodeID' ) and
00238          !$http->hasPostVariable( 'SortOrderMap' ) and
00239          !$http->hasPostVariable( 'SortFieldMap' ) )
00240         return;
00241 
00242     $mainNodeID = false;
00243     if ( $http->hasPostVariable( 'MainNodeID' ) )
00244     {
00245         $mainNodeID = trim( $http->postVariable( 'MainNodeID' ) );
00246         if ( strlen( $mainNodeID ) == 0 )
00247             return;
00248     }
00249 
00250     // Check if dropdown placement is used
00251     if ( $http->hasPostVariable( 'MainAssignmentElementNumber' ) )
00252     {
00253         $elementNumber = $http->postVariable( 'MainAssignmentElementNumber' );
00254 
00255         $mainNodeID = $setPlacementNodeIDArray[$elementNumber];
00256     }
00257 
00258     $nodeID = eZContentObjectTreeNode::findNode( $mainNodeID, $object->attribute('id') );
00259     eZDebugSetting::writeDebug( 'kernel-content-edit', $nodeID, 'nodeID' );
00260 //    $object->setAttribute( 'main_node_id', $nodeID );
00261     $nodeAssignments = eZNodeAssignment::fetchForObject( $object->attribute( 'id' ), $version->attribute( 'version' ) ) ;
00262     eZDebugSetting::writeDebug( 'kernel-content-edit', $mainNodeID, "mainNodeID" );
00263 
00264 
00265     $setPlacementNodeIDArray = array_unique( $setPlacementNodeIDArray );
00266     eZDebugSetting::writeDebug( 'kernel-content-edit', $setPlacementNodeIDArray, '$setPlacementNodeIDArray' );
00267     $remoteIDFieldMap = array();
00268     if ( $http->hasPostVariable( 'SetRemoteIDFieldMap' ) )
00269         $remoteIDFieldMap = $http->postVariable( 'SetRemoteIDFieldMap' );
00270     $remoteIDOrderMap = array();
00271     if ( $http->hasPostVariable( 'SetRemoteIDOrderMap' ) )
00272         $remoteIDOrderMap = $http->postVariable( 'SetRemoteIDOrderMap' );
00273 
00274     $db = eZDB::instance();
00275     $db->begin();
00276     if ( count( $setPlacementNodeIDArray ) > 0 )
00277     {
00278         foreach ( $setPlacementNodeIDArray as $setPlacementRemoteID => $setPlacementNodeID )
00279         {
00280             $hasAssignment = false;
00281             foreach ( $nodeAssignments as $nodeAssignment )
00282             {
00283                 if ( $nodeAssignment->attribute( 'remote_id' ) == $setPlacementRemoteID )
00284                 {
00285                     eZDebugSetting::writeDebug( 'kernel-content-edit', "Remote ID $setPlacementRemoteID already in use for node " . $nodeAssignment->attribute( 'parent_node' ), 'node_edit' );
00286                     if ( isset( $remoteIDFieldMap[$setPlacementRemoteID] ) )
00287                         $nodeAssignment->setAttribute( 'sort_field',  $remoteIDFieldMap[$setPlacementRemoteID] );
00288                     if ( isset( $remoteIDOrderMap[$setPlacementRemoteID] ) )
00289                         $nodeAssignment->setAttribute( 'sort_order', $remoteIDOrderMap[$setPlacementRemoteID] );
00290                     $nodeAssignment->setAttribute( 'parent_node', $setPlacementNodeID );
00291                     $nodeAssignment->sync();
00292                     $hasAssignment = true;
00293                     break;
00294                 }
00295             }
00296             if ( !$hasAssignment )
00297             {
00298                 eZDebugSetting::writeDebug( 'kernel-content-edit', "Adding to node $setPlacementNodeID", 'node_edit' );
00299                 $sortField = null;
00300                 $sortOrder = null;
00301                 if ( isset( $remoteIDFieldMap[$setPlacementRemoteID] ) )
00302                     $sortField = $remoteIDFieldMap[$setPlacementRemoteID];
00303                 if ( isset( $remoteIDOrderMap[$setPlacementRemoteID] ) )
00304                     $sortOrder = $remoteIDOrderMap[$setPlacementRemoteID];
00305                 $version->assignToNode( $setPlacementNodeID, 0, 0, $sortField, $sortOrder, $setPlacementRemoteID );
00306             }
00307         }
00308         $nodeAssignments = eZNodeAssignment::fetchForObject( $object->attribute( 'id' ), $version->attribute( 'version' ) );
00309     }
00310 
00311     $sortOrderMap = false;
00312     if ( $http->hasPostVariable( 'SortOrderMap' ) )
00313         $sortOrderMap = $http->postVariable( 'SortOrderMap' );
00314     $sortFieldMap = false;
00315     if ( $http->hasPostVariable( 'SortFieldMap' ) )
00316         $sortFieldMap = $http->postVariable( 'SortFieldMap' );
00317 
00318 //     $assigedNodes = eZContentObjectTreeNode::fetchByContentObjectID( $object->attribute('id') );
00319     foreach ( $nodeAssignments as $nodeAssignment )
00320     {
00321         eZDebugSetting::writeDebug( 'kernel-content-edit', $nodeAssignment, "nodeAssignment" );
00322         if ( $sortFieldMap !== false )
00323         {
00324             if ( isset( $sortFieldMap[$nodeAssignment->attribute( 'id' )] ) )
00325                 $nodeAssignment->setAttribute( 'sort_field', $sortFieldMap[$nodeAssignment->attribute( 'id' )] );
00326         }
00327 
00328         if ( $sortOrderMap !== false )
00329         {
00330             $sortOrder = 1;
00331             if ( isset( $sortOrderMap[$nodeAssignment->attribute( 'id' )] ) and
00332                  $sortOrderMap[$nodeAssignment->attribute( 'id' )] == 1 )
00333                 $sortOrder = $sortOrderMap[$nodeAssignment->attribute( 'id' )];
00334             else
00335                 $sortOrder = 0;
00336 
00337             $nodeAssignment->setAttribute( 'sort_order', $sortOrder );
00338         }
00339 
00340 
00341         if ( $nodeAssignment->attribute( 'is_main' ) == 1 and
00342              $nodeAssignment->attribute( 'parent_node' ) != $mainNodeID )
00343         {
00344             $nodeAssignment->setAttribute( 'is_main', 0 );
00345         }
00346         else if ( $nodeAssignment->attribute( 'is_main' ) == 0 and
00347                   $nodeAssignment->attribute( 'parent_node' ) == $mainNodeID )
00348         {
00349             $nodeAssignment->setAttribute( 'is_main', 1 );
00350         }
00351         $nodeAssignment->store();
00352     }
00353     $db->commit();
00354 }
00355 
00356 function checkNodeActions( $module, $class, $object, $version, $contentObjectAttributes, $editVersion, $editLanguage, $fromLanguage )
00357 {
00358     // If the object has been previously published we do not allow node assignment operations
00359     if ( $object->attribute( 'status' ) != eZContentObject::STATUS_DRAFT )
00360     {
00361         if ( !$module->isCurrentAction( 'BrowseForPrimaryNodes' ) )
00362         {
00363             return;
00364         }
00365     }
00366 
00367     $http = eZHTTPTool::instance();
00368 
00369     if ( $module->isCurrentAction( 'BrowseForNodes' ) ||
00370          $module->isCurrentAction( 'BrowseForPrimaryNodes' ) )
00371     {
00372         // Remove custom actions from attribute editing.
00373         $http->removeSessionVariable( 'BrowseCustomAction' );
00374 
00375         $ignoreNodesSelect = array();
00376         $ignoreNodesClick  = array();
00377         $assigned = $version->nodeAssignments();
00378         $publishedAssigned = $object->assignedNodes( false );
00379         $isTopLevel = false;
00380         foreach ( $publishedAssigned as $element )
00381         {
00382             $append = false;
00383             if ( $element['parent_node_id'] == 1 )
00384                 $isTopLevel = true;
00385             foreach ( $assigned as $ass )
00386             {
00387                 if ( $ass->attribute( 'parent_node' ) == $element['parent_node_id'] )
00388                     $append = true;
00389             }
00390 
00391             /* If the current version (draft) has no assigned nodes then
00392              * we should disallow adding assignments under nodes
00393              * the previous published version is assignned to.
00394              * Thus we avoid fatal errors in eZ Publish.
00395              */
00396             if ( count($assigned) == 0 )
00397             {
00398                 $ignoreNodesSelect[] = $element['node_id'];
00399                 $ignoreNodesClick[]  = $element['node_id'];
00400             }
00401 
00402             if ( $append )
00403             {
00404                 $ignoreNodesSelect[] = $element['node_id'];
00405                 $ignoreNodesClick[]  = $element['node_id'];
00406                 $ignoreNodesSelect[] = $element['parent_node_id'];
00407             }
00408         }
00409         if ( !$isTopLevel )
00410         {
00411             $ignoreNodesSelect = array_unique( $ignoreNodesSelect );
00412             $objectID = $object->attribute( 'id' );
00413             $action = 'AddNodeAssignment';
00414             if ( $module->isCurrentAction( 'BrowseForPrimaryNodes' ) )
00415             {
00416                 $action = 'AddPrimaryNodeAssignment';
00417             }
00418             eZContentBrowse::browse( array( 'action_name' => $action,
00419                                             'description_template' => 'design:content/browse_placement.tpl',
00420                                             'keys' => array( 'class' => $class->attribute( 'id' ),
00421                                                              'class_id' => $class->attribute( 'identifier' ),
00422                                                              'classgroup' => $class->attribute( 'ingroup_id_list' ),
00423                                                              'section' => $object->attribute( 'section_id' ) ),
00424                                             'ignore_nodes_select' => $ignoreNodesSelect,
00425                                             'ignore_nodes_click'  => $ignoreNodesClick,
00426                                             'content' => array( 'object_id' => $objectID,
00427                                                                 'object_version' => $editVersion,
00428                                                                 'object_language' => $editLanguage ),
00429                                             'from_page' => "/content/edit/$objectID/$editVersion/$editLanguage/$fromLanguage" ),
00430                                      $module );
00431 
00432             return eZModule::HOOK_STATUS_CANCEL_RUN;
00433         }
00434     }
00435 
00436     // If node assignment handling is diabled we return
00437     $useNodeAssigments = true;
00438     if ( $http->hasPostVariable( 'UseNodeAssigments' ) )
00439         $useNodeAssigments = (bool)$http->postVariable( 'UseNodeAssigments' );
00440 
00441     if ( !$useNodeAssigments )
00442         return;
00443 
00444     // Remove custom actions from attribute editing.
00445     $http->removeSessionVariable( 'BrowseCustomAction' );
00446 
00447     if ( $module->isCurrentAction( 'ConfirmAssignmentDelete' ) && $http->hasPostVariable( 'RemoveNodeID' ) )
00448     {
00449         $nodeID = $http->postVariable( 'RemoveNodeID' );
00450         $db = eZDB::instance();
00451         $db->begin();
00452         $version->removeAssignment( $nodeID );
00453         $db->commit();
00454     }
00455 
00456     if ( $module->isCurrentAction( 'DeleteNode' ) )
00457     {
00458 
00459         if ( $http->hasPostVariable( 'RemoveNodeID' ) )
00460         {
00461             $nodeID = $http->postVariable( 'RemoveNodeID' );
00462         }
00463 
00464         $mainNodeID = $http->postVariable( 'MainNodeID' );
00465 
00466 //         if ( $nodeID != $mainNodeID )
00467         {
00468             $objectID = $object->attribute( 'id' );
00469             $publishedNode = eZContentObjectTreeNode::fetchNode( $objectID, $nodeID );
00470             if ( $publishedNode != null )
00471             {
00472                 $publishParentNodeID = $publishedNode->attribute( 'parent_node_id' );
00473                 if ( $publishParentNodeID > 1 )
00474                 {
00475                     $childrenCount = $publishedNode->childrenCount();
00476                     if ( $childrenCount != 0 )
00477                     {
00478                         $module->redirectToView( 'removenode', array( $objectID, $editVersion, $editLanguage, $nodeID ) );
00479                         return eZModule::HOOK_STATUS_CANCEL_RUN;
00480                     }
00481                     else
00482                     {
00483                         $db = eZDB::instance();
00484                         $db->begin();
00485                         $version->removeAssignment( $nodeID );
00486                         $db->commit();
00487                     }
00488                 }
00489             }
00490             else
00491             {
00492                 $nodeAssignment = eZNodeAssignment::fetch( $objectID, $version->attribute( 'version' ), $nodeID );
00493                 if ( $nodeAssignment->attribute( 'from_node_id' ) != 0 )
00494                 {
00495                     $publishedNode = eZContentObjectTreeNode::fetchNode( $objectID, $nodeAssignment->attribute( 'from_node_id' ) );
00496                     $childrenCount = 0;
00497                     if ( $publishedNode !== null )
00498                         $childrenCount = $publishedNode->childrenCount();
00499                     if ( $childrenCount != 0 )
00500                     {
00501                         $module->redirectToView( 'removenode', array( $objectID, $editVersion, $editLanguage, $nodeID ) );
00502                         return eZModule::HOOK_STATUS_CANCEL_RUN;
00503                     }
00504                 }
00505                 $db = eZDB::instance();
00506                 $db->begin();
00507                 $version->removeAssignment( $nodeID );
00508                 $db->commit();
00509             }
00510         }
00511     }
00512 
00513     if ( $module->isCurrentAction( 'RemoveAssignments' ) )
00514     {
00515         if( $http->hasPostVariable( 'AssignmentIDSelection' ) )
00516         {
00517             $selected       = $http->postVariable( 'AssignmentIDSelection' );
00518             $objectID       = $object->attribute( 'id' );
00519             $versionInt     = $version->attribute( 'version' );
00520             $hasChildren    = false;
00521             $assignmentsIDs = array();
00522             $assignments    = array();
00523 
00524             // Determine if at least one node of ones we remove assignments for has children.
00525             foreach ( $selected as $parentNodeID )
00526             {
00527                 $assignment = eZNodeAssignment::fetch( $objectID, $versionInt, $parentNodeID );
00528                 if( !$assignment )
00529                 {
00530                     eZDebug::writeWarning( "No assignment found for object $objectID version $versionInt, parent node $parentNodeID" );
00531                     continue;
00532                 }
00533 
00534                 $assignmentID     =  $assignment->attribute( 'id' );
00535                 $assignmentsIDs[] =  $assignmentID;
00536                 $assignments[]    = $assignment;
00537                 $node             = $assignment->attribute( 'node' );
00538 
00539                 if( !$node )
00540                     continue;
00541 
00542                 if( $node->childrenCount( false ) > 0 )
00543                     $hasChildren = true;
00544 
00545                 unset( $assignment );
00546             }
00547 
00548             if ( $hasChildren )
00549             {
00550                 // We need user confirmation if at least one node we want to remove assignment for contains children.
00551                 // Aactual removal is done in content/removeassignment in this case.
00552                 $http->setSessionVariable( 'AssignmentRemoveData',
00553                                            array( 'remove_list'   => $assignmentsIDs,
00554                                                   'object_id'     => $objectID,
00555                                                   'edit_version'  => $versionInt,
00556                                                   'edit_language' => $editLanguage,
00557                                                   'from_language' => $fromLanguage ) );
00558                 $module->redirectToView( 'removeassignment' );
00559                 return eZModule::HOOK_STATUS_CANCEL_RUN;
00560 
00561             }
00562             else
00563             {
00564                 // Just remove all the selected locations.
00565                 $mainNodeChanged = false;
00566                 $db = eZDB::instance();
00567                 $db->begin();
00568                 foreach ( $assignments as $assignment )
00569                 {
00570                     $assignmentID = $assignment->attribute( 'id' );
00571                     if ( $assignment->attribute( 'is_main' ) )
00572                         $mainNodeChanged = true;
00573                     eZNodeAssignment::removeByID( $assignmentID );
00574                 }
00575                 if ( $mainNodeChanged )
00576                     eZNodeAssignment::setNewMainAssignment( $objectID, $versionInt );
00577                 $db->commit();
00578                 unset( $mainNodeChanged );
00579             }
00580             unset( $assignmentsIDs, $assignments );
00581 
00582         }
00583         else
00584         {
00585             eZDebug::writeNotice( 'No nodes to remove selected' );
00586         }
00587     }
00588 
00589     if ( $module->isCurrentAction( 'MoveNode' ) )
00590     {
00591         $objectID = $object->attribute( 'id' );
00592         if ( $http->hasPostVariable( 'MoveNodeID' ) )
00593         {
00594             $fromNodeID = $http->postVariable( 'MoveNodeID' ); //$sourceNodeID[0];
00595             $oldAssignmentParentID = $fromNodeID;
00596             $fromNodeAssignment = eZNodeAssignment::fetch( $objectID, $version->attribute( 'version' ), $fromNodeID );
00597             $publishParentNodeID = $fromNodeAssignment->attribute( 'parent_node' );
00598             if ( $publishParentNodeID > 1 )
00599             {
00600                 if( $fromNodeAssignment->attribute( 'from_node_id' ) != 0 )
00601                 {
00602                     $fromNodeID = $fromNodeAssignment->attribute( 'from_node_id' );
00603                     $oldAssignmentParentID = $fromNodeAssignment->attribute( 'parent_node' );
00604                 }
00605 
00606                 // we don't allow moving object to itself, to its descendants or parent object(s)
00607                 $objectAssignedNodes = $object->attribute( 'assigned_nodes' );
00608 
00609                 // nodes that are not allowed to select (via checkbox or radiobutton) when browsing
00610                 $ignoreNodesSelectArray = array();
00611 
00612                 // nodes that are not allowed to click on
00613                 $ignoreNodesClickArray  = array();
00614                 foreach( $objectAssignedNodes as $curAN )
00615                 {
00616                     // current node should be neither selectable, nor clickable
00617                     $ignoreNodesClickArray[]  = $curAN->NodeID;
00618                     $ignoreNodesSelectArray[] = $curAN->NodeID;
00619 
00620                     // parent node should be only clickable, but not selectable
00621                     $ignoreNodesSelectArray[] = $curAN->ParentNodeID;
00622                 }
00623 
00624                 eZContentBrowse::browse( array( 'action_name' => 'MoveNodeAssignment',
00625                                                 'description_template' => 'design:content/browse_move_placement.tpl',
00626                                                 'keys' => array( 'class' => $class->attribute( 'id' ),
00627                                                                  'class_id' => $class->attribute( 'identifier' ),
00628                                                                  'classgroup' => $class->attribute( 'ingroup_id_list' ),
00629                                                                  'section' => $object->attribute( 'section_id' ) ),
00630                                                 'start_node' => $fromNodeID,
00631                                                 'persistent_data' => array( 'FromNodeID' => $fromNodeID,
00632                                                                             'OldAssignmentParentID' => $oldAssignmentParentID ),
00633                         'ignore_nodes_select' => $ignoreNodesSelectArray,
00634                         'ignore_nodes_click'  => $ignoreNodesClickArray,
00635                                                 'content' => array( 'object_id' => $objectID,
00636                                                                     'previous_node_id' => $fromNodeID,
00637                                                                     'object_version' => $editVersion,
00638                                                                     'object_language' => $editLanguage ),
00639                                                 'from_page' => "/content/edit/$objectID/$editVersion/$editLanguage" ),
00640                                          $module );
00641 
00642                 return eZModule::HOOK_STATUS_CANCEL_RUN;
00643             }
00644         }
00645     }
00646 }
00647 
00648 function handleNodeTemplate( $module, $class, $object, $version, $contentObjectAttributes, $editVersion, $editLanguage, $tpl )
00649 {
00650     // When the object has been published we will use the nodes as
00651     // node-assignments by faking the list, this is required since new
00652     // version of the object does not have node-assignments/
00653     // When the object is a draft we use the normal node-assignment list
00654     $assignedNodeArray = array();
00655     $versionedAssignedNodeArray = $version->attribute( 'parent_nodes' );
00656     $parentNodeIDMap = array();
00657     $nodes = $object->assignedNodes();
00658     $i = 0;
00659     foreach ( $nodes as $node )
00660     {
00661         $data = array( 'id' => null,
00662                        'contentobject_id' => $object->attribute( 'id' ),
00663                        'contentobject_version' => $version->attribute( 'version' ),
00664                        'parent_node' => $node->attribute( 'parent_node_id' ),
00665                        'sort_field' => $node->attribute( 'sort_field' ),
00666                        'sort_order' => $node->attribute( 'sort_order' ),
00667                        'is_main' => ( $node->attribute( 'main_node_id' ) == $node->attribute( 'node_id' ) ? 1 : 0 ),
00668                        'parent_remote_id' => $node->attribute( 'remote_id' ),
00669                        'op_code' => eZNodeAssignment::OP_CODE_NOP );
00670         $assignment = eZNodeAssignment::create( $data );
00671         $assignedNodeArray[$i] = $assignment;
00672         $parentNodeIDMap[$node->attribute( 'parent_node_id' )] = $i;
00673         ++$i;
00674     }
00675     foreach ( $versionedAssignedNodeArray as $nodeAssignment )
00676     {
00677         $opCode = $nodeAssignment->attribute( 'op_code' );
00678         if ( ( $opCode & 1 ) == eZNodeAssignment::OP_CODE_NOP ) // If the execute bit is not set it will be ignored.
00679         {
00680             continue;
00681         }
00682         // Only add assignments whose parent is not present in the published nodes.
00683         if ( isset( $parentNodeIDMap[$nodeAssignment->attribute( 'parent_node' )] ) )
00684         {
00685             if ( $opCode == eZNodeAssignment::OP_CODE_CREATE ) // CREATE entries are just skipped
00686             {
00687                 continue;
00688             }
00689             // Or if they have an op_code (move,remove) set, in which case they overwrite the entry
00690             $index = $parentNodeIDMap[$nodeAssignment->attribute( 'parent_node' )];
00691             $assignedNodeArray[$index]->setAttribute( 'id', $nodeAssignment->attribute( 'id' ) );
00692             $assignedNodeArray[$index]->setAttribute( 'from_node_id', $nodeAssignment->attribute( 'from_node_id' ) );
00693             $assignedNodeArray[$index]->setAttribute( 'remote_id', $nodeAssignment->attribute( 'remote_id' ) );
00694             $assignedNodeArray[$index]->setAttribute( 'op_code', $nodeAssignment->attribute( 'op_code' ) );
00695             continue;
00696         }
00697         if ( $opCode == eZNodeAssignment::OP_CODE_REMOVE ||
00698              $opCode == eZNodeAssignment::OP_CODE_MOVE )
00699         {
00700             // The node-assignment has a remove/move operation but the node does not exist.
00701             // We will not show it in this case.
00702             continue;
00703         }
00704         $assignedNodeArray[$i] = $nodeAssignment;
00705         ++$i;
00706     }
00707     eZDebugSetting::writeDebug( 'kernel-content-edit', $assignedNodeArray, "assigned nodes array" );
00708     $remoteMap = array();
00709 
00710     $db = eZDB::instance();
00711     $db->begin();
00712     foreach ( $assignedNodeArray as $assignedNodeKey => $assignedNode )
00713     {
00714         $node = $assignedNode->getParentNode();
00715         if ( $node !== null )
00716         {
00717             $remoteID = $assignedNode->attribute( 'remote_id' );
00718             if ( $remoteID > 0 )
00719             {
00720                 if ( isset( $remoteMap[$remoteID] ) )
00721                 {
00722                     if ( is_array( $remoteMap[$remoteID] ) )
00723                     {
00724                         $remoteMap[$remoteID][] = $assignedNode;
00725                     }
00726                     else
00727                     {
00728                         $currentRemote = $remoteMap[$remoteID];
00729                         unset( $remoteMap[$remoteID] );
00730                         $remoteMap[$remoteID] = array();
00731                         $remoteMap[$remoteID][] = $currentRemote;
00732                         $remoteMap[$remoteID][] = $assignedNode;
00733                     }
00734                 }
00735                 else
00736                 {
00737                     $remoteMap[$remoteID] = $assignedNode;
00738                 }
00739             }
00740         }
00741         else
00742         {
00743             $assignedNode->purge();
00744             if( isset( $assignedNodeArray[$assignedNodeKey] ) )
00745                 unset( $assignedNodeArray[$assignedNodeKey] );
00746         }
00747     }
00748     $db->commit();
00749     eZDebugSetting::writeDebug( 'kernel-content-edit', $assignedNodeArray, "assigned nodes array" );
00750 
00751     $currentVersion = $object->version( $editVersion );
00752     $publishedNodeArray = array();
00753     if ( $currentVersion )
00754         $publishedNodeArray = $currentVersion->attribute( 'parent_nodes' );
00755     $mainParentNodeID = $version->attribute( 'main_parent_node_id' );
00756 
00757     $tpl->setVariable( 'assigned_node_array', $assignedNodeArray );
00758     $tpl->setVariable( 'assigned_remote_map', $remoteMap );
00759     $tpl->setVariable( 'published_node_array', $publishedNodeArray );
00760     $tpl->setVariable( 'main_node_id', $mainParentNodeID );
00761 }
00762 
00763 function initializeNodeEdit( $module )
00764 {
00765     $module->addHook( 'post_fetch', 'checkNodeAssignments' );
00766     $module->addHook( 'post_fetch', 'checkNodeMovements' );
00767     $module->addHook( 'pre_commit', 'storeNodeAssignments' );
00768     $module->addHook( 'action_check', 'checkNodeActions' );
00769     $module->addHook( 'pre_template', 'handleNodeTemplate' );
00770 }
00771 
00772 ?>