You are here

function regcode_admin_import_submit in Registration codes 6

Same name and namespace in other branches
  1. 5.3 regcode_admin.inc.php \regcode_admin_import_submit()

Handle the processing of a submitted import form

File

./regcode.admin.php, line 343
Functions and pages needed for the administration interface for the regcode module.

Code

function regcode_admin_import_submit($form, &$form_state) {
  module_load_include('regcode.api', 'regcode', 'php');

  // Set some variblaes
  $action = $form_state['values']['regcode_import_action'];
  $delimiter = $form_state['values']['regcode_import_delimiter'];
  $enclosure = $form_state['values']['regcode_import_enclosure'];
  $fields = explode(',', $form_state['values']['regcode_import_fieldorder']);
  $fields = array_map('trim', $fields);
  $rows = $success = 0;

  // Check for clean
  if ($action === 'clean') {
    regcode_clean_codes('truncate');
  }

  // Parse and save
  $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 (empty($code['is_active'])) {
      $code['is_active'] = 1;
    }
    if (empty($code['maxuses'])) {
      $code['maxuses'] = 1;
    }
    $status = regcode_save_code($code, $action);
    if ($status) {
      $success++;
    }
  }

  // Status
  drupal_set_message(t('Successfully inserted @rows rows out of @total', array(
    '@rows' => $success,
    '@total' => $rows,
  )));

  // Close
  fclose($fh);
}