eZContentOperationCollection::updateSectionID PHP Method

updateSectionID() public static method

public static updateSectionID ( $objectID, $versionNum )
    public static function updateSectionID($objectID, $versionNum)
    {
        $object = eZContentObject::fetch($objectID);
        $version = $object->version($versionNum);
        if ($versionNum == 1 or $object->attribute('current_version') == $versionNum) {
            $newMainAssignment = null;
            $newMainAssignments = eZNodeAssignment::fetchForObject($objectID, $versionNum, 1);
            if (isset($newMainAssignments[0])) {
                $newMainAssignment = $newMainAssignments[0];
            }
            // we should not update section id for toplevel nodes
            if ($newMainAssignment && $newMainAssignment->attribute('parent_node') != 1) {
                // We should check if current object already has been updated for section_id
                // If yes we should not update object section_id by $parentNodeSectionID
                $sectionID = $object->attribute('section_id');
                if ($sectionID > 0) {
                    return;
                }
                $newParentObject = $newMainAssignment->getParentObject();
                if (!$newParentObject) {
                    return array('status' => eZModuleOperationInfo::STATUS_CANCELLED);
                }
                $parentNodeSectionID = $newParentObject->attribute('section_id');
                $object->setAttribute('section_id', $parentNodeSectionID);
                $object->store();
            }
            return;
        }
        $newMainAssignmentList = eZNodeAssignment::fetchForObject($objectID, $versionNum, 1);
        $newMainAssignment = count($newMainAssignmentList) ? array_pop($newMainAssignmentList) : null;
        $currentVersion = $object->attribute('current');
        // Here we need to fetch published nodes and not old node assignments.
        $oldMainNode = $object->mainNode();
        if ($newMainAssignment && $oldMainNode && $newMainAssignment->attribute('parent_node') != $oldMainNode->attribute('parent_node_id')) {
            $oldMainParentNode = $oldMainNode->attribute('parent');
            if ($oldMainParentNode) {
                $oldParentObject = $oldMainParentNode->attribute('object');
                $oldParentObjectSectionID = $oldParentObject->attribute('section_id');
                if ($oldParentObjectSectionID == $object->attribute('section_id')) {
                    $newParentNode = $newMainAssignment->attribute('parent_node_obj');
                    if (!$newParentNode) {
                        return;
                    }
                    $newParentObject = $newParentNode->attribute('object');
                    if (!$newParentObject) {
                        return;
                    }
                    $newSectionID = $newParentObject->attribute('section_id');
                    if ($newSectionID != $object->attribute('section_id')) {
                        $oldSectionID = $object->attribute('section_id');
                        $object->setAttribute('section_id', $newSectionID);
                        $db = eZDB::instance();
                        $db->begin();
                        $object->store();
                        $mainNodeID = $object->attribute('main_node_id');
                        if ($mainNodeID > 0) {
                            eZContentObjectTreeNode::assignSectionToSubTree($mainNodeID, $newSectionID, $oldSectionID);
                        }
                        $db->commit();
                    }
                }
            }
        }
    }

