Wednesday 17 May 2006 2:09:33 am
Thank's for advice ! Endly, here is a working example, this a CLI script :
<?php
/*
* Created on 16 mai 2006
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
include_once( 'lib/ezutils/classes/ezcli.php' ); include_once( 'kernel/classes/ezscript.php' );
include_once( "kernel/classes/ezpersistentobject.php" );
include_once( "kernel/classes/ezcontentclass.php" );
include_once( "kernel/classes/ezcontentobjecttreenode.php" );
include_once( "kernel/classes/eznodeassignment.php" );
include_once( "kernel/classes/datatypes/ezuser/ezuser.php" ); include_once( "lib/ezutils/classes/ezoperationhandler.php");
$cli =& eZCLI::instance(); $cli->output("Starting inject article script client");
$script =& eZScript::instance( array( 'debug-message' => 'Injection Script',
'use-session' => false,
'use-modules' => true,
'use-extensions' => true )
);
$script->startup();
$script->setUseDebugOutput( true );
$script->setAllowedDebugLevels( EZ_LEVEL_DEBUG );
$script->setUseDebugAccumulators( true ); $script->setUseDebugTimingPoints( true );
// $sys =& eZSys::instance(); $script->initialize(); $cli->output('Starting content injection...'); // $ini =& eZINI::instance();
$classRef = "article";
$parentNodeID = 2;
// FETCH INJECTING USER
// $injectUser = eZUser::fetchByName('admin');
$injectUser = eZUser::fetch( 14 );
if ( !$injectUser )
{
$cli->output("Admin user not found");
$script->shutdown(1, "Unable to fetch user");
}
$userID = $injectUser->attribute('contentobject_id');
$cli->output("Have user : " . $userID);
// FETCH CONTENT CLASS
$noteClass = eZContentClass::fetchByIdentifier( $classRef );
$cli->output( "Instanciating class : " . $noteClass->attribute('name') );
// GET PARENT NODE AND SECTION
$parentTreeNode = eZContentObjectTreeNode::fetch( $parentNodeID );
$cli->output( "Parent node path : " . $parentTreeNode->attribute('path_string') ); $cli->output( "Node node id : " . $parentTreeNode->attribute('path_identification_string') );
$parentContentObject = $parentTreeNode->attribute('object');
$sectionID = $parentContentObject->attribute('section_id');
$cli->output( "Inserting in section ID " . $sectionID );
// INSTANCIATION
$contentObject = & $noteClass->instantiate( $userID, $sectionID );
// ASSIGNATION
$assignProps = array(
'contentobject_id' => $contentObject->attribute('id'),
'contentobject_version' => $contentObject->attribute('current_version'),
'parent_node' => $parentTreeNode->attribute('node_id'),
'is_main' => 1
);
$cli->output("Assignement :" . print_r($assignProps) );
$nodeAssignment = & eZNodeAssignment::create( $assignProps );
$nodeAssignment->store();
// VERSIONNING
$version =& $contentObject->version( 1 );
$cli->output( "Version : " . $version );
$version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT ); $version->store();
// STORE EMPTY OBJECT
$contentObject->setAttribute('name', 'artKLJLKJ');
$contentObject->store();
// SET ATTRIBUTES
$attribs = & $contentObject->contentObjectAttributes();
foreach( $attribs as $anAttr )
{
$attID = $anAttr->attribute('contentclass_attribute_identifier'); $cli->output("Setting attribute : " . $attID );
switch( $attID )
{
case 'title' :
$anAttr->setAttribute('data_text', "Article Injection Exemple");
break;
case 'short_title' :
$anAttr->setAttribute('data_text', "NEW QUICK ARTICLE");
break;
case 'author' :
$anAttr->setAttribute('data_text', "Oscar Wilde");
break;
case 'intro' :
$anAttr->setAttribute('data_text', "Example of article injected by a client script into EZ...");
break;
case 'body' :
$anAttr->setAttribute('data_text', "The process has bean <b>automatized</b>...");
break;
}
$anAttr->store();
}
$contentObject->setAttribute('status', EZ_VERSION_STATUS_DRAFT);
$contentObject->store();
$pubProps = array(
'object_id' => $contentObject->attribute('id'),
'version' => 1 );
$operationResult = eZOperationHandler::execute( 'content', 'publish', $pubProps );
$cli->output("Publish result : " . print_r( $operationResult ));
if ( !isset( $operationResult['status'] ) || $operationResult['status'] != EZ_MODULE_OPERATION_CONTINUE )
{
if ( isset( $operationResult['result'] ) && isset( $operationResult['result']['content'] ) )
$failReason = $operationResult['result']['content'];
else
$failReason = "unknown error";
$cli->error( "Publishing failed: $failReason" );
unset( $failReason );
}
$script->shutdown();
$cli->output("Done."); ?>
|