View source
<?php
function regcode_roles_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/user/regcodes/roles':
$output = '<p>' . t('Automate role assignment based on regcode. Create a list of rules which are applied when users register.') . '</p>';
break;
}
return $output;
}
function regcode_roles_menu() {
$items = array();
$items['admin/user/regcodes/roles'] = array(
'title' => t('Roles'),
'description' => t('Automate role assignment'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'regcode_roles_admin',
),
'type' => MENU_LOCAL_TASK,
'access arguments' => array(
'administer registration codes',
),
'weight' => 100,
);
$items['admin/user/regcodes/roles/delete/%'] = array(
'page callback' => 'regcode_roles_delete_rule_confirm',
'page arguments' => array(
5,
),
'type' => MENU_CALLBACK,
'access arguments' => array(
'administer registration codes',
),
);
return $items;
}
function regcode_admin_delete_confirm($rid) {
if (empty($_GET['token']) || !drupal_valid_token($_GET['token'], $rid)) {
return MENU_ACCESS_DENIED;
}
regcode_roles_delete_rule($rid);
drupal_set_message(t('Rule was deleted (Rule #@rule)', array(
'@rule' => $rid,
)));
drupal_goto('admin/user/regcodes/roles');
}
function regcode_roles_delete_rule($rid) {
db_query('DELETE FROM {regcode_roles} WHERE id = %d', $rid);
}
function regcode_roles_admin() {
$form = array();
$form['regcode_roles']['new'] = array(
'#type' => 'fieldset',
'#title' => 'Create a new rule',
);
$form['regcode_roles']['new']['role'] = array(
'#type' => 'select',
'#title' => 'Assign role',
'#options' => user_roles(),
);
$form['regcode_roles']['new']['category'] = array(
'#type' => 'select',
'#title' => 'When a user uses a regcode in category',
'#options' => regcode_get_categories(),
);
if (module_exists('role_expire')) {
$form['regcode_roles']['new']['expiry_duration'] = array(
'#type' => 'textfield',
'#title' => 'Which expires in (number of days)',
);
$form['regcode_roles']['new']['expiry_date'] = array(
'#type' => 'textfield',
'#title' => 'Or expires on (fixed date)',
);
}
$form['regcode_roles']['new']['submit'] = array(
'#type' => 'submit',
'#value' => 'Create rule',
);
$form['regcode_roles']['list'] = array(
'#type' => 'markup',
'#value' => regcode_roles_get_list_markup(),
);
return $form;
}
function regcode_roles_get_list_markup() {
$headings = array(
t('ID'),
t('Category'),
t('Role ID'),
t('Role'),
);
if (module_exists('role_expire')) {
$headings[] = t('Expire date');
$headings[] = t('Expire duration');
}
$headings[] = t('Actions');
$rows = regcode_roles_get_rules();
foreach ($rows as &$row) {
$row = array_map('check_plain', $row);
if ($row['expire_date']) {
$row['expire_date'] = format_date($row['expire_date'], 'small');
}
$row['action'] = l('Remove', 'admin/user/regcodes/roles/delete/' . $row['id'], array(
'query' => array(
'token' => drupal_get_token($row['id']),
),
));
}
return theme('table', $headings, $rows);
}
function regcode_roles_admin_validate($form, $form_state) {
if (module_exists('role_expire')) {
if (!empty($form_state['values']['expiry_duration'])) {
if ((int) $form_state['values']['expiry_duration'] < 1) {
form_set_error('expiry_duration', t('Invalid duration provided, duration must be greater than or equal to 1 day.'));
}
}
if (!empty($form_state['values']['expiry_date'])) {
if (!($expire_date = strtotime($form_state['values']['expiry_date']))) {
form_set_error('expiry_date', t('Invalid date provided, please provide a date in a format strtotime can handle.'));
}
elseif ($expire_date < time()) {
form_set_error('expiry_date', t('Expiry date must be in the future.'));
}
}
}
}
function regcode_roles_admin_submit($form, $form_state) {
$role = $form_state['values']['role'];
$category = $form_state['values']['category'];
$expiry_date = NULL;
$expiry_duration = NULL;
if (module_exists('regcode')) {
if (!empty($form_state['values']['expiry_date'])) {
$expiry_date = strtotime($form_state['values']['expiry_date']);
}
elseif (!empty($form_state['values']['expiry_duration'])) {
$expiry_duration = (int) $form_state['values']['expiry_duration'];
}
}
regcode_roles_create_rule($category, $role, $expiry_date, $expiry_duration);
drupal_set_message(t('New registration code rule created'));
}
function regcode_roles_get_rules($category = '') {
$query = 'SELECT id, category, regcode_roles.role AS rid, role.name AS role, expire_date, expire_duration
FROM {regcode_roles} AS regcode_roles LEFT JOIN {role} AS role
ON (regcode_roles.role = role.rid)';
if (empty($category)) {
$res = db_query($query);
}
else {
$res = db_query($query . " WHERE category='%s'", $category);
}
$rules = array();
while ($row = db_fetch_array($res)) {
$rules[] = $row;
}
return $rules;
}
function regcode_roles_create_rule($category, $role, $expire_date, $expire_duration) {
$expire_date = !$expire_date ? 'NULL' : (int) $expire_date;
$expire_duration = !$expire_duration ? 'NULL' : (int) $expire_duration;
db_query("INSERT INTO {regcode_roles} (category, role, expire_date, expire_duration) VALUES ('%s', %d, %s, %s)", $category, $role, $expire_date, $expire_duration);
}
function regcode_roles_regcode_used(&$edit, &$account, $regcode) {
if (!is_array($regcode)) {
return;
}
$rules = regcode_roles_get_rules($regcode['category']);
if (count($rules)) {
if (!is_array($edit['roles'])) {
$edit['roles'] = array();
}
foreach ($rules as $rule) {
$edit['roles'][$rule['rid']] = $rule['role'];
}
}
if (module_exists('role_expire')) {
foreach ($rules as $rule) {
if ($rule['expire_date']) {
$expiry_timestamp = $rule['expire_date'];
}
elseif ($rule['expire_duration']) {
$expiry_timestamp = time() + $rule['expire_duration'] * 60 * 60 * 24;
}
role_expire_write_record($account->uid, $rule['rid'], $expiry_timestamp);
}
}
}