Forums / Developer / Moving Objects via PHP & Cron

Moving Objects via PHP & Cron

Author Message

Alex Jones

Friday 11 June 2004 8:31:36 am

Okay, I've hit a brick wall on this and beyond frustration. I'm sure there is a simple script out there, but I am unable to create it. All I need to do is a script that will fetch all items in a folder, check the date held in a text field and archive any items whose date is more than 60 days past. Now, I have been able to create this in pure PHP, manipulating the database directly, but the system doesn't see the changes, so the item is not 'moved'. I have tried multiple variations of a script, working off of other contributions, but nothing works. So, I am putting a call out there, can anyone please provide a basic foundation script that will:
1. Fetch all items in a given folder
2. Return the value of a specific attribute (text field) for each item
3. Change the location of an item
4. Store the changes made to the item

I have the code that will calculate the time difference and determine whether or not an item should be changed. I just need the eZ wrapper around it.

Any help would be truly appreciated!

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Hans Melis

Friday 11 June 2004 8:59:03 am

Hi Alex,

I have such a framework script. :) It's pretty basic but it should do what you want to do. How can I send it to you?

--
Hans

Hans
http://blog.hansmelis.be

Paul Borgermans

Friday 11 June 2004 9:02:33 am

pubsvn/community or contributions?

;-)

-paul

eZ Publish, eZ Find, Solr expert consulting and training
http://twitter.com/paulborgermans

Alex Jones

Friday 11 June 2004 9:26:51 am

Hans, that would be extremely helpful, thank you! I can be reached at 'alexj' at 'agrussell.com'.

Thank you so much!

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Hans Melis

Friday 11 June 2004 9:35:05 am

At least Paul is still thinking straight on a Friday ;)

I've sent it to you, Alex, and I've also added it to pubsvn. For those interested: http://pubsvn.ez.no/community/trunk/cronjobs/move_framework.php

--
Hans

Hans
http://blog.hansmelis.be

Alex Jones

Friday 11 June 2004 9:42:51 am

That's great Hans. :) I will try it out right after lunch.

Again, thank you for such a speedy response.

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Paul Borgermans

Friday 11 June 2004 10:19:18 am

And at least we have one of the incarnations of the "unpublish" feature requested by so many, provided you add a little code ;-)

BTW, with this conjob modified a bit and notifications setup properly, you can implement much more "workflow-like" tasks

Hans: didn't know it is friday already, should go home .. I'll pickup the book from y desk now :-)

Alex: bon appetit

-paul

eZ Publish, eZ Find, Solr expert consulting and training
http://twitter.com/paulborgermans

Alex Jones

Friday 11 June 2004 11:46:29 am

I've started working with the framework Hans was kind enough to provide, but I am running into a problem. I went through and defined the node IDs as noted in the directions in the file and added a couple of <i>cli</i> calls to monitor via the command line. But the script doesn't appear to be executing properly. Specifically, the foreach loop isn't kicked off. I also commented out the move command as I do not have it all set up as it should be. Below is the script as I currently have it, can anyone point out what is wrong?

include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
include_once( 'kernel/classes/ezcontentobject.php' );
include_once( 'lib/ezutils/classes/ezcli.php' );

$cli =& eZCLI::instance();

$parentNodeID = '186';
$newParentNodeID = '650';
$attribIdentifier = 'price';

// This is an array that contains all fetch parameters (like in templates), except the parent's nodeid.
// Leave empty for default fetch
$params = array();

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

$inc = 0;
foreach( $childNodes as $child )
{
  $inc++;
  $cli->output( '-- In foreach --');
  $childObj = $child->attribute( 'object' );
  $objData = $childObj->dataMap();

  $value = $objData[$attribIdentifier]->content();
	
  $doMove = false;

  // Insert code to check $value, set $doMove to true if this objects needs to be moved
  if( $doMove === true )
    {
    //$cli->output( '$child->move( ' . $newParentNodeID . ' );');
    }
  $cli->output( $inc );
}

I've double-checked the supplied Node IDs, so I know they are right. I'm sure I am missing something obvious, but I'm willing to kick myself to get through this. ;)

Thanks again,

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Hans Melis

Friday 11 June 2004 12:01:05 pm

Hmmm...

Try this:
Replace

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

With

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

The script's quite old, so the API might have changed since then.

--
Hans

Hans
http://blog.hansmelis.be

Alex Jones

Friday 11 June 2004 12:05:19 pm

Thanks for the speedy reply. But that still doesn't kick off the <i>foreach</i> loop. :( Any other ideas perchance?

FYI, I'm trying this on a 3.3-3 install.

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Hans Melis

Friday 11 June 2004 12:23:35 pm

Does the anonymous user have access to the objects you're trying to fetch?

EDIT: Scratch that. I'm going throught the ezp api, and I've found something weird... Stay tuned

Hans
http://blog.hansmelis.be

Alex Jones

Friday 11 June 2004 12:28:47 pm

They have read access to that folder and the class of items I need to change.

Alex

