Snippet: PHP Snippet Add Location to Object / Add Usergroup to User

Table of contents:

Compatibility

This example works with eZ Publish 4.x

About

This function allows adding a location to an extisting object even adds a usergroup to an existing user. The Code was taken and modified from kernel/content/action.php where it says " if ( $module->isCurrentAction( 'AddAssignment' ) )"

Example

/**
  * Adds User to the chosen Usergroup
  * objectID=UserobjectID
  * newNodeID=usergroupNodeID
  * @param integer $objectID
  * @param unknown_type $newNodeID
  */
 function addLocation($objectID,$newNodeID)
 {
  //$selectedNodeIDArray = eZContentBrowse::result( 'AddNodeAssignment' );
  eZDebug::writeDebug("LocationAdd: objectID".$objectID."\nnewNodeID: ".$newNodeID);
   $object = eZContentObject::fetch( $objectID );
   $nodeID = $object->attribute("main_node_id");
   //$version = $object->currentVersion();
  $ini = eZINI::instance();
  $userClassID = $ini->variable( "UserSettings", "UserClassID" );
  // $node = eZContentObjectTreeNode::fetch( $nodeID );
  $selectedNodeIDArray = array($newNodeID);
        $node = eZContentObjectTreeNode::fetch( $selectedNodeIDArray[0] );
  
  if ( !is_array( $selectedNodeIDArray ) )
            $selectedNodeIDArray = array();

        $nodeAssignmentList = eZNodeAssignment::fetchForObject( $objectID, $object->attribute( 'current_version' ), 0, false );
        $assignedNodes = $object->assignedNodes();

        $parentNodeIDArray = array();
        $setMainNode = false;
        $hasMainNode = false;
        foreach ( $assignedNodes as $assignedNode )
        {
            if ( $assignedNode->attribute( 'is_main' ) )
                $hasMainNode = true;

            $append = false;
            foreach ( $nodeAssignmentList as $nodeAssignment )
            {
                if ( $nodeAssignment['parent_node'] == $assignedNode->attribute( 'parent_node_id' ) )
                {
                    $append = true;
                    break;
                }
            }
            if ( $append )
            {
                $parentNodeIDArray[] = $assignedNode->attribute( 'parent_node_id' );
            }
        }
        if ( !$hasMainNode )
            $setMainNode = true;

        $existingNode = eZContentObjectTreeNode::fetch( $nodeID );
        $mainNodeID = $existingNode->attribute( 'main_node_id' );
        $objectName = $object->attribute( 'name' );

        $db = eZDB::instance();
        $db->begin();
        $locationAdded = false;
        $node = eZContentObjectTreeNode::fetch( $nodeID );
        foreach ( $selectedNodeIDArray as $selectedNodeID )
        {
            if ( !in_array( $selectedNodeID, $parentNodeIDArray ) )
            {
                $parentNode = eZContentObjectTreeNode::fetch( $selectedNodeID );
                $parentNodeObject = $parentNode->attribute( 'object' );

                /*$canCreate = ( ( $parentNode->checkAccess( 'create', $class->attribute( 'id' ), $parentNodeObject->attribute( 'contentclass_id' ) ) == 1 ) ||
                               ( $parentNode->canAddLocation() && $node->canRead() ) );*/
                $canCreate=true;

                if ( $canCreate )
                {
                    $insertedNode = $object->addLocation( $selectedNodeID, true );

                    // Now set is as published and fix main_node_id
                    $insertedNode->setAttribute( 'contentobject_is_published', 1 );
                    $insertedNode->setAttribute( 'main_node_id', $node->attribute( 'main_node_id' ) );
                    $insertedNode->setAttribute( 'contentobject_version', $node->attribute( 'contentobject_version' ) );
                    // Make sure the url alias is set updated.
                    $insertedNode->updateSubTreePath();
                    $insertedNode->sync();

                    $locationAdded = true;
                }
            }
        }
        if ( $locationAdded )
        {
            if ( $object->attribute( 'contentclass_id' ) == $userClassID )
            {
                eZUser::cleanupCache();
            }
        }
        $db->commit();

        //include_once( 'kernel/classes/ezcontentcachemanager.php' );
        eZContentCacheManager::clearContentCacheIfNeeded( $objectID );
  
 }