Moving Objects via PHP & Cron

Author Message

Timmothy Green

Tuesday 26 April 2005 1:47:24 pm

This thread has been inactive for a while. I'm curious whether any progress was made. I'm working on a similar project and am stuck basically where this thread leaves off.

50: $node = eZContentObjectTreeNode::fetch($node_id);
51: $node->move($newParentNodeID);

Returns this error:

Fatal error: Call to a member function on a non-object in C:\ezpublish-3.5.1\move_framework.php on line 51

A var_dump of $node returns NULL, but I know the value of $node_id is valid.

Automation Technologies, Inc.
http://www.ati4it.com

Pål J Didriksen

Sunday 03 July 2005 2:12:14 am

Timmothy:
I have used this code, wich works well to fetch the node, provided you know the object id. (In my case, the object id of a user account, $userID).

$object = eZContentObject::fetch($userID);
$node = eZContentObjectTreeNode::fetch($object->attribute('main_node_id'));

However, trying to move the node does not completely succeed. I am using this call:

$node->move($to_node_id);

Looking up the ezcontentobject_tree table, I find that parent_node_id and path_string have been correctly changed to the new location. The path_identification_string, however, refers to the old location. Consequently, the object is left in an unfinished state. When I open the object for editing, and then saves it, it gets cleaned up, but falls back to the old location.

I have searched the forum, and seen "$node->move($to_node_id);" several places, but to me it doesn't seem to work properly.

Any experience with this?

Ralph Ekekihl

Monday 04 July 2005 1:45:08 am

Hi,

I have also struggled alot with trying to get the $node->move($to_node_id) to work..

I managed to get it to move the object to a new node, but after moving a node with it, the moved node changes node id everytime i republish it.

Searched on the forums, found someone else with the same problem, but no solution.. myabe a bug in ezpublish?

The code I use to move my objects to a new node is the following:

		$object =& eZContentObject::fetch( $objectID );
		$nodeID =& $object->attribute('main_node_id');
		$version =& $object->currentVersion();
		$versionNumber =& $version->attribute('version');
				
		$nodeAssignments = $version->nodeAssignments();
		
		eZContentObjectTreeNode::remove($nodeID);
		$version->removeAssignment($newNodeID);
		$version->assignToNode($newNodeID, 1, 0);
		$version->store();
				
		include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
		$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $objectID, 'version' => $versionNumber ) );

Where ObjectID is the object to be moved and newNodeID is the new Node that it is going to be assigned to..

//Ralph

Contactivity B.V.
http://www.contactivity.com

Pål J Didriksen

Monday 04 July 2005 7:14:51 am

Ralph,

it seems we have the same problem... I tried different variants of code, including something very similar to your code, but whichever solution, the object ended up with a node assignment as some kind of a combination of the old and the new assignment.

An idea could be to track down what the kernel does when you manually move an object by pushing the "move"-button. I want to do the exact same thing in code. Haven't had the time to look further into it after I temporarly gave up the $node->move function last night.

Pål J

Pål J Didriksen

Monday 11 July 2005 7:46:30 am

At last I managed to get it working... :)

My code now looks like this, and from the first couple of tests, it seems to be working:

$to_node_id=237;

// Fetch subtree of objects that should be moved
$params = array();
$childNodes =& eZContentObjectTreeNode::subTree( $params, $parentNodeID );

// Loop through each object
    foreach( $childNodes as $child )
    {
        $thisNode = $child -> MainNodeID;
        $userID = $child -> ContentObjectID;

       // Fetch the user object
       $object =& eZContentObject::fetch( $userID );

        // Fetch the current version
        $version =& eZContentObjectVersion::fetchVersion(  $object->attribute('current_version'), $userID );

        // Unpublish this version
        $version->unpublish();

        // Add new assignment, and make it main
        $version->assignToNode($to_node_id, 1);

        // Remove old assignment
        eZContentObjectTreeNode::remove( $thisNode );

        // Re-publish version again
        $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $userID,
                                                        'version' => $object->attribute('current_version') ) );
}

