View source
<?php
function jquery_countdown_menu() {
$items = array();
$items['admin/config/user-interface/jquery-countdown'] = array(
'title' => t('jQuery Countdown Settings'),
'access arguments' => array(
'administer jquery countdown',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'jquery_countdown_admin',
),
'file' => 'jquery_countdown.admin.inc',
);
$items['ajax/jquery-countdown-terminate'] = array(
'access arguments' => array(
'access content',
),
'page callback' => 'jquery_countdown_invoke_rule',
'file' => 'jquery_countdown.pages.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
function jquery_countdown_block_info() {
$blocks = array();
$blocks['jquery_countdown']['info'] = t('jQuery Countdown Block');
$blocks['jquery_countdown']['cache'] = DRUPAL_NO_CACHE;
return $blocks;
}
function jquery_countdown_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'jquery_countdown':
$block['subject'] = check_plain(variable_get('jquery_countdown_event_name', t('jQuery Countdown')));
$block['content'] = _jquery_countdown_block_content($delta);
break;
}
return $block;
}
function _jquery_countdown_block_content($delta = '') {
$init_format = date("F d, Y g:i a", time());
$format = '';
$msg_format = check_plain(variable_get('jquery_countdown_msg_format', ''));
$format .= preg_match("/%years/", $msg_format) ? 'Y' : $format;
$format .= preg_match("/%months/", $msg_format) ? 'O' : $format;
$format .= preg_match("/%weeks/", $msg_format) ? 'W' : $format;
$format .= preg_match("/%days/", $msg_format) ? 'D' : $format;
$format .= preg_match("/%hours/", $msg_format) ? 'H' : $format;
$format .= preg_match("/%minutes/", $msg_format) ? 'M' : $format;
$format .= preg_match("/%seconds/", $msg_format) ? 'S' : $format;
$event_name = check_plain(variable_get('jquery_countdown_event_name', ''));
$until = date("F d, Y g:i a", strtotime(variable_get('jquery_countdown_target', '')));
$description = t(variable_get('jquery_countdown_description', 'Until @event_name') . ' ', array(
'@event_name' => $event_name,
));
$options = array(
'until' => $until,
'format' => $format,
'description' => $description,
'onExpiry' => 'Drupal.jQueryCountdownEvent',
'expiryText' => check_plain(variable_get('jquery_countdown_exp_txt', '')),
);
return theme('jquery_countdown', array(
'options' => $options,
'id' => 'jquery_countdown-' . $delta,
));
}
function jquery_countdown_add($selector = NULL, $options = array()) {
drupal_add_library('jquery_countdown', 'jquery.countdown');
static $added_selectors = array();
if (!empty($selector) && !isset($added_selectors[$selector])) {
$added_selectors[$selector] = $options;
drupal_add_js(array(
'jquery_countdown' => array(
$selector => $options,
),
), 'setting');
}
}
function jquery_countdown_library() {
$libraries = array();
$libraries['jquery.countdown'] = array(
'title' => 'jQuery Countdown',
'website' => 'http://keith-wood.name/countdown.html',
'version' => '1.5.9',
'js' => array(
drupal_get_path('module', 'jquery_countdown') . '/jquery_countdown/jquery.countdown.js' => array(),
drupal_get_path('module', 'jquery_countdown') . '/jquery_countdown.js' => array(),
drupal_get_path('module', 'jquery_countdown') . '/jquery.countdown-drupal.js' => array(
'group' => JS_THEME,
),
),
'css' => array(
drupal_get_path('module', 'jquery_countdown') . '/jquery_countdown/jquery.countdown.css' => array(
'type' => 'file',
'media' => 'screen',
),
),
);
return $libraries;
}
function jquery_countdown_field_formatter_info() {
$formatters = array(
'jquery_countdown' => array(
'label' => t('jQuery Countdown'),
'field types' => array(
'date',
'datestamp',
'datetime',
),
),
);
return $formatters;
}
function jquery_countdown_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
$formatter = $display['type'];
$vars = array(
'entity' => $entity,
'entity_type' => $entity_type,
'field' => $field,
'instance' => $instance,
'langcode' => $langcode,
'items' => $items,
'display' => $display,
'dates' => array(),
'options' => array(
'until' => date("F d, Y g:i a", time()),
'format' => 'DHMS',
'description' => '',
),
);
$selected_deltas = array();
if (!empty($entity->date_id)) {
foreach ((array) $entity->date_id as $key => $id) {
list($module, $nid, $field_name, $selected_delta, $other) = explode(':', $id);
if ($field_name == $field['field_name']) {
$selected_deltas[] = $selected_delta;
}
}
}
switch ($display['type']) {
case 'jquery_countdown':
default:
foreach ($items as $delta => $item) {
if (!empty($entity->date_id) && !in_array($delta, $selected_deltas)) {
continue;
}
else {
$vars['delta'] = $delta;
$vars['item'] = $item;
$vars['id'] = $instance['field_name'] . '-' . $instance['id'];
$vars['options']['until'] = date("F d, Y g:i a +0000", strtotime($item['value']));
$element[$delta] = array(
'#markup' => theme('jquery_countdown', $vars),
);
}
}
break;
}
return $element;
}
function jquery_countdown_theme($existing, $type, $theme, $path) {
return array(
'jquery_countdown' => array(
'variables' => array(
'options' => NULL,
'id' => NULL,
'content' => NULL,
),
),
);
}
function theme_jquery_countdown($variables) {
$id = $variables['id'];
$options = $variables['options'];
$content = $variables['content'];
$id = drupal_html_id($id);
jquery_countdown_add('#' . $id, $options);
return '<div id="' . $id . '" class="jquery-countdown">' . $content . '</div>';
}
function jquery_countdown_permission() {
return array(
'administer jquery countdown' => array(
'title' => t('Administer jquery countdown'),
),
);
}