You are here

public function AutobanTestForm::buildForm in Automatic IP ban (Autoban) 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 FormInterface::buildForm

File

src/Form/AutobanTestForm.php, line 55

Class

AutobanTestForm
Test autoban rule.

Namespace

Drupal\autoban\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $rule = '') {
  $from_analyze = FALSE;
  if (AutobanUtils::isFromAnalyze($rule)) {
    $params = $this
      ->getRequest()->query
      ->all();
    $from_analyze = !empty($params['type']) && !empty($params['message']);
  }
  else {
    $params = [];
  }
  $header = [
    $this
      ->t('Count'),
    $this
      ->t('Ip address'),
  ];
  $controller = $this->autoban;
  if ($from_analyze) {
    $result = $controller
      ->getBannedIp($rule, $params);
    $ips_arr = [];
  }
  else {
    $result = $controller
      ->getBannedIp($rule);
    $header[] = $this
      ->t('Ban status');
  }
  $rows = [];
  $build = [];
  $ips_arr = [];
  if (!empty($result)) {
    $banManager = NULL;
    if (!$from_analyze) {
      $banManagerData = $controller
        ->getBanManagerDataRule($rule);
      if ($banManagerData) {
        $banManager = $banManagerData['ban_manager'];
      }
    }

    // Rows collect.
    foreach ($result as $item) {
      $data = [
        $item->hcount,
        $item->hostname,
      ];
      if ($from_analyze) {
        $ips_arr[] = $item->hostname;
      }
      else {
        $data[] = $banManager ? $banManager
          ->isBanned($item->hostname) ? $this
          ->t('Banned') : $this
          ->t('Not banned') : '?';
      }
      $rows[] = [
        'data' => $data,
      ];
    }

    // Add action buttons.
    $buttons = [];
    $destination = [
      'destination' => Url::fromRoute('<current>', $params)
        ->toString(),
    ];
    if (!$from_analyze && !empty($banManagerData)) {
      $entity = Autoban::load($rule);
      $url = Url::fromRoute('autoban.ban', [
        'rule' => $entity
          ->id(),
      ], [
        'query' => $destination,
        'attributes' => [
          'class' => 'button button-action button--primary button--small',
        ],
      ]);
      $text = $this
        ->t('Ban IP (@name @type)', [
        '@name' => $banManagerData['ban_name'],
        '@type' => $banManagerData['ban_type'],
      ]);
      $buttons['ban'] = Link::fromTextAndUrl($text, $url)
        ->toString();
    }
    else {
      if (count($ips_arr)) {
        $banManagerList = $controller
          ->getBanProvidersList();
        if (!empty($banManagerList)) {
          $ips = implode(',', $ips_arr);
          foreach ($banManagerList as $id => $item) {
            $url = Url::fromRoute('autoban.direct_ban', [
              'ips' => $ips,
              'provider' => $id,
            ], [
              'query' => $destination,
              'attributes' => [
                'class' => 'button button-action button--primary button--small',
              ],
            ]);
            $buttons[$id] = Link::fromTextAndUrl($item['name'], $url)
              ->toString();
          }
        }
      }
    }
    $build['buttons'] = [
      '#theme' => 'item_list',
      '#items' => $buttons,
      '#attributes' => [
        'class' => 'action-links',
      ],
    ];
  }
  $build['test_table'] = [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => $this
      ->t('No hostnames was found.'),
  ];
  return $build;
}