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

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 31 2025 01:21:10
Script start
Timing: Jan 31 2025 01:21:10
Module start 'layout'
Timing: Jan 31 2025 01:21:10
Module start 'content'
Timing: Jan 31 2025 01:21:10
Module end 'content'
Timing: Jan 31 2025 01:21:10
Script end

Main resources:

Total runtime0.0221 sec
Peak memory usage4,096.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0045 588.2500151.2266
Module start 'layout' 0.00450.0030 739.4766220.7188
Module start 'content' 0.00750.0132 960.1953997.9297
Module end 'content' 0.02080.0013 1,958.125033.9922
Script end 0.0220  1,992.1172 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002511.2767140.0002
Check MTime0.00104.3921140.0001
Mysql Total
Database connection0.00062.739810.0006
Mysqli_queries0.00188.365230.0006
Looping result0.00000.035610.0000
Template Total0.00094.110.0009
Template load0.00073.338110.0007
Template processing0.00020.784010.0002
Override
Cache load0.00052.273310.0005
General
dbfile0.002310.420380.0003
String conversion0.00000.030240.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs