From daa714e5ef9a3954f8364fb8e9be838d3205cbb9 Mon Sep 17 00:00:00 2001 From: jacob Date: Wed, 6 Oct 2010 17:05:54 +0000 Subject: [PATCH] Added functionality in the main page for managing roles --- user.inc.php | 3 +- user_main.php | 271 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 182 insertions(+), 92 deletions(-) diff --git a/user.inc.php b/user.inc.php index aa51685..b95d68c 100644 --- a/user.inc.php +++ b/user.inc.php @@ -338,8 +338,9 @@ function user_remove_role(&$u, $role) } /* Delete the role */ + $roleId = $u['roles'][$role]['roles_id']; unset($u['roles'][$role]); - mysql_query("DELETE FROM user_roles WHERE roles_id={$role['id']} AND users_id='$id'"); + mysql_query("DELETE FROM user_roles WHERE roles_id={$roleId} AND users_id='$id'"); /* Save this user */ user_save($u); diff --git a/user_main.php b/user_main.php index 2f4012b..8f4582e 100644 --- a/user_main.php +++ b/user_main.php @@ -21,30 +21,66 @@ the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -?> - $row['id'], 'name' => $row['name'] ); - } - - - $u = user_load($_SESSION['users_id']); +} +$u = user_load($_SESSION['users_id']); if(array_key_exists('action', $_GET)){ switch($_GET['action']){ case 'register': register_new_role(); break; + case 'draw_roles': + draw_roles(); + break; + + case 'remove': + $role = $_GET['role']; + /* Like delete, only we're only deleting a role, not the whole account */ + happy_("{$roles[$role]['name']} role successfully removed."); + echo i18n("Removed"); + user_remove_role($u, $role); + exit; + + case 'activate': + $role = $_GET['role']; + if(!array_key_exists($role, $u['roles'])) { + /* Hand crafting URLs? */ + echo "HALT: can't activate a role the user doesn't have"; + exit; + } + $u['roles'][$role]['active'] = 'yes'; + user_save($u); + happy_("{$roles[$role]['name']} role for %1 successfully activated",array($config['FAIRYEAR'])); + echo i18n("Active"); + exit; + + case 'deactivate': + $role = $_GET['role']; + if(!array_key_exists($role, $u['roles'])) { + /* Hand crafting URLs? */ + echo "HALT: can't deactivate a role the user doesn't have"; + exit; + } + $u['roles'][$role]['active'] = 'no'; + user_save($u); + happy_("{$roles[$role]['name']} role for %1 successfully deactivated",array($config['FAIRYEAR'])); + echo i18n("Deactivated"); + exit; + + default: break; } @@ -52,7 +88,9 @@ if(array_key_exists('action', $_GET)){ } - send_header("Main Page", array()); +send_header("Main Page", array()); + +// throw in our javascript functions ?> %1",array($_SESSION['name'])); - echo "
"; - echo "
"; +// draw the main body of the page +echo "

"; - echo "This is a placeholder for the main user page until all the specific user-role pages are removed. For now, heres your roles: "; - echo "
"; - echo "
"; +//only display the named greeting if we have their name +echo i18n("Hello %1",array($_SESSION['name'])); - // get a list of all roles that this user can potentially sign up for - $rlist = array(); - $q = mysql_query("SELECT * FROM roles"); - $available = array(); - $registered = array(); - while($row = mysql_fetch_assoc($q)){ - $roleid = $row['type']; - $idx = $roleid . "_registration_type"; - if(array_key_exists($idx, $config)){ - // this is a role that can potentially be registered for - if(is_array($u['roles']) && array_key_exists($row['type'], $u['roles'])){ - $registered[$row['type']] = $row['name']; - }else{ - $available[$row['type']] = $row['name']; - } - } - } +echo "

This is a placeholder for the main user page until all the specific user-role pages are removed. For now, here's what you can do:

"; +draw_roles(); +echo "
"; +echo i18n('Other Options and Things To Do').':
'; +echo ''; - if(count($registered) > 0){ - echo "

You are currently registered for the following roles:

"; - foreach($registered as $type => $title){ - echo "$title
"; - } - echo "
"; - } +send_footer(); - if(count($available) > 0){ - echo "

The following roles are available:

"; - $rowNumber = 0; - echo ""; - foreach($available as $type => $title){ - echo ''; - echo ""; - } - echo "
$title"; - draw_signup_form($type); - echo "
"; - } - echo "
"; - echo i18n('Other Options and Things To Do').':
'; - echo ''; +function draw_roles(){ + // get a list of all roles that this user can potentially sign up for + global $u, $config; + $rlist = array(); + $q = mysql_query("SELECT * FROM roles"); + $available = array(); + $registered = array(); + while($row = mysql_fetch_assoc($q)){ + $roleid = $row['type']; + $idx = $roleid . "_registration_type"; + if(array_key_exists($idx, $config)){ + // this is a role that can potentially be registered for + if(is_array($u['roles']) && array_key_exists($row['type'], $u['roles'])){ + $registered[$row['type']] = $row['name']; + }else{ + $available[$row['type']] = $row['name']; + } + } + } -/* -echo "
";
-echo htmlentities(print_r($u, true));
-echo htmlentities(print_r($config, true));
-echo "
"; -*/ + echo '
'; + if(count($registered) > 0){ + $rowNumber = 0; + echo "

" . i18n("You are currently registered for the following roles") . ":

"; + echo '
'; + echo ""; + foreach($registered as $role => $title){ + echo ''; + echo ""; - send_footer(); + if($u['roles'][$role]['active'] == 'yes') { + $cs = i18n('Active'); + $cl = 'happy'; + $a = 'disabled="disabled"'; + $d = ''; + } else { + $cs = i18n('Deactivated'); + $cl = 'notice'; + $a = ''; + $d = 'disabled="disabled"'; + } +?> + + +"; + } + echo "
" . i18n($title) . "
+ + + + + +
"; + } + + if(count($available) > 0){ + echo "

" . i18n("The following roles are available") . ":

"; + $rowNumber = 0; + echo ""; + foreach($available as $type => $title){ + echo ''; + echo ""; + } + echo "
$title"; + draw_signup_form($type); + echo "
"; + } + echo "
"; +} function draw_signup_form($type){ global $config; global $roleDat; switch($type) { case 'volunteer': - // returns "notopenyet", "closed", or "open" $reg_open = user_volunteer_registration_status(); $reg_mode = $config['volunteer_registration_type']; // $reg_single_password = $config['volunteer_registration_singlepassword']; @@ -263,21 +361,12 @@ function register_new_role(){ // see if they're already registered for it $role_index = $roleDat[$role]['id']; $query = "SELECT COUNT(*) FROM user_roles WHERE users_id = $uid AND roles_id=$role_index"; + echo $query; $results = mysql_fetch_array(mysql_query($query)); if($results[0] != 0){ return false; } - // not already regiseterd, let's go ahead and hook 'em up - $query = "INSERT INTO user_roles (accounts_id, users_id, roles_id, active, complete)"; - $query .= " VALUES($accounts_id, $uid, $role_index, 'yes', 'no')"; - echo $query; - return true; - /* - if(mysql_query($query)){ - return true; - }else{ - return false; - } - */ + user_add_role($u, $role, $password); + user_save($u); }