Commit 7c745e95 authored by Chris Roane's avatar Chris Roane
Browse files

Issue #[1425392] by klausi: Re-worked the JS and applied some major changes

- Surrounded some output text that I missed with the t() function.
- Took out the help text from the main config page and included it in hook_help(). This is in the .module file that was in the previous commit.
- Took out some basic JavaScript and put it into a separate JS file.
- Took out using the filter_xss() function where it was needed.
- Simplified a few if statements.
- I re-wrote the whole splash display mechanism. All of the JavaScript is now in a separate file and we send drupal settings variables to use in the JS. This makes the JS much easier to maintain and debug. This required a lot of re-working in the splashify_init() function.
parent 69c736be
Loading
Loading
Loading
Loading
+14 −0
Changes for js/spotify_admin_where.js: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
jQuery(document).ready(function(){
  if(jQuery("#edit-splashify-where-desktop-listpages").val() != '') {
    jQuery("#desktop_listpages").show();
  }

  jQuery("input[name='splashify_where_desktop_page']").change(function(){
    if(jQuery('#edit-splashify-where-desktop-page-list').is(':checked')){
      jQuery("#desktop_listpages").show();
    } else {
      jQuery("#desktop_listpages").hide();
      jQuery("#edit-splashify-where-desktop-listpages").val('');
    }
  });
});
 No newline at end of file

js/spotify_init.js

0 → 100644
+56 −0
Changes for js/spotify_init.js: 56 added lines, 0 removed lines.
Original line number Diff line number Diff line
(function ($) {

  Drupal.behaviors.splashify = {
    attach: function (context, settings){

      var splash = $.jStorage.get("splash");
      var nowtimeSeconds = settings.splashify.js_nowtime;
      var splashalways = settings.splashify.js_splash_always;
      if(!splash || splash < nowtimeSeconds || splashalways=='1'){
        // Set when the splash variable should expire.
        $.jStorage.set("splash", settings.splashify.js_expiretime);

        // If we are displaying each splash page in sequence, we need to
        // determine the next url.
        if(settings.splashify.js_mode_settings.urls){
          var new_url_index = 0;
          var last_url = $.jStorage.get('splashlasturl', '');
          var what_urls = settings.splashify.js_mode_settings.urls;
          var last_url_index = what_urls.indexOf(last_url);
          if(last_url_index > -1 && last_url_index+1 < settings.splashify.js_mode_settings.total_urls){
            new_url_index = last_url_index + 1;
          }
          var url = what_urls[new_url_index];
          $.jStorage.set('splashlasturl', url);

          // We have the correct url, now set the main url value.
          settings.splashify.js_mode_settings.url = url;
        }

        // Do the splash action.
        if(settings.splashify.js_mode == 'colorbox'){
          // Open a ColorBox
          $(window).load(function(){
            $.colorbox({
              transition:'elastic',
              iframe:'true',
              href:settings.splashify.js_mode_settings.url,
              width:settings.splashify.js_mode_settings.size_width,
              height:settings.splashify.js_mode_settings.size_height
            });
          });
        } else if(settings.splashify.js_mode == 'redirect'){
          // Redirect
          window.location.replace(settings.splashify.js_mode_settings.url);
        } else if(settings.splashify.js_mode == 'window'){
          // Open a popup window
          $(window).load(function(){
            window.open(settings.splashify.js_mode_settings.url, 'splash', settings.splashify.js_mode_settings.size);
          });
        }
      }

    }
  };

})(jQuery);
+39 −70
Changes for splashify.admin.inc: 39 added lines, 70 removed lines.
Original line number Diff line number Diff line
@@ -13,46 +13,21 @@
 * "Splashify" settings tab.
 */
