You are here

function regcode_ie_admin_import_submit in Registration codes 7

Same name and namespace in other branches
  1. 6.2 regcode_ie/regcode_ie.module \regcode_ie_admin_import_submit()
  2. 7.2 regcode_ie/regcode_ie.module \regcode_ie_admin_import_submit()

Process import form.

File

regcode_ie/regcode_ie.module, line 150
Import and export functionality for regcode module.

Code

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);
}