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?

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 18 2025 18:38:55
Script start
Timing: Jan 18 2025 18:38:55
Module start 'layout'
Timing: Jan 18 2025 18:38:55
Module start 'content'
Timing: Jan 18 2025 18:38:55
Module end 'content'
Timing: Jan 18 2025 18:38:55
Script end

Main resources:

Total runtime0.0171 sec
Peak memory usage2,048.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0079 588.1406152.6406
Module start 'layout' 0.00790.0042 740.781339.4766
Module start 'content' 0.01210.0032 780.257889.4922
Module end 'content' 0.01530.0018 869.750034.3047
Script end 0.0171  904.0547 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002413.9255140.0002
Check MTime0.00105.9393140.0001
Mysql Total
Database connection0.00084.509710.0008
Mysqli_queries0.005934.133930.0020
Looping result0.00000.116810.0000
Template Total0.00158.810.0015
Template load0.00084.462510.0008
Template processing0.00074.292810.0007
Override
Cache load0.00053.173410.0005
General
dbfile0.00021.143180.0000
String conversion0.00000.058440.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