diff --git a/clock.module b/clock.module index e7bb5a3..654944c 100755 --- a/clock.module +++ b/clock.module @@ -1,276 +1,281 @@ -' . t('About') . ''; - $output .= '

' . t('Clock module allows the display of a clock on your site.') . '

'; - $output .= '

' . t('Uses') . '

'; - $output .= '
'; - $output .= '
' . t('Administering the clock') . '
'; - $output .= '
' . t('Since the clock is a block, it can be administered via its block settings page which is accessible from the block administration page. 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'), - )) . '
'; - $output .= '
'; - $output .= '
' . t('Time zone') . '
'; - $output .= '
' . t('The time zone of the clock.') . '
'; - $output .= '
'; - $output .= '
' . t('Site time zone') . '
'; - $output .= '
' . t('The time zone that has been set on the regional settings page.', array( - '@regional-admin' => url('admin/config/regional/settings'), - )) . '
'; - $output .= '
' . t('User time zone') . '
'; - $output .= '
' . t('This option will only show up, if user-configurable time zones are enabled. The time zone the user has selected.') . '
'; - $output .= '
' . t('Local time zone') . '
'; - $output .= '
' . 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 .= '
' . t('Custom time zone') . '
'; - $output .= '
' . t('A custom time zone that can be selected in the select box below the radio buttons.') . '
'; - $output .= '
'; - $output .= '
' . t('Date format') . '
'; - $output .= '
' . t('The date format that the clock is to be displayed in. All of the date types that have been set on the date and time settings page are available here.', array( - '@format-admin' => url('admin/config/regional/date-time'), - )) . '
'; - $output .= '
' . t('JavaScript updating') . '
'; - $output .= '
' . 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.') . '
'; - $output .= '
'; - $output .= '
'; - 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('Local time zone 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('User time zone is the time zone the Drupal user has selected on his or her profile page. Local time zone 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) { - // 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. - $date_format = variable_get('date_format_' . $date_type, key(system_get_date_formats($date_type))); - $date_types[$date_type] = $info['title'] . ' (' . date_format_date(date_now(_clock_get_timezone()), 'custom', $date_format) . ')'; - } - $form['date_type'] = array( - '#type' => 'radios', - '#title' => t('Date type'), - '#description' => t('You can configure these date types on the date and time configuration page.', 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. - * - * @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() { - $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 = '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. - * - * @return - * The date format of the date type used for the clock. - */ -function _clock_get_date_format() { - $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. - $date_format = variable_get('date_format_' . $date_type, 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, - 'time' => $time, - '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 '
' . $clock . '
'; -} - +' . t('About') . ''; + $output .= '

' . t('Clock module allows the display of a clock on your site.') . '

'; + $output .= '

' . t('Uses') . '

'; + $output .= '
'; + $output .= '
' . t('Administering the clock') . '
'; + $output .= '
' . t('Since the clock is a block, it can be administered via its block settings page which is accessible from the block administration page. 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'), + )) . '
'; + $output .= '
'; + $output .= '
' . t('Time zone') . '
'; + $output .= '
' . t('The time zone of the clock.') . '
'; + $output .= '
'; + $output .= '
' . t('Site time zone') . '
'; + $output .= '
' . t('The time zone that has been set on the regional settings page.', array( + '@regional-admin' => url('admin/config/regional/settings'), + )) . '
'; + $output .= '
' . t('User time zone') . '
'; + $output .= '
' . t('This option will only show up, if user-configurable time zones are enabled. The time zone the user has selected.') . '
'; + $output .= '
' . t('Local time zone') . '
'; + $output .= '
' . 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 .= '
' . t('Custom time zone') . '
'; + $output .= '
' . t('A custom time zone that can be selected in the select box below the radio buttons.') . '
'; + $output .= '
'; + $output .= '
' . t('Date format') . '
'; + $output .= '
' . t('The date format that the clock is to be displayed in. All of the date types that have been set on the date and time settings page are available here.', array( + '@format-admin' => url('admin/config/regional/date-time'), + )) . '
'; + $output .= '
' . t('JavaScript updating') . '
'; + $output .= '
' . 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.') . '
'; + $output .= '
'; + $output .= '
'; + 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('Local time zone 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('User time zone is the time zone the Drupal user has selected on his or her profile page. Local time zone 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) { + // 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. + $date_format = variable_get('date_format_' . $date_type, key(system_get_date_formats($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 date and time configuration page.', 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 the returned time zone is used directly in date_format_date(). If + * the site default time zone is returned for the 'Local' time zone. 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. + * + * @return + * The date format of the date type used for the clock. + */ +function _clock_get_date_format() { + $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. + $date_format = variable_get('date_format_' . $date_type, 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, + 'time' => $time, + '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 '
' . $clock . '
'; +} + diff --git a/clock.test b/clock.test index c16214e..2cd8c3a 100755 --- a/clock.test +++ b/clock.test @@ -18,47 +18,43 @@ class ClockBlockTestCase extends DrupalWebTestCase { public function setUp() { parent::setUp('block', 'date_api', 'clock'); - $privileged_user = $this->drupalCreateUser(array('administer blocks', 'administer site configuration')); - // Save the test user's uid to access the user edit page later. - variable_set('test_user_id', $privileged_user->uid); - $this->drupalLogin($privileged_user); - // Enable the clock block (and the main content block) in main content area. - $theme = variable_get('theme_default', ''); - $edit = array(); - $edit['blocks[clock_clock][region]'] = 'content'; - $edit['blocks[system_main][region]'] = 'content'; - $this->drupalPost("admin/structure/block/list/$theme", $edit, 'Save blocks'); + $this->privileged_user = $this->drupalCreateUser(array('administer blocks', 'administer site configuration')); + $this->drupalLogin($this->privileged_user); + // Enable the clock block and the main content block in the main content + // area. + $edit = array( + 'blocks[clock_clock][region]' => 'content', + 'blocks[system_main][region]' => 'content', + ); + $this->drupalPost("admin/structure/block", $edit, 'Save blocks'); } public function testClockBlock() { - // Should $language_content be used instead? global $language; // Test the default display of the clock. $clock = date_format_date(date_now(variable_get('date_default_timezone', 'UTC')), 'long'); $this->assertText($clock, 'Ensure that the clock is correctly displayed by default.'); - $edit = array(); - $edit['time_zone_type'] = 3; + $edit = array('time_zone_type' => 3); $this->drupalPost('admin/structure/block/manage/clock/clock/configure', $edit, 'Save block'); $this->assertText($clock, 'Ensure that the clock falls back to the site time with Local time zone enabled and without JavaScript.'); // Test the user time zone. - $edit = array(); - $edit['timezone'] = 'Pacific/Fiji'; - $this->drupalPost('user/' . variable_get('test_user_id', '1') . '/edit', $edit, 'Save'); // Make sure user-configurable time zones are enabled. variable_set('configurable_timezones', 1); + $edit = array('timezone' => 'Pacific/Fiji'); + $this->drupalPost('user/' . $this->privileged_user->uid. '/edit', $edit, 'Save'); // Set the clock block to display the user time zone. - $edit = array(); - $edit['time_zone_type'] = 2; + $edit = array('time_zone_type' => 2); $this->drupalPost('admin/structure/block/manage/clock/clock/configure', $edit, 'Save block'); $clock = date_format_date(date_now('Pacific/Fiji'), 'long'); $this->assertText($clock, 'Ensure that the clock is correctly displayed in the user time zone.'); // Test a custom time zone. - $edit = array(); - $edit['time_zone_type'] = 4; - $edit['time_zone_custom'] = 'Africa/Lubumbashi'; + $edit = array( + 'time_zone_type' => 4, + 'time_zone_custom' => 'Africa/Lubumbashi', + ); $this->drupalPost('admin/structure/block/manage/clock/clock/configure', $edit, 'Save block'); $clock = date_format_date(date_now('Africa/Lubumbashi'), 'long'); $this->assertText($clock, 'Ensure that the clock is correctly displayed in a custom date format');