function splashify_admin_main() {
  return <<<EOD
  <p>Access the tabs on this page to update the settings for this module.</p>
  <p>This module was based on the Drupal 6 module <a href="http://drupal.org/project/splash" target="_blank">Splash</a>.
  Instead of using cookies to determine when the splash page shows up, we use
  <a href="http://www.jstorage.info/" target="_blank">jStorage</a>. This allows
  us to not depend on cookies and provides support for other browsers. The big
  benefit, however, is that this prevents search engines from indexing the
  splash page in place of the correct page. The idea here is not to trick
  Google, but to make sure the splash page is not indexed instead of the
  correct page.</p>
  <p>Below is a breakdown of the features of this module:</p>
  <ul>
    <li><strong>Mobile</strong>: When the <a href="http://drupal.org/project/mobile_tools" target="_blank">Mobile Tools</a>
    module is installed, allows for settings to be set to create or prevent a
    splash page on mobile devices. These settings are separate from the desktop
    settings that can be defined.</li>
    <li><strong>Search Engine Friendly</strong>: Instead of redirecting the
    user from PHP, we redirect with JS. This prevents the redirect from
    happening when search engine crawlers visit the page. And if JS is
    disabled, the original page shows up and no redirect happens. So your
    splash pages should not be indexed over the top of the page that should be
    indexed!</li>
    <li><strong>Scope</strong>: Allow the splash page to appear on any page of
    your site. For example, you can have the splash page appear when someone
    goes to an interior page (but the system prevents the splash page from
    showing up again when they click another link within your site).</li>
    <li><strong>Lightbox</strong>: Supports <a href="http://drupal.org/project/colorbox" target="_blank">ColorBox</a>
    as the lightbox jQuery plugin.</li>
  </ul>
  <br />
  <h2>What do we consider a splash page?</h2>
  <p>We define a splash page as being content that shows up at a specified
  interval, at a specified location, one time. If someone goes to a certain
  page for the first time, we want to make sure the splash content shows up.
  But if the person is coming from an internal link on the site, we don't want
  the splash content to show up over and over again (otherwise, the splash page
  would become a nuisance).</p>
  <p>We determine how often the splash page should show up by what is defined
  under the "When" tab.</p>
EOD;
  $output = '';
  $output .= '<p>' . t('Take a look at the !splashify_help. You can also look at the !splashify_project to see a list of features.', array(
    '!splashify_help' => l(t('Splashify Help Page'), 'admin/help/splashify'),
    '!splashify_project' => l(t('Splashify Project Page'),
      'http://drupal.org/sandbox/chrisroane/1423456', array(
        'attributes' => array(
          'target' => '_blank',
        ),
      )
    ),
  )) . '</p>';
  
  $output .= '<p>' . t('Use the tabs above to access the settings for this module.') . '</p>';
  
  return $output;
}

