| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @file 4 * Enable different locales to have their own date formats. 5 */ 6 7 /** 8 * Implementation of hook_init(). 9 * 10 * Initialize date formats according to the user's current locale. 11 */ 12 function date_locale_init() { 13 global $conf; 14 global $language; 15 16 // Don't do this on the general date and time formats settings page, as we 17 // want to display the defaults, not the ones specific to the language we're 18 // currently browsing the site in. 19 if (!drupal_match_path($_GET['q'], 'admin/settings/date-time/formats')) { 20 $languages = array($language->language); 21 if (module_exists('site_country')) { 22 $country_code = variable_get('site_country_default_country', ''); 23 if (!empty($country_code)) { 24 $country_language = $language->language . '-' . $country_code; 25 array_unshift($languages, $country_language); 26 } 27 } 28 drupal_alter('date_format_languages', $languages); 29 30 // Setup appropriate date formats for this locale. 31 $formats = date_locale_get_locale_date_format($languages); 32 foreach ($formats as $format_type => $format) { 33 $conf[$format_type] = $format; 34 } 35 } 36 } 37 38 /** 39 * Implementation of hook_menu(). 40 */ 41 function date_locale_menu() { 42 $items = array(); 43 $items['admin/settings/date-time/locale'] = array( 44 'title' => 'Locale date settings', 45 'description' => 'Configure date formats for each locale', 46 'type' => MENU_LOCAL_TASK, 47 'page callback' => 'drupal_get_form', 48 'page arguments' => array('date_locale_format_form'), 49 'access arguments' => array('administer site configuration'), 50 'weight' => 1, 51 ); 52 return $items; 53 } 54 55 /** 56 * Select locale date format details from database. 57 * 58 * @param $languages 59 * An array of language codes. 60 * @return 61 * An array of date formats. 62 */ 63 function date_locale_get_locale_date_format($languages) { 64 $formats = array(); 65 66 // Get list of different format types. 67 $format_types = date_get_format_types(); 68 $short_default = variable_get('date_format_short', 'm/d/Y - H:i'); 69 70 // Loop through each language until we find one with some date formats 71 // configured. 72 foreach ($languages as $language) { 73 $date_formats = date_format_locale($language); 74 if (!empty($date_formats)) { 75 // We have locale-specific date formats, so check for their types. If 76 // we're missing a type, use the default setting instead. 77 foreach ($format_types as $type => $type_info) { 78 $format = $date_formats[$type]; 79 80 // If format exists for this language, use it. 81 if (!empty($format)) { 82 $formats['date_format_' . $type] = $format; 83 } 84 // Otherwise get default variable setting. If this is not set, default 85 // to the short format. 86 else { 87 $formats['date_format_' . $type] = variable_get('date_format_' . $type, $short_default); 88 } 89 } 90 91 // Return on the first match. 92 return $formats; 93 } 94 } 95 96 // No locale specific formats found, so use defaults. 97 $system_types = array('short', 'medium', 'long'); 98 // Handle system types separately as they have defaults if no variable exists. 99 $formats['date_format_short'] = $short_default; 100 $formats['date_format_medium'] = variable_get('date_format_medium', 'D, m/d/Y - H:i'); 101 $formats['date_format_long'] = variable_get('date_format_long', 'l, F j, Y - H:i'); 102 103 // For non-system types, get the default setting, otherwise use the short 104 // format. 105 foreach ($format_types as $type => $type_info) { 106 if (!in_array($type, $system_types)) { 107 $formats['date_format_' . $type] = variable_get('date_format_' . $type, $short_default); 108 } 109 } 110 111 return $formats; 112 } 113 114 /** 115 * Display list of enabled languages to configure date formats for. 116 */ 117 function date_locale_format_form($form_state) { 118 $form = array(); 119 120 if (!isset($form_state['values'])) { 121 $step = 'languages'; 122 } 123 else { 124 $step = 'config'; 125 } 126 $form['step'] = array( 127 '#type' => 'value', 128 '#value' => $step, 129 ); 130 131 // Form part 1: show language selection. 132 if ($step == 'languages') { 133 // Get list of languages. 134 $languages = locale_language_list('native'); 135 136 // If site_country module is enabled, add country specific languages to 137 // languages array. 138 if (module_exists('site_country')) { 139 $country_code = variable_get('site_country_default_country', ''); 140 if (!empty($country_code)) { 141 foreach ($languages as $langcode => $name) { 142 $country_language = $langcode . '-' . $country_code; 143 if (drupal_strlen($langcode) == 2 && !in_array($country_language, array_keys($languages))) { 144 $languages[$country_language] = "$name ($country_code)"; 145 } 146 } 147 } 148 } 149 150 $form['langcode'] = array( 151 '#title' => t('Language'), 152 '#type' => 'select', 153 '#options' => $languages, 154 '#multiple' => false, 155 ); 156 157 $form['buttons']['submit'] = array( 158 '#type' => 'submit', 159 '#value' => t('Search'), 160 '#submit' => array('date_locale_format_form_language_submit'), 161 ); 162 163 } 164 165 // Form part 2: show date formats for this language. 166 else { 167 // Add Drupal core's system.js and js settings. 168 date_api_add_system_javascript(); 169 $languages = locale_language_list('native'); 170 $langcode = $form_state['values']['langcode']; 171 $language_name = $languages[$langcode]; 172 173 // Display the current language name. 174 $form['language_information'] = array( 175 '#value' => t('Date format settings for %language_name', array('%language_name' => $language_name)), 176 '#prefix' =>'<p style="font-size: 1.2em;">', 177 '#suffix' =>'</p>', 178 ); 179 180 // Get list of date format types. 181 $types = date_get_format_types(); 182 183 // Get list of available formats. 184 $formats = date_get_formats(); 185 $choices = array(); 186 foreach ($formats as $type => $list) { 187 foreach ($list as $f => $format) { 188 $choices[$f] = date_format_date(date_now(), 'custom', $f); 189 } 190 } 191 192 // Get configured formats for each language. 193 $locale_formats = date_format_locale($langcode); 194 // Display a form field for each format type. 195 foreach ($types as $type => $type_info) { 196 if (!empty($locale_formats) && in_array($type, array_keys($locale_formats))) { 197 $default = $locale_formats[$type]; 198 } 199 else { 200 $default = variable_get('date_format_' . $type, array_shift(array_keys($formats))); 201 } 202 include_once('./'. drupal_get_path('module', 'date_api') .'/date_api.admin.inc'); 203 date_api_date_format_select_field($form, $type, $type_info, $default, $choices); 204 } 205 206 $form['buttons']['submit'] = array( 207 '#type' => 'submit', 208 '#value' => t('Save'), 209 '#submit' => array('date_locale_format_form_formats_submit'), 210 ); 211 $form['buttons']['cancel'] = array( 212 '#type' => 'submit', 213 '#value' => t('Cancel'), 214 '#submit' => array('date_locale_format_form_formats_cancel'), 215 ); 216 } 217 218 return $form; 219 } 220 221 /** 222 * Submit handler for choosing a language on the date_locale_format_form. 223 * 224 * @param $form 225 * Array, containing the form structure. 226 * @param &$form_state 227 * The 'rebuild' key inside $form_state['rebuild'] structure, overrides the 228 * 'redirect' key: when it is set to TRUE, the form will be rebuilt from 229 * scratch and displayed on screen. 230 */ 231 function date_locale_format_form_language_submit($form, &$form_state) { 232 $form_state['rebuild'] = TRUE; 233 $form_state['storage']['langcode'] = $form_state['values']['langcode']; 234 } 235 236 /** 237 * Submit handler for choosing a language on the date_locale_format_form. 238 */ 239 function date_locale_format_form_formats_submit($form, &$form_state) { 240 $langcode = $form_state['storage']['langcode']; 241 242 // Get list of date format types. 243 $types = date_get_format_types(); 244 foreach ($types as $type => $type_info) { 245 $format = $form_state['values']['date_format_' . $type]; 246 if ($format == 'custom') { 247 $format = $form_state['values']['date_format_' . $type . '_custom']; 248 } 249 date_locale_locale_format_save($langcode, $type, $format); 250 } 251 drupal_set_message(t('Configuration saved.')); 252 $form_state['storage'] = FALSE; 253 $form_state['rebuild'] = FALSE; 254 $form_state['redirect'] = 'admin/settings/date-time/locale'; 255 } 256 257 /** 258 * 'Cancel' button handler for choosing a language on the 259 * date_locale_format_form. 260 */ 261 function date_locale_format_form_formats_cancel($form, &$form_state) { 262 $form_state['storage'] = FALSE; 263 $form_state['rebuild'] = FALSE; 264 $form_state['redirect'] = 'admin/settings/date-time/locale'; 265 } 266 267 /** 268 * Save locale specific date formats to the database. 269 * 270 * @param $langcode 271 * Language code, can be 2 characters, e.g. 'en' or 5 characters, e.g. 272 * 'en-CA'. 273 * @param $type 274 * Date format type, e.g. 'short', 'medium'. 275 * @param $format 276 * The date format string. 277 */ 278 function date_locale_locale_format_save($langcode, $type, $format) { 279 $locale_format = array(); 280 $locale_format['language'] = $langcode; 281 $locale_format['type'] = $type; 282 $locale_format['format'] = $format; 283 284 $is_existing = db_result(db_query("SELECT COUNT(*) FROM {date_format_locale} WHERE language = '%s' AND type = '%s'", $langcode, $type)); 285 if ($is_existing) { 286 $keys = array('type', 'language'); 287 drupal_write_record('date_format_locale', $locale_format, $keys); 288 } 289 else { 290 drupal_write_record('date_format_locale', $locale_format); 291 } 292 293 }
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 |