View source
<?php
function regcode_voucher_menu() {
$items = array();
$items['admin/user/regcodes/voucher'] = array(
'title' => 'Voucher',
'description' => 'Settings for using registration codes after registration',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'regcode_voucher_admin',
),
'access arguments' => array(
'administer registration codes',
),
'weight' => 20,
);
$items['user/%user/regcode'] = array(
'title' => 'Voucher',
'description' => 'Enter a voucher code',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'regcode_voucher',
),
'access callback' => '_regcode_voucher_accesscheck',
'access arguments' => array(
1,
'tab',
),
'weight' => 20,
);
return $items;
}
function regcode_voucher_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'form':
$form = array();
if (_regcode_voucher_accesscheck($account, 'editform')) {
$form['regcode'] = array(
'#type' => 'fieldset',
'#title' => check_plain(variable_get('regcode_voucher_fieldset_title', t('Registration Code'))),
);
$form['regcode']['regcode_code'] = array(
'#type' => 'textfield',
'#title' => check_plain(variable_get('regcode_voucher_field_title', t('Registration Code'))),
'#description' => check_plain(variable_get('regcode_voucher_field_description', t('Please enter your registration code.'))),
'#required' => FALSE,
);
}
return $form;
break;
case 'update':
if (!empty($edit['regcode_code'])) {
regcode_user('insert', $edit, $account);
drupal_set_message(check_plain(variable_get('regcode_voucher_message', 'Voucher code used successfully.')));
}
break;
}
}
function regcode_voucher() {
$form = array();
$form['regcode_introtext'] = array(
'#type' => 'markup',
'#value' => variable_get('regcode_voucher_introtext', ''),
);
$form['regcode_code'] = array(
'#type' => 'textfield',
'#title' => check_plain(variable_get('regcode_voucher_field_title', t('Registration Code'))),
'#description' => check_plain(variable_get('regcode_voucher_field_description', t('Please enter your registration code.'))),
'#required' => FALSE,
);
$form['regcode_submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function regcode_voucher_admin() {
$form = array();
$form['regcode_voucher_fieldset_title'] = array(
'#type' => 'textfield',
'#title' => t('Field set title'),
'#description' => t('The title of the voucher code fieldset'),
'#default_value' => variable_get('regcode_voucher_fieldset_title', variable_get('regcode_fieldset_title', '')),
);
$form['regcode_voucher_field_title'] = array(
'#type' => 'textfield',
'#title' => t('Field label'),
'#description' => t('The label of the voucher code textfield.'),
'#default_value' => variable_get('regcode_voucher_field_title', variable_get('regcode_field_title', '')),
);
$form['regcode_voucher_field_description'] = array(
'#type' => 'textfield',
'#title' => t('Field description'),
'#description' => t('The description under the voucher code textfield.'),
'#default_value' => variable_get('regcode_voucher_field_description', variable_get('regcode_field_description', '')),
);
$form['regcode_voucher_display'] = array(
'#type' => 'checkboxes',
'#title' => t('Voucher form'),
'#options' => array(
'editform' => 'Display on user edit page',
'tab' => 'Create tab in user profile',
),
'#default_value' => variable_get('regcode_voucher_display', array()),
);
$form['regcode_voucher_introtext'] = array(
'#type' => 'textarea',
'#title' => t('Voucher page text'),
'#description' => t('Text to be displayed on the voucher page'),
'#default_value' => variable_get('regcode_voucher_introtext', ''),
);
$form['regcode_voucher_message'] = array(
'#type' => 'textfield',
'#title' => t('Voucher used message'),
'#description' => t('Text to be displayed when the voucher was used successfully.'),
'#default_value' => variable_get('regcode_voucher_message', t('Voucher code used successfully.')),
);
$form['regcode_voucher_allowed_roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Allow these roles'),
'#description' => t('Select roles which can enter registration codes post registration.'),
'#options' => user_roles(),
'#default_value' => variable_get('regcode_voucher_allowed_roles', array()),
);
$form['regcode_voucher_disallowed_roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Disallow these roles'),
'#description' => t('Select roles which can not enter registration codes post registration.'),
'#options' => user_roles(),
'#default_value' => variable_get('regcode_voucher_disallowed_roles', array()),
);
return system_settings_form($form);
}
function regcode_voucher_submit($form, $form_state) {
$edit = $form_state['values'];
$account = $GLOBALS['user'];
regcode_user('insert', $edit, $account);
drupal_set_message(check_plain(variable_get('regcode_voucher_message', 'Voucher code used successfully.')));
drupal_goto('user');
}
function regcode_voucher_validate($form, $form_state) {
$edit = $form_state['values'];
$account = $GLOBALS['user'];
regcode_user('validate', $edit, $account, 'account');
}
function _regcode_voucher_accesscheck($account, $context = '') {
$enabled = array_filter(variable_get('regcode_voucher_display', array()));
if (!in_array($context, $enabled)) {
return FALSE;
}
$allowed = array_filter(variable_get('regcode_voucher_allowed_roles', array()));
$disallowed = array_filter(variable_get('regcode_voucher_disallowed_roles', array()));
$access = FALSE;
foreach ($account->roles as $rid => $role) {
if (isset($allowed[$rid])) {
$access = TRUE;
}
}
foreach ($account->roles as $rid => $role) {
if (isset($disallowed[$rid])) {
$access = FALSE;
}
}
return $access;
}