You are here

public function HostMultipleDeleteConfirm::submitForm in http:BL 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/HostMultipleDeleteConfirm.php, line 166

Class

HostMultipleDeleteConfirm
Provides a multiple host deletion confirmation form.

Namespace

Drupal\httpbl\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getValue('confirm') && !empty($this->hostInfo)) {
    $total_count = 0;
    $unban_hosts = [];
    $delete_hosts = [];

    /** @var \Drupal\httpbl\HostInterface[] $hosts */
    $hosts = $this->storage
      ->loadMultiple(array_keys($this->hostInfo));
    foreach ($this->hostInfo as $id => $host_ips) {
      foreach ($host_ips as $host_ip) {
        $host = $hosts[$id];
        if ($this->banManager
          ->isBanned($host_ip)) {
          $unban_hosts[$id] = $host;
          $delete_hosts[$id] = $host;
        }
        elseif (!isset($unban_hosts[$id])) {
          $delete_hosts[$id] = $host;
        }
      }
    }
    if ($unban_hosts) {
      foreach ($unban_hosts as $unban_host) {
        $this->banManager
          ->unbanIp($unban_host
          ->getHostIp());
      }
      $this->logTrapper
        ->trapNotice('Unbanned @count hosts.', array(
        '@count' => count($unban_hosts),
      ));
      $unBanned_count = count($unban_hosts);
      drupal_set_message($this
        ->formatPlural($unBanned_count, 'Un-banned 1 host.', 'Un-banned @count hosts.'));
    }
    if ($delete_hosts) {

      // Delete directly through storage by sending the array of work for it to do.
      $this->storage
        ->delete($delete_hosts);
      $delete_count = count($delete_hosts);
      $this->logTrapper
        ->trapNotice('Deleted @count hosts.', array(
        '@count' => $delete_count,
      ));
      drupal_set_message($this
        ->formatPlural($delete_count, 'Deleted 1 host.', 'Deleted @count hosts.'));
    }
    $this->tempStoreFactory
      ->get('host_multiple_delete_confirm')
      ->delete(\Drupal::currentUser()
      ->id());
  }
  $form_state
    ->setRedirect('entity.host.collection');
}