You are here

countdown.module in Countdown 7

Count to, or from, a specified date and display the output in a block

File

countdown.module
View source
<?php

/**
 * @file
 * Count to, or from, a specified date and display the output in a block
 */

/**
 * Implement hook_help().
 *
 * @param string $section
 * @return string
 */
function countdown_help($path, $arg) {
  $output = '';
  switch ($path) {
    case 'admin/help#countdown':
      $output = t("Don't forget to configure the event name and date in Administer/blocks/Countdown configure");
      break;
  }
  return $output;
}

/**
 * Implement hook_block_list().
 */
function countdown_block_info() {
  $blocks[0]['info'] = 'Countdown';
  return $blocks;
}

/**
 * Implement hook_block_configure().
 */
function countdown_block_configure($delta = '') {
  if ($delta == 0) {
    $form['countdown_event_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Event Name'),
      '#default_value' => variable_get('countdown_event_name', ''),
      '#size' => 30,
      '#maxlength' => 200,
      '#description' => t("Event name you're counting to or from."),
      '#required' => TRUE,
    );
    $form['countdown_url'] = array(
      '#type' => 'textfield',
      '#title' => t('Event URL'),
      '#default_value' => variable_get('countdown_url', ''),
      '#size' => 30,
      '#maxlength' => 200,
      '#description' => t("Turn the event description into a link to more information about the event."),
      '#required' => FALSE,
    );
    $form['countdown_accuracy'] = array(
      '#type' => 'radios',
      '#title' => t('Accuracy'),
      '#default_value' => variable_get('countdown_accuracy', 'd'),
      '#options' => array(
        'd' => t('days'),
        'h' => t('hours'),
        'm' => t('minutes'),
        's' => 'seconds',
      ),
      '#description' => t('Select the smallest amount of detail to display. For example, selecting "days" will display only days, selecting "hours" will display the number of days and hours.'),
    );
    $time = time();
    $timestamp = variable_get('countdown_timestamp', $time);
    $form['target_time'] = array(
      '#type' => 'fieldset',
      '#title' => t('Target date/time'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#description' => t('Select a date relative to the server time: %s', array(
        '%s' => format_date($time),
      )),
    );
    for ($years = array(), $i = 1970; $i < 2032; $years[$i] = $i, $i++) {
    }
    $form['target_time']['year'] = array(
      '#type' => 'select',
      '#title' => t('Year'),
      '#default_value' => (int) date('Y', $timestamp),
      '#options' => $years,
    );
    unset($years);
    $form['target_time']['month'] = array(
      '#type' => 'select',
      '#title' => t('Month'),
      '#default_value' => (int) date('n', $timestamp),
      '#options' => array(
        1 => t('January'),
        2 => t('February'),
        3 => t('March'),
        4 => t('April'),
        5 => t('May'),
        6 => t('June'),
        7 => t('July'),
        8 => t('August'),
        9 => t('September'),
        10 => t('October'),
        11 => t('November'),
        12 => t('December'),
      ),
    );
    for ($month_days = array(), $i = 1; $i < 32; $month_days[$i] = $i, $i++) {
    }
    $form['target_time']['day'] = array(
      '#type' => 'select',
      '#title' => t('Day'),
      '#default_value' => (int) date('j', $timestamp),
      '#options' => $month_days,
    );
    unset($month_days);
    for ($hrs = array(), $i = 0; $i < 24; $hrs[] = $i, $i++) {
    }
    $form['target_time']['hour'] = array(
      '#type' => 'select',
      '#title' => t('Hour'),
      '#default_value' => (int) date('G', $timestamp),
      '#options' => $hrs,
    );
    unset($hrs);
    for ($mins = array(), $i = 0; $i < 60; $mins[] = $i, $i++) {
    }
    $form['target_time']['min'] = array(
      '#type' => 'select',
      '#title' => t('Minute'),
      '#default_value' => (int) date('i', $timestamp),
      '#options' => $mins,
    );
    $form['target_time']['sec'] = array(
      '#type' => 'select',
      '#title' => t('Seconds'),
      '#default_value' => (int) date('s', $timestamp),
      '#options' => $mins,
    );
    return $form;
  }
}

/**
 * Implement hook_block_save().
 */
function countdown_block_save($delta = '', $edit = array()) {
  if ($delta == 0) {
    variable_set('countdown_event_name', $edit['countdown_event_name']);
    variable_set('countdown_url', $edit['countdown_url']);
    variable_set('countdown_accuracy', $edit['countdown_accuracy']);
    variable_set('countdown_timestamp', mktime((int) $edit['hour'], (int) $edit['min'], (int) $edit['sec'], (int) $edit['month'], (int) $edit['day'], (int) $edit['year']));
  }
}

/**
 * Implement hook_block_view().
 */
function countdown_block_view($delta = '') {
  if (user_access('access content') && $delta == 0) {
    $block['subject'] = variable_get('countdown_block_title', t('Countdown'));
    $block['content'] = theme('countdown');
    return $block;
  }
}

/**
 * Implement hook_theme().
 *
 * Theme function for similar block
 */
function countdown_theme() {
  return array(
    'countdown' => array(
      'template' => 'countdown',
    ),
  );
}

Functions