/**
@@ -62,7 +37,7 @@ function splashify_admin_when_form() {
  $form = array();

  $form['description'] = array(
    '#markup' => '<p>When should the splash page show up?</p>',
    '#markup' => '<p>' . t('When should the splash page show up?') . '</p>',
  );

  $form['desktop'] = array(
@@ -93,11 +68,21 @@ function splashify_admin_when_form() {
    $disable_mobile = FALSE;
  }

  $mobiletools_link = t('In order to use this feature, you need to have the !mobiltools module enabled.', array(
    '!mobiltools' => l(t('Mobile Tools'),
      'http://drupal.org/project/mobile_tools', array(
        'attributes' => array(
          'target' => '_blank',
        ),
      )
    ),
  ));

  $form['mobile']['splashify_when_mobile'] = array(
    '#type' => 'checkbox',
    '#title' => 'Enable Unique Mobile Splash',
    '#title' => t('Enable Unique Mobile Splash'),
    '#default_value' => variable_get('splashify_when_mobile', 0),
    '#description' => 'In order to use this feature, you need to have the <a href="http://drupal.org/project/mobile_tools" target="_blank">Mobile Tools</a> module enabled.',
    '#description' => $mobiletools_link,
    '#disabled' => $disable_mobile,
  );

@@ -120,7 +105,7 @@ function splashify_admin_when_form() {
      '#type' => 'checkbox',
      '#title' => 'Test Mobile',
      '#default_value' => variable_get('splashify_when_mobile_test', 0),
      '#description' => 'Turn this option on to force the mobile settings to take affect so you can test from your desktop browser.',
      '#description' => t('Turn this option on to force the mobile settings to take affect so you can test from your desktop browser.'),
      '#disabled' => $disable_mobile,
    );
  }
@@ -135,27 +120,11 @@ function splashify_admin_where_form() {
  $form = array();

  // Add Javascript to handle the List Pages field.
  $javascript = <<<EOD
  jQuery(document).ready(function(){
    if(jQuery("#edit-splashify-where-desktop-listpages").val() != '') {
      jQuery("#desktop_listpages").show();
    }

    jQuery("input[name='splashify_where_desktop_page']").change(function(){
      if(jQuery('#edit-splashify-where-desktop-page-list').is(':checked')){
        jQuery("#desktop_listpages").show();
      } else {
        jQuery("#desktop_listpages").hide();
        jQuery("#edit-splashify-where-desktop-listpages").val('');
      }
    });
  });
EOD;

  drupal_add_js($javascript, array('type' => 'inline'));
  $js_file = drupal_get_path('module', 'splashify') . '/js/spotify_admin_where.js';
  drupal_add_js($js_file, 'file');
  
  $form['description'] = array(
    '#markup' => '<p>Where should the splash page come up?</p>',
    '#markup' => '<p>' . t('Where should the splash page come up?') . '</p>',
  );

  $form['desktop'] = array(
@@ -224,7 +193,7 @@ EOD;
  }
  else {
    $form['mobile']['splashify_what_mobile_mode'] = array(
      '#markup' => '<p>' . t('In order to specify mobile options, you need to enable the When: Enable Unique Mobile Splash option.') . '</p>',
      '#markup' => '<p>' . t('In order to specify mobile options, you need to enable the "When: Enable Unique Mobile Splash" option.') . '</p>',
    );
  }

@@ -238,7 +207,7 @@ function splashify_admin_what_form() {
  $form = array();

  $form['description'] = array(
    '#markup' => '<p>What should show up for the splash page?</p>',
    '#markup' => '<p>' . t('What should show up for the splash page?') . '</p>',
  );

  $form['desktop'] = array(
@@ -299,7 +268,7 @@ function splashify_admin_what_form() {
  }
  else {
    $form['mobile']['splashify_what_mobile_mode'] = array(
      '#markup' => '<p>' . t('In order to specify mobile options, you need to enable the When: Enable Unique Mobile Splash option.') . '</p>',
      '#markup' => '<p>' . t('In order to specify mobile options, you need to enable the "When: Enable Unique Mobile Splash" option.') . '</p>',
    );
  }

@@ -313,7 +282,7 @@ function splashify_admin_how_form() {
  $form = array();

  $form['description'] = array(
    '#markup' => '<p>How should the splash page come up?</p>',
    '#markup' => '<p>' . t('How should the splash page come up?') . '</p>',
  );

  // Specify the display mode options for the splash page.
@@ -326,7 +295,7 @@ function splashify_admin_how_form() {
    $splashify_how_mode_options['lightbox'] = t('Open in a Lightbox (colorbox)');
  }
  else {
    $colorbox_warning = t("In order to access the lightbox option, you need to have the !colorbox module installed.", array(
    $colorbox_warning = t('In order to access the lightbox option, you need to have the !colorbox module installed.', array(
      '!colorbox' => l(t('Colorbox'),
        'http://drupal.org/project/colorbox', array(
          'attributes' => array(
@@ -336,7 +305,7 @@ function splashify_admin_how_form() {
      ),
    ));

    drupal_set_message(filter_xss($colorbox_warning), 'warning');
    drupal_set_message($colorbox_warning, 'warning');
  }

  $form['desktop'] = array(
@@ -405,7 +374,7 @@ function splashify_admin_how_form() {
  }
  else {
    $form['mobile']['splashify_how_mobile_mode'] = array(
      '#markup' => '<p>' . t('In order to specify mobile options, you need to enable the When: Enable Unique Mobile Splash option.') . '</p>',
      '#markup' => '<p>' . t('In order to specify mobile options, you need to enable the "When: Enable Unique Mobile Splash" option.') . '</p>',
    );
  }

+64 −129
Changes for splashify.display.inc: 64 added lines, 129 removed lines.
Original line number Diff line number Diff line
@@ -15,27 +15,22 @@
 */
