You are here

function clock_block in Clock 6

Implements hook_block().

File

./clock.module, line 57
Functions to manage the display of a clock.

Code

function clock_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks['clock'] = array(
        'info' => t('Clock'),
        // The clock needs to be updated continuously and should not be cached.
        'cache' => BLOCK_NO_CACHE,
      );
      return $blocks;
    case 'configure':
      drupal_add_js(drupal_get_path('module', 'clock') . '/clock.js');

      // Time zone options.
      $form['timezone'] = array(
        '#type' => 'radios',
        '#title' => t('Time zone settings'),
        '#description' => t('<em>Local time zone</em> is the user\'s operating system time zone.'),
        '#weight' => 1,
        '#default_value' => variable_get('clock_timezone_type', '1'),
        '#options' => array(
          '1' => t('Site time zone'),
          '3' => t('Local time zone'),
          '4' => t('Custom time zone'),
        ),
        '#prefix' => '<div class="edit-timezone">',
        '#suffix' => '</div>',
      );

      // In case of user-configurable timezones show it as an option.
      if (variable_get('configurable_timezones', 1) == 1) {
        $form['timezone']['#description'] = t('<em>Local time zone</em> is the user\'s operating system time zone, while <em>User time zone</em> is the time zone of the Drupal user.');
        $form['timezone']['#options'] = array(
          '1' => t('Site time zone'),
          '2' => t('User time zone'),
          '3' => t('Local time zone'),
          '4' => t('Custom time zone'),
        );
      }
      $form['timezone']['custom_timezone'] = array(
        '#type' => 'select',
        '#multiple' => FALSE,
        '#weight' => 2,
        '#default_value' => variable_get('clock_custom_timezone', 'UTC'),
        '#options' => date_timezone_names(),
        '#prefix' => '<div class="edit-custom-timezone">',
        '#suffix' => '</div>',
      );

      // Format the current time with each of the three default date formats to display them in the form.
      $timezone = clock_get_timezone(TRUE);
      $short = date_format_date(date_now($timezone), $type = 'short');
      $medium = date_format_date(date_now($timezone), $type = 'medium');
      $long = date_format_date(date_now($timezone), $type = 'long');

      // Date format options.
      $form['date_format'] = array(
        '#type' => 'radios',
        '#title' => t('Date format settings'),
        '#weight' => 3,
        '#default_value' => variable_get('clock_date_format_type', 'short'),
        '#options' => array(
          'short' => t('Short date format (@short)', array(
            '@short' => $short,
          )),
          'medium' => t('Medium date format (@medium)', array(
            '@medium' => $medium,
          )),
          'long' => t('Long date format (@long)', array(
            '@long' => $long,
          )),
        ),
      );

      // If there are custom date formats, gather them and prepare them to display them in the form.
      $result = db_query("SELECT format FROM {date_formats} WHERE type = 'custom'");
      $date_formats = array();
      while ($date_format = db_fetch_object($result)) {
        $date_formats[$date_format->format] = date_format_date(date_now($timezone), $type = 'custom', $format = $date_format->format);
      }
      if ($date_formats) {
        $form['date_format']['#options']['custom'] = t('Custom date format');
        $form['date_format']['#prefix'] = '<div class="edit-date-format">';
        $form['date_format']['#suffix'] = '</div>';
        $form['date_format']['custom_date_format'] = array(
          '#type' => 'select',
          '#multiple' => FALSE,
          '#weight' => 4,
          '#default_value' => variable_get('clock_custom_date_format', 'g:i a'),
          '#options' => $date_formats,
          '#prefix' => '<div class="edit-custom-date-format">',
          '#suffix' => '</div>',
        );
      }

      // JavaScript options.
      $form['js'] = array(
        '#type' => 'checkbox',
        '#title' => t('Use JavaScript to continuously update the clock'),
        '#weight' => 5,
        '#default_value' => variable_get('clock_js', '1'),
      );
      return $form;
    case 'save':
      variable_set('clock_timezone_type', $edit['timezone']);
      if ($edit['timezone'] == '4') {
        variable_set('clock_custom_timezone', $edit['custom_timezone']);
      }
      variable_set('clock_date_format_type', $edit['date_format']);
      if ($edit['date_format'] == 'custom') {
        variable_set('clock_custom_date_format', $edit['custom_date_format']);
      }
      variable_set('clock_js', $edit['js']);
      return;
    case 'view':
      $timezone = clock_get_timezone();
      $date_format = clock_get_date_format();
      $js = variable_get('clock_js', '1');
      $block['subject'] = 'Clock';
      $block['content'] = theme_clock($timezone, $date_format, $js);
      return $block;
  }
}