You are here

function regcode_save in Registration codes 8

Same name and namespace in other branches
  1. 6.2 regcode.module \regcode_save()
  2. 7.2 regcode.module \regcode_save()
  3. 7 regcode.module \regcode_save()

Saves code in the database and calls the regcode_presave hook.

Parameters

object $code: A code object (required fields are code, begins, expires, is_active, and maxuses.

int $action: Action to perform when saving the code.

Return value

bool The regcode ID if the code was saved. Otherwise FALSE.

1 call to regcode_save()
RegcodeAdminCreateForm::submitForm in src/Form/RegcodeAdminCreateForm.php
Form submission handler.

File

./regcode.module, line 300
Main functionality and hooks of regcode module.

Code

function regcode_save($code, $action = REGCODE_MODE_REPLACE) {

  // Sanity check.
  if (empty($code) || empty($code->code)) {
    return FALSE;
  }

  // Trigger the regcode_save hook.
  foreach (\Drupal::moduleHandler()
    ->getImplementations('regcode_presave') as $module) {
    $hook = $module . '_regcode_presave';
    $hook($code);
  }

  // Insert mode.
  if ($action == REGCODE_MODE_REPLACE) {
    \Drupal::database()
      ->delete('regcode')
      ->condition('code', $code->code)
      ->execute();
  }

  // Insert.
  $rid = \Drupal::database()
    ->insert('regcode')
    ->fields([
    'created' => \Drupal::time()
      ->getRequestTime(),
    'begins' => empty($code->begins) ? NULL : (int) $code->begins,
    'expires' => empty($code->expires) ? NULL : (int) $code->expires,
    'code' => Html::escape($code->code),
    'is_active' => isset($code->is_active) ? $code->is_active : 1,
    'maxuses' => isset($code->maxuses) ? (int) $code->maxuses : 1,
  ])
    ->execute();
  return $rid;
}