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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By Clarity
      Hello everyone!
      I'm new here and I want to set an user avatar to my profile. However, I don't see any option in my personal cabinet that can do it. Could you please tell me if there are there some requirements for user for setting an avatar or I'm missing something?
    • By theoretic
      Hi there! And thanks for Processwire!
      I've got an interesting case concerning access to current user page. It appears that PW somehow limits access to the frontend page of current user.
      I'm speaking about a specific PW configuration. We have two kind of users: 'regular' users with native user template and member users with specific member template and specific members parent page (by the way, it's so cool that PW allows to use custom user templates and custom parent for certain user pages!). So a member with name Joe has a page with member template and url like /members/joe .
      The members template has some access limitations: only member users can see pages with member template. It works like a charm in most situations. For example, user Bill (who has member template and is logged in) can browse a page with url /members/ann which also is a member page with member template.
      And now, meet the glitch! The above-mentioned Bill cannot get to his own page /members/bill ! PW generates 404 page instead.
      I see no reason for this behavior. From my point of view any member should have access to any member page in this situation. What am i doing wrong? Any advice is welcome!
    • By VeiJari
      Hello,
      I'm trying to create a page via api and populate values to it. I can populate everything except user pages to a page reference array.
      Code: 
      $dataUsers = $data->project->users; foreach($dataUsers as $dataUser) { $newProject->projectUsers->add(wire()->pages->find('template=user, id=' . $dataUser->id)); } I'm receiving my data via JSON.
      Is there something I'm missing?
      Thanks for help
    • By louisstephens
      So I went to revisit a project that I put on hold and was met with the following error when trying to trash a page "Operator '&' is not supported for 'status' in ../wire/core/PageFinder.php: 2211
      else if(!$database->isOperator($operator)) { throw new PageFinderSyntaxException("Operator '$operator' is not supported for '$field'."); } I have searched through my templates just to make sure I didn't do anything screwy, but I have no clue why this error is being generated. I can individually delete an item from the trash, but I cant empty the entire trash. 
    • By neonwired
      I'm hoping someone has seen this before. There doesn't appear to be an issue with the user info.

       

×
×
  • Create New...