Add comment blocks
[civicrm-core.git] / CRM / Admin / Form / Setting / Localization.php
index 0d4f06e9e0f232335a483bf459c9d5c387b02820..bb7b40d87eab587e86335ccc960fce05ec3766aa 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2015                                |
+ | Copyright CiviCRM LLC (c) 2004-2017                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2015
+ * @copyright CiviCRM LLC (c) 2004-2017
  */
 
 /**
  * This class generates form components for Localization.
  */
 class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting {
-  // use this variable to store mappings that we compute in buildForm and also
-  // use in postProcess (CRM-1496)
-  protected $_currencySymbols;
+
+  protected $_settings = array(
+    'contact_default_language' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+    'countryLimit' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+    'customTranslateFunction' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+    'defaultContactCountry' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+    'defaultContactStateProvince' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+    'defaultCurrency' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+    'fieldSeparator' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+    'inheritLocale' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+    'lcMessages' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+    'legacyEncoding' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+    'monetaryThousandSeparator' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+    'monetaryDecimalPoint' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+    'moneyformat' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+    'moneyvalueformat' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+    'provinceLimit' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
+  );
 
   /**
    * Build the form object.
@@ -45,26 +60,18 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting {
   public function buildQuickForm() {
     $config = CRM_Core_Config::singleton();
 
-    $i18n = CRM_Core_I18n::singleton();
     CRM_Utils_System::setTitle(ts('Settings - Localization'));
 
-    $locales = CRM_Core_I18n::languages();
     $warningTitle = json_encode(ts("Warning"));
+    $defaultLocaleOptions = CRM_Admin_Form_Setting_Localization::getDefaultLocaleOptions();
+
     $domain = new CRM_Core_DAO_Domain();
     $domain->find(TRUE);
-    if ($domain->locales) {
-      // for multi-lingual sites, populate default language drop-down with available languages
-      $lcMessages = array();
-      foreach ($locales as $loc => $lang) {
-        if (substr_count($domain->locales, $loc)) {
-          $lcMessages[$loc] = $lang;
-        }
-      }
-      $this->addElement('select', 'lcMessages', ts('Default Language'), $lcMessages);
 
+    if ($domain->locales) {
       // add language limiter and language adder
-      $this->addCheckBox('languageLimit', ts('Available Languages'), array_flip($lcMessages), NULL, NULL, NULL, NULL, '   ');
-      $this->addElement('select', 'addLanguage', ts('Add Language'), array_merge(array('' => ts('- select -')), array_diff($locales, $lcMessages)));
+      $this->addCheckBox('languageLimit', ts('Available Languages'), array_flip($defaultLocaleOptions), NULL, NULL, NULL, NULL, '   ');
+      $this->addElement('select', 'addLanguage', ts('Add Language'), array_merge(array('' => ts('- select -')), array_diff(CRM_Core_I18n::languages(), $defaultLocaleOptions)));
 
       // add the ability to return to single language
       $warning = ts('This will make your CiviCRM installation a single-language one again. THIS WILL DELETE ALL DATA RELATED TO LANGUAGES OTHER THAN THE DEFAULT ONE SELECTED ABOVE (and only that language will be preserved).');
@@ -75,9 +82,6 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting {
       );
     }
     else {
-      // for single-lingual sites, populate default language drop-down with all languages
-      $this->addElement('select', 'lcMessages', ts('Default Language'), $locales);
-
       $warning = ts('Enabling multiple languages changes the schema of your database, so make sure you know what you are doing when enabling this function; making a database backup is strongly recommended.');
       $this->assign('warning', $warning);
       $warning = json_encode($warning);
@@ -91,63 +95,11 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting {
         );
       }
     }
-    $this->addElement('select', 'contact_default_language', ts('Default Language for users'), array(
-      '*default*' => ts('Use default site language'),
-      'undefined' => ts('Leave undefined'),
-      'current_site_language' => ts('Use language in use at the time'),
-    ));
-    $this->addElement('checkbox', 'inheritLocale', ts('Inherit CMS Language'));
-    $this->addElement('text', 'monetaryThousandSeparator', ts('Thousands Separator'), array('size' => 2));
-    $this->addElement('text', 'monetaryDecimalPoint', ts('Decimal Delimiter'), array('size' => 2));
-    $this->addElement('text', 'moneyformat', ts('Monetary Amount Display'));
-    $this->addElement('text', 'moneyvalueformat', ts('Monetary Value Display'));
-
-    $country = array();
-    CRM_Core_PseudoConstant::populate($country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active');
-    $i18n->localizeArray($country, array('context' => 'country'));
-    asort($country);
-
-    $includeCountry = &$this->addElement('advmultiselect', 'countryLimit',
-      ts('Available Countries') . ' ', $country,
-      array(
-        'size' => 5,
-        'style' => 'width:150px',
-        'class' => 'advmultiselect',
-      )
-    );
-
-    $includeCountry->setButtonAttributes('add', array('value' => ts('Add >>')));
-    $includeCountry->setButtonAttributes('remove', array('value' => ts('<< Remove')));
-
-    $includeState = &$this->addElement('advmultiselect', 'provinceLimit',
-      ts('Available States and Provinces') . ' ', $country,
-      array(
-        'size' => 5,
-        'style' => 'width:150px',
-        'class' => 'advmultiselect',
-      )
-    );
-
-    $includeState->setButtonAttributes('add', array('value' => ts('Add >>')));
-    $includeState->setButtonAttributes('remove', array('value' => ts('<< Remove')));
-
-    $this->addElement('select', 'defaultContactCountry', ts('Default Country'), array('' => ts('- select -')) + $country);
-    $this->addChainSelect('defaultContactStateProvince', array('label' => ts('Default State/Province')));
-
-    // we do this only to initialize currencySymbols, kinda hackish but works!
-    $config->defaultCurrencySymbol();
-
-    $symbol = $config->currencySymbols;
-    foreach ($symbol as $key => $value) {
-      $this->_currencySymbols[$key] = "$key";
-      if ($value) {
-        $this->_currencySymbols[$key] .= " ($value)";
-      }
-    }
-    $this->addElement('select', 'defaultCurrency', ts('Default Currency'), $this->_currencySymbols);
+    $this->addElement('select', 'contact_default_language', ts('Default Language for users'),
+      CRM_Admin_Form_Setting_Localization::getDefaultLanguageOptions());
 
     $includeCurrency = &$this->addElement('advmultiselect', 'currencyLimit',
-      ts('Available Currencies') . ' ', $this->_currencySymbols,
+      ts('Available Currencies') . ' ', self::getCurrencySymbols(),
       array(
         'size' => 5,
         'style' => 'width:150px',
@@ -158,10 +110,6 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting {
     $includeCurrency->setButtonAttributes('add', array('value' => ts('Add >>')));
     $includeCurrency->setButtonAttributes('remove', array('value' => ts('<< Remove')));
 
-    $this->addElement('text', 'legacyEncoding', ts('Legacy Encoding'));
-    $this->addElement('text', 'customTranslateFunction', ts('Custom Translate Function'));
-    $this->addElement('text', 'fieldSeparator', ts('Import / Export Field Separator'), array('size' => 2));
-
     $this->addFormRule(array('CRM_Admin_Form_Setting_Localization', 'formRule'));
 
     parent::buildQuickForm();
@@ -217,6 +165,8 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting {
     // retrieve default values for currencyLimit
     $this->_defaults['currencyLimit'] = array_keys(CRM_Core_OptionGroup::values('currencies_enabled'));
 
+    $this->_defaults['languageLimit'] = Civi::settings()->get('languageLimit');
+
     // CRM-5111: unset these two unconditionally, we don’t want them to stick – ever
     unset($this->_defaults['makeMultilingual']);
     unset($this->_defaults['makeSinglelingual']);
@@ -226,11 +176,6 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting {
   public function postProcess() {
     $values = $this->exportValues();
 
-    // FIXME: stupid QF not submitting unchecked checkboxen…
-    if (!isset($values['inheritLocale'])) {
-      $values['inheritLocale'] = 0;
-    }
-
     //cache contact fields retaining localized titles
     //though we changed localization, so reseting cache.
     CRM_Core_BAO_Cache::deleteGroup('contact fields');
@@ -241,9 +186,6 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting {
     // we do this only to initialize monetary decimal point and thousand separator
     $config = CRM_Core_Config::singleton();
 
-    // set default Currency Symbol
-    $values['defaultCurrencySymbol'] = $config->defaultCurrencySymbol($values['defaultCurrency']);
-
     // save enabled currencies and defaul currency in option group 'currencies_enabled'
     // CRM-1496
     if (empty($values['currencyLimit'])) {
@@ -262,9 +204,10 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting {
     // get labels for all the currencies
     $options = array();
 
+    $currencySymbols = self::getCurrencySymbols();
     for ($i = 0; $i < count($values['currencyLimit']); $i++) {
       $options[] = array(
-        'label' => $this->_currencySymbols[$values['currencyLimit'][$i]],
+        'label' => $currencySymbols[$values['currencyLimit'][$i]],
         'value' => $values['currencyLimit'][$i],
         'weight' => $i + 1,
         'is_active' => 1,
@@ -305,12 +248,105 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting {
     // if we manipulated the language list, return to the localization admin screen
     $return = (bool) (CRM_Utils_Array::value('makeMultilingual', $values) or CRM_Utils_Array::value('addLanguage', $values));
 
+    $filteredValues = $values;
+    unset($filteredValues['makeMultilingual']);
+    unset($filteredValues['makeSinglelingual']);
+    unset($filteredValues['addLanguage']);
+    unset($filteredValues['languageLimit']);
+
+    Civi::settings()->set('languageLimit', CRM_Utils_Array::value('languageLimit', $values));
+
     // save all the settings
-    parent::commonProcess($values);
+    parent::commonProcess($filteredValues);
 
     if ($return) {
       CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/setting/localization', 'reset=1'));
     }
   }
 
+  /**
+   * @return array
+   */
+  public static function getAvailableCountries() {
+    $i18n = CRM_Core_I18n::singleton();
+    $country = array();
+    CRM_Core_PseudoConstant::populate($country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active');
+    $i18n->localizeArray($country, array('context' => 'country'));
+    asort($country);
+    return $country;
+  }
+
+  /**
+   * Get the default locale options.
+   *
+   * @return array
+   */
+  public static function getDefaultLocaleOptions() {
+    $domain = new CRM_Core_DAO_Domain();
+    $domain->find(TRUE);
+    $locales = CRM_Core_I18n::languages();
+    if ($domain->locales) {
+      // for multi-lingual sites, populate default language drop-down with available languages
+      $defaultLocaleOptions = array();
+      foreach ($locales as $loc => $lang) {
+        if (substr_count($domain->locales, $loc)) {
+          $defaultLocaleOptions[$loc] = $lang;
+        }
+      }
+    }
+    else {
+      $defaultLocaleOptions = $locales;
+    }
+    return $defaultLocaleOptions;
+  }
+
+  /**
+   * Get a list of currencies (with their symbols).
+   *
+   * @return array
+   *   Array('USD' => 'USD ($)').
+   */
+  public static function getCurrencySymbols() {
+    $symbols = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array(
+      'labelColumn' => 'symbol',
+      'orderColumn' => TRUE,
+    ));
+    $_currencySymbols = array();
+    foreach ($symbols as $key => $value) {
+      $_currencySymbols[$key] = "$key";
+      if ($value) {
+        $_currencySymbols[$key] .= " ($value)";
+      }
+    }
+    return $_currencySymbols;
+  }
+
+  public static function onChangeLcMessages($oldLocale, $newLocale, $metadata, $domainID) {
+    if ($oldLocale == $newLocale) {
+      return;
+    }
+
+    $session = CRM_Core_Session::singleton();
+    if ($newLocale && $session->get('userID')) {
+      $ufm = new CRM_Core_DAO_UFMatch();
+      $ufm->contact_id = $session->get('userID');
+      if ($newLocale && $ufm->find(TRUE)) {
+        $ufm->language = $newLocale;
+        $ufm->save();
+        $session->set('lcMessages', $newLocale);
+      }
+    }
+  }
+
+  /**
+   * @return array
+   */
+  public static function getDefaultLanguageOptions() {
+    return array(
+      '*default*' => ts('Use default site language'),
+      'undefined' => ts('Leave undefined'),
+      'current_site_language' => ts('Use language in use at the time'),
+    );
+  }
+
 }