Jump to content

Deleting entire page tree problems


adrian
 Share

Recommended Posts

Hi everyone,

I know that:

$this->pages->delete($parent_page, true);

mostly works, but the problem is when one of the child pages is not deletable - you get an error and the script terminates.

So I thought I'd write a custom approach to delete pages by order of number of path segments, so that children are always deleted before their parents, like this:

// create empty page array
$res = new PageArray();
foreach($pagestodelete as $item) {
    // temporarily add pathsegments property to items
    $item->pathsegments = count(explode('/',$item->path));
    $res->add($item);
}
//sort based on their level in the page hierarchy so we can delete children before their parents
$items = $res->filter("sort=-pathsegments");

foreach($items as $item){
    if($item->deleteable()) {
        $item->delete();
    }
}

This seems like it should work, but the deleteable() check is not excluding pages as it should, so I am getting a can't be deleted error.

Also, even though all the children of a page have been deleted first, I still get the "Can't delete Page $page because it has one or more children" error.

Maybe I am going about this all wrong, but my brain has stopped working now!

Thanks very much to anyone with any ideas :)

Link to comment
Share on other sites

I should remember to just always look at the PW source before asking a question :)

Putting the following right after I delete does the trick. Still it seems clunky to have to do this - is there something I am missing?

$item->status = Page::statusDeleted;

I take that back - I don't think it works at all  :)

EDIT: Can anyone see why the delete method has to throw an exception when a child page is not deleteable. Why not just issue a warning and continue with the next child? 

  • Like 1
Link to comment
Share on other sites

  • 1 month later...
EDIT: Can anyone see why the delete method has to throw an exception when a child page is not deleteable. Why not just issue a warning and continue with the next child? 

I might not be totally understanding the scenario, but if a child can't be deleted then the parent definitely can't be deleted either. Otherwise, you'd end up with orphaned pages in the DB. So there wouldn't be much purpose in issuing a warning or doing anything other than halting immediately. Something that would result in orphaned pages in the DB should be a fatal error, at least in my opinion. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...