You are here

countdown.inc in Countdown 7.2

File

plugins/countdown/countdown.inc
View source
<?php

/**
 * @file
 * Provides a custom countdown script.
 *
 * This is the original countdown script from the 7.x-1.x branch of this module.
 * Known limitations:
 *  - is currently broken!
 *  - only allows one countdown per page.
 */
$plugin = array(
  'label' => t('Countdown Plugin'),
  'description' => t('A simple countdown.'),
  'callback' => 'countdown_countdown_build',
  'supported_options' => array(
    'countdown_elements',
    'event_name',
    'event_url',
  ),
);

/**
 * Countdown build callback.
 *
 * @param $plugin
 *  The plugin definition.
 * @param $countdown_options
 *  The array of options from the countdown box plugin.
 *
 * @return
 *  A build array.
 */
function countdown_countdown_build($plugin, $countdown_options) {
  $output = '';
  $time = time();
  $difference = $countdown_options['countdown_target_timestamp'] - $time;
  if ($difference < 0) {
    $passed = 1;
    $difference = abs($difference);
  }
  else {
    $passed = 0;
  }
  $accuracy = date_granularity_precision($countdown_options['countdown_elements']);
  $days_left = floor($difference / 60 / 60 / 24);
  $hrs_left = floor(($difference - $days_left * 60 * 60 * 24) / 60 / 60);
  $min_left = floor(($difference - $days_left * 60 * 60 * 24 - $hrs_left * 60 * 60) / 60);
  $secs_left = floor($difference - $days_left * 60 * 60 * 24 - $hrs_left * 60 * 60 - $min_left * 60);
  $output .= format_plural($days_left, '1 day', '@count days');
  if ($accuracy == 'hour' || $accuracy == 'minute' || $accuracy == 'second') {
    $output .= format_plural($hrs_left, ', 1 hour', ', @count hours');
  }
  if ($accuracy == 'minute' || $accuracy == 'second') {
    $output .= format_plural($min_left, ', 1 minute', ', @count minutes');
  }
  if ($accuracy == 'second') {
    $output .= format_plural($secs_left, ', 1 second', ', @count seconds');
  }
  $event_name = $countdown_options['event_name'];
  $url = $countdown_options['event_url'];
  $event_name = empty($url) || $url == 'http://' ? $event_name : l($event_name, $url, array(
    'absolute' => TRUE,
  ));
  $output .= t($passed ? ' since !s.' : ' until !s.', array(
    '!s' => $event_name,
  ));
  if ($accuracy != 'd') {
    $path = $plugin['path'] . '/countdown';
    drupal_add_js($path . '/countdown.js');
    $output .= <<<___EOS___
<script type="text/javascript"><!--
  init_countdown('{<span class="php-variable">$accuracy</span>}');
// --></script>
___EOS___;
  }
  return $output;
}

Functions

Namesort descending Description
countdown_countdown_build Countdown build callback.