[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/date/date_locale/ -> date_locale.module (source)

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


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7