You are here

popup_maker.module in Popup Maker - All popup types 7

Same filename and directory in other branches
  1. 8 popup_maker.module

File

popup_maker.module
View source
<?php

define('SGPM_SERVICE_URL', 'https://popupmaker.com/');
function popup_maker_menu() {
  $items = array();
  $items['admin/config/content/popup_maker'] = array(
    'title' => 'Popup Maker',
    'description' => 'Edit Popup Maker settings',
    'page callback' => 'popup_maker_settings_view',
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'popup_maker.admin.inc',
  );
  return $items;
}
function popup_maker_theme() {
  return array(
    'popup_maker_settings' => array(
      'variables' => array(
        'settings' => array(),
        'token' => null,
        'edit' => false,
        'editingPopupId' => null,
        'displayRules' => null,
      ),
      'template' => 'popup_maker_settings',
    ),
    'popup_maker_display_rule' => array(
      'variables' => array(
        'rule' => array(),
        'index' => '{index}',
        'last' => false,
        'displayRules' => array(),
      ),
      'template' => 'popup_maker_display_rule',
    ),
    'popup_maker_edit_popup' => array(
      'variables' => array(
        'settings' => array(),
        'token' => null,
        'editingPopupId' => null,
      ),
      'template' => 'popup_maker_edit_popup',
    ),
    'popup_maker_api_credentials' => array(
      'variables' => array(
        'settings' => array(),
        'token' => null,
      ),
      'template' => 'popup_maker_api_credentials',
    ),
    'popup_maker_popups' => array(
      'variables' => array(
        'settings' => array(),
        'token' => null,
      ),
      'template' => 'popup_maker_popups',
    ),
  );
}
function popup_maker_page_build(&$page) {
  if ($node = menu_get_object()) {
    $config = variable_get('popup_maker_settings', array(
      'api_key' => null,
      'popups' => array(),
      'popupSettings' => array(),
      'user' => array(),
    ));
    $popups = $config['popups'];
    if (empty($popups)) {
      return;
    }
    $popupSettings = $config['popupSettings'];
    $template = '<script>
			window.SGPMPopupLoader=window.SGPMPopupLoader||{ids:[],popups:{},call:function(w,d,s,l,id){
				w[\'sgp\']=w[\'sgp\']||function(){(w[\'sgp\'].q=w[\'sgp\'].q||[]).push(arguments[0]);};
				var sg1=d.createElement(s),sg0=d.getElementsByTagName(s)[0];
				if(SGPMPopupLoader && SGPMPopupLoader.ids && SGPMPopupLoader.ids.length > 0){SGPMPopupLoader.ids.push(id); return;}
				SGPMPopupLoader.ids.push(id);
				sg1.onload = function(){SGPMPopup.openSGPMPopup();}; sg1.async=true; sg1.src=l;
				sg0.parentNode.insertBefore(sg1,sg0);
				return {};
			}};';
    foreach ($popups as $id => $popup) {
      if (!$popupSettings[$id]['enabled']) {
        continue;
      }
      $allowed = true;
      if (!empty($popupSettings[$id]['displayOptions'])) {
        $allowed = false;
        foreach ($popupSettings[$id]['displayOptions'] as $displayOption) {
          if (strpos($displayOption['param'], '_all')) {
            $endIndex = strpos($displayOption['param'], '_all');
            $contentType = substr($displayOption['param'], 0, $endIndex);
            $currentContentType = $node->type;
            if ($displayOption['operator'] === '==') {
              if ($currentContentType === $contentType) {
                $allowed = true;
              }
            }
            elseif ($currentContentType !== $contentType) {
              $allowed = true;
            }
          }
          else {
            if (strpos($displayOption['param'], '_selected')) {
              $values = $displayOption['value'];
              if ($displayOption['operator'] === '==') {
                if (in_array($node->nid, $values)) {
                  $allowed = true;
                }
              }
              elseif (!in_array($node->nid, $values)) {
                $allowed = true;
              }
            }
          }
        }
      }
      if ($allowed) {
        $template .= "SGPMPopupLoader.call(window,document,'script','" . SGPM_SERVICE_URL . "assets/lib/SGPMPopup.min.js','" . $popup['hashId'] . "');";
      }
    }
    $template .= '</script>';
    $page['page_bottom']['popup_maker'] = array(
      '#markup' => $template,
    );
  }
}