You are here

jquery_countdown.module in jQuery Countdown 6

provides the jquery_countdown theme function and serversync callback

File

jquery_countdown.module
View source
<?php

/**
 * @file
 * provides the jquery_countdown theme function and serversync callback
 */

// the default javascript serversync callback...
define('JQUERY_COUNTDOWN_SERVERSYNC_CALLBACK', 'Drupal.jQueryCountdown.serverSync');

// default path to the jqueryCountdown Library
define('JQUERY_COUNTDOWN_PATH', 'sites/all/libraries/jquery.countdown');

/**
 * Implementation of hook_menu()
 * provides the ajax callback for serversync option
 */
function jquery_countdown_menu() {
  $items = array();
  $items['jquery_countdown/serversync'] = array(
    'title' => 'jquery_countdown server time',
    'page callback' => 'jquery_countdown_serversync',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_help().
 */
function jquery_countdown_help($path, $args) {
  switch ($path) {
    case 'admin/help#jquery_countdown':
      $output = '<p>' . t('The <a href="@jquerycountdown">jQuery Countdown</a> plugin provides a live countdown timer. The following are some examples of it in use...', array(
        '@jquerycountdown' => 'http://keith-wood.name/countdown.html',
      )) . '</p>';

      // In this example, we use the theme function.
      $output .= '<p>' . theme('jquery_countdown', array(
        'since' => 'January 1, 2009',
        'format' => 'YOWDHMS',
        'description' => t('Since New Year'),
      )) . '</p>';

      // Provide the div and add the countdown timer to count from 10.
      $output .= '<h3>' . t('Blast off in 10 seconds') . '</h3><div id="countdownfrom20"></div>';
      jquery_countdown_add('#countdownfrom20', array(
        'until' => 10,
        // Numbers are counted as seconds.
        'format' => 'S',
        // Just display seconds.
        'expiryText' => t('<h1>Blast Off!</h1>'),
      ));

      // Provide the div and add the countdown timer to count until February 12th, 2020.
      $output .= '<h3>' . t('Countdown to February 12th, 2020') . '</h3><div id="countdownto2020"></div>';
      jquery_countdown_add('#countdownto2020', array(
        'until' => 'February 12, 2020',
      ));

      // Provide the div and add the countdown timer to count until 2010.
      $output .= '<h3>' . t('Countdown to 2030') . '</h3><div id="countdownto2030"></div>';
      jquery_countdown_add('#countdownto2030', array(
        'until' => array(
          2030,
          0,
          0,
        ),
      ));
      return $output;
      break;
  }
}

/**
 * page callback: simply returns the current server time and exit drupal
 */
function jquery_countdown_serversync() {
  drupal_set_header("Cache-Control: no-cache, must-revalidate");
  drupal_set_header("Expires: Fri, 1 Jan 2010 00:00:00 GMT");
  $now = new DateTime();
  echo $now
    ->format("M j, Y H:i:s O") . "\n";
  exit;
}

/**
 * Adds a jQuery Countdown JavaScript element to the page.
 *
 * @param $selector
 *   (optional) The jQuery selector to apply the countdown to. If a
 *   selector isn't provided, the jQuery Countdown plugin will just be
 *   added to the page.
 * @param $options
 *   (optional) The jQuery Countdown parameters to pass to the creation
 *   of the element. The available parameters can be found at:
 *   http://keith-wood.name/countdown.html#quick
 *   
 *   The date arguments (until or since), can either be a number
 *   representing the number of seconds, a string used to construct
 *   the JavaScript Date object, or the argument list passed to the
 *   JavaScript Date object.
 *
 *   The onExpiry and onTick arguments is JavaScript that will be
 *   passed through "eval()" when the events are called.
 */
function jquery_countdown_add($selector = NULL, $options = array()) {
  global $language;

  // Add jQuery Countdown only if it hasn't been added yet.
  static $jquery_countdown_added = FALSE;
  static $added_selectors = array();
  if ($jquery_countdown_added == FALSE) {

    // Add the stylesheet and the plugin depending on if we wanted the compressed one of not.
    drupal_add_css(jquery_countdown_get_css());
    drupal_add_js(jquery_countdown_get_js());

    // Enable localization if available by uses the drupal localize t function.
    if (isset($language->language)) {
      drupal_add_js(drupal_get_path('module', 'jquery_countdown') . '/jquery.countdown-drupal.js');
    }

    // Register the jQuery Countdown behaviour.
    drupal_add_js(drupal_get_path('module', 'jquery_countdown') . '/jquery_countdown.js');
    $jquery_countdown_added = TRUE;
  }

  // Make sure to only add the same selector once.
  if (!empty($selector) && !isset($added_selectors[$selector])) {

    // store the options directly in our static selector
    $added_selectors[$selector] = $options;

    // Add the countdown element to the settings so that it's processed by the behaviours.
    drupal_add_js(array(
      'jquery_countdown' => array(
        $selector => $options,
      ),
    ), 'setting');
  }
}

/**
 * Return the JS filename of the jQuery.Countdown plugin library.
 *
 * @return
 *   Boolean indicating if the JS is located.
 */
function jquery_countdown_get_js() {
  $library_path = jquery_countdown_get_path();
  if (file_exists($library_path . '/jquery.countdown.js') && file_exists($library_path . '/jquery.countdown.min.js')) {

    // use minified version if Javascript preprocessing is enabled...
    if (variable_get('preprocess_js', 0)) {
      $jquery_countdown_script = 'jquery.countdown.min.js';
    }
    else {
      $jquery_countdown_script = 'jquery.countdown.js';
    }
    return $library_path . '/' . $jquery_countdown_script;
  }
  else {
    drupal_set_message(t('You need to download the !jquerycountdownjs and extract the entire contents of the archive into the %path folder of your server.', array(
      '!jquerycountdownjs' => l(t('jQuery.Countdown plugin'), 'http://keith-wood.name/countdown.html'),
      '%path' => $library_path,
    )), 'error', FALSE);
    return FALSE;
  }
}

/**
 * Return the CSS filename of the jQuery.Countdown plugin library
 */
function jquery_countdown_get_css() {
  $library_path = jquery_countdown_get_path();
  if (file_exists($library_path . '/jquery.countdown.css')) {
    return $library_path . '/jquery.countdown.css';
  }
  return FALSE;
}

/**
 * Return the path to the jQuery.Countdown plugin.
 */
function jquery_countdown_get_path() {
  static $library_path = NULL;

  // Try to locate the library path in any possible setup.
  if ($library_path == NULL) {

    // First check the default location.
    $path = JQUERY_COUNTDOWN_PATH;
    if (is_dir($path)) {
      $library_path = $path;
    }
    elseif ($library_path == NULL && module_exists('libraries')) {
      if ($path = libraries_get_path('jquery.countdown')) {
        $library_path = $path;
      }
    }
  }
  return $library_path;
}

/**
 * Implementation of hook_theme().
 * jquery_countdown:
 * you can use this theme function in own module or template to display a countdown...
 *
 * Example usage:
 *  echo theme('jquery_countdown', array(
 *   'since' => date("F d, Y g:i a", mktime(0,0,0,0,0,2009)),
 *   'format' => 'YOWDHMS',
 *   'description' => t('Since New Year'),
 * ));
 * 
 * jquery_countdown_layout:
 *   used to provide a configurable way of jquery.countdown layouts
 */
function jquery_countdown_theme($existing, $type, $theme, $path) {
  return array(
    'jquery_countdown' => array(
      'arguments' => array(
        'options' => NULL,
        'content' => NULL,
        'id' => NULL,
      ),
      'file' => 'jquery_countdown.theme.inc',
    ),
    'jquery_countdown_layout' => array(
      'arguments' => array(
        'jcid' => 'jquery_countdown',
      ),
      'template' => 'jquery-countdown-layout',
      'file' => 'jquery_countdown.theme.inc',
    ),
  );
}
function _jquery_countdown_settings_form($options = array()) {
  $form = array();
  $form['jquery_countdown'] = array(
    '#type' => 'fieldset',
    '#title' => t('jQuery countdown settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  );
  $default_value = isset($options['admin_title']) ? $options['admin_title'] : NULL;
  $form['jquery_countdown']['admin_title'] = array(
    '#type' => 'textfield',
    '#default_value' => $default_value,
    '#title' => t('Administrative title'),
    '#description' => t('This title will be used administratively to identify this block. If blank, a default title will be used.'),
  );
  $default_value = isset($options['date']) ? $options['date'] : NULL;
  $datetype = module_exists('date_popup') ? 'date_popup' : 'date_select';
  $form['jquery_countdown']['date'] = array(
    '#type' => $datetype,
    '#title' => t('Countdown date'),
    '#required' => TRUE,
    '#default_value' => $default_value,
    '#date_format' => 'Y-m-d g:i a',
  );
  $default_value = isset($options['until']) ? $options['until'] : 'until';
  $form['jquery_countdown']['until'] = array(
    '#type' => 'radios',
    '#title' => t('Counter type'),
    '#required' => TRUE,
    '#options' => array(
      'until' => 'Count down to date',
      'since' => 'Count up since date',
    ),
    '#default_value' => $default_value,
    '#description' => t('Whether the counter should count down to a date in the future or up from a date in the past.'),
  );
  $default_value = isset($options['serversync']) ? $options['serversync'] : TRUE;
  $form['jquery_countdown']['serversync'] = array(
    '#type' => 'checkbox',
    '#title' => t('use servertime'),
    '#description' => t('if checked the countdown uses the servertime instead of the client time as base time'),
    '#default_value' => $default_value,
  );
  $default_value = isset($options['format']) ? $options['format'] : array(
    'D',
    'H',
    'M',
    'S',
  );
  $form['jquery_countdown']['format'] = array(
    '#type' => 'select',
    '#title' => t('Granularity'),
    '#required' => TRUE,
    '#multiple' => TRUE,
    '#options' => array(
      'Y' => 'Years',
      'O' => 'Months',
      'W' => 'Weeks',
      'D' => 'Days',
      'H' => 'Hours',
      'M' => 'Minutes',
      'S' => 'Seconds',
    ),
    '#default_value' => $default_value,
    '#description' => t('Which elements of the time to show. Default value is Days, Hours, Minutes, and Seconds.'),
  );
  $default_value = isset($options['compact']) ? $options['compact'] : FALSE;
  $form['jquery_countdown']['compact'] = array(
    '#type' => 'checkbox',
    '#title' => t('use compact mode'),
    '#description' => t('the compact mode remove the full labels'),
    '#default_value' => $default_value,
  );
  $default_value = isset($options['layout']) ? $options['layout'] : FALSE;
  $form['jquery_countdown']['layout'] = array(
    '#type' => 'checkbox',
    '#title' => t('use layout template file'),
    '#description' => t('if enabled you can use a template file for the layout'),
    '#default_value' => $default_value,
  );
  $default_value = isset($options['caption']) ? $options['caption'] : NULL;
  $form['jquery_countdown']['caption'] = array(
    '#type' => 'textarea',
    '#title' => t('Counter description'),
    '#default_value' => $default_value,
    '#description' => t('This text is shown beneath the counter.'),
  );
  return $form;
}

Functions

Namesort descending Description
jquery_countdown_add Adds a jQuery Countdown JavaScript element to the page.
jquery_countdown_get_css Return the CSS filename of the jQuery.Countdown plugin library
jquery_countdown_get_js Return the JS filename of the jQuery.Countdown plugin library.
jquery_countdown_get_path Return the path to the jQuery.Countdown plugin.
jquery_countdown_help Implementation of hook_help().
jquery_countdown_menu Implementation of hook_menu() provides the ajax callback for serversync option
jquery_countdown_serversync page callback: simply returns the current server time and exit drupal
jquery_countdown_theme Implementation of hook_theme(). jquery_countdown: you can use this theme function in own module or template to display a countdown...
_jquery_countdown_settings_form

Constants