Moving Objects via PHP & Cron

Moving Objects via PHP & Cron

Friday 11 June 2004 8:31:36 am - 25 replies

Author Message

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

You must be logged in to post messages in this topic!

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.