Skip to content

Commit

Permalink
Adds administration section for user roles.
Browse files Browse the repository at this point in the history
  • Loading branch information
elplatt committed Jul 3, 2011
1 parent 69dfc4a commit 7ea3004
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 1 deletion.
10 changes: 10 additions & 0 deletions crm/include/member/page.inc.php
Expand Up @@ -136,6 +136,15 @@ function member_page (&$data, $page, $options) {
array_unshift($data['Plan'], $plan);
}

// Add role tab
if (user_access('member_membership_edit')) {
if (!isset($data['Roles'])) {
$data['Roles'] = array();
}
$roles = theme('user_role_edit_form', $cid);
array_unshift($data['Roles'], $roles);
}

break;

case 'membership':
Expand All @@ -160,3 +169,4 @@ function member_page (&$data, $page, $options) {
break;
}
}

2 changes: 1 addition & 1 deletion crm/include/theme.inc.php
Expand Up @@ -317,7 +317,7 @@ function theme_form_text ($field) {
* @return The themed html for the checkbox.
*/
function theme_form_checkbox ($field) {
$output = '<fieldset class="form-row">';
$output = '<fieldset class="form-row form-row-checkbox">';
$output .= '<input type="checkbox" name="' . $field['name'] . '" value="1"';
if ($field['checked']) {
$output .= ' checked="checked"';
Expand Down
148 changes: 148 additions & 0 deletions crm/include/user/user.inc.php
Expand Up @@ -58,6 +58,39 @@ function user_get_user($cid = 0) {
return $user;
}

/**
* Get role data for one or more users.
*
* @param $opts Options, possible keys are:
* cid Filter results to only those matching the given cid.
* @return An array with each element representing one user's roles.
*/
function user_role_data ($opts = NULL) {

// Construct query
$sql = "SELECT * FROM `role` WHERE 1 ";

// Add filter
if (!empty($opts['cid'])) {
$cid = mysql_real_escape_string($opts['cid']);
$sql .= " AND `cid`='$cid'";
}

// Excecute query
$res = mysql_query($sql);
if (!$res) { die(mysql_error()); }

// Construct array of role data
$roles = array();
$row = mysql_fetch_assoc($res);
while ($row) {
$roles[] = $row;
$row = mysql_fetch_assoc($res);
}

return $roles;
}

/**
* Check if a user has the given role.
*
Expand Down Expand Up @@ -430,3 +463,118 @@ function theme_user_reset_password_confirm_form ($code) {

return theme_form(user_reset_password_confirm_form($code));
}

/**
* Return the form structure for editing user roles.
*
* @param $cid The cid of the user.
* @return The form structure.
*/
function user_role_edit_form ($cid) {

// Get user data
$data = user_role_data(array('cid'=>$cid));
$role = $data[0];

$form = array(
'type' => 'form',
'method' => 'post',
'command' => 'user_role_update',
'hidden' => array(
'cid' => $cid
),
'fields' => array(
array(
'type' => 'fieldset',
'label' => 'Update Roles',
'fields' => array(
array(
'type' => 'checkbox',
'label' => 'Director',
'name' => 'director',
'checked' => $role['director']
),
array(
'type' => 'checkbox',
'label' => 'President',
'name' => 'president',
'checked' => $role['president']
),
array(
'type' => 'checkbox',
'label' => 'VP',
'name' => 'vp',
'checked' => $role['vp']
),
array(
'type' => 'checkbox',
'label' => 'Secretary',
'name' => 'secretary',
'checked' => $role['secretary']
),
array(
'type' => 'checkbox',
'label' => 'Treasurer',
'name' => 'treasurer',
'checked' => $role['treasurer']
),
array(
'type' => 'checkbox',
'label' => 'Web Admin',
'name' => 'webAdmin',
'checked' => $role['webAdmin']
),
array(
'type' => 'submit',
'name' => 'submitted',
'value' => 'Update'
)
)
)
)
);
return $form;
}

/**
* Handle user role update request.
*
* @return The url to display on completion.
*/
function command_user_role_update () {
global $esc_post;

// Check permissions
if (!user_access('user_edit')) {
error_register('Current user does not have permission: user_edit');
return 'members.php';
}

// Construct query
$sql = "
UPDATE `role`
SET
`director`='$esc_post[director]'
, `president`='$esc_post[president]'
, `vp`='$esc_post[vp]'
, `secretary`='$esc_post[secretary]'
, `treasurer`='$esc_post[treasurer]'
, `webAdmin`='$esc_post[webAdmin]'
WHERE `cid`='$esc_post[cid]'
";
$res = mysql_query($sql);
if (!$res) { die(mysql_error()); }

return "member.php?cid=$_POST[cid]&tab=roles";
}


/**
* Return themed html for a user role edit form.
*
* @param $cid The cid for the user to edit.
* @return The themed html string.
*/
function theme_user_role_edit_form ($cid) {
return theme('form', user_role_edit_form($cid));
}
11 changes: 11 additions & 0 deletions crm/style.css
Expand Up @@ -105,6 +105,17 @@ tr:first-child td {
background-color: #eeeeee;
}

/* General form styles */

.form-row-checkbox input {
float: left;
margin-right: 0.5em;
}

.form-row-checkbox label {
float: left;
}

/* Layout */

.container {
Expand Down

0 comments on commit 7ea3004

Please sign in to comment.