function splashify_init() {
  global $base_url;
  $library = libraries_load('jstorage', 'minified');

  // Make sure the jStorage library is installed.
  if (!file_exists('sites/all/libraries/jstorage/jstorage.min.js') && !file_exists('sites/all/libraries/jstorage/jstorage.js')) {
    $jstorage_error = t('In order for Splashify to work, the !jstorage library needs to be installed under: sites/all/libraries/jstorage/ .', array(
      '!jstorage' => l(t('jStorage'),
      'http://www.jstorage.info/', array(
        'attributes' => array(
          'target' => '_blank',
        ),
      )),
    ));

    drupal_set_message(filter_xss($jstorage_error), 'error');
  if (!$library['installed']) {
    drupal_set_message('In order for Splashify to work, the jStorage library needs to be installed. See the README.txt file for more information.', 'error');
    return;
  }

  // If this is the cron script or drush, do not run the splashify code.
  if ((function_exists('drush_verify_cli') && call_user_func('drush_verify_cli')) || (strpos($_SERVER['PHP_SELF'], 'cron.php') !== FALSE)) {
  $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.
   */
@@ -53,20 +48,16 @@ function splashify_init() {


  /*
   * Step #2: Should we display the splash?
   * Step #2: Should we display the splash page?
   */

  // Default to not showing the splash page.
  $splash_display = FALSE;
  $splash_reason = '';

  if (!isset($_GET['splash'])) {
    $_GET['splash'] = '';
  }

  /*
   * Determine if this page should show the splash page.
   */

  $splash_correct_page = FALSE;
  if ($where_page == 'all') {
    // Display on all pages.
@@ -98,26 +89,26 @@ function splashify_init() {

  if (empty($what_content_value)) {
    // The What: Content field is not specified.
    $splash_reason = 'No What: Content value.';
    $splash_display = FALSE;
  }
  elseif ($_GET['splash'] == 'off') {
    // Special case for preventing the splash page from showing up.
    $splash_reason = 'Splash page forced not to be displayed.';
    $splash_display = FALSE;
  }
  elseif ($_GET['splash'] == 'on') {
    // Special case for forcing the splash page to show up.
    $splash_display = TRUE;
    $splash_reason = 'Splash page forced to be displayed.';
  }
  elseif ($splash_correct_page) {
    // We need to show the splash page on this page.
    // Make sure we are not coming from an internal page.
    // We need to show the splash page on this page. Do one last check.
    if (($parsed_url = parse_url($base_url)) && stristr($_SERVER['HTTP_REFERER'], $parsed_url['host'])) {
      $splash_reason = 'Correct page is loaded from an internal link.';
      // This page was loaded from an internal page. Do not show the splash
      // page.
      $splash_display = FALSE;
    }
    else {
      // We passed all of the tests...display the splash page!
      $splash_display = TRUE;
      $splash_reason = 'This is the correct page.';
    }
  }
  
@@ -133,11 +124,7 @@ function splashify_init() {
   * At this point, we know that $splash_display == TRUE.
   */


  /*
   * Determine the time values.
   */

  // Determine the time values.

  // Controls when the jStorage variable should expire next. In other words,
  // when should the splash page show up again?
@@ -148,7 +135,7 @@ function splashify_init() {
  $js_nowtime = time();

  // This variable is a way to always show the splash in the JS.
  $js_splash_always = 'false';
  $js_splash_always = FALSE;

  // Once.
  if ($when_frequency == 'once') {
@@ -169,28 +156,20 @@ function splashify_init() {
  elseif ($when_frequency == 'always' || $_GET['splash'] == 'on') {
    // This should make the splash always show up.
    $js_expiretime = time();
    $js_splash_always = 'true';
    $js_splash_always = TRUE;
  }

  /*
   * If we are working with a url, we need to specify that url value here
   */

  $action_url = '';
  $js_before = '';

  // If we are working with a url, we need to specify that url value here.
  $url = '';
  $js_mode_settings = array();
  if ($what_mode == 'template' || $what_mode == 'fullscreen') {

    // We need to redirect to the /splash page.
    // Define the action url.
    $js_before .= "var action_url = 'splash';";
    // We need to redirect to the /splash page. Define the action url.
    $js_mode_settings['url'] = 'splash';
  }
  elseif ($what_mode == 'sequence' || $what_mode == 'random') {

    // We are working with url values in the What: Content field.
    // We are working with url values in the "What: Content" field.
    $paths = preg_split('/[\n\r]+/', $what_content_value);
    if ($what_mode == 'sequence') {

      /*
       * Since we are using jStorage to store the last splash page that was
       * displayed, we need to create an array of the sequential url's and
@@ -198,85 +177,57 @@ function splashify_init() {
       * url)...all through JS.
       */

      // We don't use this value in this case, so set it to empty.
      $js_mode_settings['url'] = '';

      // Create the JS array of url values.
      $js_before .= 'var what_urls = [];';
      $path_count = 0;

      // This holds all of the url values in the correct order.
      $js_mode_settings['urls'] = array();
      $js_mode_settings['total_urls'] = count($paths);
      foreach ($paths as $path) {
        $path = url(drupal_strip_dangerous_protocols(trim($path)));
        $js_before .= "what_urls[{$path_count}] = '{$path}';";
        $path_count++;
        $js_mode_settings['urls'][] = url(drupal_strip_dangerous_protocols(trim($path)));
      }

      // Default the new url index value to the first url.
      $js_before .= "var new_url_index = 0;";

      // Have we already loaded a splash page? If so, grab the index of
      // that url.
      $js_before .= "var last_url = $.jStorage.get('splashlasturl', '');";
      $js_before .= "var last_url_index = what_urls.indexOf(last_url);";

      // Make sure the last url index value is correct.
      // If so, set the new index url to the [last index url] + 1.
      $js_before .= "if(last_url_index > -1 && last_url_index+1 < {$path_count}) { new_url_index = last_url_index + 1; }";

      // Define the action url.
      $js_before .= "var action_url = what_urls[new_url_index];";

      // Set the splash url used for this page, so we can display the correct
      // splash on the next page load.
      $js_before .= "$.jStorage.set('splashlasturl', action_url);";
    }
    elseif ($what_mode == 'random') {

      $next_index = array_rand($paths);

      // Define the action url.
      $js_before = "var action_url = '" . url(drupal_strip_dangerous_protocols(trim($paths[$next_index]))) . "';";
      $js_mode_settings['url'] = url(drupal_strip_dangerous_protocols(trim($paths[$next_index])));
    }
  }

  /*
   * Generate the JS code that should be executed when the splash page should
   * show up.
   */

  // Define the last remaining JS variables we need to send.
  $size_action = $how_size ? explode('x', $how_size) : FALSE;
  $js_action = '';
  $js_mode = '';
  if ($how_mode == 'redirect') {

    // Redirect to a different url.
    $js_action = "{$js_before} window.location = action_url;";
    $js_mode = 'redirect';
  }
  elseif ($how_mode == 'window') {

    // Open up a popup window.
    $size_str = $size_action ? ', "width=' . $size_action[0] . ',height=' . $size_action[1] . '"' : '';
    $js_action = "{$js_before} window.onload = function() { window.open(action_url, 'splash'{$size_str}); };";
    $js_mode = 'window';
    $js_mode_settings['size'] = $size_action ? 'width=' . $size_action[0] . ',height=' . $size_action[1] : '';
  }
  elseif ($how_mode == 'lightbox' && module_exists('colorbox')) {
    // Display a ColorBox.
    $js_mode = 'colorbox';

    // Open up a lightbox.
    $colorbox_settings = array();
    $colorbox_settings['transition'] = "transition:'elastic'";
    $colorbox_settings['iframe'] = "iframe:'true'";
    $colorbox_settings['href'] = 'href:action_url';

    // Set size.
    // Get the size of the lightbox.
    if (count($size_action) > 1) {
      $colorbox_settings['width'] = 'width:' . $size_action[0];
      $colorbox_settings['height'] = 'height:' . $size_action[1];
      $colorbox_width = $size_action[0];
      $colorbox_height = $size_action[1];
    }
    else {
      // The colorbox has strange behavior when we don't set the dimensions.
      $colorbox_settings['width'] = 'width:800';
      $colorbox_settings['height'] = 'height:600';
      // Default size settings for the colorbox.
      $colorbox_width = 800;
      $colorbox_height = 600;
    }

    $colorbox_load_string = implode(', ', $colorbox_settings);

    // Generate the JS to load the colorbox after the page loads.
    $js_action = '$(document).ready(function(){ ' . $js_before . ' $.colorbox({' . $colorbox_load_string . '}); });';
    $js_mode_settings['size_width'] = $colorbox_width;
    $js_mode_settings['size_height'] = $colorbox_height;
  }
  else {
    // Do nothing! This is a fail safe.
@@ -284,46 +235,30 @@ function splashify_init() {
  }

  /*
   * Finally: Generate the JS that puts it all together!
   * 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.
   * on when it last showed up. It then executes the JS action code, based
   * on the specified settings.
   */

  $splash_js = <<<EOD
  (function ($) {
    var splash = $.jStorage.get("splash");
    var nowtimeSeconds = {$js_nowtime};
    if(!splash || splash < nowtimeSeconds || {$js_splash_always}){
      // Set when the splash variable should expire.
      $.jStorage.set("splash", {$js_expiretime});

      // Do the splash action.
      {$js_action}
    }
  })(jQuery);
EOD;

  // Add jstorage (minified or not)
  if (file_exists('sites/all/libraries/jstorage/jstorage.min.js')) {
    $jstorage_file = 'sites/all/libraries/jstorage/jstorage.min.js';
  }
  else {
    $jstorage_file = 'sites/all/libraries/jstorage/jstorage.js';
  }
  // Make our splash settings variables available to our JavaScript.
  $js_settings = array(
    'js_nowtime' => $js_nowtime,
    'js_splash_always' => $js_splash_always ? '1' : '0',
    'js_expiretime' => $js_expiretime,
    'js_action' => $js_action,
    'js_mode' => $js_mode,
    'js_mode_settings' => $js_mode_settings,
  );
  drupal_add_js(array('splashify' => $js_settings), 'setting');

  drupal_add_js($jstorage_file, array(
  // Include the main JS file that does the heavy lifting.
  $js_spotify_int = drupal_get_path('module', 'splashify') . '/js/spotify_init.js';
  drupal_add_js($js_spotify_int, array(
    'type' => 'file',
    'scope' => 'header',
    'group' => JS_LIBRARY,
    'weight' => 0,
    'cache' => FALSE,
  ));

  drupal_add_js($splash_js, array(
    'type' => 'inline',
    'scope' => 'header',
    'group' => JS_DEFAULT,
    'weight' => 5,
    'cache' => FALSE,