regcode_voucher.module in Registration codes 7
Same filename and directory in other branches
Main code and hooks for regcode voucher module.
File
regcode_voucher/regcode_voucher.moduleView source
<?php
/**
* @file
* Main code and hooks for regcode voucher module.
*/
/**
* Implements hook_menu().
*/
function regcode_voucher_menu() {
$items = array();
$items['admin/config/people/regcode/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_form',
),
'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_profiletab_form',
),
'access callback' => '_regcode_voucher_accesscheck',
'access arguments' => array(
1,
'tab',
),
'weight' => 20,
);
return $items;
}
/**
* Implements hook_form_FORM_ID_alter().
*
* This adds an extra formset to the user edit page which allows the user to
* save a code when editing their profile page.
*/
function regcode_voucher_form_user_profile_form_alter(&$form, $form_state) {
$account = $GLOBALS['user'];
if (!_regcode_voucher_accesscheck($account, 'editform')) {
return;
}
$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' => 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,
'#element_validate' => array(
'regcode_code_element_validate',
),
);
}
/**
* Implements hook_user_update().
*/
function regcode_voucher_user_update(&$edit, $account, $category) {
if (empty($edit['regcode_code'])) {
return;
}
$code = regcode_code_consume($edit['regcode_code'], $account->uid);
if (is_object($code)) {
drupal_set_message(check_plain(variable_get('regcode_voucher_message', 'Voucher code used successfully.')));
}
}
/**
* User code entry page.
*/
function regcode_voucher_profiletab_form() {
$form = array();
$form['regcode_introtext'] = array(
'#markup' => filter_xss_admin(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;
}
/**
* Administration page.
*/
function regcode_voucher_admin_form($form, &$form_state) {
$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' => t('Display on user edit page'),
'tab' => t('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);
}
/**
* Submit handler for regcode_voucher_profiletab_form().
*/
function regcode_voucher_profiletab_form_submit($form, $form_state) {
$account = user_load(arg(1));
$code = regcode_code_consume($form_state['values']['regcode_code'], $account->uid);
if (is_object($code)) {
drupal_set_message(check_plain(variable_get('regcode_voucher_message', t('Voucher code used successfully.'))));
}
drupal_goto('user/' . $account->uid);
}
/**
* Validate handler for regcode_voucher_admin_form().
*/
function regcode_voucher_profiletab_form_validate($form, $form_state) {
$code = regcode_code_validate($form_state['values']['regcode_code']);
if (!is_object($code)) {
form_error($form['regcode_code'], regcode_errormsg($code));
watchdog('regcode', 'User entered invalid registration code (@code)', array(
'@code' => $form_state['values']['regcode_code'],
), WATCHDOG_WARNING);
}
}
/**
* Check whether an account has access to the voucher system.
*/
function _regcode_voucher_accesscheck($account, $context = '') {
// Do not show for other users.
global $user;
if ($account->uid != $user->uid) {
return FALSE;
}
// Pages enabled.
$enabled = array_filter(variable_get('regcode_voucher_display', array()));
if (!in_array($context, $enabled)) {
return FALSE;
}
// Role based access.
$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;
}
Functions
Name | Description |
---|---|
regcode_voucher_admin_form | Administration page. |
regcode_voucher_form_user_profile_form_alter | Implements hook_form_FORM_ID_alter(). |
regcode_voucher_menu | Implements hook_menu(). |
regcode_voucher_profiletab_form | User code entry page. |
regcode_voucher_profiletab_form_submit | Submit handler for regcode_voucher_profiletab_form(). |
regcode_voucher_profiletab_form_validate | Validate handler for regcode_voucher_admin_form(). |
regcode_voucher_user_update | Implements hook_user_update(). |
_regcode_voucher_accesscheck | Check whether an account has access to the voucher system. |