You are here

public function AdvbanAdmin::submitForm in Advanced ban 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/AdvbanAdmin.php, line 269

Class

AdvbanAdmin
Displays banned IP addresses.

Namespace

Drupal\advban\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $ip = trim($form_state
    ->getValue('ip'));
  $ip_end = trim($form_state
    ->getValue('ip_end'));
  $expiry_duration_index = $form_state
    ->getValue('expiry_duration');
  $expiry_date = NULL;
  if ($expiry_duration_index != AdvbanHelper::ADVBAN_NEVER) {
    $expiry_duration = $this->ipManager
      ->expiryDurations($expiry_duration_index);
    $expiry_date = strtotime($expiry_duration);
  }
  else {
    $expiry_duration = AdvbanHelper::ADVBAN_NEVER;
  }

  // Store last expiry duration value.
  if ($this
    ->config('advban.settings')
    ->get('save_last_expiry_duration')) {
    $default_expiry_duration = $this
      ->config('advban.settings')
      ->get('default_expiry_duration');
    if (!$default_expiry_duration) {
      $default_expiry_duration = AdvbanHelper::ADVBAN_NEVER;
    }
    if ($expiry_duration != $default_expiry_duration) {
      $this->config
        ->getEditable('advban.settings')
        ->set('default_expiry_duration', $expiry_duration)
        ->save();
      $this
        ->messenger()
        ->addMessage($this
        ->t('Save new expiry duration: %expiry_duration', [
        '%expiry_duration' => $expiry_duration,
      ]));
    }
  }
  $this->ipManager
    ->banIp($ip, $ip_end, $expiry_date);
  $formatIp = $this->ipManager
    ->formatIp($ip, $ip_end);
  $this
    ->messenger()
    ->addMessage(!$expiry_date ? $this
    ->t('The IP address(es) %ip has been banned.', [
    '%ip' => $formatIp,
  ]) : $this
    ->t('The IP address(es) %ip has been banned until %expiry', [
    '%ip' => $formatIp,
    '%expiry' => $this->dateFormatter
      ->format($expiry_date),
  ]));
  $form_state
    ->setRedirect('advban.admin_page');
}