You are here

public function SmartIpAdminSettingsForm::buildForm in Smart IP 8.2

Same name and namespace in other branches
  1. 8.4 src/Form/SmartIpAdminSettingsForm.php \Drupal\smart_ip\Form\SmartIpAdminSettingsForm::buildForm()
  2. 8.3 src/Form/SmartIpAdminSettingsForm.php \Drupal\smart_ip\Form\SmartIpAdminSettingsForm::buildForm()

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 ConfigFormBase::buildForm

File

src/Form/SmartIpAdminSettingsForm.php, line 50
Contains \Drupal\smart_ip\Form\SmartIpAdminSettingsForm.

Class

SmartIpAdminSettingsForm
Smart IP main admin settings page.

Namespace

Drupal\smart_ip\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('smart_ip.settings');
  $dataSource = $config
    ->get('data_source');
  if (!empty($dataSource)) {
    $errorSourceId = \Drupal::state()
      ->get('smart_ip.request_db_error_source_id') ?: '';
    if (!empty($errorSourceId)) {

      // Container for update status and manual update
      $form['smart_ip_bin_database_update'] = array(
        '#type' => 'fieldset',
        '#title' => t('Database Update Status'),
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
        '#states' => array(
          'visible' => array(
            ':input[name="smart_ip_source"]' => array(
              'value' => $errorSourceId,
            ),
          ),
        ),
      );
      $message = \Drupal::state()
        ->get('smart_ip.request_db_error_message') ?: '';
      if (!empty($message)) {
        $message = "<div class='messages messages--error'>{$message}</div>";
      }
      $form['smart_ip_bin_database_update']['smart_ip_bin_update_database'] = array(
        '#type' => 'submit',
        '#value' => t('Update database now'),
        '#submit' => array(
          array(
            get_class($this),
            'manualUpdate',
          ),
        ),
        '#prefix' => $message,
      );
    }

    // Container for manual lookup
    $form['smart_ip_manual_lookup'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Manual lookup'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    );
    $form['smart_ip_manual_lookup']['smart_ip_lookup'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('IP address'),
      '#description' => $this
        ->t('An IP address may be looked up by entering the address above then pressing the %lookup button below.', array(
        '%lookup' => t('Lookup'),
      )),
    );
    $storage = $form_state
      ->getStorage();
    $lookupResponse = isset($storage['smart_ip_message']) ? $storage['smart_ip_message'] : '';
    $form['smart_ip_manual_lookup']['smart_ip_lookup_button'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Lookup'),
      '#submit' => array(
        array(
          get_class($this),
          'manualLookup',
        ),
      ),
      '#ajax' => array(
        'callback' => array(
          get_class($this),
          'manualLookupAjax',
        ),
        'effect' => 'fade',
      ),
      '#suffix' => '<div id="smart-ip-location-manual-lookup">' . $lookupResponse . '</div>',
    );
  }

  // Container for Smart IP source
  $form['smart_ip_data_source_selection'] = array(
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Smart IP source'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );

  // Smart IP source selection
  $form['smart_ip_data_source_selection']['smart_ip_data_source'] = array(
    '#type' => 'radios',
    '#title' => $this
      ->t('Select Smart IP data source'),
    '#options' => array(),
    '#default_value' => $dataSource,
  );

  /** @var \Drupal\smart_ip\AdminSettingsEvent $event */
  $event = \Drupal::service('smart_ip.admin_settings_event');

  // Allow Smart IP source module to add their form elements
  $event
    ->setForm($form);
  $event
    ->setFormState($form_state);
  \Drupal::service('event_dispatcher')
    ->dispatch(SmartIpEvents::DISPLAY_SETTINGS, $event);
  $form = $event
    ->getForm();
  $form_state = $event
    ->getFormState();
  if (empty($form['smart_ip_data_source_selection']['smart_ip_data_source']['#options'])) {

    // No Smart IP data source module enabled
    $form['smart_ip_data_source_selection']['smart_ip_data_source'] = array(
      '#markup' => $this
        ->t('You do not have any Smart IP data source module enabled. Please
          enable at least one @here.', array(
        '@here' => Link::fromTextAndUrl($this
          ->t('here'), Url::fromRoute('system.modules_list', [], [
          'fragment' => 'edit-modules-smart-ip-data-source',
        ]))
          ->toString(),
      )),
    );
  }

  // Container for Smart IP preference
  $form['smart_ip_preferences'] = array(
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Smart IP settings'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $userRoles = user_roles();
  $roles = array();
  foreach ($userRoles as $role_id => $role) {
    $roles[$role_id] = $role
      ->label();
  }
  $form['smart_ip_preferences']['smart_ip_roles_to_geolocate'] = array(
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Roles To Geolocate'),
    '#default_value' => $config
      ->get('roles_to_geolocate'),
    '#options' => $roles,
    '#description' => $this
      ->t('Select the roles you wish to geolocate. Note that selecting the
        anonymous role will add substantial overhead.'),
  );
  $form['smart_ip_preferences']['smart_ip_save_user_location_creation'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t("Save user's location details upon creation"),
    '#default_value' => $config
      ->get('save_user_location_creation'),
    '#description' => $this
      ->t("One time storing of user's location details upon registration."),
  );
  $form['smart_ip_preferences']['smart_ip_allowed_pages'] = array(
    '#title' => $this
      ->t("Acquire/update user's geolocation on specific Drupal native pages"),
    '#type' => 'textarea',
    '#rows' => 5,
    '#description' => $this
      ->t("Specify pages by using their paths. Enter one path per line. The '*' \n        character is a wildcard. Example paths are %user for the current user's \n        page and %user-wildcard for every user page. %front is the front page. \n        Leave blank if all pages.", array(
      '%user' => '/user',
      '%user-wildcard' => '/user/*',
      '%front' => '<front>',
    )),
    '#default_value' => $config
      ->get('allowed_pages'),
  );

  // Container for Smart IP debug tool
  $form['smart_ip_debug_tool'] = array(
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Smart IP debug tool'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['smart_ip_debug_tool']['smart_ip_debug'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Admin debug'),
    '#default_value' => $config
      ->get('debug_mode'),
    '#description' => $this
      ->t('Enables administrator to spoof an IP Address for debugging purposes.'),
  );
  $form['smart_ip_debug_tool']['smart_ip_test_ip_address'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('IP address to use for testing'),
    '#default_value' => $config
      ->get('debug_mode_ip'),
  );
  return parent::buildForm($form, $form_state);
}