Forums / Developer / Dynamically creating multiple locations

Dynamically creating multiple locations

Author Message

Andy Caiger

Tuesday 10 July 2007 11:41:15 pm

Hello!

We are trying to import some XML data into eZ Publish and would like the import extension to assign a location for the newly created object depending on the data we are importing. In most cases a single imported object would have multiple locations in the eZ Publish content tree. Can anyone suggest some PHP code to use to:

(a) automatically create a new location in the content tree, depending on the data read in
(b) automatically place a newly imported object in that specific location, depending on the data read in
(c) automatically add a location for an existing object, depending on the data read in.

The import extensions we've seen so far all require the user to enter a location for the new objects, we want them to be located dynamically.

Thanks!

Andy

EAB - Integrated Internet Success
Offices in England, France & China.
http://www.eab.co.uk http://www.eab-china.com http://www.eab-france.com

Fabrice PEREZ

Thursday 12 July 2007 12:24:25 am

Hello Andy,

So, to create new object in the content tree:

/* To find the id of the class without stock it in .ini */
$class = eZContentClass::fetchByIdentifier( 'folder' );
$contentObject = $class->instantiateIn( $languageToUse, $userId, $sectionId, false, EZ_VERSION_STATUS_INTERNAL_DRAFT );
/* You create the temp node*/
$node = eZNodeAssignment::create( array( 
             'contentobject_id' => $contentObject->attribute( 'id' ),
	     'contentobject_version' => $contentObject->attribute( 'current_version' ),
	     'parent_node' => $idOfTheParentNode,
	     'is_main' => 1,
	     'sort_field' => $class->attribute( 'sort_field' ),
	     'sort_order' => $class->attribute( 'sort_order' ) ) );
/* Store your temp node */
$node->store();

/* Create the first version */
$version =& $contentObject->version( 1 );
$version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT );
$version->store();

/* Set the name of the new object */
$contentObject->setName( $theNameOfTheNewObject );

/* Fetch the datamap of the object to modify the attributes */
$contentObjectDataMap =& $contentObject->dataMap();

/* Set the value of the title */
$contentObjectDataMap['title']->setAttribute( 'data_text', $theNameOfTheNewObject );
$contentObjectDataMap['title']->store();
$contentObject->store();

/* After you publish the new object */
include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 
                                                        'object_id' => $contentObject->attribute( 'id' ),
                                                        'version' => $version->attribute( 'version' ) ) );

Here, you have your new object.
After, if you want to add locations to your object :

$insertedNode =& $contentObject->addLocation( $nodeToLink, true );
$insertedNode->setAttribute( 'contentobject_is_published', 1 );
$insertedNode->setAttribute( 'main_node_id', $contentObject->mainNodeID() );
$insertedNode->setAttribute( 'contentobject_version', $contentObject->attribute('current_version') );
$insertedNode->updateSubTreePath();
$insertedNode->sync();

That's all.
Hope this help

Fabrice, Internethic