You are here

public function SmartIpAdminSettingsForm::buildForm in Smart IP 8.3

Same name and namespace in other branches
  1. 8.4 src/Form/SmartIpAdminSettingsForm.php \Drupal\smart_ip\Form\SmartIpAdminSettingsForm::buildForm()
  2. 8.2 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 51
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');
  $thisClass = get_class($this);
  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'] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Database Update Status'),
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
        '#states' => [
          'visible' => [
            ':input[name="smart_ip_data_source"]' => [
              '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'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Update database now'),
        '#submit' => [
          [
            $thisClass,
            'manualUpdate',
          ],
        ],
        '#prefix' => $message,
      ];
    }

    // Container for manual lookup.
    $form['smart_ip_manual_lookup'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Manual lookup'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    ];
    $form['smart_ip_manual_lookup']['smart_ip_lookup'] = [
      '#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.', [
        '%lookup' => $this
          ->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'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Lookup'),
      '#submit' => [
        [
          $thisClass,
          'manualLookup',
        ],
      ],
      '#ajax' => [
        'callback' => [
          $thisClass,
          'manualLookupAjax',
        ],
        'effect' => 'fade',
      ],
      '#suffix' => '<div id="smart-ip-location-manual-lookup">' . $lookupResponse . '</div>',
    ];
  }

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

  // Smart IP fallback data source status.
  $fallbackDataSource = [];
  if (isset($_SERVER['GEOIP_COUNTRY_NAME'])) {
    $modGeoipStat = $this
      ->t('available');
    $fallbackDataSource[] = 0;
  }
  else {
    $modGeoipStat = $this
      ->t('not available');
  }
  if (isset($_SERVER['HTTP_X_GEOIP_COUNTRY'])) {
    $xHeaderStat = $this
      ->t('available');
    $fallbackDataSource[] = 1;
  }
  else {
    $xHeaderStat = $this
      ->t('not available');
  }
  if (isset($_SERVER['HTTP_CF_IPCOUNTRY'])) {
    $cfHeaderStat = $this
      ->t('available');
    $fallbackDataSource[] = 2;
  }
  else {
    $cfHeaderStat = $this
      ->t('not available');
  }
  if (isset($_SERVER['HTTP_X_AKAMAI_EDGESCAPE'])) {
    $esHeaderStat = $this
      ->t('available');
    $fallbackDataSource[] = 3;
  }
  else {
    $esHeaderStat = $this
      ->t('not available');
  }
  $form['smart_ip_data_source_selection']['smart_ip_fallback_data_source'] = [
    '#type' => 'checkboxes',
    '#disabled' => TRUE,
    '#title' => $this
      ->t('Smart IP fallback data source status'),
    '#description' => $this
      ->t("If your selected main Smart IP data source below failed to return \n        user's geolocation data, the available Smart IP fallback data source \n        will provide the user's geolocation as fallback."),
    '#default_value' => $fallbackDataSource,
    '#options' => [
      $this
        ->t("MaxMind's Apache module @mod_geoip (@status)", [
        '@mod_geoip' => Link::fromTextAndUrl($this
          ->t('mod_geoip'), Url::fromUri('http://dev.maxmind.com/geoip/legacy/mod_geoip2'))
          ->toString(),
        '@status' => $modGeoipStat,
      ]),
      $this
        ->t("X-GeoIP-Country: XX header, set by e.g. nginx (@status)", [
        '@status' => $xHeaderStat,
      ]),
      $this
        ->t('Cloudflare IP Geolocation: your website must be using
          Cloudflare CDN and "IP Geolocation" option must be enabled at your
          @settings (@status)', [
        '@settings' => Link::fromTextAndUrl($this
          ->t('Cloudflare settings'), Url::fromUri('https://www.cloudflare.com/cloudflare-settings'))
          ->toString(),
        '@status' => $cfHeaderStat,
      ]),
      $this
        ->t('Akamai EdgeScape: your website must be using Akamai CDN and
          "EdgeScape Geolocation" option must be enabled at your @settings
          (@status)', [
        '@settings' => Link::fromTextAndUrl($this
          ->t('Akamai settings'), Url::fromUri('https://control.akamai.com/apps/auth'))
          ->toString(),
        '@status' => $esHeaderStat,
      ]),
    ],
  ];

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

  // Container for Smart IP preference.
  $form['smart_ip_preferences'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Smart IP settings'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  ];
  $roles = Role::loadMultiple();
  $userRoles = [];

  /** @var \Drupal\user\Entity\Role $role */
  foreach ($roles as $roleId => $role) {
    $userRoles[$roleId] = $role
      ->get('label');
  }
  $form['smart_ip_preferences']['smart_ip_roles_to_geolocate'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Roles to Geolocate'),
    '#default_value' => $config
      ->get('roles_to_geolocate'),
    '#options' => $userRoles,
    '#description' => $this
      ->t('Select the roles you wish to geolocate. Note that selecting the
        anonymous role will add substantial overhead.'),
  ];
  $euVisitorsDontSaveLabel = $this
    ->t("Don't save location details of visitors from GDPR countries");
  $form['smart_ip_preferences']['smart_ip_save_user_location_creation'] = [
    '#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. \n        Note: If '@label' is enabled, it will not save visitors from EU countries \n        their location details.", [
      '@label' => $euVisitorsDontSaveLabel,
    ]),
  ];
  $form['smart_ip_preferences']['smart_ip_eu_visitor_dont_save'] = [
    '#type' => 'checkbox',
    '#title' => $euVisitorsDontSaveLabel,
    '#default_value' => $config
      ->get('eu_visitor_dont_save'),
    '#description' => $this
      ->t("If a visitor's country is an GDPR country, its location details will not be saved."),
  ];
  $geotimezoneExists = \Drupal::moduleHandler()
    ->moduleExists('geotimezone');
  if (!$geotimezoneExists) {
    $tzFormatDesc = $this
      ->t('Please install and enable @module.', [
      '@module' => Link::fromTextAndUrl($this
        ->t('Geo Time Zone'), Url::fromUri('https://www.drupal.org/project/geotimezone'))
        ->toString(),
    ]);
  }
  else {
    $tzFormatDesc = $this
      ->t('Select the format of the time zone field.');
  }
  $form['smart_ip_preferences']['smart_ip_timezone_format'] = [
    '#type' => 'select',
    '#disabled' => !$geotimezoneExists,
    '#title' => $this
      ->t('Time zone format'),
    '#description' => $tzFormatDesc,
    '#default_value' => $config
      ->get('timezone_format'),
    '#options' => [
      'identifier' => $this
        ->t('Identifier (E.g Asia/Manila)'),
      'offset' => $this
        ->t('UTC/GMT Offset (E.g +08:00)'),
    ],
  ];
  $form['smart_ip_preferences']['smart_ip_allowed_pages'] = [
    '#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.", [
      '%user' => '/user',
      '%user-wildcard' => '/user/*',
      '%front' => '<front>',
    ]),
    '#default_value' => $config
      ->get('allowed_pages'),
  ];
  $form['smart_ip_preferences']['smart_ip_excluded_ips'] = [
    '#title' => $this
      ->t('IP addresses to exclude from look up'),
    '#type' => 'textarea',
    '#rows' => 5,
    '#description' => $this
      ->t('Specify the IP addresses to exclude from being looked up. Enter
        one IP address per line.'),
    '#default_value' => $config
      ->get('excluded_ips'),
  ];

  // Container for Smart IP debug tool.
  $form['smart_ip_debug_tool'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Smart IP debug tool'),
    '#description' => $this
      ->t('Note: Make sure that the debug role is also enabled in "Roles to
        Geolocate" under "Smart IP settings" above. If a user has multiple
        roles, the precedence of what debug IP address of a role will be used is
        determined in alphabetical order but the "authenticated role will always
        be the last priority. Eg. if a user has "authenticated", "editor" and
        "moderator" roles, (assuming all debug roles are enabled) the debug IP
        address that will be used is the "editor" role.'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#weight' => 1,
  ];
  $rolesDebug = $config
    ->get('roles_in_debug_mode');
  $rolesDebugIp = $config
    ->get('roles_in_debug_mode_ip');

  /** @var \Drupal\user\Entity\Role $role */
  foreach ($roles as $roleId => $role) {
    $form['smart_ip_debug_tool']["smart_ip_debug_{$roleId}"] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('@role role in debug mode', [
        '@role' => $role
          ->get('label'),
      ]),
      '#default_value' => isset($rolesDebug[$roleId]) && $rolesDebug[$roleId] ? TRUE : FALSE,
      '#description' => $this
        ->t('Enables @role role to spoof an IP Address for debugging purposes.', [
        '@role' => $role
          ->get('label'),
      ]),
    ];
    $form['smart_ip_debug_tool']["smart_ip_test_ip_address_{$roleId}"] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('IP address to use for @role role testing', [
        '@role' => $role
          ->get('label'),
      ]),
      '#default_value' => isset($rolesDebugIp[$roleId]) ? $rolesDebugIp[$roleId] : NULL,
    ];
  }

  /** @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'] = [
      '#markup' => $this
        ->t('You do not have any Smart IP data source module enabled. Please
          enable at least one @here.', [
        '@here' => Link::fromTextAndUrl($this
          ->t('here'), Url::fromRoute('system.modules_list', [], [
          'fragment' => 'edit-modules-smart-ip-data-source',
        ]))
          ->toString(),
      ]),
    ];
  }
  return parent::buildForm($form, $form_state);
}