You are here

public function SmartIpEventSubscriber::validateFormSettings in Smart IP 8.3

Same name in this branch
  1. 8.3 modules/device_geolocation/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\device_geolocation\EventSubscriber\SmartIpEventSubscriber::validateFormSettings()
  2. 8.3 modules/smart_ip_maxmind_geoip2_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_maxmind_geoip2_bin_db\EventSubscriber\SmartIpEventSubscriber::validateFormSettings()
  3. 8.3 modules/smart_ip_ipinfodb_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_ipinfodb_web_service\EventSubscriber\SmartIpEventSubscriber::validateFormSettings()
  4. 8.3 modules/smart_ip_ip2location_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_ip2location_bin_db\EventSubscriber\SmartIpEventSubscriber::validateFormSettings()
  5. 8.3 modules/smart_ip_abstract_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_abstract_web_service\EventSubscriber\SmartIpEventSubscriber::validateFormSettings()
  6. 8.3 modules/smart_ip_maxmind_geoip2_web_service/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_maxmind_geoip2_web_service\EventSubscriber\SmartIpEventSubscriber::validateFormSettings()
Same name and namespace in other branches
  1. 8.4 modules/smart_ip_maxmind_geoip2_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_maxmind_geoip2_bin_db\EventSubscriber\SmartIpEventSubscriber::validateFormSettings()

Act on validation of main Smart IP admin settings form.

Parameters

\Drupal\smart_ip\AdminSettingsEvent $event: Smart IP admin settings override event for event listeners.

Overrides SmartIpDataSourceInterface::validateFormSettings

File

modules/smart_ip_maxmind_geoip2_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php, line 288
Contains \Drupal\smart_ip_maxmind_geoip2_bin_db\EventSubscriber\SmartIpEventSubscriber.

Class

SmartIpEventSubscriber
Core functionality of this Smart IP data source module. Listens to Smart IP override events.

Namespace

Drupal\smart_ip_maxmind_geoip2_bin_db\EventSubscriber

Code

public function validateFormSettings(AdminSettingsEvent $event) {

  /** @var \Drupal\Core\Form\FormStateInterface $formState */
  $formState = $event
    ->getFormState();
  if ($formState
    ->getValue('smart_ip_data_source') == self::sourceId()) {
    $autoUpdate = $formState
      ->getValue('maxmind_geoip2_bin_db_auto_update');
    if ($autoUpdate && $formState
      ->getValue('maxmind_geoip2_bin_db_version') == MaxmindGeoip2BinDb::LITE_VERSION && $formState
      ->isValueEmpty('maxmind_geoip2_bin_db_user_account')) {
      $formState
        ->setErrorByName('maxmind_geoip2_bin_db_user_account', t('Please provide MaxMind GeoIP2 user account ID.'));
    }
    if ($autoUpdate && $formState
      ->getValue('maxmind_geoip2_bin_db_version') == MaxmindGeoip2BinDb::LITE_VERSION && $formState
      ->isValueEmpty('maxmind_geoip2_bin_db_license_key') || $formState
      ->getValue('maxmind_geoip2_bin_db_version') == MaxmindGeoip2BinDb::LINCENSED_VERSION && $formState
      ->isValueEmpty('maxmind_geoip2_bin_db_license_key')) {
      $formState
        ->setErrorByName('maxmind_geoip2_bin_db_license_key', t('Please provide MaxMind GeoIP2 license key.'));
    }
    if ($autoUpdate || !$autoUpdate && $formState
      ->isValueEmpty('maxmind_geoip2_bin_db_custom_path')) {
      if (empty(PrivateStream::basePath())) {
        $formState
          ->setErrorByName('maxmind_geoip2_bin_db_auto_update', t('Your private file system path is not yet configured.
              Please check your @filesystem.', [
          '@filesystem' => Link::fromTextAndUrl(t('File system'), Url::fromRoute('system.file_system_settings'))
            ->toString(),
        ]));
      }
      else {

        /** @var \Drupal\Core\File\FileSystem $filesystem */
        $filesystem = \Drupal::service('file_system');
        $privateFolder = $filesystem
          ->realpath(DatabaseFileUtilityBase::DRUPAL_FOLDER);
        $file = DatabaseFileUtility::getFilename($formState
          ->getValue('maxmind_geoip2_bin_db_version'), $formState
          ->getValue('maxmind_geoip2_bin_db_edition'));
        if (!file_exists("{$privateFolder}/{$file}")) {
          if ($autoUpdate) {
            if (empty($privateFolder)) {
              $message = t('There is an @issue in Drupal private file system path.', [
                '@issue' => Link::fromTextAndUrl(t('issue'), Url::fromRoute('system.status', [], [
                  'fragment' => 'error',
                ]))
                  ->toString(),
              ]);
            }
            else {
              $message = t('Initially you need to manually download the @file and
                   upload it to your server at @path. The next succeeding
                   updates should be automatic.', [
                '@file' => $file,
                '@path' => $privateFolder,
              ]);
            }
          }
          else {
            $message = t('Please upload the @file at @path.', [
              '@file' => $file,
              '@path' => $privateFolder,
            ]);
          }
          $formState
            ->setErrorByName('maxmind_geoip2_bin_db_auto_update', $message);
        }
      }
    }
    elseif (!$formState
      ->isValueEmpty('maxmind_geoip2_bin_db_custom_path')) {
      $folder = $formState
        ->getValue('maxmind_geoip2_bin_db_custom_path');
      $file = DatabaseFileUtility::getFilename($formState
        ->getValue('maxmind_geoip2_bin_db_version'), $formState
        ->getValue('maxmind_geoip2_bin_db_edition'));
      if (!file_exists("{$folder}/{$file}")) {
        $formState
          ->setErrorByName('maxmind_geoip2_bin_db_auto_update', t('Please upload the @file at @path.', [
          '@file' => $file,
          '@path' => $folder,
        ]));
      }
    }
  }
}