regcode_admin.inc.php in Registration codes 5.3
regcode_admin.inc.php contains the top-level logic for the administration pages for the registration-code module
File
regcode_admin.inc.phpView source
<?php
/**
* @file
* regcode_admin.inc.php contains the top-level logic for the
* administration pages for the registration-code module
*/
// include the low-level functions
require_once 'regcode_api.inc.php';
// =================
// = backend admin =
// =================
/**
* Return the form associated with the module settings.
*
* @return
* The settings form.
*/
function regcode_admin_settings() {
$roles = user_roles();
$form['#cache'] = FALSE;
$form['regcode_optional'] = array(
'#type' => 'checkbox',
'#title' => t("Make registration code optional"),
'#default_value' => variable_get('regcode_optional', 0),
'#description' => t('If checked, users can register without a registration code') . '. ' . t('Alternatively, there is also a permission to allow certain roles (e.g. admins) to bypass´registration code entry for administratively added users.'),
);
$form['regcode_fieldtitle'] = array(
'#type' => 'textfield',
'#title' => t("Field title"),
'#description' => t('The title of the registration code textfield'),
'#default_value' => variable_get('regcode_fieldtitle', ''),
);
$form['regcode_fielddescription'] = array(
'#type' => 'textfield',
'#title' => t("Field description"),
'#description' => t('The description of the registration code textfield'),
'#default_value' => variable_get('regcode_fielddescription', ''),
);
$form['regcode_import'] = array(
'#type' => 'fieldset',
'#title' => t("CSV import"),
'#description' => t('Settings to use for code import.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['regcode_import']['regcode_import_action'] = array(
'#type' => 'select',
'#title' => t("Action"),
'#default_value' => variable_get('regcode_import_action', 'skip'),
'#options' => array(
'skip' => t('Skip'),
'overwrite' => t('Overwrite'),
'clean' => t('Clean'),
),
'#description' => t('Action to perform when importing the data.<br />
- <b>Skip</b>: Add new codes skipping those which already exists.<br />
- <b>Overwrite</b>: Add new codes overwriting those which already exists.<br />
- <b>Clean</b>: Erases *all* existing codes before importing new codes.<br />
'),
);
$form['regcode_import']['regcode_import_csv_delimiter'] = array(
'#type' => 'textfield',
'#title' => t("Field delimiter"),
'#description' => t('Character used as delimiter in csv import of registration codes'),
'#default_value' => variable_get('regcode_import_csv_delimiter', ','),
);
$form['regcode_import']['regcode_import_csv_text_enclosure'] = array(
'#type' => 'textfield',
'#title' => t("Text enclosure"),
'#default_value' => variable_get('regcode_import_csv_text_enclosure', '"'),
'#description' => t('Character used as enclosure around text content fields on CSV import.'),
);
$form['regcode_import']['regcode_import_csv_fieldorder'] = array(
'#type' => 'textfield',
'#title' => t("Field order"),
'#default_value' => variable_get('regcode_import_csv_fieldorder', 'code'),
'#description' => t('Order in which fields are expected in the CSV file (field names separated by delimiter set above).
<br />Leave BLANK to use a field header line in the CSV data.
<br />Example: "IGNORE,code,rid,info"
<br />Available field names:
<pre>
IGNORE - Ignore the data at this field position.
code - The code string itself.
available - "1" (available) or "0" (not available).
reuse - "1" (for many users) or "0" (only for one user once).
rid - Role-id to grant the user upon registration with this code.
expire - Date/timestamp to expire this code from being available (or leave empty).
revoke - Date/timestamp to revoke the registration/role granted by this code ( " ).
info - String field for free custom usage.
</pre>'),
);
regcode_form_add_codetemplate($form);
$form = system_settings_form($form);
return $form;
}
/**
* Return the code list page content with(in) the according filter form
*
* @return
* The settings form.
*/
function regcode_admin_list() {
// get usable filter fields and operands
$fields = regcode_get_fields(TRUE);
$operands = regcode_get_filter_operands();
// retrieve & check user input values
$order = db_escape_string($_REQUEST['order']);
// check_plain( $_REQUEST['sort'] ),
$sort = db_escape_string($_REQUEST['sort']);
$filter_field = db_escape_string($_REQUEST['filter_field']);
$filter_operand = db_escape_string($_REQUEST['filter_operand']);
$filter_data = db_escape_string($_REQUEST['filter_data']);
// set query options
$options = array(
'pager' => $pager_id = 1,
'limit' => $pager_max = 25,
);
if ($filter_field) {
$options['filter_field'] = $filter_field;
}
if ($filter_operand) {
$options['filter_operand'] = $filter_operand;
}
if ($filter_data) {
$options['filter_data'] = $filter_data;
}
if ($order) {
$options['order'] = regcode_get_field_key($order);
}
// $order;
if ($sort) {
$options['sort'] = $sort;
}
// get code list
$codes = regcode_get_codes($options);
if (empty($codes)) {
$codes = array();
}
// prepare table rows for display
foreach ($codes as $code) {
$code['u.name'] = l($code['u.name'], 'user/' . $code['c.uid']);
unset($code['c.uid']);
unset($code['c.rid']);
$code['c.created'] = $code['c.created'] ? format_date($code['c.created'], 'format_short') : '';
$code['c.used'] = $code['c.used'] ? format_date($code['c.used'], 'format_short') : '';
$code['c.expire'] = $code['c.expire'] ? format_date($code['c.expire'], 'format_short') : '';
$code['c.revoke'] = $code['c.revoke'] ? format_date($code['c.revoke'], 'format_short') : '';
$rows[]['data'] = $code;
}
// prepare table header
if (empty($rows[0]['data'])) {
$rows[0]['data'] = array();
}
foreach ($rows[0]['data'] as $key => $value) {
$header[] = array(
'data' => t($fields[$key]),
'field' => $key,
);
}
// theme table and pager
if (count($rows)) {
$output .= theme('table', $header, $rows);
}
$output .= t('!numrows records', array(
'!numrows' => $GLOBALS['pager_total_items'][$pager_id],
));
$output .= theme('pager', NULL, $pager_max, $pager_id);
// declare filter form elements
$filter_fields = regcode_get_fields(TRUE, TRUE);
asort($filter_fields);
$form['#method'] = 'get';
$form['filter_field'] = array(
'#type' => 'select',
'#title' => t("Show entries where"),
'#default_value' => $filter_field,
'#options' => $filter_fields,
);
$form['filter_operand'] = array(
'#type' => 'select',
'#default_value' => $filter_operand,
'#options' => regcode_get_filter_operands(),
);
$form['filter_data'] = array(
'#type' => 'textfield',
'#default_value' => $filter_data,
);
$form['filter_submt'] = array(
'#type' => 'submit',
'#title' => t('Filter'),
'#value' => 'submit',
);
$form['order'] = array(
'#type' => 'hidden',
'#value' => $order,
);
$form['sort'] = array(
'#type' => 'hidden',
'#value' => $sort,
);
$form['list'] = array(
'#value' => $output,
);
return $form;
}
/**
* Return the form associated with the registration code import admin page
*
* @return
* The import form array
*/
function regcode_admin_import() {
$form['#cache'] = FALSE;
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['regcode_import_action'] = array(
'#type' => 'select',
'#title' => t("Action"),
'#default_value' => variable_get('regcode_import_action', 'skip'),
'#options' => array(
'skip' => t('Skip'),
'overwrite' => t('Overwrite'),
'clean' => t('Clean'),
),
'#description' => t('Action to perform when importing the data.<br />
- <b>Skip</b>: Add new codes skipping those which already exists.<br />
- <b>Overwrite</b>: Add new codes overwriting those which already exists.<br />
- <b>Clean</b>: Erases *all* existing codes before importing new codes.<br />
'),
);
if ($form['regcode_import_action']['#default_value'] == 'clean') {
$form['regcode_import_action_check'] = array(
'#type' => 'checkbox',
'#title' => t("Really CLEAN (wipe out) all existing codes with the selected import action?"),
'#default_value' => 0,
'#required' => TRUE,
'#validate' => array(
'regcode_import_action_check_validation' => array(),
),
);
}
$form['regcode_import_file'] = array(
'#type' => 'file',
'#title' => t("File"),
'#description' => t('File upload with data to add/import (plaintext list or CSV format)'),
);
$form['regcode_import_text'] = array(
'#type' => 'textarea',
'#cols' => 80,
'#rows' => 32,
'#title' => t("Data"),
'#description' => t('Direct text input with data to add/import (plaintext list or CSV format)'),
);
regcode_form_add_codetemplate(&$form);
$form['submit'] = array(
'#type' => 'submit',
'#title' => t('Import'),
'#value' => 'submit',
);
return $form;
}
/**
* Handle the processing of a submitted import form
*
*/
function regcode_admin_import_submit($form_id, $form_values) {
// assemble code template
foreach (regcode_get_fields(FALSE, TRUE) as $field_name => $field_title) {
$field_name = str_replace('c.', '', $field_name);
$field_input = check_plain(trim($form_values['regcode_template_' . $field_name]));
if (trim($field_input) != '') {
$code_template[$field_name] = $field_input;
}
}
// determine whether to import from file ...
$import_file = file_check_upload('regcode_import_file');
if (is_object($import_file)) {
$result = regcode_import_file($import_file->filepath, $form_values['regcode_import_action'], $code_template);
unlink($import_file->filepath);
}
else {
$result = regcode_import_text($form_values['regcode_import_text'], $form_values['regcode_import_action'], $code_template);
}
if ($result) {
watchdog('RegistrationCode', t('Registration Code Import successful.'));
}
else {
drupal_set_message(t('Registration Code Import FAILED.'));
watchdog('RegistrationCode', t('Registration Code Import FAILED.'));
}
}
/**
* Validates the regcode_import_action_check checkbox
*
* @param element
* The element of the checkbox
*/
function regcode_import_action_check_validation($element) {
if (empty($element['#value'])) {
form_error($element, t('!title field is required.', array(
'!title' => filter_xss_admin($element['#title']),
)));
}
return;
}
/**
* Add the fieldset for code template to a given form
*
* @param form
* The reference to the form to add the code-template-fieldset to
*/
function regcode_form_add_codetemplate(&$form) {
$form['regcode_template'] = array(
'#type' => 'fieldset',
'#title' => t('Code template'),
'#description' => t('Default field data to use as template for every imported code, if not given in import data.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$fields = regcode_get_fields(FALSE, TRUE);
unset($fields['c.code']);
foreach ($fields as $field_name => $field_title) {
$field_name = str_replace('c.', '', $field_name);
$form['regcode_template']['regcode_template_' . $field_name] = array(
'#type' => 'textfield',
'#title' => $field_title,
'#default_value' => variable_get('regcode_template_' . $field_name, ''),
);
}
$form['regcode_template']['regcode_template_rid']['#type'] = 'select';
$form['regcode_template']['regcode_template_rid']['#options'] = user_roles();
$form['regcode_template']['regcode_template_rid']['#options'][''] = '-';
}
Functions
Name | Description |
---|---|
regcode_admin_import | Return the form associated with the registration code import admin page |
regcode_admin_import_submit | Handle the processing of a submitted import form |
regcode_admin_list | Return the code list page content with(in) the according filter form |
regcode_admin_settings | Return the form associated with the module settings. |
regcode_form_add_codetemplate | Add the fieldset for code template to a given form |
regcode_import_action_check_validation | Validates the regcode_import_action_check checkbox |