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

eZ debug

Timing: Jan 31 2025 01:27:54
Script start
Timing: Jan 31 2025 01:27:54
Module start 'content'
Timing: Jan 31 2025 01:27:54
Module end 'content'
Timing: Jan 31 2025 01:27:54
Script end

Main resources:

Total runtime0.2023 sec
Peak memory usage8,192.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0094 588.0469370.2891
Module start 'content' 0.00940.0129 958.33591,001.6719
Module end 'content' 0.02230.1800 1,960.00783,890.4922
Script end 0.2022  5,850.5000 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00502.4955200.0003
Check MTime0.00150.7638200.0001
Mysql Total
Database connection0.00070.323710.0007
Mysqli_queries0.105952.34141410.0008
Looping result0.00160.78681390.0000
Template Total0.179588.710.1795
Template load0.00090.443810.0009
Template processing0.178688.264510.1786
Override
Cache load0.00060.314410.0006
Sytem overhead
Fetch class attribute can translate value0.00221.082010.0022
XML
Image XML parsing0.00030.128010.0003
General
dbfile0.00522.5781200.0003
String conversion0.00000.008030.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

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

Time used to render debug report: 0.0002 secs