View source
<?php
function clock_help($path, $arg) {
switch ($path) {
case 'admin/help#clock':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Clock module allows the display of a clock on your site.') . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Administering the clock') . '</dt>';
$output .= '<dd>' . t('Since the clock is a block, it can be administered via its <a href="@clock-settings">block settings page</a> which is accessible from the <a href="@block-admin">block administration page</a>. In addition to the usual block configuration options there are a number of options.', array(
'@clock-settings' => url('admin/structure/block/manage/clock/clock/configure'),
'@block-admin' => url('admin/structure/block'),
)) . '</dd>';
$output .= '<dl>';
$output .= '<dt>' . t('Time zone') . '</dt>';
$output .= '<dd>' . t('The time zone of the clock.') . '</dd>';
$output .= '<dl>';
$output .= '<dt>' . t('Site time zone') . '</dt>';
$output .= '<dd>' . t('The time zone that has been set on the <a href="@regional-admin">regional settings page</a>.', array(
'@regional-admin' => url('admin/config/regional/settings'),
)) . '</dd>';
$output .= '<dt>' . t('User time zone') . '</dt>';
$output .= '<dd>' . t('This option will only show up, if user-configurable time zones are enabled. The time zone the user has selected.') . '</dd>';
$output .= '<dt>' . t('Local time zone') . '</dt>';
$output .= '<dd>' . t('The local time zone on the computer of the visiting user (anonymous and authenticated). Since JavaScript is used to calculate this time, it falls back to the site time zone if the visitor has JavaScript disabled.');
$output .= '<dt>' . t('Custom time zone') . '</dt>';
$output .= '<dd>' . t('A custom time zone that can be selected in the select box below the radio buttons.') . '</dd>';
$output .= '</dl>';
$output .= '<dt>' . t('Date format') . '</dt>';
$output .= '<dd>' . t('The date format that the clock is to be displayed in. All of the date types that have been set on the <a href="@format-admin">date and time settings page</a> are available here.', array(
'@format-admin' => url('admin/config/regional/date-time'),
)) . '</dd>';
$output .= '<dt>' . t('JavaScript updating') . '</dt>';
$output .= '<dd>' . t('Whether or not the clock should be updated continuously via JavaScript. This is especially useful if seconds are displayed. It only works if the visitor has JavaScript enabled.') . '</dd>';
$output .= '</dl>';
$output .= '</dl>';
return $output;
}
}
function clock_block_info() {
$blocks['clock'] = array(
'info' => t('Clock'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
function clock_block_configure($delta = '') {
if ($delta == 'clock') {
$time_zone = variable_get('clock_time_zone', 2);
$time_zone_type = $time_zone == 1 || $time_zone == 2 || $time_zone == 3 ? $time_zone : 4;
$custom_time_zone = $time_zone_type == 4 ? $time_zone : 'UTC';
$form['time_zone_type'] = array(
'#type' => 'radios',
'#title' => t('Time zone settings'),
'#description' => t('<em>Local time zone</em> is the time zone on the operating system of the person visiting (regardless of anonymous or authenticated).'),
'#options' => array(
1 => t('Site time zone'),
3 => t('Local time zone'),
4 => t('Custom time zone'),
),
'#default_value' => $time_zone_type,
);
if (variable_get('configurable_timezones', 1) == 1) {
$form['time_zone_type']['#options'][2] = t('User time zone');
$form['time_zone_type']['#description'] = t('<em>User time zone</em> is the time zone the Drupal user has selected on his or her profile page. <em>Local time zone</em> is the time zone on the operating system of the person visiting (regardless of anonymous or authenticated).');
ksort($form['time_zone_type']['#options']);
}
$time_zone_names = date_timezone_names();
array_shift($time_zone_names);
$form['time_zone_custom'] = array(
'#type' => 'select',
'#options' => $time_zone_names,
'#default_value' => $custom_time_zone,
'#states' => array(
'visible' => array(
':input[name="time_zone_type"]' => array(
'value' => "4",
),
),
),
);
$date_types = array();
foreach (system_get_date_types() as $date_type => $info) {
$date_format = _clock_get_date_format($date_type);
$date_types[$date_type] = $info['title'] . ' (' . date_format_date(date_now(_clock_get_timezone(TRUE)), 'custom', $date_format) . ')';
}
$form['date_type'] = array(
'#type' => 'radios',
'#title' => t('Date type'),
'#description' => t('You can configure these date types on the <a href="@date-and-time-admin">date and time configuration page</a>.', array(
'@date-and-time-admin' => url('admin/config/regional/date-time'),
)),
'#weight' => 3,
'#default_value' => variable_get('clock_date_type', 'long'),
'#options' => $date_types,
);
$form['update'] = array(
'#type' => 'checkbox',
'#title' => t('Update the clock every second'),
'#description' => t('This only works with JavaScript enabled.'),
'#weight' => 5,
'#default_value' => variable_get('clock_update', '1'),
);
return $form;
}
}
function clock_block_save($delta = '', $edit = array()) {
if ($delta == 'clock') {
$time_zone = $edit['time_zone_type'] == 4 ? $edit['time_zone_custom'] : $edit['time_zone_type'];
variable_set('clock_time_zone', $time_zone);
variable_set('clock_date_type', $edit['date_type']);
variable_set('clock_update', $edit['update']);
return;
}
}
function clock_block_view($delta = '') {
if ($delta == 'clock') {
$block = array();
$block['subject'] = t('Clock');
$block['content'] = array(
'#theme' => 'clock',
'#time_zone' => _clock_get_timezone(),
'#date_format' => _clock_get_date_format(),
'#update' => variable_get('clock_update', 1),
);
return $block;
}
}
function _clock_get_timezone($date_format_date = FALSE) {
$time_zone = variable_get('clock_time_zone', 2);
switch ($time_zone) {
case 1:
$time_zone = variable_get('date_default_timezone', 'UTC');
break;
case 2:
$time_zone = NULL;
break;
case 3:
$time_zone = $date_format_date ? variable_get('date_default_timezone', 'UTC') : 'Local';
break;
}
return $time_zone;
}
function _clock_get_date_format($date_type = NULL) {
if (!isset($date_type)) {
$date_type = variable_get('clock_date_type', 'long');
}
$date_format = variable_get('date_format_' . $date_type, NULL);
if (!isset($date_format)) {
$date_format = key(system_get_date_formats($date_type));
}
return $date_format;
}
function clock_theme() {
return array(
'clock' => array(
'variables' => array(
'time_zone' => NULL,
'date_format' => '',
'update' => 1,
),
),
);
}
function theme_clock($variables) {
$time_zone = $variables['time_zone'];
$date_format = $variables['date_format'];
$update = $variables['update'];
if ($time_zone == 'Local' || $update == 1) {
$local = 0;
if ($time_zone == 'Local') {
$local = 1;
$time_zone = variable_get('date_default_timezone', 'UTC');
}
drupal_add_js(drupal_get_path('module', 'clock') . '/clock.js');
$time = date_format_date(date_now($time_zone), 'custom', 'F j, Y H:i:s', 'en');
$offset_name = date_format_date(date_now($time_zone), 'custom', 'T');
$offset_seconds = date_format_date(date_now($time_zone), 'custom', 'Z');
$daylight_savings_time = date_format_date(date_now($time_zone), 'custom', 'I');
drupal_add_js(array(
'time_zone' => $time_zone,
'date_format' => $date_format,
'update' => $update,
'local' => $local,
'offset_name' => $offset_name,
'offset_seconds' => $offset_seconds,
'daylight_savings_time' => $daylight_savings_time,
), 'setting');
}
$clock = date_format_date(date_now($time_zone), 'custom', $date_format);
return '<div class="clock">' . $clock . '</div>';
}