Community & Support

hook_user $op = 'insert' customized data. SQL don't execute.

Hi guys:
I was trying to set up a module which can sign up with different roles. I know a module called 'rolesignup' and i found a 6.x version. but i want more specific requirements for this module. I already done a module can limits the profile fields to show depending on the roles. So when user register they can fill out the form by register different roles. The problem right now is:
For the register module when it save the profile the sql i used doesn't execute. Here's the code check it out.

<?php
/**
* Implemetation of hook_perm()
*
* @return unknown
*/
function role_register_perm(){
return array('register for role');
}

/**
* Implemetation of hook_menu().
*
* @return unknown
*/
function role_register_menu(){
$items = array();

$items['user/register/role/%'] = array(
'page callback' => 'role_register_page',
'page arguments' => arg(3),
'access arguments' => array('register for role'),
'type' => MENU_CALLBACK
);
return $items;
}

/**
* Enter description here...
*
*/
function role_register_page($role=NULL){
$roles = user_roles(true,'register for role');
// drupal_set_message('<pre>'.print_r($roles).'</pre>');
if (isset($roles[arg(3)])) {
$_SESSION['role'] = arg(3);
drupal_goto('user/register');
}else {
drupal_not_found();
}
}

/**
* Implemetation of hook_form_alter().
*
* @param unknown_type $form
* @param unknown_type $form_state
* @param unknown_type $form_id
*/
function role_register_form_alter(&$form, $form_state, $form_id){
if($form_id == 'user_register'){
$roles = user_roles();
// print '<pre>';print_r($_SESSION['role']);print '</pre>';
if(isset($_SESSION['role'])){
drupal_set_title(ucfirst($roles[$_SESSION['role']]).' Register');
//get what fields should in this role that this
//anonymous user trying to register
$fields = array();
$query = "SELECT * FROM {profile_fields} WHERE fid IN
( SELECT fid FROM {profile_extension_role} WHERE rid = %d )";
$result = db_query($query,$_SESSION['role']);
while ($fields_in_role = db_fetch_object($result)){
if( !is_array($fields[$fields_in_role->category])
|| !in_array($fields_in_role->name,$fields[$fields_in_role->category]) ){
$fields[$fields_in_role->category][] = $fields_in_role->name;
}
}

//get all the categories
$categories = array();
$result_s = db_query("SELECT * FROM {profile_fields pf}");
while ($category = db_fetch_object($result_s)){
if( !is_array($categories[$category->category])
|| !in_array($category->name,$categories[$category->category])){
$categories[$category->category][] = $category->name;
}
}

foreach ($categories as $category => $names){
if(!in_array($category,array_keys($fields))){
//disable the category if shouldn't show
unset($form[$category]);
}else{
foreach ($names as $name){
if(!in_array($name,$fields[$category])){
unset($form[$category][$name]);
}
}
}
}

}else{
//otherwise redirect back for get which role trying to register
drupal_goto('user/register/role/invalid', drupal_get_destination());
}
}
}

/**
* Implementation of hook_user()
*/
function role_register_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'insert':
$roles = user_roles(true,'register for role');
if ($roles[$_SESSION['role']] && !in_array($_SESSION['role'], array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
drupal_set_message($account->uid);
drupal_set_message($_SESSION['role']);
                                /*THIS SQL NEVER EXECUTE*/
db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $account->uid, $_SESSION['role']);
}
break;
case 'register':
if(!$category || $category == "account") {
if (!$_SESSION['role']) {
drupal_goto('user/register/role/invalid', drupal_get_destination());
}
// print '<pre>';print_r($account);print '</pre>';
// drupal_set_message($_SESSION['role']);
}
break;
}
}

another issue is when register done it save into {profile_values} some empty rows. I think the reason is i unset the form fields that shouldn't appear. and the profile module implement the hook_user also. and it calls the profile_save_profile. Is there any way can solve this? and why the sql is doesn't work? Any clue is appreciate!

Comments

Hi your sql executed,

Hi
your sql executed, but hook_user($op="insert") executed before user_save and user_save will delete all this user's roles. see user.module line 352

====================
http://aymoo.cn

====================
http://aymoo.cn

How ridiculous is

How ridiculous is that?

<?php
 
// user.module function user_save
   // Reload user roles if provided.
   
if (isset($array['roles']) && is_array($array['roles'])) {
     
db_query('DELETE FROM {users_roles} WHERE uid = %d', $account->uid);

      foreach (
array_keys($array['roles']) as $rid) {
        if (!
in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
         
db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $account->uid, $rid);
        }
      }
    }
?>

--
"I'm not concerned about all hell breaking loose, but that a PART of hell will break loose... it'll be much harder to detect." - George Carlin
--
Personal: http://www.nicklewis.org
Work: http://www.chapterthree.com

And how can this problem be

And how can this problem be overcome? I have to add some roles with an expiry date during the account creation (I am using ubercart) and used the hook_user with $op='insert'. The ubercart role changes work fine but the role which has to be written into the user_roles table gets deleted within user_save.

Any hints are welcome; thanks
Stephan

Found it myself!

Never mind:

by adding the role into the $edit all the roles are written to the table:

$edit['roles'] += array($rid => $role);

Remind you that:
$edit->roles[$rid] = $role;
did NOT work (Newbie error :)

Thx a lot, i've been

Thx a lot, i've been suffering from this problem also.

But it make more sense to me to remove those two lines, since they are 'Just to be safe' as commented.

The two lines are line 255, 352 in user.module.

Andy Hui
CTO
Soul Design Group (SDG) Limited
e: andy@soul-dg.com
w: www.soul-dg.com
t: (852) 25271788

You are my savior

I am also a newb

can I just say:

$edit['roles'] += array('4' => 'member');

you ended my suffering haha