Jump to content

Adding a user on page save


onjegolders
 Share

Recommended Posts

I've had a little look at the page cloning module and am trying to figure out a way of doing something similar whereby when a page of type member gets added, a new "user" page is also added, with the same name.

Would this be something that could be done using this tool or would I be able to do something with a page save hook? If so, any tips on where to get started?

I've already achieved this through front end forms and the API but as the rest of the administration in this case runs through the backend I thought it may be more logical to use the admin.

Thank you.

Link to comment
Share on other sites

I think it would be easy to use the page save hook. Did you have a look at the helloworld module?

This is completely untested, but the module could be something on this direction

<?php

class CreateNewUser extends WireData implements Module {

    public static function getModuleInfo() {

        return array(
            'title' => 'Create new user',
            'singular' => true,
            'autoload' => true,
            );
    }

    public function init() {
        $this->pages->addHookAfter('save', $this, 'createTheUser');
    }

    public function createTheUser($event) {
        if($this->page->template == 'custom_user'){
            // create the new user page
        }
    }    
}
Link to comment
Share on other sites

Thanks buddy, I did look at the Hello World module but wasn't sure on which parts I'd need to start changing. Would this take the form of a module which I'd upload to site/modules?

Also, could you create the page using the same API calls, eg:

$p = new Page();

$p->of(false);

$p->save();

Thanks Diogo, as always ;)

Link to comment
Share on other sites

Also, could you create the page using the same API calls

I guess so, methods you can use the same way, only variables have to be changed to the method or the form $this->something... hope I'm not saying something wrong :)

Link to comment
Share on other sites

Thanks Diogo, I tried creating $u = new user(); and soforth but didn't seem to work. I'll have to have another dig around though I can't seem to find an example of creating a new user/page within a module.

$users->add("name")

Add new User with the given name and return it. http://processwire.com/api/cheatsheet/?filter=users

But maybe i've misunderstood the problem.

  • Like 1
Link to comment
Share on other sites

I think he is trying to create two pages, one manually, the other dynamically after the first one. How I understand it is that: Creating a new page memberX under the folder called member should trigger the creation of a page userX under the folder user

Edit: Maybe I am the one confused :) Since users, roles, etc in PW are also pages, the term "user" page can mean a new user? nway, will let onjegolders explain :)

Edited by kongondo
  • Like 1
Link to comment
Share on other sites

Also, could you create the page using the same API calls, eg:

$p = new Page();

$p->of(false);

$p->save();

I don't think you can do it like that. A template must first be assigned before saving the page. See here.

$skyscraper = new Page();
$skyscraper->template = $templates->get("skyscraper"); 
$skyscraper->parent = $pages->get("/cities/atlanta/"); 
$skyscraper->title = "W-Hotel Tower"; 
$skyscraper->height = 400;
$skyscraper->year = 2009;
$skyscraper->body = "This is a nice hotel and it has a helicopter landing pad on top of it.";
$skyscraper->save(); 

Unless, I misunderstand you..

Link to comment
Share on other sites

After saving the page with template "custom_user" - new user will be created with name==page name.

I don't test it. And I wonder what I'm doing at all in the conversation of people with 700+ posts =))

...
public function createTheUser($event) {
        if($this->page->template == 'custom_user'){
             $newusername = $this->page->name;
             $newuser = $users->add($newusername);
             $newuser->pass = 'some password';
             $newuser->save();
        }
    }   
...
  • Like 1
Link to comment
Share on other sites

Thanks both of you!

You're both sort of right though kogondo sums it up when he says I want to dynamically create a user in users when a members is added in the page tree under members.

That snippet was actually just a glimpse, my code was fuller but I'll try k0rn's approach of $users->add();

Will report back. Thanks.

Link to comment
Share on other sites

Seem to have it working and I think the key was to add in $page = $event->arguments[0]; though perhaps someone else could confirm?

public function createTheUser($event) {

        $page = $event->arguments[0]; 

        if($page->template == 'member'){
          
          $newuser = new User(); // add a new user
          $newuser->of(false);
          $newuser->name = $page->name;
          $newuser->pass = "newpass123"; 
          $newuser->save();
          
          $this->message("New user also added $newuser->name."); 
        }

    }    

Edit: Though I'm now getting a duplicate entry error for parent id on the page creation. But surely they don't share a parent.

Solved: I realised this was hooking on page save, so that each time it was saved, it was trying to add a new user, the correct hood is "added".

Link to comment
Share on other sites

Glad you solved it :) I confess I don't understand enough of this to confirm if the code is right, but I'm sure someone will :)

But since we are at it. Why do you want too have the users in two different places? Doesn't seem like a very dry solution to me...

Link to comment
Share on other sites

Glad you solved it :) I confess I don't understand enough of this to confirm if the code is right, but I'm sure someone will :)

But since we are at it. Why do you want too have the users in two different places? Doesn't seem like a very dry solution to me...

I think it's because when I was originally trying to set up member profiles, lots of the discussions I had with Ryan indicated that $users had some limitations (security measures) that would mean searching and displaying would be a bit more work (not sure how much). Basically if I was going to be extending a lot then it may be a better idea to use a page in the tree.

Will have to find the thread but it would be great to actually just use $users, would solve a lot of headaches :)

Link to comment
Share on other sites

But you really need the users? Can't you achieve all you want just with the users as normal pages? Are they going to have access to the backend?

Link to comment
Share on other sites

I agree with Diogo's line of thinking (although am totally ignorant of the whole picture ;)). So, Diogo, you mean...for instance, the member page of a potential user John could be "member page of John" and have the John bit (what would have been the user name) as a field in the member page itself either as title or other? 

Link to comment
Share on other sites

Hmmmm Diogo, giving me ideas and teasing me with the ultimate power of PW! I now have too many choices to make!

For the moment I just added an updateUser function to my member profile where a password field gets transferred to the corresponding user.

public function updateTheUser($event) {

        $page = $event->arguments[0]; 

        if($page->template == 'member' && $page->isChanged("password")){
          
          $the_user = $this->users->get("name=$page->name");
          $the_user->of(false);
          $the_user->pass = $page->password;
          $the_user->save();
          
          $this->message("User $the_user->name also updated."); 
        }

    }    

Think I may leave it there, I'm not sure if somewhere down the line I'll end up being glad to have the $user functionality?

Link to comment
Share on other sites

Seem to have it working and I think the key was to add in $page = $event->arguments[0]; though perhaps someone else could confirm?

Hi onjegolders, and Yes!  :) 

And this one should work too, I believe, but without garanty: $page = $event->object;

(I have seen this and used it by myself, and when hooking something with page, I think this will work.)

Link to comment
Share on other sites

Am thinking again at what Diogo said. Maybe an issue with creating your own users rather than using the default ones would be you would lose all the valuable session stuff?

Knowing the user is logged in and obviously not having to log in each page.


Am sure this can all be re-added to $session but not sure how much would be involved and whether it would be worth it?

Link to comment
Share on other sites

Thanks Horst, any idea what these two things mean?

I see them when looking at other modules but not quite sure when to use which...

I think when hooking into page the event->object always is the page.

$event->arguments is an array, and I would think that it holds the params like the original function you hook into. If you hook into function ___funcname($param0, $param1,$param2), I think you can get in your function $param0 = $event->arguments[0], $param1 = $event->arguments[1], ...

EDIT:

see: HookEvent #18 and following lines

and see the new API-DOCs for Hooks #read_arguments!  (and all above and below) :)

Edited by horst
  • Like 1
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...