You are here

function popup_maker_page_build in Popup Maker - All popup types 7

File

./popup_maker.module, line 65

Code

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,
    );
  }
}