You are here

function tinybox_init in TinyBox (Simple Splash) 7.2

Same name and namespace in other branches
  1. 6 tinybox.module \tinybox_init()
  2. 7 tinybox.module \tinybox_init()

Implements hook_init().

File

./tinybox.module, line 45

Code

function tinybox_init() {
  global $uid;

  // Necessary to check if we're on the frontpage
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

  //Tinybox for Splash Screen
  $tinybox_splash_source_value = variable_get('tinybox_splash_source_value', '');
  $tinybox_splash_php = variable_get('tinybox_splash_php', '');
  $tinybox_width = variable_get('tinybox_width', 0);
  $tinybox_height = variable_get('tinybox_height', 0);
  $tinybox_autohide = variable_get('tinybox_autohide', 0);
  $tinybox_css = variable_get('tinybox_css', '');
  $tinybox_css = $tinybox_css == '' ? drupal_get_path('module', 'tinybox') : $tinybox_css;
  if ($tinybox_splash_source_value && drupal_is_front_page()) {
    if (module_exists('overlay') && path_is_admin($_GET['q'])) {

      //Known bug: does not work if F5 pressed!
      return;
    }
    if (!empty($tinybox_splash_php)) {
      if (!php_eval($tinybox_splash_php)) {
        return;
      }
    }
    drupal_add_js('sites/all/libraries/tinybox/tinybox.js', 'file');
    drupal_add_css($tinybox_css . '/tinybox.css');
    $tinybox_splash_source_type = variable_get('tinybox_splash_source_type', 'nid');
    $tinybox_splash_delay = variable_get('tinybox_splash_delay', 0);
    if ($tinybox_splash_delay) {
      if (!isset($_SESSION['tinybox_session_time'])) {
        $_SESSION['tinybox_session_time'] = time();
      }
      if (time() - $_SESSION['tinybox_session_time'] <= $tinybox_splash_delay) {
        return;
      }
      else {
        $_SESSION['tinybox_session_time'] = time();
      }
    }
    if ($tinybox_splash_source_type == 'nid') {
      $node = node_load($tinybox_splash_source_value);
      $tinybox_content = $node->body['und']['0']['safe_value'];
    }
    if ($tinybox_splash_source_type == 'content_type') {
      $sql = "SELECT nid FROM {node} n WHERE n.type = :type AND n.status=1 ORDER BY n.created DESC LIMIT 1";
      $result = db_query($sql, array(
        ':type' => $tinybox_splash_source_value,
      ));
      $record = $result
        ->fetchObject();
      $node = node_load($record->nid);
      $tinybox_content = $node->body['und']['0']['safe_value'];
    }
    if ($tinybox_splash_source_type == 'views') {
      if (module_exists('views')) {
        $view = views_get_view($tinybox_splash_source_value);
        if ($view) {
          $view
            ->execute();
          $tinybox_content = $view
            ->render();
        }
        else {
          $tinybox_content = 'Views not found: ' . $tinybox_splash_source_value;
        }
      }
      else {
        $tinybox_content = t('Require Views module.');
      }
    }
    $tinybox_content = str_replace(chr(13), '', $tinybox_content);
    $tinybox_content = str_replace(chr(10), '', $tinybox_content);
    $overlay_check = '';
    if (module_exists('overlay') && $uid == 1) {
      $overlay_check = 'if (Drupal.overlay.isOpen) { return; }';
    }
    drupal_add_js('
      window.onload = function() {
        ' . $overlay_check . '
        TINY.box.show({
          html: \'' . $tinybox_content . '\',
          boxid:\'tbox\',width:' . $tinybox_width . ',height:' . $tinybox_height . ',fixed:false,
          maskid:\'blackmask\',maskopacity:40,
          autohide:' . $tinybox_autohide . ',
          closejs:function(){closeJS()}
        });
        document.getElementsByClassName(\'tmask\')[0].onclick=null;
      }
    ', 'inline');
  }
}