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,30 +21,66 @@
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("user.inc.php");
user_auth_required();
// grab data for the available role types require_once("common.inc.php");
$roleDat = array(); require_once("user.inc.php");
$q = mysql_query("SELECT * FROM roles"); user_auth_required();
while($row = mysql_fetch_assoc($q)){
$roleDat[$row['type']] = array( // grab data for the available role types
$roleDat = array();
$q = mysql_query("SELECT * FROM roles");
while($row = mysql_fetch_assoc($q)){
$roleDat[$row['type']] = array(
'id' => $row['id'], 'id' => $row['id'],
'name' => $row['name'] 'name' => $row['name']
); );
} }
$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;
} }
@ -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
?> ?>
<script type="text/javascript"> <script type="text/javascript">
function register(role){ function register(role){
@ -63,88 +101,148 @@ 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
//only display the named greeting if we have their name // draw the main body of the page
echo i18n("Hello <b>%1</b>",array($_SESSION['name'])); echo "<div><p>";
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: "; //only display the named greeting if we have their name
echo "<br />"; echo i18n("Hello <strong>%1</strong>",array($_SESSION['name']));
echo "<br />";
// get a list of all roles that this user can potentially sign up for 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>";
$rlist = array(); draw_roles();
$q = mysql_query("SELECT * FROM roles"); echo "<br />";
$available = array(); echo i18n('Other Options and Things To Do').':<br />';
$registered = array(); echo '<ul>';
while($row = mysql_fetch_assoc($q)){ echo '<li><a href="user_edit.php">'.i18n('Change Password').'</a> - '.i18n('Change your email, username, and password').'</li>';
$roleid = $row['type']; echo '<li><a href="user_edit.php">'.i18n('Activate/Deactivate Roles').'</a> - '.
$idx = $roleid . "_registration_type"; i18n('Activate/Deactiate/Remove/Delete roles or your entire account').
if(array_key_exists($idx, $config)){ '</li>';
// this is a role that can potentially be registered for echo '<li>'.i18n('To logout, use the "Logout" link in the upper-right of the page').'</li>';
if(is_array($u['roles']) && array_key_exists($row['type'], $u['roles'])){ echo '</ul>';
$registered[$row['type']] = $row['name'];
}else{
$available[$row['type']] = $row['name'];
}
}
}
if(count($registered) > 0){ send_footer();
echo "<h4>You are currently registered for the following roles:</h4>";
foreach($registered as $type => $title){
echo "$title<br/>";
}
echo "<br/>";
}
if(count($available) > 0){
echo "<h4>The following roles are available:</h4>";
$rowNumber = 0;
echo "<table class=\"summarytable\">";
foreach($available as $type => $title){
echo '<tr class="';
if(($rowNumber++) % 2) echo 'odd';
else echo 'even';
echo '">';
echo "<td><strong>$title</strong></td><td>";
draw_signup_form($type);
echo "</td></tr>";
}
echo "</table>";
}
echo "<br />"; function draw_roles(){
echo i18n('Other Options and Things To Do').':<br />'; // get a list of all roles that this user can potentially sign up for
echo '<ul>'; global $u, $config;
echo '<li><a href="user_edit.php">'.i18n('Change Password').'</a> - '.i18n('Change your email, username, and password').'</li>'; $rlist = array();
echo '<li><a href="user_edit.php">'.i18n('Activate/Deactivate Roles').'</a> - '. $q = mysql_query("SELECT * FROM roles");
i18n('Activate/Deactiate/Remove/Delete roles or your entire account'). $available = array();
'</li>'; $registered = array();
echo '<li>'.i18n('To logout, use the "Logout" link in the upper-right of the page').'</li>'; while($row = mysql_fetch_assoc($q)){
echo '</ul>'; $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 '<div id="roles">';
echo "<pre>"; if(count($registered) > 0){
echo htmlentities(print_r($u, true)); $rowNumber = 0;
echo htmlentities(print_r($config, true)); echo "<h4>" . i18n("You are currently registered for the following roles") . ":</h4>";
echo "</pre>"; 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>";
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"';
}
?>
<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){
echo "<h4>" . i18n("The following roles are available") . ":</h4>";
$rowNumber = 0;
echo "<table class=\"summarytable\" style=\"width:95%\">";
foreach($available as $type => $title){
echo '<tr class="';
if(($rowNumber++) % 2) echo 'odd';
else echo 'even';
echo '">';
echo "<td style=\"width:10em\"><strong>$title</strong></td><td>";
draw_signup_form($type);
echo "</td></tr>";
}
echo "</table>";
}
echo "</div>";
}
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;
}
*/
} }