You are here

public function SettingsController::refreshData in Popup Maker - All popup types 8

To refresh the data of popup.

2 calls to SettingsController::refreshData()
SettingsController::refreshPopups in src/Controller/SettingsController.php
To refresh the popups.
SettingsController::updateApiKey in src/Controller/SettingsController.php
To ypdate the API key.

File

src/Controller/SettingsController.php, line 236

Class

SettingsController
Settings Controller to the popup maker module.

Namespace

Drupal\popup_maker\Controller

Code

public function refreshData($apiKey) {
  if (empty($apiKey)) {
    $this->messenger
      ->addError('Api Key is required to connect to the service');
    return FALSE;
  }
  $data = [
    'apiKey' => $apiKey,
    'appname' => 'Drupal',
  ];
  $client = $this->httpClient;
  try {
    $request = $client
      ->post(SettingsController::POPUP_MAKER_SERVICE_URL . 'app/connect', [
      'form_params' => $data,
    ]);
    $data = json_decode($request
      ->getBody(), TRUE);
  } catch (RequestException $e) {
    $this->logger
      ->get('popup_maker')
      ->error($e);
    return FALSE;
  }
  $config = $this->configFactory
    ->getEditable('popup_maker.settings');
  if (!isset($data['isAuthenticate']) || !$data['isAuthenticate']) {
    $this->messenger
      ->addError('Please, provide a valid Api Key');
    return FALSE;
  }
  $config
    ->set('popups', array_reverse($data['popups'], TRUE))
    ->set('user', $data['user'])
    ->save();
  $popupSettings = $config
    ->get('popupSettings');
  if (empty($popupSettings)) {
    $popupSettings = [];
  }
  foreach ($data['popups'] as $popupId => $popup) {
    if (!isset($popupSettings[$popupId])) {
      $popupSettings[$popupId] = [
        'enabled' => 0,
        'displayOptions' => [],
      ];
    }
  }
  $config
    ->set('popupSettings', $popupSettings)
    ->save();
  $this->messenger
    ->addStatus('Data imported from service successfully');
  return TRUE;
}