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 */
$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);

View File

@ -21,30 +21,66 @@
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
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
$roleDat = array();
$q = mysql_query("SELECT * FROM roles");
while($row = mysql_fetch_assoc($q)){
$roleDat[$row['type']] = array(
require_once("common.inc.php");
require_once("user.inc.php");
user_auth_required();
// 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'],
'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
?>
<script type="text/javascript">
function register(role){
@ -63,88 +101,148 @@ if(array_key_exists('action', $_GET)){
'password' : $('#' + role + '_password').val()
},
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>
<?php
//only display the named greeting if we have their name
echo i18n("Hello <b>%1</b>",array($_SESSION['name']));
echo "<br />";
echo "<br />";
// draw the main body of the page
echo "<div><p>";
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 "<br />";
echo "<br />";
//only display the named greeting if we have their name
echo i18n("Hello <strong>%1</strong>",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 "<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>";
draw_roles();
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>';
if(count($registered) > 0){
echo "<h4>You are currently registered for the following roles:</h4>";
foreach($registered as $type => $title){
echo "$title<br/>";
}
echo "<br/>";
}
send_footer();
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 />";
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>';
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 "<pre>";
echo htmlentities(print_r($u, true));
echo htmlentities(print_r($config, true));
echo "</pre>";
*/
echo '<div id="roles">';
if(count($registered) > 0){
$rowNumber = 0;
echo "<h4>" . i18n("You are currently registered for the following roles") . ":</h4>";
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){
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);
}