You are here

clock.module in Clock 7

Same filename and directory in other branches
  1. 6 clock.module
  2. 7.2 clock.module

File

clock.module
View source
<?php

/**
* Implements hook_help().
*/
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;
  }
}

/**
 * Implements hook_block_info().
 */
function clock_block_info() {
  $blocks['clock'] = array(
    'info' => t('Clock'),
    // The clock needs to be updated continuously and should not be cached.
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

/**
 * Implement hook_block_configure().
 */
function clock_block_configure($delta = '') {
  if ($delta == 'clock') {

    // Time zone options.
    $time_zone = variable_get('clock_time_zone', 2);

    // If the time zone is not set to the integer values 1, 2 or 3 corresponding
    // to 'Site time zone', 'User time zone' and 'Local time zone' it contains
    // the name of a custom time zone, so the time zone type must be set to
    // integer value 4.
    $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,
    );

    // In case of user-configurable timezones show it as an option.
    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).');

      // Make the "User time zone" option appear second.
      ksort($form['time_zone_type']['#options']);
    }

    // date_timezone_names() returns an empty first row.
    $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,
      // Show the select list only if "Custom time zone" is selected.
      '#states' => array(
        'visible' => array(
          ':input[name="time_zone_type"]' => array(
            'value' => "4",
          ),
        ),
      ),
    );

    // Date type options.
    // Format the current time with each date type for display in the form. If
    // the user has changed the time zone since the last page load,
    // the formatted time is displayed in the wrong, old time zone.
    $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,
    );

    // Update options.
    $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;
  }
}

/**
 * Implements hook_block_save().
 */
function clock_block_save($delta = '', $edit = array()) {
  if ($delta == 'clock') {

    // If the time zone type is 'Custom time zone' store the name of the custom
    // time zone in $time_zone. Otherwise, store the value of the time zone
    // type.
    $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;
  }
}

/**
 * Implements hook_block_view().
 */
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;
  }
}

/**
 * Gets the correct timezone to display.
 *
 * @param $date_format_date
 *   Whether or not the returned time zone is directly used in
 *   date_format_date(). Defaults to FALSE.
 *
 * @return
 *   The name of the timezone, NULL if the user's time zone is to be used or
 *   'Local' if the user's local time is to be used.
 */
function _clock_get_timezone($date_format_date = FALSE) {
  $time_zone = variable_get('clock_time_zone', 2);
  switch ($time_zone) {

    // Site time zone.
    case 1:
      $time_zone = variable_get('date_default_timezone', 'UTC');
      break;

    // User time zone.
    case 2:
      $time_zone = NULL;
      break;

    // Local time zone.
    case 3:
      $time_zone = $date_format_date ? variable_get('date_default_timezone', 'UTC') : 'Local';
      break;
  }

  // If the time zone type is 'Custom time zone', $time_zone directly contains
  // the name of the time zone.
  return $time_zone;
}

/**
 * Gets the correct date format.
 *
 * @param $date_type
 *   (optional) The date type to return the date format for. If omitted, uses
 *   the date type which is set for the clock.
 *
 * @return
 *   The date format of the date type used for the clock.
 */
function _clock_get_date_format($date_type = NULL) {
  if (!isset($date_type)) {
    $date_type = variable_get('clock_date_type', 'long');
  }

  // Each date format type has a corresponding variable. If it is not set,
  // get the list of date formats for that type and use the first one. We
  // cannot use the fallback directly in variable_get(), because
  // system_get_date_formats() can return FALSE for custom date types. In
  // that case, the variable is always set, though.
  $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;
}

/**
 * Implements hook_theme.
 */
function clock_theme() {
  return array(
    'clock' => array(
      'variables' => array(
        'time_zone' => NULL,
        'date_format' => '',
        'update' => 1,
      ),
    ),
  );
}

/*
 * Provide a default implementation of theme_clock().
 */
function theme_clock($variables) {

  // Initialize the 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;

      // Use the site time zone as a fallback for non-JavaScript users.
      $time_zone = variable_get('date_default_timezone', 'UTC');
    }
    drupal_add_js(drupal_get_path('module', 'clock') . '/clock.js');

    // Pass the needed variables to JavaScript.
    // Create a time string, from which JavaScript can create a date. The time
    // string contains the month name, which needs to be in English.
    $time = date_format_date(date_now($time_zone), 'custom', 'F j, Y H:i:s', 'en');

    // Get the name of the offset, e.g. 'GMT'.
    $offset_name = date_format_date(date_now($time_zone), 'custom', 'T');

    // Get the time zone offset in seconds.
    $offset_seconds = date_format_date(date_now($time_zone), 'custom', 'Z');

    // Get Daylight Savings Time information. '1' for yes, '0' for no.
    $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');
  }

  // Create a date object with the correct timezone and format it with the correct date format.
  $clock = date_format_date(date_now($time_zone), 'custom', $date_format);
  return '<div class="clock">' . $clock . '</div>';
}

Functions

Namesort descending Description
clock_block_configure Implement hook_block_configure().
clock_block_info Implements hook_block_info().
clock_block_save Implements hook_block_save().
clock_block_view Implements hook_block_view().
clock_help Implements hook_help().
clock_theme Implements hook_theme.
theme_clock
_clock_get_date_format Gets the correct date format.
_clock_get_timezone Gets the correct timezone to display.