You are here

function regcode_admin_create_submit in Registration codes 6

Same name and namespace in other branches
  1. 6.2 regcode.admin.php \regcode_admin_create_submit()
  2. 7.2 regcode.admin.inc \regcode_admin_create_submit()
  3. 7 regcode.admin.php \regcode_admin_create_submit()

Submit handler for regcode_admin_create

File

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

Code

function regcode_admin_create_submit($form, &$form_state) {
  $code = array();

  // Convert dates into timestamps
  if (!empty($form_state['values']['regcode_create_begins'])) {
    $code['begins'] = strtotime($form_state['values']['regcode_create_begins']);
  }
  if (!empty($form_state['values']['regcode_create_expires'])) {
    $code['expires'] = strtotime($form_state['values']['regcode_create_expires']);
  }

  // Grab form values
  $code['is_active'] = 1;
  $code['category'] = $form_state['values']['regcode_create_category'];
  $code['maxuses'] = $form_state['values']['regcode_create_maxuses'];

  // Start creating codes
  module_load_include('regcode.api', 'regcode', 'php');
  for ($i = 0; $i < (int) $form_state['values']['regcode_create_number']; $i++) {
    $code['code'] = $form_state['values']['regcode_create_code'];

    // Generate a code
    if (empty($code['code']) || $form_state['values']['regcode_create_number'] > 1) {
      $gen = regcode_generate_code($form_state['values']['regcode_create_length'], $form_state['values']['regcode_create_format'], $form_state['values']['regcode_create_case']);
      $code['code'] .= $gen;
    }

    // Save code
    if (regcode_save_code($code, 'skip')) {
      drupal_set_message(t('Created registration code (%code)', array(
        '%code' => $code['code'],
      )));
    }
    else {
      drupal_set_message(t('Unable to create code (%code) as code already exists', array(
        '%code' => $code['code'],
      )), 'warning');
    }
  }
}