Added functionality in the main page for managing roles

This commit is contained in:
jacob 2010-10-06 17:05:54 +00:00
parent 052024b6d6
commit daa714e5ef
2 changed files with 182 additions and 92 deletions

View File

@ -338,8 +338,9 @@ function user_remove_role(&$u, $role)
} }
/* Delete the role */ /* Delete the role */
$roleId = $u['roles'][$role]['roles_id'];
unset($u['roles'][$role]); 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 */ /* Save this user */
user_save($u); user_save($u);

View File

@ -21,8 +21,7 @@
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. Boston, MA 02111-1307, USA.
*/ */
?>
<?
require_once("common.inc.php"); require_once("common.inc.php");
require_once("user.inc.php"); require_once("user.inc.php");
user_auth_required(); user_auth_required();
@ -37,14 +36,51 @@
); );
} }
$u = user_load($_SESSION['users_id']); $u = user_load($_SESSION['users_id']);
if(array_key_exists('action', $_GET)){ if(array_key_exists('action', $_GET)){
switch($_GET['action']){ switch($_GET['action']){
case 'register': case 'register':
register_new_role(); register_new_role();
break; 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: default:
break; break;
} }
@ -53,6 +89,8 @@ if(array_key_exists('action', $_GET)){
send_header("Main Page", array()); send_header("Main Page", array());
// throw in our javascript functions
?> ?>
<script type="text/javascript"> <script type="text/javascript">
function register(role){ function register(role){
@ -63,22 +101,67 @@ if(array_key_exists('action', $_GET)){
'password' : $('#' + role + '_password').val() 'password' : $('#' + role + '_password').val()
}, },
function(result){ function(result){
alert(result); $('#roles').load('user_main.php?action=draw_roles');
} }
); );
} }
function activate(role){
$("#rolestatus_"+role).load("<?=$config['SFIABDIRECTORY']?>/user_main.php?action=activate&users_id=<?=$u['id']?>&role="+role,$('#rolesform').serializeArray());
$("#activate_"+role).attr('disabled', 'disabled');
$("#deactivate_"+role).removeAttr('disabled');
$("#remove_"+role).removeAttr('disabled');
$("#rolestatus_"+role).removeClass('notice');
$("#rolestatus_"+role).addClass('happy');
return false;
}
function deactivate(role){
$("#rolestatus_"+role).load("<?=$config['SFIABDIRECTORY']?>/user_main.php?action=deactivate&users_id=<?=$u['id']?>&role="+role,$('#rolesform').serializeArray());
$("#activate_"+role).removeAttr('disabled');
$("#deactivate_"+role).attr('disabled', 'disabled');
$("#remove_"+role).attr('disabled', 'disabled');
$("#rolestatus_"+role).removeClass('happy');
$("#rolestatus_"+role).addClass('notice');
return false;
}
function remove(role){
var con = confirmClick("<?=i18n("Are you sure you want to remove this role from your account?\\nThis action cannot be undone.")?>");
if(con == true) {
$.get("<?=$config['SFIABDIRECTORY']?>/user_main.php?action=remove&users_id=<?=$u['id']?>&role="+role,function(){
$('#roles').load('user_main.php?action=draw_roles');
});
}
}
</script> </script>
<?php <?php
// draw the main body of the page
echo "<div><p>";
//only display the named greeting if we have their name //only display the named greeting if we have their name
echo i18n("Hello <b>%1</b>",array($_SESSION['name'])); echo i18n("Hello <strong>%1</strong>",array($_SESSION['name']));
echo "<br />";
echo "<br />";
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 "<p>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: </p></div>";
echo "<br />"; draw_roles();
echo "<br />"; echo "<br />";
echo i18n('Other Options and Things To Do').':<br />';
echo '<ul>';
echo '<li><a href="user_edit.php">'.i18n('Change Password').'</a> - '.i18n('Change your email, username, and password').'</li>';
echo '<li><a href="user_edit.php">'.i18n('Activate/Deactivate Roles').'</a> - '.
i18n('Activate/Deactiate/Remove/Delete roles or your entire account').
'</li>';
echo '<li>'.i18n('To logout, use the "Logout" link in the upper-right of the page').'</li>';
echo '</ul>';
send_footer();
function draw_roles(){
// get a list of all roles that this user can potentially sign up for // get a list of all roles that this user can potentially sign up for
global $u, $config;
$rlist = array(); $rlist = array();
$q = mysql_query("SELECT * FROM roles"); $q = mysql_query("SELECT * FROM roles");
$available = array(); $available = array();
@ -96,55 +179,70 @@ if(array_key_exists('action', $_GET)){
} }
} }
echo '<div id="roles">';
if(count($registered) > 0){ if(count($registered) > 0){
echo "<h4>You are currently registered for the following roles:</h4>"; $rowNumber = 0;
foreach($registered as $type => $title){ echo "<h4>" . i18n("You are currently registered for the following roles") . ":</h4>";
echo "$title<br/>"; echo '<form class="editor" id="rolesform">';
echo "<table class=\"summarytable\" style=\"width:95%;margin-bottom:1em\">";
foreach($registered as $role => $title){
echo '<tr class="';
if(($rowNumber++) % 2) echo 'odd';
else echo 'even';
echo '">';
echo "<td style=\"width:10em\"><strong>" . i18n($title) . "</strong></td>";
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 "<br/>"; ?>
<td><div class="<?=$cl?>" id="rolestatus_<?=$role?>"><?=$cs?></div></td>
<td>
<button style="width: 100px;" id="activate_<?=$role?>" <?=$a?> onclick="activate('<?=$role?>');return false;" ><?=i18n("Activate")?></button>
<button style="width: 100px;" id="deactivate_<?=$role?>" <?=$d?> onclick="deactivate('<?=$role?>');return false;" ><?=i18n("Deactivate")?></button>
<button style="width: 100px;" id="remove_<?=$role?>" <?=$d?> onclick="remove('<?=$role?>');return false;" ><?=i18n("Remove")?></button>
</td>
<?php
echo "</tr>";
}
echo "</table></form>";
} }
if(count($available) > 0){ if(count($available) > 0){
echo "<h4>The following roles are available:</h4>"; echo "<h4>" . i18n("The following roles are available") . ":</h4>";
$rowNumber = 0; $rowNumber = 0;
echo "<table class=\"summarytable\">"; echo "<table class=\"summarytable\" style=\"width:95%\">";
foreach($available as $type => $title){ foreach($available as $type => $title){
echo '<tr class="'; echo '<tr class="';
if(($rowNumber++) % 2) echo 'odd'; if(($rowNumber++) % 2) echo 'odd';
else echo 'even'; else echo 'even';
echo '">'; echo '">';
echo "<td><strong>$title</strong></td><td>"; echo "<td style=\"width:10em\"><strong>$title</strong></td><td>";
draw_signup_form($type); draw_signup_form($type);
echo "</td></tr>"; echo "</td></tr>";
} }
echo "</table>"; echo "</table>";
} }
echo "</div>";
echo "<br />"; }
echo i18n('Other Options and Things To Do').':<br />';
echo '<ul>';
echo '<li><a href="user_edit.php">'.i18n('Change Password').'</a> - '.i18n('Change your email, username, and password').'</li>';
echo '<li><a href="user_edit.php">'.i18n('Activate/Deactivate Roles').'</a> - '.
i18n('Activate/Deactiate/Remove/Delete roles or your entire account').
'</li>';
echo '<li>'.i18n('To logout, use the "Logout" link in the upper-right of the page').'</li>';
echo '</ul>';
/*
echo "<pre>";
echo htmlentities(print_r($u, true));
echo htmlentities(print_r($config, true));
echo "</pre>";
*/
send_footer();
function draw_signup_form($type){ function draw_signup_form($type){
global $config; global $config;
global $roleDat; global $roleDat;
switch($type) { switch($type) {
case 'volunteer': case 'volunteer':
// returns "notopenyet", "closed", or "open"
$reg_open = user_volunteer_registration_status(); $reg_open = user_volunteer_registration_status();
$reg_mode = $config['volunteer_registration_type']; $reg_mode = $config['volunteer_registration_type'];
// $reg_single_password = $config['volunteer_registration_singlepassword']; // $reg_single_password = $config['volunteer_registration_singlepassword'];
@ -263,21 +361,12 @@ function register_new_role(){
// see if they're already registered for it // see if they're already registered for it
$role_index = $roleDat[$role]['id']; $role_index = $roleDat[$role]['id'];
$query = "SELECT COUNT(*) FROM user_roles WHERE users_id = $uid AND roles_id=$role_index"; $query = "SELECT COUNT(*) FROM user_roles WHERE users_id = $uid AND roles_id=$role_index";
echo $query;
$results = mysql_fetch_array(mysql_query($query)); $results = mysql_fetch_array(mysql_query($query));
if($results[0] != 0){ if($results[0] != 0){
return false; return false;
} }
// not already regiseterd, let's go ahead and hook 'em up user_add_role($u, $role, $password);
$query = "INSERT INTO user_roles (accounts_id, users_id, roles_id, active, complete)"; user_save($u);
$query .= " VALUES($accounts_id, $uid, $role_index, 'yes', 'no')";
echo $query;
return true;
/*
if(mysql_query($query)){
return true;
}else{
return false;
}
*/
} }