Jump to content

Adding User information.


n00dles101
 Share

Recommended Posts

Are are there plans in the roadmap to allow the user DB fields to be extended just like templates.

I need to add say email/contact info to any users I've created.

I can get round it at the moment, by creating fields for the information and creating pages for each

of the users.

Just converted my brothers photography site over... www.tonyswanphotography.com

(same template as your James Andrew Photo one , great minds !! )

Was running on Getsimple CMS but the Image upload/User Admin is a lot easier to do in PW.

Thanks for the help..

keep up the good work...

Mike...

Link to comment
Share on other sites

Hi Mike,

Good idea. There are definitely plans to do that. Right now you should be able to add fields as you see fit to any $user object, and it should save them in the 'data' field in the users table. (granted I haven't tested this, but it was the intention, though I may need to double check that part is actually working). But you don't see fields for them on the back end (yet). So you can set them with the API but not the admin interface. The plan is to make the users fields configurable like a template, where you can add whatever fields you want. We're not there yet, but just wanted to mention that's the goal.

Thanks,

Ryan

Link to comment
Share on other sites

Wouldn't it be possible to make available the user template the general content admin so attached fields could be modified in same way as any other template? (I guess you'd want an extra lock so you didn't accidentally change fields which would immediately disable the user admin by accident - but other than that adding/re-ordering like any other piece of content would seem in the spirit of the system esp. given the admin is pretty much built on top of the API in same way as any other content.

Agree with adamkiss that we don't want to cripple the ultra-flexible model in PW  :o

Link to comment
Share on other sites

Wouldn't it be possible to make available the user template the general content admin so attached fields could be modified in same way as any other template? (I guess you'd want an extra lock so you didn't accidentally change fields which would immediately disable the user admin by accident - but other than that adding/re-ordering like any other piece of content would seem in the spirit of the system esp. given the admin is pretty much built on top of the API in same way as any other content.

This will be possible. Actually, the plan is to change users to be derived from pages and the "user" template will be a system admin template that you can add/modify fields to like any other. Likewise, you'll be able to get/find/manipulate users using the pages functions. The existing $users and $user objects will be mapped to the pages/page API variables. This is the reason why the users, roles and permissions system is barebones in ProcessWire right now ... I wanted to put most of the time into pages, since other systems will be derived from it.

Link to comment
Share on other sites

... the plan is to change users to be derived from pages and the "user" template will be a system admin template that you can add/modify fields to like any other. Likewise, you'll be able to get/find/manipulate users using the pages functions. The existing $users and $user objects will be mapped to the pages/page API variables.

Music to my ears... so many other systems seem to put up unnecessary barriers by fencing off the user-related data objects from the ones used to manage all the other data in a system #makesnosensetome. Go for it Ryan! ProcessWire = more flexible than a yoga Guru... ;D

Link to comment
Share on other sites

@Sevarf2: actually, since via API you can put into your users (as Ryan pointed out, a bit messy ATM) anything you want, so [as I understand it] you can do THAT totally right now.

Imagine /register/ link: (and the template contents are heavy pseudo code)

<?php
  /* templates/register.php
   */

  if ($_GET['registering']) do_validation();

  if($not_valid){
   edit_register_form_to_show_errors();
  }else{
   $register_him = new User(); //this isn't pseudocode!
   $register_him->name = $validated_form['name'];
   ..
   $register_him->last_custom_val = $validated_form['last_custom_val'];
   $register_him->save();
   $session->redirect('/register/thank-you/');
  }

  do_show_empty_or_erroneous_form();
?>

Now you actually have new user, who can login to PW (if you gave him sufficient rights) with all custom data in your DB :)

@martinluff: but looking better... hot yoga mums are such a rarity these days... it's mostly that old really flexible dude again. :D

Link to comment
Share on other sites

Adam is right that you can do that part now if you want. But that's an undocumented thing, and I haven't actually tried it (just built it with that intention). If you do it, and find that it doesn't work for any reason, let me know and I'll rush an update to fix it. :)

Link to comment
Share on other sites

Actually you don't need to create the fields in the DB (at least, if it's working right). It should let you store anything and it'll encode it into the existing 'data' field. But if you aren't in a hurry to get this functionality, I agree that it may be better to wait since there are some changes in order for the users system. Regardless, I don't plan to change the methods of access (api) unless I have to.

Link to comment
Share on other sites

  • 1 month later...

I'm starting using the user function to create custom user fields, but it seems not working.

this is my code:

$u = new User(); 
$u->name = $username; 
$u->pass = $password;
$u->email = $email;
$u->order_id = $order_id;
$u->ipaddress = $ipaddress;
$u->amount = $amount;
$u->signupdate = $signupdate;
$u->addRole($role); 
$u->save();

$session->redirect('/congratulation/');

