| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @file 4 * This module will make the alter the user and site timezone forms to 5 * select a timezone name instead of a timezone offset. 6 * 7 * This module won't be needed once core starts tracking timezone names 8 * instead of offsets. 9 */ 10 11 /** 12 * Make sure a timezone has been selected. 13 */ 14 function date_timezone_init() { 15 $tz_name = variable_get('date_default_timezone_name', NULL); 16 if (!empty($user->uid) && $_GET['q'] != 'admin/settings/date-time' && empty($tz_name)) { 17 drupal_set_message(t('The Date Timezone module requires you to <a href="@link">set the site timezone name</a>.', array('@link' => url('admin/settings/date-time'))), 'error'); 18 } 19 } 20 21 /** 22 * Implementation of hook_menu(). 23 */ 24 function date_timezone_menu() { 25 $items = array(); 26 $items['user/timezone'] = array( 27 'title' => 'User timezone', 28 'page callback' => 'user_timezone', 29 'access callback' => TRUE, 30 'type' => MENU_CALLBACK, 31 ); 32 return $items; 33 } 34 35 /** 36 * Implementation of hook_form_alter(). 37 * 38 * Override system handling of user and site timezone selection. 39 */ 40 function date_timezone_form_alter(&$form, &$form_state, $form_id) { 41 if ($form_id == 'system_date_time_settings') { 42 date_timezone_site_form($form); 43 if (!isset($form['#after_build'])) { 44 $form['#after_build'] = array(); 45 } 46 $form['#after_build'][] = 'date_timezone_site_form_after_build'; 47 } 48 elseif ($form_id == 'user_profile_form' && variable_get('configurable_timezones', 1) && isset($form['timezone'])) { 49 date_timezone_user_form($form); 50 if (!isset($form['#after_build'])) { 51 $form['#after_build'] = array(); 52 } 53 $form['#after_build'][] = 'date_timezone_user_form_after_build'; 54 } 55 } 56 57 /** 58 * Create a form for the site timezone names. 59 * Display a list of timezone names instead of offsets. 60 */ 61 function date_timezone_site_form(&$form) { 62 drupal_add_js(drupal_get_path('module', 'date_timezone') .'/date_timezone.js'); 63 $form['locale']['#element_validate'] = array('date_timezone_update_site'); 64 65 $timezone = variable_get('date_default_timezone_name', NULL); 66 $form['locale']['date_default_timezone_name'] = array( 67 '#type' => 'select', 68 '#title' => t('Default time zone'), 69 '#default_value' => $timezone, 70 '#options' => date_timezone_names(FALSE, TRUE), // Force an update before setting a site default. 71 '#description' => t('Select the default site time zone. If in doubt, choose the timezone that is closest to your location which has the same rules for daylight saving time.'), 72 '#weight' => -10, 73 '#offset' => variable_get('date_default_timezone', 0), 74 ); 75 // Add the JavaScript callback to automatically set the timezone. 76 if (empty($timezone)) { 77 drupal_add_js(' 78 // Global Killswitch 79 if (Drupal.jsEnabled) { 80 $(document).ready(function() { 81 Drupal.setDefaultTimezone(); 82 }); 83 }', 'inline'); 84 } 85 return $form; 86 } 87 88 /** 89 * Hide the original form. 90 * 91 * We have to do this in after_build in case the Event module 92 * is enabled since the Event module will do its form_alter() 93 * after the Date module. 94 */ 95 function date_timezone_site_form_after_build(&$form) { 96 97 // Set the value, and make sure it's a legal value. 98 $value = $form['locale']['date_default_timezone']['#default_value']; 99 $options = $form['locale']['date_default_timezone']['#options']; 100 if (!array_key_exists($value, $options)) { 101 //$value = array_pop($options); 102 $value = NULL; 103 } 104 $form['locale']['date_default_timezone']['#type'] = 'hidden'; 105 $form['locale']['date_default_timezone']['#value'] = $value; 106 return $form; 107 } 108 109 /** 110 * Create a form for the site timezone names. 111 * Display a list of timezone names instead of offsets. 112 */ 113 function date_timezone_user_form(&$form) { 114 drupal_add_js(drupal_get_path('module', 'date_timezone') .'/date_timezone.js'); 115 116 $account = $form['_account']['#value']; 117 $form['timezone']['#uid'] = $account->uid; 118 $form['timezone']['#element_validate'] = array('date_timezone_update_user'); 119 120 $timezone = $account->timezone_name ? $account->timezone_name : variable_get('date_default_timezone_name', NULL); 121 $form['timezone']['timezone_name'] = array( 122 '#type' => 'select', 123 '#title' => t('Default time zone'), 124 '#default_value' => $timezone, 125 '#options' => date_timezone_names(), 126 '#description' => t('Select your current local time. If in doubt, choose the timezone that is closest to your location which has the same rules for daylight saving time. Dates and times throughout this site will be displayed using this time zone.'), 127 ); 128 // Add the JavaScript callback to automatically set the timezone. 129 if (empty($timezone)) { 130 drupal_add_js(' 131 // Global Killswitch 132 if (Drupal.jsEnabled) { 133 $(document).ready(function() { 134 Drupal.setDefaultTimezone(); 135 }); 136 }', 'inline'); 137 } 138 return $form; 139 } 140 141 /** 142 * Hide the original form. 143 * 144 * We have to do this in after_build in case the Event module 145 * is enabled since the Event module will do its form_alter() 146 * after the Date module. 147 */ 148 function date_timezone_user_form_after_build($form) { 149 // Set the value, and make sure it's a legal value. 150 $value = $form['timezone']['timezone']['#default_value']; 151 $options = $form['timezone']['timezone']['#options']; 152 if (!array_key_exists($value, $options)) { 153 //$value = array_pop($options); 154 $value = NULL; 155 } 156 $form['timezone']['timezone']['#type'] = 'hidden'; 157 $form['timezone']['timezone']['#value'] = $value; 158 return $form; 159 } 160 161 /** 162 * Callback from site timezone settings form to update site timezone info. 163 * When the timezone name is updated, update the offset as well. 164 */ 165 function date_timezone_update_site($element, &$form_state) { 166 $timezone = $element['date_default_timezone_name']['#value']; 167 if (empty($timezone)) { 168 $offset = $element['date_default_timezone_name']['#offset']; 169 } 170 else { 171 variable_set('date_default_timezone_name', $timezone); 172 $date = date_make_date('now', $timezone); 173 $offset = date_offset_get($date); 174 } 175 176 // Reset the original form to the expected value. 177 if (module_exists('event') && db_table_exists('event_timezones')) { 178 $event_zone = date_event_zonelist_by_name(str_replace('_', ' ', $timezone)); 179 // The event module will update the timezone and zone id, using this value. 180 if (!empty($event_zone['timezone'])) { 181 form_set_value($element['date_default_timezone'], $event_zone['timezone'] .'|'. $offset, $form_state); 182 } else { 183 form_set_value($element['date_default_timezone'], $offset, $form_state); 184 } 185 } 186 else { 187 form_set_value($element['date_default_timezone'], $offset, $form_state); 188 } 189 } 190 191 /** 192 * Callback from user timezone settings form to update user timezone info. 193 * When the timezone name is updated, update the offset as well. 194 */ 195 function date_timezone_update_user($element, &$form_state) { 196 $timezone = $element['timezone_name']['#value']; 197 if (!empty($timezone)) { 198 $date = date_make_date('now', $timezone); 199 $offset = date_offset_get($date); 200 } 201 202 // Reset the original form to the expected value. 203 if (module_exists('event') && db_table_exists('event_timezones')) { 204 $event_zone = date_event_zonelist_by_name(str_replace('_', ' ', $timezone)); 205 // The event module will update the timezone and zone id using this value. 206 if (!empty($event_zone['timezone'])) { 207 form_set_value($element['timezone'], $event_zone['timezone'] .'|'. $offset, $form_state); 208 } else { 209 form_set_value($element['timezone'], $offset, $form_state); 210 } 211 } 212 else { 213 form_set_value($element['timezone'], $offset, $form_state); 214 } 215 } 216 217 /** 218 * Update the site timezone offset when cron runs. 219 * 220 * This is to make sure that modules that rely on the timezone offset 221 * have current information to process. 222 */ 223 function date_timezone_cron() { 224 $date = date_now(variable_get('date_default_timezone_name', NULL)); 225 $offset = date_offset_get($date); 226 if ($offset != variable_get('date_default_timezone', 0)) { 227 variable_set('date_default_timezone', $offset); 228 } 229 } 230 231 /** 232 * Update user timezone information at login. 233 * 234 * This is to make sure that modules that rely on the timezone offset 235 * have current information to process. 236 */ 237 function date_timezone_user($op, &$edit, &$account, $category = NULL) { 238 if (isset($account->uid) && $op == 'login' && variable_get('configurable_timezones', 1)) { 239 if (strlen($account->timezone_name)) { 240 $date = date_now($account->timezone_name); 241 $offset = date_offset_get($date); 242 if ($offset != $account->timezone) { 243 $account->timezone = $offset; 244 db_query("UPDATE {users} SET timezone='%s' WHERE uid = %d", $offset, $account->uid); 245 } 246 } 247 else { 248 // If the user doesn't already have a timezone name selected, 249 // default it to the site timezone name and offset. 250 $timezone = variable_get('date_default_timezone_name', NULL); 251 if (!empty($timezone)) { 252 $date = date_now($timezone); 253 $offset = date_offset_get($date); 254 db_query("UPDATE {users} SET timezone_name = '%s', timezone='%s' WHERE uid = %d", $timezone, $offset, $account->uid); 255 } 256 } 257 } 258 } 259 260 /** 261 * Menu callback; Retrieve a JSON object containing a suggested time 262 * zone name. 263 */ 264 function user_timezone($abbreviation = '', $offset = -1, $is_daylight_saving_time = NULL) { 265 // An abbreviation of "0" passed in the callback arguments should be 266 // interpreted as the empty string. 267 $abbreviation = $abbreviation ? $abbreviation : ''; 268 $timezone = function_exists('timezone_name_from_abbr') ? timezone_name_from_abbr($abbreviation, intval($offset), $is_daylight_saving_time) : 'UTC'; 269 // The client date is passed in for debugging purposes. 270 $date = isset($_GET['date']) ? $_GET['date'] : ''; 271 // Log a debug message. 272 watchdog('timezone', 'Detected time zone: %timezone; client date: %date; abbreviation: %abbreviation; offset: %offset; daylight saving time: %is_daylight_saving_time.', array('%timezone' => $timezone, '%date' => $date, '%abbreviation' => $abbreviation, '%offset' => $offset, '%is_daylight_saving_time' => $is_daylight_saving_time)); 273 drupal_json($timezone); 274 } 275 276 /** 277 * Create replacement values for deprecated timezone names. 278 */ 279 function date_timezone_replacement($old) { 280 ('./'. drupal_get_path('module', 'date_timezone') .'/date_timezone.install'); 281 return _date_timezone_replacement($old); 282 } 283 284 /** 285 * Helper function to update Event module timezone information. 286 */ 287 function date_event_zonelist_by_name($name) { 288 if (!module_exists('event') || !db_table_exists('event_timezones')) { 289 return array(); 290 } 291 static $zone_names = array(); 292 293 if (!isset($zone_names[$name])) { 294 $zone = db_fetch_array(db_query("SELECT * FROM {event_timezones} WHERE name = '%s'", $name)); 295 $zone_names[$name] = $zone; 296 } 297 298 return $zone_names[$name]; 299 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |