You are here

popup_maker.admin.inc in Popup Maker - All popup types 7

File

popup_maker.admin.inc
View source
<?php

function popup_maker_settings_view() {
  if (isset($_GET['task'])) {
    switch ($_GET['task']) {
      case 'updateApiKey':
        popup_maker_update_api_key();
        break;
      case 'refreshPopups':
        popup_maker_refresh_popups();
        break;
      case 'updateStatus':
        popup_maker_update_status();
        break;
      case 'updatePopup':
        popup_maker_update_popup();
        break;
    }
  }
  $variables = array(
    'settings' => variable_get('popup_maker_settings', array(
      'api_key' => null,
      'popups' => array(),
      'popupSettings' => array(),
      'user' => array(),
    )),
    'token' => drupal_get_token('popup_maker'),
  );
  if (isset($_GET['popupId'])) {
    $variables['edit'] = true;
    $variables['editingPopupId'] = $_GET['popupId'];
    $contentTypes = node_type_get_types();
    if (!empty($contentTypes)) {
      foreach ($contentTypes as $key => $contentType) {
        $displayRules[$key] = [];
        $nodes = node_load_multiple(array(), array(
          'type' => $key,
        ));
        foreach ($nodes as $node) {
          $displayRules[$key][$node->nid] = $node->title;
        }
      }
    }
    $variables['displayRules'] = $displayRules;
  }
  drupal_add_js(drupal_get_path('module', 'popup_maker') . '/js/admin.js', array(
    'scope' => 'footer',
  ));
  drupal_add_css(drupal_get_path('module', 'popup_maker') . '/css/admin.css');
  return theme('popup_maker_settings', $variables);
}
function popup_maker_update_popup() {
  if (!drupal_valid_token($_POST['_csrf_token'], 'popup_maker')) {
    drupal_set_message('Could not validate the security token', 'error');
    return false;
  }
  $id = $_POST['id'];
  $config = variable_get('popup_maker_settings', array(
    'api_key' => null,
    'popups' => array(),
    'popupSettings' => array(),
    'user' => array(),
  ));
  $config['popupSettings'][$id]['displayOptions'] = $_POST['sgpm_rules'];
  variable_set('popup_maker_settings', $config);
  drupal_goto('admin/config/content/popup_maker', array(
    'query' => array(
      'popupId' => $id,
    ),
  ));
}
function popup_maker_update_status() {
  if (isset($_POST['sgpm-disable-popup'])) {
    popup_maker_disable_popup();
  }
  if (isset($_POST['sgpm-enable-popup'])) {
    popup_maker_enable_popup();
  }
  drupal_goto('admin/config/content/popup_maker');
}
function popup_maker_disable_popup() {
  if (!isset($_POST['id'])) {
    drupal_set_message('Parameter "id" is required to disable a popup', 'error');
    return false;
  }
  $id = abs(intval($_POST['id']));
  if ($id != $_POST['id']) {
    drupal_set_message('Parameter "id" must be non negative integer', 'error');
    return false;
  }
  $config = variable_get('popup_maker_settings', array(
    'api_key' => null,
    'popups' => array(),
    'popupSettings' => array(),
    'user' => array(),
  ));
  if (empty($config['popupSettings'])) {
    $config['popupSettings'] = array();
  }
  if (!isset($config['popupSettings'][$_POST['id']])) {
    $config['popupSettings'][$_POST['id']] = array();
  }
  $config['popupSettings'][$_POST['id']]['enabled'] = 0;
  variable_set('popup_maker_settings', $config);
  drupal_set_message('Popup Disabled successfully');
}
function popup_maker_enable_popup() {
  if (!isset($_POST['id'])) {
    drupal_set_message('Parameter "id" is required to enable a popup', 'error');
    return false;
  }
  $id = abs(intval($_POST['id']));
  if ($id != $_POST['id']) {
    drupal_set_message('Parameter "id" must be non negative integer', 'error');
    return false;
  }
  $config = variable_get('popup_maker_settings', array(
    'api_key' => null,
    'popups' => array(),
    'popupSettings' => array(),
    'user' => array(),
  ));
  if (empty($config['popupSettings'])) {
    $config['popupSettings'] = array();
  }
  if (!isset($config['popupSettings'][$_POST['id']])) {
    $config['popupSettings'][$_POST['id']] = array();
  }
  $config['popupSettings'][$_POST['id']]['enabled'] = 1;
  variable_set('popup_maker_settings', $config);
  drupal_set_message('Popup Enabled successfully');
}
function popup_maker_refresh_popups() {
  $config = variable_get('popup_maker_settings', array(
    'api_key' => null,
    'popups' => array(),
    'popupSettings' => array(),
    'user' => array(),
  ));
  if (popup_maker_refresh_data($config['api_key'])) {
    drupal_set_message('Popups list refreshed successfully');
  }
  drupal_goto('admin/config/content/popup_maker');
}
function popup_maker_update_api_key() {
  if (!isset($_POST['sgpm-api-key-submit'])) {
    drupal_set_message('Invalid request', 'error');
    return false;
  }
  if (!drupal_valid_token($_POST['_csrf_token'], 'popup_maker')) {
    drupal_set_message('Could not validate the security token', 'error');
    return false;
  }
  if (popup_maker_refresh_data($_POST['sgpm-api-key'])) {
    $config = variable_get('popup_maker_settings', array(
      'api_key' => null,
      'popups' => array(),
      'popupSettings' => array(),
      'user' => array(),
    ));
    $config['api_key'] = $_POST['sgpm-api-key'];
    variable_set('popup_maker_settings', $config);
    drupal_set_message('New api key saved successfully');
  }
  drupal_goto('admin/config/content/popup_maker');
}
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;
}