You are here

function regcode_admin_create_submit in Registration codes 7

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

Process creation form.

File

./regcode.admin.php, line 239
Functions and pages needed for the admin UI of regcode module.

Code

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

  // Convert dates into timestamps.
  foreach (array(
    'begins',
    'expires',
  ) as $field) {
    $value = $form_state['values']['regcode_create_' . $field];
    $code->{$field} = NULL;
    if ($value['year'] != 0) {
      $code->{$field} = mktime(0, 0, 0, $value['month'], $value['day'], $value['year']);
    }
  }

  // Grab form values.
  $code->is_active = 1;
  $code->maxuses = $form_state['values']['regcode_create_maxuses'];
  $terms = $form_state['values']['regcode_create_tags'];

  // Start creating codes.
  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($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, $terms, REGCODE_MODE_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');
    }
  }
}