Jump to content

Trash pages after user has been deleted - process module [SOLVED]


gebeer
 Share

Recommended Posts

Hi all,

I'm struggling in putting together a process module.

I want to trash all pages that belong to a specific user after the user has been deleted.

I found one related thread by onjegolders where he is doing things the other way around.

In my case I need a process module that hooks after user delete. And I would like to use the wire/modules/Process/ProcessPageTrash.module.

It is my first time creating a pw process module. I have read through the basics of creating modules including the wiki article and looked at the code of ProcessPageTrash.module. But I'm having a hard time putting things together.

This is what I've got so far:

class ProcessCleanupUserPages extends Process {

  public static function getModuleInfo() {

      return array(
          'title' => 'Cleanup User Data',
          'version' => 0.1,
          'summary' => 'Deletes all pages that where created by a user after user has been deleted',
          'singular' => true,
          'permanent' => false,
          'permission' => 'user-admin',
          'requires' => 'ProcessPageTrash',
          'autoload' => "process=ProcessPageList",  //  What to add here - autoload only on user list page
          );
  }

  public function init() {
      $this->users->addHookAfter('delete', $this, '___execute()'); //  is this correct?
  }

  public function ___execute() {

    //get user ID
    $userID = $this->id or $event->id //  How to get the user ID?

    //get all pages that were created by the deleted user
    $userPages = $pages->get("created_users_id={$userID}");

    //  syntax for trash pages - call module ProcessPageTrash on $userPages
    }

    
    protected function render() { //  print out message to admin after pages are successfully trashed

    }

}
?>

I added comments with "???" where I don't know exactly what to do.

Any help would be much appreciated. Thank you.

Link to comment
Share on other sites

Just a small detail but for "behind" the scenes hooks and such thing you don't need a Process module. Those are for admin pages that have a process added (the module). The ___execute is for returning markup that should show on the admin page with this process.

You would create a simple WireData module like the HelloWorld.module.

And you don't need that ProcessPageTrash module, that is also an admin page to delete trashed pages.

So you need a WireData module that autoloads, Process module shouldn't ever be autoload.

autoload on a selector "process=ProcessPageList" doesn't work, and if at all I think it's not suitable cause that's a module working mostly with ajax. Not 100% sure without trying.

autoload=>true should be sufficient. You check anyway what you want to delete and already add the hook to users.

$this->users->addHookAfter('delete', $this, '___execute()'); //  is this correct

This is a hook for when a page was deleted, but afterwards, so you never know what page was deleted.

May it would be better to use addHookBefore("delete", $this, "hookDeleteUsers");

Then not "___execute()" but a custom function, and you use the $event to catch the page being deleted

public function hookDeleteUsers($event){
    $userpage = $event->arguments("page");
    if($userpage->id){
         // do some stuff example
         $userPage = $pages->get("created_users_id={$userpage->id}");
         $this->pages->trash($userPage);
    }
}

http://cheatsheet.processwire.com/?filter=trash

  • Like 3
Link to comment
Share on other sites

Thanks again Soma, for your help.

Your explanations again are really clear to understand.

Based on what I know now, I will try and put the module together.

@admins: I should have posted this in the modules forum. You're welcome to move it over there.

Link to comment
Share on other sites

Based on Somas input, my CleanupUserPages.module now contains:

<?php

class CleanupUserPages extends WireData implements Module {

  public static function getModuleInfo() {

      return array(
          'title' => 'Cleanup User Data',
          'version' => 0.1,
          'summary' => 'Trashes all pages that where created by a user after user has been deleted',
          'singular' => true,
          'permanent' => false, 
          'permission' => 'user-admin',
          'autoload' => true,  
          );
  }

  public function init() {
      $this->users->addHookBefore("delete", $this, "hookDeleteUsers");
  }

  public function hookDeleteUsers($event){
      $userpage = $event->arguments("page");
      if($userpage->id){
           $userPages = $pages->get("created_users_id={$userpage->id}");
           $this->pages->trash($userPages);
           $this->message("pages deleted");
      }
  }
}
?>

I have installed the module.

But when I delete a user, nothing happens.

To test if the module gets called, I added

var_dump($userpages); return;

right after

$userpage = $event->arguments("page");

But still nothing happens. It seems like the module code is not being executed at all.

Does $this->users->addHookBefore make sense or do I need to define the hook in some other way?

Link to comment
Share on other sites

I often just add an exit("hallo"); inside the hook to test if it gets called at all. If return it won't really be visible most of the times.

Also then $pages would be wire("pages") or $this->pages ... and get(selector) will return 1 page not multiple, if need mutliple you need a find(selector)

$userPage = wire("pages")->get("created_users_id={$userpage->id}");
wire("pages")->trash($userPage);

then pages->trash($page) is only for single pages so you would need a loop if multiple pages.

  • Like 1
Link to comment
Share on other sites

OK,

my hookDeleteUsers function now reads

  public function hookDeleteUsers($event){
    exit("hallo");
      $userpage = $event->arguments("page");
      if($userpage->id){
        $userPages = wire("pages")->find("created_users_id={$userpage->id}");
        foreach ($userPages as $userPage) {
          wire("pages")->trash($userPage);
        }
        $this->message("All pages of user {$userpage->name} deleted");
      }
  }

But I don't get a "hallo" on delete and all user's pages are still there. So I guess the hook doesn't get called.

Link to comment
Share on other sites

Ah I think that $this->users isn't working here, you have to use pages

$this->pages->addHookBefore("delete", $this, "hookDeleteUsers");

And then check in the hook if you're dealing with a user page.

  • Like 1
Link to comment
Share on other sites

@kongondo

Thanks for moving it.

Unfortunately I was just about to post at the same time and things got messed up so I'm overwriting this post.

Finally my hook module is working. ^-^ Big thanks to Soma again!

Here's the final code:

<?php

class CleanupUserPages extends WireData implements Module {

  public static function getModuleInfo() {

      return array(
          'title' => 'Cleanup User Data',
          'version' => 0.1,
          'summary' => 'Trashes all pages that where created by a user after user has been deleted',
          'singular' => true,
          'permanent' => false, 
          'permission' => 'user-admin',
          'autoload' => true,  
          );
  }

  public function init() {
      $this->pages->addHookBefore("delete", $this, "hookDeleteUsers");
  }

  public function hookDeleteUsers($event){

      $page = $event->arguments("page");
      
      if ($page->id && $page->template->name === "user") {
          
          $userPages = wire("pages")->find("created_users_id={$page->id}");

          foreach ($userPages as $userPage) {
            wire("pages")->trash($userPage);
          }

          $this->message("All pages of user {$page->name} deleted");
        }
  }
}
?>

I first had problems accessing the page template. The $page object returned by $event->arguments("page") works different from the standard $page object in templates.

It took me a while to find that I have to use $page->template->name === "user".

exit(var_dump($page)) was helpful.

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

×
×
  • Create New...