You are here

function popup_maker_refresh_data in Popup Maker - All popup types 7

2 calls to popup_maker_refresh_data()
popup_maker_refresh_popups in ./popup_maker.admin.inc
popup_maker_update_api_key in ./popup_maker.admin.inc

File

./popup_maker.admin.inc, line 204

Code

function popup_maker_refresh_data($apiKey) {
  if (empty($apiKey)) {
    drupal_set_message('Api Key is required to connect to the service', 'error');
    return false;
  }
  $data = array(
    'apiKey' => $apiKey,
    'appname' => 'Drupal',
  );
  $options = array(
    'method' => 'POST',
    'data' => drupal_http_build_query($data),
    'headers' => array(
      'Content-Type' => 'application/x-www-form-urlencoded',
    ),
  );
  $result = drupal_http_request(SGPM_SERVICE_URL . 'app/connect', $options);
  if (!isset($result->data)) {
    drupal_set_message('Failed to connect to service', 'error');
  }
  $data = json_decode($result->data, true);
  $config = variable_get('popup_maker_settings', array(
    'api_key' => null,
    'popups' => array(),
    'popupSettings' => array(),
    'user' => array(),
  ));
  if (!isset($data['isAuthenticate']) || !$data['isAuthenticate']) {
    drupal_set_message('Please, provide a valid Api Key', 'error');
    return false;
  }
  $config['popups'] = array_reverse($data['popups'], true);
  $config['user'] = $data['user'];
  if (empty($config['popupSettings'])) {
    $config['popupSettings'] = array();
  }
  foreach ($data['popups'] as $popupId => $popup) {
    if (!isset($config['popupSettings'][$popupId])) {
      $config['popupSettings'][$popupId] = array(
        'enabled' => 0,
        'displayOptions' => [],
      );
    }
  }
  variable_set('popup_maker_settings', $config);
  drupal_set_message('Data imported from service successfully');
  return true;
}