eZ Community » Forums » eZ Publish 5 Platform » Delete children objects where create...
expandshrink

Delete children objects where create data is less than “X” hour - eZ Publish 5

Delete children objects where create data is less than “X” hour - eZ Publish 5

Monday 06 November 2017 9:03:35 am - 1 reply

Hi, I need simple php code for deleting children objects in ez publish 5, i'm trying to create cron job file and delete objects in ez publish where created date is leess then "x" hour , in my case it is 24 hour. if some have had wrote this code before, please share it me too, it would be very helpful for me.
Best regards happy.gif Emoticon

Friday 10 November 2017 1:51:11 am

Hello Nick,

In your case the code in question should be very simple to write.

You simply need to fetch the content objects which were created after the given date formula (aka last 24 hours).

We cover the concept in general here on eZpedia, https://en.ezpedia.org/solution/how_to_fetch_content_based_on_view_parameter_date_range

Here is a brief example:

$deleteIDArray = eZContentObjectTreeNode::subTreeByNodeID( array( 
                'Limit' => 10000,
                'Depth' => 4,
                'ClassFilterType' => 'include',
                'ClassFilterArray' => array( 'blog_post' ),
                'SortBy', array( 'published', false() ),
                'AttributeFilter', array( array( 'published', '>=', $startDateTimestamp ),
                                   array( 'published', '<', $endDateTimestamp ) )
                ), 2 );
// print_r( count( $deleteIDArray ) );
 
foreach ( $deleteIDArray as $deleteItem )
{
    $node = $deleteItem['node'];
    $nodeName = $deleteItem['node_name'];
    if ( $node === null )
    {
        $cli->error( "\nSubtree remove Error!\nCannot find subtree '$nodeName'." );
        continue;
    }
    $nodeID = $node->attribute( 'node_id' );
    $childCount = $deleteItem['child_count'];
    $objectNodeCount = $deleteItem['object_node_count'];
    $cli->output( "Node id: $nodeID" );
    $cli->output( "Node name: $nodeName" );
    $canRemove = $deleteItem['can_remove'];
    if ( !$canRemove )
    {
        $cli->error( "\nSubtree remove Error!\nInsufficient permissions. You do not have permissions to remove the subtree with nodeID: $nodeID\n" );
        continue;
    }
    $cli->output( "Child count: $childCount" );
    $cli->output( "Object node count: $objectNodeCount" );
 
    // Remove subtrees
    eZContentObjectTreeNode::removeSubtrees( array( $nodeID ), $moveToTrash );
    
    // We should make sure that all subitems have been removed.
    $itemInfo = eZContentObjectTreeNode::subtreeRemovalInformation( array( $nodeID ) );
    $itemTotalChildCount = $itemInfo['total_child_count'];
    $itemDeleteList = $itemInfo['delete_list'];
    if ( count( $itemDeleteList ) != 0 or ( $childCount != 0 and $itemTotalChildCount != 0 ) )
        $cli->error( "\nWARNING!\nSome subitems have not been removed.\n" );
    else
        $cli->output( "Successfuly DONE.\n" );
}

This is relatively simple to do then iterate over each object with it's given ID and simply remove it from the system. The above example was cobbled together from this much older and slightly different script: https://github.com/brookinsconsulting/ezecosystem/blob/master/ezpublish_legacy/extension/ezecosystem/bin/php/ezsubtreeremoveallblogs.php

I hope this helps!

Cheers,
Heath

expandshrink

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

36 542 Users on board!

Forums menu

Proudly Developed with from