Usage Example

 public static function publishNode($parentNodeID, $objectID, $versionNum, $mainNodeID)
 {
     $object = eZContentObject::fetch($objectID);
     $nodeAssignment = eZNodeAssignment::fetch($objectID, $versionNum, $parentNodeID);
     $version = $object->version($versionNum);
     $fromNodeID = $nodeAssignment->attribute('from_node_id');
     $originalObjectID = $nodeAssignment->attribute('contentobject_id');
     $nodeID = $nodeAssignment->attribute('parent_node');
     $opCode = $nodeAssignment->attribute('op_code');
     $parentNode = eZContentObjectTreeNode::fetch($nodeID);
     // if parent doesn't exist, return. See issue #18320
     if (!$parentNode instanceof eZContentObjectTreeNode) {
         eZDebug::writeError("Parent node doesn't exist. object id: {$objectID}, node_assignment id: " . $nodeAssignment->attribute('id'), __METHOD__);
         return;
     }
     $parentNodeID = $parentNode->attribute('node_id');
     $existingNode = null;
     $db = eZDB::instance();
     $db->begin();
     if (strlen($nodeAssignment->attribute('parent_remote_id')) > 0) {
         $existingNode = eZContentObjectTreeNode::fetchByRemoteID($nodeAssignment->attribute('parent_remote_id'));
     }
     if (!$existingNode) {
     }
     $existingNode = eZContentObjectTreeNode::findNode($nodeID, $object->attribute('id'), true);
     $updateSectionID = false;
     // now we check the op_code to see what to do
     if (($opCode & 1) == eZNodeAssignment::OP_CODE_NOP) {
         // There is nothing to do so just return
         $db->commit();
         if ($mainNodeID == false) {
             return $object->attribute('main_node_id');
         }
         return;
     }
     $updateFields = false;
     if ($opCode == eZNodeAssignment::OP_CODE_MOVE || $opCode == eZNodeAssignment::OP_CODE_CREATE) {
         //            if ( $fromNodeID == 0 || $fromNodeID == -1)
         if ($opCode == eZNodeAssignment::OP_CODE_CREATE || $opCode == eZNodeAssignment::OP_CODE_SET) {
             // If the node already exists it means we have a conflict (for 'CREATE').
             // We resolve this by leaving node-assignment data be.
             if ($existingNode == null) {
                 $parentNode = eZContentObjectTreeNode::fetch($nodeID);
                 $user = eZUser::currentUser();
                 if (!eZSys::isShellExecution() and !$user->isAnonymous()) {
                     eZContentBrowseRecent::createNew($user->id(), $parentNode->attribute('node_id'), $parentNode->attribute('name'));
                 }
                 $updateFields = true;
                 $existingNode = $parentNode->addChild($object->attribute('id'), true);
                 if ($fromNodeID == -1) {
                     $updateSectionID = true;
                 }
             } elseif ($opCode == eZNodeAssignment::OP_CODE_SET) {
                 $updateFields = true;
             }
         } elseif ($opCode == eZNodeAssignment::OP_CODE_MOVE) {
             if ($fromNodeID == 0 || $fromNodeID == -1) {
                 eZDebug::writeError("NodeAssignment '" . $nodeAssignment->attribute('id') . "' is marked with op_code='{$opCode}' but has no data in from_node_id. Cannot use it for moving node.", __METHOD__);
             } else {
                 // clear cache for old placement.
                 $additionalNodeIDList = array($fromNodeID);
                 eZContentCacheManager::clearContentCacheIfNeeded($objectID, $versionNum, $additionalNodeIDList);
                 $originalNode = eZContentObjectTreeNode::fetchNode($originalObjectID, $fromNodeID);
                 if ($originalNode->attribute('main_node_id') == $originalNode->attribute('node_id')) {
                     $updateSectionID = true;
                 }
                 $originalNode->move($parentNodeID);
                 $existingNode = eZContentObjectTreeNode::fetchNode($originalObjectID, $parentNodeID);
                 $updateFields = true;
             }
         }
     } elseif ($opCode == eZNodeAssignment::OP_CODE_REMOVE) {
         $db->commit();
         return;
     }
     if ($updateFields) {
         if (strlen($nodeAssignment->attribute('parent_remote_id')) > 0) {
             $existingNode->setAttribute('remote_id', $nodeAssignment->attribute('parent_remote_id'));
         }
         $existingNode->setAttribute('sort_field', $nodeAssignment->attribute('sort_field'));
         $existingNode->setAttribute('sort_order', $nodeAssignment->attribute('sort_order'));
     }
     $existingNode->setAttribute('contentobject_is_published', 1);
     eZDebug::createAccumulatorGroup('nice_urls_total', 'Nice urls');
     if ($mainNodeID > 0) {
         $existingNodeID = $existingNode->attribute('node_id');
         if ($existingNodeID != $mainNodeID) {
             eZContentBrowseRecent::updateNodeID($existingNodeID, $mainNodeID);
         }
         $existingNode->setAttribute('main_node_id', $mainNodeID);
     } else {
         $existingNode->setAttribute('main_node_id', $existingNode->attribute('node_id'));
     }
     $existingNode->store();
     if ($updateSectionID) {
         eZContentOperationCollection::updateSectionID($objectID, $versionNum);
     }
     $db->commit();
     if ($mainNodeID == false) {
         return $existingNode->attribute('node_id');
     }
 }