Found the tip to unpublish the object before changing the assignment here:
http://ez.no/community/forum/developer/add_location_to_user_with_template_programming_solved

I also changed the code for removing the old assignment to <i> eZContentObjectTreeNode::remove( $thisNode );</i>

Now I'm happy again! ;)

Betsy Gamrat

Sunday 09 July 2006 6:31:09 am

Hi,

Thank you to all the previous posters. I am submitting my implementation of the move code.

I used this thread and this one: http://ez.no/community/forum/developer/moving_an_object_through_php

I also used <b>create.php</b> - <i>Copyright (C) 1999-2004 Björn Dieding, xrow GbR Germany. All rights reserved.</i>

include_once( 'kernel/classes/ezcontentobject.php' );
include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
include_once( 'kernel/classes/ezcontentobjecttreenodeoperations.php' );

include_once( "lib/ezutils/classes/ezextension.php" );
include_once( "lib/ezutils/classes/ezmodule.php" );
include_once( 'lib/ezutils/classes/ezcli.php' );
include_once( 'kernel/classes/ezscript.php' );

$script =& eZScript::instance( array( 'debug-message' => true,
                                      'use-session' => true,
                                      'use-modules' => true,
                                      'use-extensions' => true ) );

$script->startup();
$script->initialize();
include_once("lib/ezutils/classes/ezcli.php");
$cli =& eZCLI::instance();
$cli->setUseStyles( true ); // enable colors

$params=array();
$parentNodeID=100; // For example
$newfolderNodeID=200; 

$childNodes =& eZContentObjectTreeNode::subTree($params, $parentNodeID );

if (count($childNodes) === 0)
{
      echo "No data returned\n"; die();
}
foreach( $childNodes as $child )
{
      $childObj = $child->attribute( 'object' );
      $nodeID=$child->NodeID;
      $objData = $childObj->dataMap();
      $name= $objData['name']->content();  

      $doMove = ($name === "target_value");  // For example
      // Insert code to check $name, set $doMove to true if this objects needs to be moved
      if ($doMove)
      {
            if (!eZContentObjectTreeNodeOperations::move( $nodeID, $newfolderNodeID )) 
              echo "Move failed\n";
      }
}

$cli->output("Done",true);
$script->shutdown();

I stripped out my application specific code, but this code should work, or be close enough.

I used <b>var_dump($variable);</b> to help me "see" the data - and it was very valuable.

I also created a very simple class, with only enough attributes to test the code - and ran it in a development area (different server) to reduce the risk of corrupting live data.

Good luck,

Betsy

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 22:36:34
Script start
Timing: Jan 18 2025 22:36:34
Module start 'layout'
Timing: Jan 18 2025 22:36:34
Module start 'content'
Timing: Jan 18 2025 22:36:35
Module end 'content'
Timing: Jan 18 2025 22:36:35
Script end

Main resources:

Total runtime1.0572 sec
Peak memory usage4,096.0000 KB
Database Queries69

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0055 589.5703152.6250
Module start 'layout' 0.00550.0031 742.195339.9063
Module start 'content' 0.00851.0468 782.1016667.5313
Module end 'content' 1.05530.0018 1,449.632822.2969
Script end 1.0571  1,471.9297 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00330.3162160.0002
Check MTime0.00130.1227160.0001
Mysql Total
Database connection0.00120.114510.0012
Mysqli_queries0.978392.5332690.0142
Looping result0.00070.0678670.0000
Template Total1.025997.020.5129
Template load0.00200.193520.0010
Template processing1.023896.841620.5119
Template load and register function0.00010.009610.0001
states
state_id_array0.00090.083110.0009
state_identifier_array0.00110.100320.0005
Override
Cache load0.00180.1733560.0000
Sytem overhead
Fetch class attribute can translate value0.00080.078240.0002
Fetch class attribute name0.00150.1418100.0001
XML
Image XML parsing0.00270.251940.0007
class_abstraction
Instantiating content class attribute0.00000.0022120.0000
General
dbfile0.00150.1436340.0000
String conversion0.00000.000740.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
6content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
6content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
13content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
7content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 36
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs