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_ip2location_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php \Drupal\smart_ip_ip2location_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_ip2location_bin_db/src/EventSubscriber/SmartIpEventSubscriber.php, line 312
Contains \Drupal\smart_ip_ip2location_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_ip2location_bin_db\EventSubscriber

Code

public function validateFormSettings(AdminSettingsEvent $event) {

  /** @var \Drupal\Core\Form\FormStateInterface $formState */
  $formState = $event
    ->getFormState();
  $version = $formState
    ->getValue('ip2location_bin_db_version');
  $autoUpdate = $formState
    ->getValue('ip2location_bin_db_auto_update');
  if ($version == Ip2locationBinDb::LINCENSED_VERSION) {
    $edition = $formState
      ->getValue('ip2location_bin_db_edition_licensed');
  }
  else {
    $edition = $formState
      ->getValue('ip2location_bin_db_edition_lite');
  }
  if ($formState
    ->getValue('smart_ip_data_source') == self::sourceId()) {
    if ($formState
      ->isValueEmpty('ip2location_bin_db_token') && $version == Ip2locationBinDb::LINCENSED_VERSION && $autoUpdate) {
      $formState
        ->setErrorByName('ip2location_bin_db_token', t('Please provide IP2Location download token.'));
    }
    if ($autoUpdate && $version == Ip2locationBinDb::LITE_VERSION) {
      $formState
        ->setErrorByName('ip2location_bin_db_auto_update', t('This feature is not yet available for Lite version.'));
    }
    if ($autoUpdate) {

      /** @var \Drupal\Core\File\FileSystem $filesystem */
      $filesystem = \Drupal::service('file_system');
      $privateFolder = $filesystem
        ->realpath(DatabaseFileUtilityBase::DRUPAL_FOLDER);
      if (empty(PrivateStream::basePath())) {
        $formState
          ->setErrorByName('ip2location_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(),
        ]));
      }
      elseif (empty($privateFolder)) {
        $formState
          ->setErrorByName('ip2location_bin_db_auto_update', t('There is an @issue in Drupal private file system path.', [
          '@issue' => Link::fromTextAndUrl(t('issue'), Url::fromRoute('system.status', [], [
            'fragment' => 'error',
          ]))
            ->toString(),
        ]));
      }
      else {

        /** @var \Drupal\Core\StreamWrapper\PrivateStream $privateStream */
        $privateStream = \Drupal::service('stream_wrapper.private');
        $file = DatabaseFileUtility::getFilename($version, $edition, Ip2locationBinDb::IPV4_VERSION);
        $privateStream
          ->setUri(DatabaseFileUtilityBase::DRUPAL_FOLDER);
        $folder = $privateStream
          ->realpath();
        $dbFile = "{$folder}/{$file}";
        $error = $this
          ->checkBinFile($dbFile, Ip2locationBinDb::IPV4_VERSION);
        if ($error['code'] == Ip2locationBinDb::DB_NOT_EXIST_ERROR) {
          $formState
            ->set('smart_ip_ip2location_bin_db_show_manual_update', TRUE);
          $formState
            ->setRebuild(TRUE);
          $formState
            ->setErrorByName('ip2location_bin_db_auto_update', t('Initially you need to manually download the @file and upload it
              to your server at @folder. The next succeeding updates should be
              automatic.', [
            '@file' => $file,
            '@folder' => $folder,
          ]));
        }
        else {
          $file = DatabaseFileUtility::getFilename($version, $edition, Ip2locationBinDb::IPV6_VERSION);
          $dbFile = "{$folder}/{$file}";
          $error = $this
            ->checkBinFile($dbFile, Ip2locationBinDb::IPV6_VERSION);
          if ($error['code'] == Ip2locationBinDb::DB_NOT_EXIST_ERROR) {
            $formState
              ->set('smart_ip_ip2location_bin_db_show_manual_update', TRUE);
            $formState
              ->setRebuild(TRUE);
            $formState
              ->setErrorByName('ip2location_bin_db_auto_update', t('Initially you need to manually download the @file and
                upload it to your server at @folder. The next succeeding updates
                should be automatic.', [
              '@file' => $file,
              '@folder' => $folder,
            ]));
          }
        }
      }
    }
    else {

      /** @var \Drupal\Core\StreamWrapper\PrivateStream $privateStream */
      $privateStream = \Drupal::service('stream_wrapper.private');
      $folder = $formState
        ->getValue('ip2location_bin_db_custom_path');
      $privateFolder = DatabaseFileUtilityBase::DRUPAL_FOLDER;
      if (!file_exists($privateFolder)) {
        $privateStream
          ->mkdir($privateFolder, NULL, STREAM_MKDIR_RECURSIVE);
        file_prepare_directory($privateFolder);
      }
      if (empty($folder)) {
        $privateStream
          ->setUri($privateFolder);
        $folder = $privateStream
          ->realpath();
      }
      $file = DatabaseFileUtility::getFilename($version, $edition, Ip2locationBinDb::IPV4_VERSION);
      $dbFile = "{$folder}/{$file}";
      $error = $this
        ->checkBinFile($dbFile, Ip2locationBinDb::IPV4_VERSION);
      $errorCode = Ip2locationBinDb::DB_NO_ERROR;
      $errorMsg = '';
      if ($error['code'] == Ip2locationBinDb::DB_NOT_EXIST_ERROR) {
        $errorCode = $error['code'];
        $errorMsg = t('The @file does not exist in @folder. Please provide a valid
            path or select a proper IP2Location product edition or upload the
            @file in @folder.', [
          '@file' => $file,
          '@folder' => $folder,
        ]);
      }
      elseif ($error['code'] == Ip2locationBinDb::DB_READ_ERROR) {
        $errorCode = $error['code'];
        $errorMsg = t('The IP2Location binary database IPV4 file @path is not valid or corrupted.', [
          '@path' => $dbFile,
        ]);
      }
      elseif ($error['code'] == Ip2locationBinDb::DB_LOAD_ERROR) {
        $errorCode = $error['code'];
        $errorMsg = t('Loading IP2Location binary database IPV4 file @path failed: @error.', [
          '@path' => $dbFile,
          '@error' => $error['msg'],
        ]);
      }
      $file = DatabaseFileUtility::getFilename($version, $edition, Ip2locationBinDb::IPV6_VERSION);
      $dbFile = "{$folder}/{$file}";
      $error = $this
        ->checkBinFile($dbFile, Ip2locationBinDb::IPV6_VERSION);
      if ($error['code'] == Ip2locationBinDb::DB_NOT_EXIST_ERROR) {
        $errorCode = $error['code'];
        $errorMsg .= t('The @file does not exist in @folder. Please provide a valid
            path or select a proper IP2Location product edition or upload the
            @file in @folder.', [
          '@file' => $file,
          '@folder' => $folder,
        ]);
      }
      elseif ($error['code'] == Ip2locationBinDb::DB_READ_ERROR) {
        $errorCode = $error['code'];
        $errorMsg .= t('The IP2Location binary database IPV6 file @path is not valid or corrupted.', [
          '@path' => $dbFile,
        ]);
      }
      elseif ($error['code'] == Ip2locationBinDb::DB_LOAD_ERROR) {
        $errorCode = $error['code'];
        $errorMsg .= t('Loading IP2Location binary database IPV6 file @path failed: @error.', [
          '@path' => $dbFile,
          '@error' => $error['msg'],
        ]);
      }
      if ($errorCode) {
        $formState
          ->setErrorByName('ip2location_bin_db_custom_path', $errorMsg);
      }
    }
  }
}