Forums / Developer / Improved code for adding a node in multiple locations

Improved code for adding a node in multiple locations

Author Message

Ammar Ibrahim

Thursday 16 November 2006 5:20:47 am

Using the new custom edit handler (http://serwatka.net/en/blog/ez_publish_3_8_new_custom_edit_handler), I managed to solve a problem while designing our new CMS.

    function publish( $contentObjectID, $contentObjectVersion )
    {
    	
    	//To check whether the node was already assigned or not
    	$isAssigned = false;
    	
    	// fetch object
        $object =& eZContentObject::fetch( $contentObjectID );
        // get content class object
        $contentClass =& $object->attribute('content_class');
        
        
        
        // check if currently published object is Article
        if ( $contentClass->attribute('id') == 16 )
        {
            include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
 
            // prepare new object data
            $parentNodeID = 69;
            
            //check if this Node is already assigned to the destination node
            $assignedNodes = $object->attribute('assigned_nodes');
            foreach ($assignedNodes as $assignedNode){
            	if( $parentNodeID == $assignedNode->ParentNodeID ){
            		$isAssigned = true;
            		break;
            	}
            }
            
            if( !$isAssigned ){
				$nodeAssignment =& eZNodeAssignment::create( 			
								array(
									'contentobject_id' => $object->attribute('id'),
									'contentobject_version' => $contentObjectVersion,
									'parent_node' => $parentNodeID,
									'is_main' => 0
								)
							);

				//print_r($nodeAssignment);
				$nodeAssignment->store();
				$operationResult = eZOperationHandler::execute( 'content', 'publish', 
									array( 
										'object_id' => $object->attribute( 'id' ), 
										'version' => $contentObjectVersion, 
									) 
								); 
            }  
			
        }
 
    }

The important thing to notice, is that I'm checking if the node is already assigned. If this check wasn't placed the the system would go in an infinite loop

Kristof Coomans

Thursday 16 November 2006 5:47:29 am

Hi
You're actually executing the publish operation inside the publish operation of the same object / version. I would watch out doing stuff like this.

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Ammar Ibrahim

Tuesday 21 November 2006 4:30:35 am

Kristof,

Thanks for the reply. What's a better way to do it then?

eZ debug

Timing: Jan 31 2025 00:29:22
Script start
Timing: Jan 31 2025 00:29:22
Module start 'content'
Timing: Jan 31 2025 00:29:22
Module end 'content'
Timing: Jan 31 2025 00:29:22
Script end

Main resources:

Total runtime0.0276 sec
Peak memory usage6,144.0000 KB
Database Queries4

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0065 588.0781370.2734
Module start 'content' 0.00650.0132 958.35161,001.7031
Module end 'content' 0.01980.0078 1,960.054770.7031
Script end 0.0275  2,030.7578 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00227.8813120.0002
Check MTime0.00093.2738120.0001
Mysql Total
Database connection0.00062.344410.0006
Mysqli_queries0.00269.253940.0006
Looping result0.00000.047520.0000
Template Total0.007426.710.0074
Template load0.00093.260910.0009
Template processing0.006523.448810.0065
Override
Cache load0.00062.301210.0006
General
dbfile0.00083.0328100.0001
String conversion0.00000.014730.0000
Note: percentages do not add up to 100% because some accumulators overlap

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.0001 secs