the user was created but with only name, pass and role...i miss something?  ???(Ryan,in previous post you told me it's not necessary to create the fields in the db first...)

Link to comment
Share on other sites

You are doing it correctly, but it looks like I gave you bad advice. It did support undefined fields within a user at one time in the past, but looks like it's no longer the case (I just spent some time in the code to confirm). I apologize for accidentally telling you something incorrect before. The whole users system is being converted to pages as the next item on my to-do list, so the users system is fairly barebones at present, as you can tell, and I've left some things out of it as a result. What I would suggest is that you start using pages to store your user information now. Setup a new page to serve as the parent for users, like /tools/users/, and remove the guest role from that page so that it's not publicly accessible. Create fields for email, order_id, ipaddress, amount, signupdate and add them to a new template you create (user.php or something like that). Then change your code:

$u = new User();
$u->name = $username;
$u->pass = $password; 
$u->addRole($role);

$p = new Page();
$p->parent = $pages->get("/tools/users/"); // you define where you want them
$p->template = $templates->get("user"); 
$p->name = $username; 
$p->email = $email;
$p->order_id = $order_id;
$p->ipaddress = $ipaddress;
$p->amount = $amount;
$p->signupdate = $signupdate; 

$u->save();
$p->save();

Then when you retrieve a user, retrieve it's corresponding page for your info:

$u = $users->get("$username"); 
$p = $pages->get("/tools/users/$username"); 

Taking it further, you could also make a function that loads users and would automatically populate your page fields into the $user object. In this way you could interact with the $user object like in the example you posted. But you would just want to use these functions below to get and save users, rather than the built in ones:

<?php

function getUser($username) {
    $u = wire("users")->get($username); 
    if(!$u) return false; 
    $p = wire("pages")->get("/tools/users/$username"); 
    foreach($p->fields as $field) {
        $u->set($field->name, $p->get($field->name)); 
    }
    return $u;
}

function saveUser(User $user) {
    $p = wire("pages")->get("/tools/users/" . $user->name); 
    if(!$p->id) $p = new Page(); 
    $p->name = $user->name; 
    foreach($p->fields as $field) {
        $p->set($field->name, $user->get($field->name)); 
    }
    $u->save(); 
    $p->save();
}

Then you could access your fields in the same way as in your original example. These are just rough examples typed directly in the browser, so they haven't been tested and you would just want to use them as a starting point rather than as a complete solution.

Link to comment
Share on other sites

Well the API to the users system probably won't change much, if at all. As you've seen, it's already nearly identical to the $page interface. So when you upgrade, I think best case is that it'll still work, but you'll have some unnecessary code. Worst case is that you'll need to convert your new pages in /tools/users/ over to the new users system (which you may want to do either way). But it won't be hard, and I think we'll be able to get you through the conversion with just a few API snippets.

Link to comment
Share on other sites

I made it and it works good with just some modifications. this is my final code:

$u = new User(); 
$u->name = $username; 
$u->pass = $password;
$u->all the custom fields here
$u->addRole($role); 

$p = $pages->get("/members/" . $user->name); 
if(!$p->id){ $p = new Page();}
$p->parent = $pages->get("/members/");
$p->template = $templates->get("member"); 
$p->name = $u->name; 
foreach($p->fields as $field) {
   $p->set($field->name, $u->get($field->name)); 
}
$u->save(); 
$p->save();

and this is the code after login:

if($user->isLoggedin()==true){
   $p = $pages->get("/members/$user/"); 
   foreach($p->fields as $field) {
       $user->set($field->name, $p->get($field->name)); 
   }
}
Link to comment
Share on other sites

Glad that worked. Thanks for posting the final code too. The only other thing you'll want to be mindful of is when you delete a user, you'll also want to delete the associated page (just to keep things organized). I can show you how to create a module that hooks into the user deletion and handles the page deletion. Or you can periodically foreach the /members/ pages and check if there are any extras there.

As you know, the users system is being redone. There are a few reasons, but one of them is that the system is designed just to have a few users in there, primarily for administrative purposes or for role placeholders for another user system. If you start getting hundreds of users in there, it's going to affect performance. In the new system, it won't matter how many users there are, since they will be built on top of the pages system.

Link to comment
Share on other sites

  • 4 months later...

Are there any plans to have show an interface for custom fields in the backend in 2.1? Perhaps list the user/role template in the templates screen but have it locked to avoid deletion. They you can add fields as per other templates and show these on the user edit screen.

Link to comment
Share on other sites

Are there any plans to have show an interface for custom fields in the backend in 2.1? Perhaps list the user/role template in the templates screen but have it locked to avoid deletion. They you can add fields as per other templates and show these on the user edit screen.

It works this way in 2.1. When editing templates you can choose it to show system templates also. There is user template that you can extend like any other template.

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.
×
×
  • Create New...