You are here

public function HostMultipleRefreshConfirm::buildForm in http:BL 8

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides ConfirmFormBase::buildForm

File

src/Form/HostMultipleRefreshConfirm.php, line 113

Class

HostMultipleRefreshConfirm
Provides a multiple host expiry refresh confirmation form.

Namespace

Drupal\httpbl\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $this->hostInfo = $this->tempStoreFactory
    ->get('host_multiple_refresh_confirm')
    ->get(\Drupal::currentUser()
    ->id());
  if (empty($this->hostInfo)) {
    return new RedirectResponse($this
      ->getCancelUrl()
      ->setAbsolute()
      ->toString());
  }

  /** @var \Drupal\httpbl\HostInterface[] $hosts */
  $hosts = $this->storage
    ->loadMultiple(array_keys($this->hostInfo));

  // Build expiry values.
  $safe_offset = \Drupal::state()
    ->get('httpbl.safe_offset') ?: 10800;
  $safe_timestamp = \Drupal::time()
    ->getRequestTime() + $safe_offset;
  $safe_time_message = \Drupal::service('date.formatter')
    ->formatTimeDiffUntil($safe_timestamp);
  $grey_offset = \Drupal::state()
    ->get('httpbl.greylist_offset') ?: 86400;
  $grey_timestamp = \Drupal::time()
    ->getRequestTime() + $grey_offset;
  $grey_time_message = \Drupal::service('date.formatter')
    ->formatTimeDiffUntil($grey_timestamp);
  $black_offset = \Drupal::state()
    ->get('httpbl.blacklist_offset') ?: 31536000;
  $black_timestamp = \Drupal::time()
    ->getRequestTime() + $black_offset;
  $black_time_message = \Drupal::service('date.formatter')
    ->formatTimeDiffUntil($black_timestamp);
  $items = [];

  // Prepare confirmations.
  foreach ($this->hostInfo as $id => $host_ips) {
    foreach ($host_ips as $host_ip) {
      $host = $hosts[$id];
      $status = $host
        ->getHostStatus();
      $key = $id . ':' . $host_ip;
      $default_key = $id . ':' . $host_ip;
      switch ($status) {
        case '0':
          $items[$default_key] = [
            'label' => [
              '#markup' => $this
                ->t('White-listed @label expiry will be refreshed to @time.', [
                '@label' => $host
                  ->label(),
                '@time' => $safe_time_message,
              ]),
            ],
            'host' => [
              '#theme' => 'item_list',
            ],
          ];
          break;
        case '1':
          $items[$default_key] = [
            'label' => [
              '#markup' => $this
                ->t('Blacklisted @label expiry will be refreshed to @time.', [
                '@label' => $host
                  ->label(),
                '@time' => $black_time_message,
              ]),
            ],
            'host' => [
              '#theme' => 'item_list',
            ],
          ];
          break;
        case '2':
          $items[$default_key] = [
            'label' => [
              '#markup' => $this
                ->t('Grey-listed @label expiry will be refreshed to @time.', [
                '@label' => $host
                  ->label(),
                '@time' => $grey_time_message,
              ]),
            ],
            'host' => [
              '#theme' => 'item_list',
            ],
          ];
          break;
      }
    }
  }
  $form['hosts'] = array(
    '#theme' => 'item_list',
    '#items' => $items,
  );
  $form = parent::buildForm($form, $form_state);
  return $form;
}