Forums / Developer / Moving Objects via PHP & Cron

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

eZ debug

Timing: Jan 19 2025 09:46:40
Script start
Timing: Jan 19 2025 09:46:40
Module start 'content'
Timing: Jan 19 2025 09:46:40
Module end 'content'
Timing: Jan 19 2025 09:46:40
Script end

Main resources:

Total runtime0.1871 sec
Peak memory usage2,048.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0061 589.4766180.8359
Module start 'content' 0.00610.0048 770.3125106.3906
Module end 'content' 0.01090.1760 876.7031535.0234
Script end 0.1870  1,411.7266 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00371.9801200.0002
Check MTime0.00140.7451200.0001
Mysql Total
Database connection0.00080.413110.0008
Mysqli_queries0.131170.08401410.0009
Looping result0.00150.80041390.0000
Template Total0.175593.810.1755
Template load0.00090.466710.0009
Template processing0.174693.327310.1746
Override
Cache load0.00060.322310.0006
Sytem overhead
Fetch class attribute can translate value0.00090.460710.0009
XML
Image XML parsing0.00030.157110.0003
General
dbfile0.00552.9261200.0003
String conversion0.00000.003130.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

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