Jump to content
gebeer

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

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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

All right. Will try and get back.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Done...

  • Like 1

Share this post


Link to post
Share on other sites

I can't mark the topic as solved. Maybe because it has been moved?

Share this post


Link to post
Share on other sites

I think it is a forum board specific thing. It seems this forum board does not have that feature enabled.

Share this post


Link to post
Share on other sites

Maybe you can add SOLVED to the thread headline. Thanks.

Share this post


Link to post
Share on other sites

Will do..but you can do it too :-)...if you edited your first post and used the full editor :-)

Share this post


Link to post
Share on other sites

Thanks!

I did try to edit the first post, but not in full editor mode. Now I know  :) 

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By neonwired
      I'm hoping someone has seen this before. There doesn't appear to be an issue with the user info.

       

    • By EyeDentify
      Hello Dear PW Gurus.

      Hope you fair well in these Corona Crisis times.

      Anyhow, i have a problem with a Cryptic error message that shows up when i am trying to Delete images out of a Images field.
      The Error message does say a little but it does not make sense to me why i can´t delete the images because of it.

      Is it some permission issue perhaps?

      I will attach screenshots of it and what i did prior to it.
      I am from sweden so ignore the funny words here and there.

      Hope you all can send me on the right track.
      i am running PW 3.0.139 on this install.

      Step1:

      Step 2: Select the images with the trashcan symbol

       
      Step 3: Cryptic Error message

      I am scratching my head on this one, have not seen this before.

      Thankfull for all help.
      /EyeDentify
    • By prestoav
      Hi there,
      I have a marketplace site that used the older Fredi module for front end editing so advertisers can create and edit their adverts (in the form of pages). After upgrading to v3.0 of Processwire I'd like to change the editing method for users to the new, core front end editing methods. However, it seems that with this method any editor can edit any page (subject to template restrictions for their user role). Using Fredi I checked the user and created user were equal before displaying edit buttons (clunky but it worked).
      Is there any stock way to restrict front end editing to the creator of the page so that my advertisers can only edit their own ads or should I test for user as before?
      Thanks in advance as always 🙂
    • By DooM
      Hey guys!
      I try to achieve a possibility for the client's admin account to change other users privileges. 
      In the CMS I have superadmin account, which is for me. My client got an account with a role user, which is limited administration account. The client also got some other people, who will edit the content with role editor  
      User story would look somehow like this:
      user role can add new roles and users user role can change privileges for all users (except superadmin). Eg. When the user role gives the editor role privilege to edit only articles. The Editor role will be able to edit only them.  
      I know this functionality is possible for superadmin, but I couldn't simulate it on the user role. Is there any easy way to achieve such a feature? Otherwise, I'll have to make a module for this I guess.
      Thanks for any help!
    • By Marcel
      Hey,
      I am about to use the tool Create Users Batcher. We have 450 users. When I tryed it with like 20 test users it worked but it took a while. Now my concerns are that 450 might be to stressful? Does anyone have experience with several hundreds of users? My thoughts are to split it in smaller goups and upload them one group after another.
      best wishes
      marcel
×
×
  • Create New...