EDIT: Oh, alright. Thanks :)

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Hans Melis

Friday 11 June 2004 12:48:57 pm

It's working fine for me now, except that the content cache wasn't cleared correctly but that's on 3.4 so you should be fine I think.

What I just noticed is that you have both node IDs declared as strings, you should remove the quotes because they're supposed to be integers. I can't find anything else...

--
Hans

Hans
http://blog.hansmelis.be

Alex Jones

Friday 11 June 2004 12:53:16 pm

Thanks Hans, I appreciate your help. I have set the two Node IDs to integers once more and I cleared the caches, but neither seemed to make a difference. :( This seems like it should be pretty straight-forward, which makes it all the more confusing. Perhaps I should take a break from it and work on something else for a bit.

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Alex Jones

Friday 11 June 2004 12:57:06 pm

Actually, is it possible to store changes fro ma script that is totally outside of eZ publish? As I mentioned above, I have all of the code I need to track down the items that need to be changed, I just can't find the right spot to overwrite the old Node ID with the new one. Do I need to hit several places in the database? I tried to set the <i>parent_node_id</i> in <i>ezcontentobject_tree</i> but apparently that isn't enough...

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Hans Melis

Friday 11 June 2004 1:05:43 pm

Well, since you're trying to update ezcontentobject_tree I'm assuming you have the node ID of each node you want to move. In that case, you can do

$node = eZContentObjectTreeNode::fetch( <node_id> );

You also know the node ID of the destination, so you can do

$node->move( <new_node_id> );

That should be enough to move something.

--
Hans

Hans
http://blog.hansmelis.be

Alex Jones

Friday 11 June 2004 1:28:13 pm

A valid point Hans, thank you. It has been a long day. ;)

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Alex Jones

Tuesday 15 June 2004 6:53:35 pm

Well I am obviously doing something really wrong as I still can't get this working. Grrrr. Below is my current version of the script. Can anyone see what I may be doing wrong?

<?php

include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
include_once( 'kernel/classes/ezcontentobject.php' );
include_once( 'lib/ezutils/classes/ezcli.php' );
include_once( 'kernel/classes/eznodeassignment.php' );

$parentNodeID = 186;
$newParentNodeID = 650;
// Set up the command line interface.
//
// This allows eZ to set up the PHP session correctly.

$cli =& eZCLI::instance();

$link = mysql_connect('localhost', 'ezpublish', '4es6zH3Ay');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
mysql_select_db("ez_cuttingedge") or die("Could not select database");
$query = "SELECT * FROM `ezcontentobject_attribute` WHERE `contentclassattribute_id` = 215 AND data_text <> ''";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
echo '[ ' . $query . ' ]' . "\n";
$rollover = '60';
$today = date("Y m d");

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  $datestring1 = $line["data_text"];
  $date1 = explode(" ",$datestring1);
  $date2 = explode(" ",$today);
  sprintf($datestring1, "%s%s%s", $date1[0], $date1[1], $date1[2]);
  sprintf($today, "%s%s%s", $date2[0], $date2[2], $date2[2]);
  $dateDiff = mktime(11,0,0,$date2[1],$date2[2],$date2[0]) - mktime(12,0,0,$date1[1],$date1[2],$date1[0]);
  $difference = floor($dateDiff/60/60/24);

  if ($rollover > $difference){
    
  } else {
    $query2 = 'SELECT * FROM ezcontentobject_tree WHERE contentobject_id = ' . $line["contentobject_id"];
    $result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
    while ($line2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
      $movingNode = $line2["node_id"];
      $node =& eZContentObjectTreeNode::fetch($movingNode);
      $node->move($newParentNodeID);
    }
  }
}

/* Free resultset */
mysql_free_result($result);
mysql_close($link);
?>

I keep getting the error <i>
PHP Fatal error: Call to a member function on a non-object in /x/local/www/cuttingedge.data/ez_upgraded/auto_archive3.php on line 56</i> when I try to run the script. The line in question is <i>$node->move($newParentNodeID);</i>.

Any thoughts?

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Paul Forsyth

Wednesday 16 June 2004 12:43:08 am

Looking at the source for the move function:

http://pubsvn.ez.no/doxygen/ezcontentobjecttreenode_8php-source.html#l02503

The error may come from the functions second parameter. Maybe the node isn't quite setup correctly.

I would suggest having a look at ezcontnentobjecttreenode.php and see what is happening at the start, maybe by a few debug lines.

paul

--
http://www.visionwt.com

Alex Jones

Wednesday 16 June 2004 7:00:15 am

Hrrrm, I think you are on to something as nothing is output to screen when I echo <i>$node</i> after the line

$node = eZContentObjectTreeNode::fetch($movingNode);

. That line doesn't throw an error though, and the variable <i>$movingNode</i> shows the right value. Note: I changed <i>$node =&</i> to <i>$node =</i> (removing the &) to be consistent with the code in ezcontnentobjecttreenode.php. The fetch query appears to be executing properly. I will add some debugs to the move function shortly, once a content import is finished.

Thanks again!

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>