You are here

function splashify_init in Splashify 7

Same name and namespace in other branches
  1. 6 splashify.display.inc \splashify_init()

Implements hook_init().

Generates the JS for the redirect, lightbox or popup window.

File

./splashify.display.inc, line 16
Handles displaying the splash page.

Code

function splashify_init() {
  global $user;
  global $base_url;

  // Load the jStorage library.
  $library = libraries_load('jstorage', 'minified');

  // If this is the cron script or drush, do not run the splashify code.
  $drush_check = function_exists('drush_verify_cli') && call_user_func('drush_verify_cli') ? TRUE : FALSE;
  $cron_check = strpos($_SERVER['PHP_SELF'], 'cron.php') !== FALSE ? TRUE : FALSE;
  if ($drush_check || $cron_check) {
    return;
  }

  /*
   * Step #1: Retrieve the admin settings.
   */
  $config = _splashify_get_config();
  $only_anonymous = $config['only_anonymous'];
  $when_frequency = $config['when_frequency'];
  $where_page = $config['where_page'];
  $where_listpages = $config['where_listpages'];
  $where_opposite = $config['where_opposite'];
  $what_mode = $config['what_mode'];
  $what_content_value = $config['what_content_value'];
  $how_mode = $config['how_mode'];
  $how_size = $config['how_size'];
  $how_delay = (int) $config['how_delay'];
  $how_autoclose = (int) $config['how_autoclose'];

  /*
   * Step #2: Should we display the splash page?
   */
  if ($only_anonymous && $user->uid > 0) {
    return;
  }
  if ($when_frequency == 'never') {
    return;
  }

  // Set variable whether to show page, default is true
  $is_role = TRUE;

  // Check whether use role setting is checked
  if ($config['when_roles']) {

    // If use role setting is checked set variable not to show.
    $is_role = FALSE;

    // Check if any roles match and then sat show to true.
    foreach ($user->roles as $v) {
      if (in_array($v, $config['when_roles_options'])) {
        $is_role = TRUE;
      }
    }
  }

  // Check the outcome of above and return if no roles match
  if (!$is_role) {
    return;
  }

  // Default to not showing the splash page.
  $splash_display = FALSE;
  $splash_get_var = '';
  if (isset($_GET['splash'])) {
    $splash_get_var = $_GET['splash'];
  }
  $splash_correct_page = FALSE;
  switch ($where_page) {
    case 'all':

      // Display on all pages.
      $splash_correct_page = TRUE;
      break;
    case 'home':
      $home_page = variable_get('site_frontpage', 'node');
      if ($where_opposite) {
        if ($_GET['q'] != $home_page) {

          // Display on every page except the home page.
          $splash_correct_page = TRUE;
        }
      }
      else {
        if ($_GET['q'] == $home_page) {

          // Display on the home page.
          $splash_correct_page = TRUE;
        }
      }
      break;
    case 'list':
      $list_paths = drupal_strtolower($where_listpages);
      $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
      $splash_correct_page = drupal_match_path($path, $list_paths);
      if ($path != $_GET['q']) {
        $splash_correct_page = $splash_correct_page || drupal_match_path($_GET['q'], $list_paths);
      }
      $splash_correct_page = ($splash_correct_page xor $where_opposite);
      break;
  }

  /*
   * Goes through the main server side checks to see if we should show the
   * splash page.
   */
  if (empty($what_content_value)) {

    // The What: Content field is not specified.
    $splash_display = FALSE;
  }
  elseif ($splash_get_var == 'off') {

    // Special case for preventing the splash page from showing up.
    $splash_display = FALSE;
  }
  elseif ($splash_get_var == 'on') {

    // Special case for forcing the splash page to show up.
    $splash_display = TRUE;

    // Modify this value to force the splash to show up in this case.
    $when_frequency = 'always';
  }
  elseif ($splash_correct_page) {

    // We need to show the splash page on this page. Do one last check.
    $parsed_url = parse_url($base_url);

    // We passed all of the tests...display the splash page!
    $splash_display = TRUE;
  }

  // If we shouldn't display the splash, end the code here.
  if (!$splash_display) {
    return;
  }

  /*
   * Step #3: Display the Splash Page
   *
   * At this point, we know that we should display the splash page.
   */

  // Controls when the jStorage variable should expire next. In other words,
  // when should the splash page show up again?
  $js_expiretime = '';

  // Since the time is based on server time, we need to pass this value to
  // the JS.
  $js_nowtime = time();

  // This variable is a way to always show the splash in the JS.
  $js_splash_always = FALSE;
  switch ($when_frequency) {
    case 'once':

      // Set to expire in one year.
      $js_expiretime = time() + 365 * 24 * 60 * 60;
      break;
    case 'daily':

      // Set to expire in 24 hours.
      $js_expiretime = time() + 24 * 60 * 60;
      break;
    case 'weekly':

      // Set to expire in 7 days.
      $js_expiretime = time() + 7 * 24 * 60 * 60;
      break;
    case 'always':

      // This should make the splash always show up on the next page load.
      $js_expiretime = time();
      $js_splash_always = TRUE;
      break;
  }

  // Deal with the mode settings.
  $js_mode_settings = array();
  $js_mode_settings['mode'] = $what_mode;
  $js_mode_settings['system_splash'] = '';
  $js_mode_settings['urls'] = '';
  if ($what_mode == 'template' || $what_mode == 'fullscreen') {

    // We need to redirect to the System generated splash page. Define the
    // action url. This variable tells the JS that we only are dealing with one
    // url.
    $js_mode_settings['system_splash'] = url('splashify-splash');
  }
  elseif ($what_mode == 'sequence' || $what_mode == 'random') {

    // Split up the textarea field by lines.
    $what_paths = preg_split('/[\\n\\r]+/', $what_content_value);

    // This holds all of the url values in the entered order.
    $js_mode_settings['urls'] = array();
    $js_mode_settings['total_urls'] = count($what_paths);
    foreach ($what_paths as $path) {
      $js_mode_settings['urls'][] = url(trim($path));
    }
  }

  // Define the last remaining JS variables we need to send.
  $size_action = $how_size ? explode('x', $how_size) : FALSE;
  $js_mode_settings['how_delay'] = $how_delay;
  $js_mode_settings['how_delay_enable'] = $how_delay > 0 ? 1 : 0;
  $js_mode_settings['how_autoclose'] = $how_autoclose + $how_delay;
  $js_mode_settings['how_autoclose_enable'] = $how_autoclose > 0 ? 1 : 0;
  $js_mode = '';
  switch ($how_mode) {
    case 'redirect':

      // Redirect to a different url.
      $js_mode = 'redirect';
      break;
    case 'window':

      // Open up a popup window.
      $js_mode = 'window';
      $js_mode_settings['size'] = $size_action ? 'width=' . $size_action[0] . ',height=' . $size_action[1] : '';
      break;
    case 'lightbox':
      if (module_exists('colorbox')) {

        // Display a ColorBox.
        $js_mode = 'colorbox';

        // Get the size of the lightbox.
        if (count($size_action) > 1) {
          $colorbox_width = $size_action[0];
          $colorbox_height = $size_action[1];
        }
        else {

          // Default size settings for the colorbox.
          $colorbox_width = 800;
          $colorbox_height = 600;
        }
        $js_mode_settings['size_width'] = $colorbox_width;
        $js_mode_settings['size_height'] = $colorbox_height;
      }
      else {
        return;
      }
      break;
    default:

      // Do nothing! This is a fail safe.
      return;
  }

  /*
   * Finally: Include the JS that puts it all together!
   *
   * At this point the page passed all of the server side checks. We now
   * implement JS code that checks if the splash page should show up, based
   * on when it last showed up. It then executes the JS action code, based
   * on the specified settings.
   */

  // Make our splash settings variables available to our JavaScript.
  $js_settings = array(
    'js_splash_always' => $js_splash_always ? '1' : '0',
    'js_expire_after' => $js_expiretime - $js_nowtime,
    'js_mode' => $js_mode,
    'js_mode_settings' => $js_mode_settings,
    'js_disable_referrer_check' => variable_get('disable_referrer_check', 0),
  );
  drupal_add_js(array(
    'splashify' => $js_settings,
  ), array(
    'type' => 'setting',
    'cache' => TRUE,
    'weight' => -100,
    'every_page' => TRUE,
  ));

  // Include the main JS file that does the heavy lifting.
  $js_splashify_int = drupal_get_path('module', 'splashify') . '/js/splashify_init.js';
  drupal_add_js($js_splashify_int, array(
    'type' => 'file',
    'scope' => 'header',
    'group' => JS_THEME,
    'cache' => TRUE,
    'weight' => -99,
    'every_page' => TRUE,
  ));
}