function uc_roles_user in Ubercart 5
Same name and namespace in other branches
- 6.2 uc_roles/uc_roles.module \uc_roles_user()
Implementation of hook_user().
File
- uc_roles/
uc_roles.module, line 128 - Grants roles upon accepted payment of products
Code
function uc_roles_user($op, &$edit, &$account, $category = NULL) {
global $user;
switch ($op) {
case 'delete':
db_query("DELETE FROM {uc_roles_expirations} WHERE uid = %d", $account->uid);
break;
case 'form':
if (user_access('administer users') && (is_null($category) || $category == 'account')) {
$role_choices = _get_role_choices(array_keys($account->roles));
//Assoc array, for Drupal form elements
$div = array(
'#prefix' => '<div class="expiration">',
'#suffix' => '</div>',
);
$polarity_widget = array(
'#type' => 'select',
'#options' => array(
'add' => '+',
'remove' => '-',
),
);
$polarity_widget = $polarity_widget + $div;
$qty_widget = array(
'#type' => 'textfield',
'#size' => 4,
'#maxlength' => 4,
);
$qty_widget = $qty_widget + $div;
$granularity_widget = array(
'#type' => 'select',
'#options' => array(
'day' => t('day(s)'),
'week' => t('week(s)'),
'month' => t('month(s)'),
'year' => t('year(s)'),
),
);
$granularity_widget = $granularity_widget + $div;
$form['account']['expirations'] = array(
'#type' => 'fieldset',
'#title' => t('Role expirations'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 10,
);
$expirations = db_query("SELECT * FROM {uc_roles_expirations} WHERE uid = %d", $account->uid);
$form['account']['expirations']['intro'] = array(
'#type' => 'markup',
'#value' => '<p>' . t('Below you can add or remove time to the expiration dates of the following roles.') . '</p>',
);
//There are expirations to administer
if (db_num_rows($expirations) > 0) {
$form['account']['expirations']['table_start'] = array(
'#type' => 'markup',
'#value' => '<table><thead><th>Role</th><th>Expiration</th><th>Add/Remove time</th></thead><tbody>',
);
while ($expiration = db_fetch_object($expirations)) {
$class = is_null($class) || $class == "even" ? "odd" : "even";
$form['account']['expirations']['role_' . $expiration->rid] = array(
'#type' => 'markup',
'#value' => '<tr class="' . $class . '"><td>' . _get_role_name($expiration->rid) . '</td><td>' . format_date($expiration->expiration, 'small') . '</td><td>',
);
$form['account']['expirations']['role_' . $expiration->rid . '_polarity'] = $polarity_widget;
$form['account']['expirations']['role_' . $expiration->rid . '_qty'] = $qty_widget;
$form['account']['expirations']['role_' . $expiration->rid . '_granularity'] = $granularity_widget;
$form['account']['expirations']['role_' . $expiration->rid . '_granularity']['#suffix'] .= '</tr>';
$form['account']['expirations']['role_' . $expiration->rid . '_expiration'] = array(
'#type' => 'value',
'#value' => $expiration->expiration,
);
}
$form['account']['expirations']['table_end'] = array(
'#type' => 'markup',
'#value' => '</tbody></table><br />',
);
}
else {
//There are no expirations to administer
$form['account']['expirations']['no_expirations'] = array(
'#type' => 'markup',
'#value' => '<table><thead><th>' . t('Role') . '</th><th>' . t('Expiration') . '</th><th>' . t('Operation') . '</th></thead><tbody><tr><td colspan="3">' . t('There are no pending expirations for roles this user.') . '</td></tr></tbody></table><br />',
);
}
//Option to allow temporary roles
if (!empty($role_choices)) {
$form['account']['expirations']['new_role'] = array(
'#type' => 'checkbox',
'#title' => t('Add role'),
) + $div;
$form['account']['expirations']['new_role_add'] = array(
'#type' => 'select',
'#default_value' => variable_get('uc_roles_default_role', NULL),
'#options' => $role_choices,
) + $div;
$form['account']['expirations']['new_role_add']['#suffix'] = t(' for ') . $form['account']['expirations']['new_role_add']['#suffix'];
$form['account']['expirations']['new_role_add_qty'] = $qty_widget;
$form['account']['expirations']['new_role_add_granularity'] = $granularity_widget;
if (($default_granularity = variable_get('uc_roles_default_granularity', 'never')) != 'never') {
$form['account']['expirations']['new_role_add_qty'] = $form['account']['expirations']['new_role_add_qty'] + array(
'#default_value' => variable_get('uc_roles_default_length', NULL),
);
$form['account']['expirations']['new_role_add_granularity'] = $form['account']['expirations']['new_role_add_granularity'] + array(
'#default_value' => $default_granularity,
);
}
}
}
return $form;
case 'submit':
//If a new temporary role is added
if ($edit['new_role'] && $category == 'account') {
db_query("INSERT INTO {uc_roles_expirations} (uid, rid, expiration) VALUES (%d, %d, %d)", $account->uid, $edit['new_role_add'], _get_expiration_date($edit['new_role_add_qty'], $edit['new_role_add_granularity']));
$edit['roles'][$edit['new_role_add']] = _get_role_name($edit['new_role_add']);
$edit['new_role'] = $edit['new_role_add'] = $edit['new_role_add_qty'] = $edit['new_role_add_granularity'] = NULL;
}
//If the expiration has been adjusted
foreach ($edit as $key => $value) {
if (preg_match("/role_(\\d+)_qty/", $key, $matches) != 0 && !empty($value)) {
$rid = $matches[1];
$qty = $edit['role_' . $rid . '_polarity'] == 'add' ? $edit['role_' . $rid . '_qty'] : -1 * $edit['role_' . $rid . '_qty'];
_role_action('renew', $account, $rid, _get_expiration_date($qty, $edit['role_' . $rid . '_granularity'], $edit['role_' . $rid . '_expiration']));
}
}
//If a user's role is removed, so is the expiration
if (!is_null($edit['roles']) && $category == 'account') {
foreach ($account->roles as $rid => $role) {
if (!in_array($rid, array_keys($edit['roles'])) && $rid != DRUPAL_AUTHENTICATED_RID) {
_role_action('revoke', $account, $rid);
}
}
}
break;
case 'validate':
//Validate the amount of time for the expiration
if ($edit['new_role'] && $category == 'account') {
if (intval($edit['new_role_add_qty']) < 1) {
form_set_error('new_role_add_qty', t('The expiration length must be a positive integer'));
}
}
//Validate adjusted expirations
foreach ($edit as $key => $value) {
if (preg_match("/role_(\\d+)_qty/", $key, $matches) != 0 && !empty($value)) {
$rid = $matches[1];
$qty = $edit['role_' . $rid . '_polarity'] == 'add' ? $edit['role_' . $rid . '_qty'] : -1 * $edit['role_' . $rid . '_qty'];
$new_expiration = _get_expiration_date($qty, $edit['role_' . $rid . '_granularity'], $edit['role_' . $rid . '_expiration']);
if (time() > $new_expiration) {
form_set_error('role_' . $rid . '_qty', t("The new expiration date, %date, has already occurred.", array(
'%date' => format_date($new_expiration, 'small'),
)));
}
}
}
break;
case 'view':
//Display role expirations
$show_expirations = variable_get('uc_roles_default_show_expiration', TRUE);
if ((user_access('view all role expirations') || $user->uid == $account->uid && $show_expirations) && $user->uid) {
$user_expirations = db_query("SELECT * FROM {uc_roles_expirations} WHERE uid = %d", $account->uid);
$items = array();
while ($expiration = db_fetch_object($user_expirations)) {
$substitution = array(
'!role_name' => _get_role_name($expiration->rid),
'!date' => format_date($expiration->expiration, 'small'),
);
$items[$expiration->rid . '_expiration'] = array(
'title' => strtr(variable_get('uc_roles_default_expiration_title', uc_get_message('uc_roles_user_expiration_title')), $substitution),
'value' => strtr(variable_get('uc_roles_default_expiration_message', uc_get_message('uc_roles_user_expiration_message')), $substitution),
'class' => 'member',
);
}
return array(
variable_get('uc_roles_default_expiration_header', uc_get_message('uc_roles_user_expiration_header')) => $items,
);
}
break;
default:
break;
}
}