You are here

function tinybox_init in TinyBox (Simple Splash) 6

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

Implements hook_init().

File

./tinybox.module, line 40

Code

function tinybox_init() {

  // Necessary to check if we're on the frontpage
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  if (drupal_is_front_page()) {
    $tinybox_splash_source_type = variable_get('tinybox_splash_source_type', 'nid');
    $tinybox_splash_source_value = variable_get('tinybox_splash_source_value', '');
    if (!$tinybox_splash_source_value) {
      return;
    }
    $tinybox_splash_delay = variable_get('tinybox_splash_delay', 0);
    if ($tinybox_splash_delay) {
      if (!isset($_SESSION['tinybox_splash_session_time'])) {
        $_SESSION['tinybox_splash_session_time'] = time();
      }
      if (time() - $_SESSION['tinybox_splash_session_time'] <= $tinybox_splash_delay) {
        return;
      }
      else {
        $_SESSION['tinybox_splash_session_time'] = time();
      }
    }
    $tinybox_splash_autohide = variable_get('tinybox_splash_autohide', 0);
    $tinybox_splash_css = variable_get('tinybox_splash_css', '');
    $tinybox_splash_css = $tinybox_splash_css == '' ? drupal_get_path('module', 'tinybox') : $tinybox_splash_css;
    drupal_add_js('sites/all/libraries/tinybox/tinybox.js', 'module');
    drupal_add_css($tinybox_splash_css . '/tinybox.css');
    if ($tinybox_splash_source_type == 'nid') {
      $node = node_load($tinybox_splash_source_value);
      $tinybox_content = str_replace(chr(13), '', $node->body);
      $tinybox_content = str_replace(chr(10), '', $tinybox_content);
    }
    if ($tinybox_splash_source_type == 'content_type') {
      $sql = "SELECT nid FROM {node} n WHERE n.type = '%s' AND n.status=1 ORDER BY n.created DESC";
      $results = db_query_range($sql, $tinybox_splash_source_value, 0, 1);
      $data = db_fetch_object($results);
      $node = node_load($data->nid);
      $tinybox_content = str_replace(chr(13), '', $node->body);
      $tinybox_content = str_replace(chr(10), '', $tinybox_content);
    }
    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);
    }
    $tinybox_splash_width = variable_get('tinybox_splash_width', '');
    $tinybox_splash_height = variable_get('tinybox_splash_height', '');
    $tinybox_splash_width = $tinybox_splash_width == '' ? 0 : $tinybox_splash_width;
    $tinybox_splash_height = $tinybox_splash_width == '' ? 0 : $tinybox_splash_height;
    drupal_add_js('
      window.onload = function() {
        TINY.box.show({
          html: \'' . $tinybox_content . '\',
          boxid:\'tbox\',width:' . $tinybox_splash_width . ',height:' . $tinybox_splash_height . ',fixed:false,
          maskid:\'blackmask\',maskopacity:40,
          autohide:' . $tinybox_splash_autohide . ',
          closejs:function(){closeJS()}
        });
        document.getElementsByClassName(\'tmask\')[0].onclick=null;
      }
    ', 'inline');
  }
}