最近一直在挣扎于profile模块的改进,我要实现的是在后台管理里可以给不同的profile定义用户角色,之后当用户注册不同角色时就可以根据不同角色的要求来填写相关的信息。profile的模块已经实现了,注册的模块也可以显示不同信息了,就是在最后往数据库插数据的时候有问题,代码如下:
<?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($form);print '</pre>';
if(isset($_SESSION['role'])){
// $form['roles'] = array('#type' => 'hidden', '#value' => $_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) {
if ($op == 'insert' || $op == 'update') {
$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']);
/*SQL DOESN'T WORK*/
db_query('DELETE FROM {users_roles} WHERE uid = %d', $account->uid);
db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $account->uid, $_SESSION['role']);
}
}
// print '<pre>';print_r($op);print '</pre>';
}
哪位高手可以帮忙?
打印$_SESSION['role']看看
打印$_SESSION['role']看看吧。
---------------------------------------------
---------------------------------------------