regcode_ie.module in Registration codes 7
Same filename and directory in other branches
Import and export functionality for regcode module.
File
regcode_ie/regcode_ie.moduleView source
<?php
/**
* @file
* Import and export functionality for regcode module.
*/
/**
* Implements hook_menu().
*/
function regcode_ie_menu() {
$items = array();
$items['admin/config/people/regcode/import'] = array(
'title' => 'Import',
'description' => 'Import registration codes',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'regcode_ie_admin_import',
),
'access arguments' => array(
'administer registration codes',
),
'type' => MENU_LOCAL_TASK,
'weight' => 40,
);
return $items;
}
/**
* Implements hook_views_api().
*/
function regcode_ie_views_api() {
return array(
'api' => 2.0,
);
}
/**
* Import registration codes form.
*/
function regcode_ie_admin_import($form, $form_state) {
// Build the descriptions.
$action_text = '<p>' . t('Action to perform when importing the data.') . '</p>';
$action_text .= '<ul><li>' . t('<strong>Skip</strong>: Add new codes skipping those which already exists.') . '</li>';
$action_text .= '<li>' . t('<strong>Overwrite</strong>: Add new codes overwriting those which already exist.') . '</li></ul>';
$fieldorder_text = '<p>' . t('Comma separated list mapping regcode fields to CSV fields, e.g. "IGNORE, code, rid, info".') . '</p>';
$fieldorder_text .= '<p>' . t('If you leave this field blank, the titles from the first row will be used.') . '</p>';
// Add all of the available fields.
$fieldorder_text .= '<ul>';
$fields = regcode_get_fields();
foreach ($fields as $key => $field) {
$fieldorder_text .= sprintf('<li><strong>%s</strong>: %s</li>', $key, $field['description']);
}
$fieldorder_text .= '</ul>';
// Build the form.
$form['#attributes']['enctype'] = 'multipart/form-data';
$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_tags'] = array(
'#type' => 'checkboxes',
'#title' => t('Tags'),
'#options' => regcode_get_vocab_terms(),
'#description' => t('You may assign tags to the imported codes.'),
'#default_value' => isset($form_state['values']['regcode_import_tags']) ? $form_state['values']['regcode_import_tags'] : array(),
);
$form['regcode_import_action'] = array(
'#type' => 'select',
'#title' => t('Action'),
'#options' => array(
REGCODE_MODE_SKIP => t('Skip'),
REGCODE_MODE_REPLACE => t('Overwrite'),
),
'#required' => TRUE,
'#description' => $action_text,
'#default_value' => isset($form_state['values']['regcode_import_action']) ? $form_state['values']['regcode_import_action'] : '',
);
$form['regcode_import_delimiter'] = array(
'#type' => 'textfield',
'#size' => 5,
'#title' => t("Field delimiter"),
'#required' => TRUE,
'#description' => t('Character used as delimiter in csv import of registration codes.'),
'#default_value' => variable_get('regcode_ie_delimiter', ','),
);
$form['regcode_import_enclosure'] = array(
'#type' => 'textfield',
'#size' => 5,
'#title' => t("Text enclosure"),
'#default_value' => variable_get('regcode_ie_enclosure', '"'),
'#required' => TRUE,
'#description' => t('Character used as enclosure around text content fields on CSV import.'),
);
$form['regcode_import_fieldorder'] = array(
'#type' => 'textfield',
'#title' => t("Field order"),
'#default_value' => variable_get('regcode_ie_fieldorder', 'code'),
'#required' => TRUE,
'#description' => $fieldorder_text,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Import'),
);
return $form;
}
/**
* Validate import form.
*/
function regcode_ie_admin_import_validate($form, &$form_state) {
variable_set('regcode_ie_delimiter', $form_state['values']['regcode_import_delimiter']);
variable_set('regcode_ie_enclosure', $form_state['values']['regcode_import_enclosure']);
variable_set('regcode_ie_fieldorder', $form_state['values']['regcode_import_fieldorder']);
// Validators for file.
$validators = array(
'file_validate_extensions' => array(
'csv',
'txt',
),
);
// Save the file.
if ($file = file_save_upload('regcode_import_file', $validators)) {
$form_state['values']['fileobj'] = $file;
drupal_set_message(t('File successfully saved'));
}
else {
form_set_error('regcode_import_file', t('Unable to parse CSV file'));
return FALSE;
}
}
/**
* Process import form.
*/
function regcode_ie_admin_import_submit($form, &$form_state) {
// Set some variables.
$action = $form_state['values']['regcode_import_action'];
$delimiter = $form_state['values']['regcode_import_delimiter'];
$enclosure = $form_state['values']['regcode_import_enclosure'];
$terms = $form_state['values']['regcode_import_tags'];
$fields = explode(',', $form_state['values']['regcode_import_fieldorder']);
$fields = array_map('trim', $fields);
$rows = $success = 0;
// Parse and save.
ini_set('auto_detect_line_endings', 1);
$fh = fopen($form_state['values']['fileobj']->destination, 'r');
while ($row = fgetcsv($fh, 1000, $delimiter, $enclosure)) {
$rows++;
$row = array_slice($row, 0, count($fields));
$code = array_combine($fields, $row);
// Check dates.
if (!empty($code['begins'])) {
$code['begins'] = strtotime($code['begins']);
if ($code['begins'] < 1) {
continue;
}
}
if (!empty($code['expires'])) {
$code['expires'] = strtotime($code['expires']);
if ($code['expires'] < 1) {
continue;
}
}
// Defaults.
if (!isset($code['is_active'])) {
$code['is_active'] = 1;
}
if (empty($code['maxuses'])) {
$code['maxuses'] = 1;
}
$status = regcode_save((object) $code, $terms, $action);
if ($status) {
$success++;
}
}
// Status.
drupal_set_message(t('Successfully inserted @rows rows out of @total', array(
'@rows' => $success,
'@total' => $rows,
)));
// Close.
fclose($fh);
}
Functions
Name | Description |
---|---|
regcode_ie_admin_import | Import registration codes form. |
regcode_ie_admin_import_submit | Process import form. |
regcode_ie_admin_import_validate | Validate import form. |
regcode_ie_menu | Implements hook_menu(). |
regcode_ie_views_api | Implements hook_views_api(). |