Jump to content

Register form with auto-create user pages


salepg
 Share

Recommended Posts

This is my 3rd post here. I have to admit that I become highly addictive on PW after painful usage of Joomla which I almost mastered, and really need to thank to Ryan for this drug!

During registration process, I wanna assign values on an automatic way. Well, I'm stucked and need your help.

Successful registration:

  • new user is created,
  • new pageInfo page is created (let say, user profile) using user_info template which contain title, headline and userinfo repeater with fields like firstName, lastName, etc.
  • user will be redirect to his userinfo page after successful registration.

I can grab repeater values (which I typed directly via Admin panel) but I don't know how to add values automatically during registration proccess. I don't even know if I have to use repeater or not... :/ (plan was to track user info changes; this can be redesign later to track only email or username changes but atm let say that I wanna track all changes)

register.php

<?php
    $headline = "Registration form";
    include "./register.inc";
    
    $errorMessage = "";
    if($input->post->register_submit) {
	// process submitted register form
	$first_name = $sanitizer->text($input->post->first_name);
	$last_name = $sanitizer->text($input->post->last_name);
	$display_name = $sanitizer->username($input->post->display_name);
	$email = $sanitizer->email($input->post->email);
	$password = $input->post->password;
	$password_confirmation = $input->post->password_confirmation;
        
	if($password!=$password_confirmation)
            $errorMessage = "<br><p class='alert alert-danger alert-error'><strong>Error!</strong> Register failed, please try again.</p>";
	
        $p = new Page();
	$p->of(false); // sets output formatting to false in order to change field values.
	$p->parent = $pages->get("/users");
	$p->template = "users";
	$p->title = $display_name; // sanitized your value from form and sets to title
	//$p->other_field = $sanitizer->text($input->post->other_field);
	$p->save(); // save the new page
        
        $p = new Page();
	$p->of(false); // sets output formatting to false in order to change field values.
	$p->parent = $pages->get("/users/$display_name");
	$p->template = "user_info";
	$p->title = "UserInfo";
	$p->save(); // save the new page
        

        $pui = $pages->get("/users/$display_name/UserInfo");
        foreach($pui->userInfo as $info) {
            $info->of(false); 
            $info->set("firstName", $first_name);
            $info->set("lastName", $last_name);
            $info->set("displayName", $display_name);
            $info->set("dateCreated", date("j F Y H:i:s"));
            $info->set("dateUpdated", date("j F Y H:i:s"));
            $info->set("email", $email);
            $info->save();
            $info->of(true);
        }

	// now add the user
	$u = new User();
        $u->of(false);
        $u->name = $display_name;
        $u->email = $email;
        $u->pass = $password; 
        $u->save();
        $u->of(true);
    }

?>

That's all for now... :)

Link to comment
Share on other sites

Hi, bit in a hurry but you can check repeaters docs here (bottom of the page you can check how to fill them via API):

https://processwire.com/api/fieldtypes/repeaters/.

Take a look here too: https://processwire.com/api/fieldtypes/repeaters/

A little hint on TrackChanges:

https://processwire.com/talk/topic/4880-questions-about-the-usage-of-trackchanges/

Btw, why (in the code above) are you creating a page with the username as title and then, as a child of if, a page with user infos?

You could just create one page per user, with title and infos in there, in just one place.

Link to comment
Share on other sites

Hi 3fingers,

Thank you for the links, I'll check it now :)

I created user page to be searchable by other users via URL directly. For example, if I wanna check your user page, i just put your nickname instead of mine at the end of URL. For example, if my user page URL is http://domain.com/users/salepg/ i can access your page replacing just my nickname with yours: http://domain.com/users/3fingers/ and check your stats or whatever is on user page.

User Info is just one piece of infos that I wanna assing to an user as I'm working on interested project - creating browser game mainly for learning purpose and secondary to publish it some day eventually. :) So, the idea is to have "subpages" for different stuff related to user as I'm expecting very big DB... but, I'm still open for advice :) I didn't think a lot about whole user picture, am I wrong?

Second solution that I had in my mind is usage od PHP classes that will be like controlers which will handle all user stuff but at this moment I don't have so much courage to jump in.

Link to comment
Share on other sites

Got it, as you may know there is no "good or bad" habit when it comes to organise contents (as long as a pre-production plan is present).

MVC, well...I'm still the "old school" guy too, mainly because I never had to face projects where there was such restriction (or property) to learn.

Link to comment
Share on other sites

Solved :)

Insteed of this code:

        $pui = $pages->get("/users/$display_name/UserInfo");
        foreach($pui->userInfo as $info) {
            $info->of(false); 
            $info->set("firstName", $first_name);
            $info->set("lastName", $last_name);
            $info->set("displayName", $display_name);
            $info->set("dateCreated", date("j F Y H:i:s"));
            $info->set("dateUpdated", date("j F Y H:i:s"));
            $info->set("email", $email);
            $info->save();
            $info->of(true);
        }

I put this:

        $p = $pages->get("/users/$display_name/UserInfo");
        $p->of(false);
        $pui = $p->userInfo->getNew();
        $pui->firstName = $first_name;
        $pui->lastName = $last_name;
        $pui->displayName = $display_name;
        $pui->dateCreated = date("j F Y H:i:s");
        $pui->dateUpdated = date("j F Y H:i:s");
        $pui->email = $email;
        $p->save();

Also, I added this code at the end of IF statement to auto login user after registration:

 if($session->login($display_name, $password)) $session->redirect("./");

I found out that if user put "eee@eee" as a email adress, form is passed successfully but email repeater field remain empty. I need to stop registration process with an error warning (user can change it during a reg. process) or accept that kind of email (so user can change it later). How to handle this?

Cheers! :)

  • 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...