3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 * This class generates form components for Localization
37 class CRM_Admin_Form_Setting_Localization
extends CRM_Admin_Form_Setting
{
38 // use this variable to store mappings that we compute in buildForm and also
39 // use in postProcess (CRM-1496)
40 protected $_currencySymbols;
43 * Build the form object.
45 public function buildQuickForm() {
46 $config = CRM_Core_Config
::singleton();
48 $i18n = CRM_Core_I18n
::singleton();
49 CRM_Utils_System
::setTitle(ts('Settings - Localization'));
51 $locales = CRM_Core_I18n
::languages();
52 $warningTitle = json_encode(ts("Warning"));
53 $domain = new CRM_Core_DAO_Domain();
55 if ($domain->locales
) {
56 // for multi-lingual sites, populate default language drop-down with available languages
57 $lcMessages = array();
58 foreach ($locales as $loc => $lang) {
59 if (substr_count($domain->locales
, $loc)) {
60 $lcMessages[$loc] = $lang;
63 $this->addElement('select', 'lcMessages', ts('Default Language'), $lcMessages);
65 // add language limiter and language adder
66 $this->addCheckBox('languageLimit', ts('Available Languages'), array_flip($lcMessages), NULL, NULL, NULL, NULL, ' ');
67 $this->addElement('select', 'addLanguage', ts('Add Language'), array_merge(array('' => ts('- select -')), array_diff($locales, $lcMessages)));
69 // add the ability to return to single language
70 $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).');
71 $this->assign('warning', $warning);
72 $warning = json_encode($warning);
73 $this->addElement('checkbox', 'makeSinglelingual', ts('Return to Single Language'),
74 NULL, array('onChange' => "if (this.checked) CRM.alert($warning, $warningTitle)")
78 // for single-lingual sites, populate default language drop-down with all languages
79 $this->addElement('select', 'lcMessages', ts('Default Language'), $locales);
81 $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.');
82 $this->assign('warning', $warning);
83 $warning = json_encode($warning);
84 $validTriggerPermission = CRM_Core_DAO
::checkTriggerViewPermission(TRUE);
86 if ($validTriggerPermission &&
89 $this->addElement('checkbox', 'makeMultilingual', ts('Enable Multiple Languages'),
90 NULL, array('onChange' => "if (this.checked) CRM.alert($warning, $warningTitle)")
95 $this->addElement('checkbox', 'inheritLocale', ts('Inherit CMS Language'));
96 $this->addElement('text', 'monetaryThousandSeparator', ts('Thousands Separator'), array('size' => 2));
97 $this->addElement('text', 'monetaryDecimalPoint', ts('Decimal Delimiter'), array('size' => 2));
98 $this->addElement('text', 'moneyformat', ts('Monetary Amount Display'));
99 $this->addElement('text', 'moneyvalueformat', ts('Monetary Value Display'));
102 CRM_Core_PseudoConstant
::populate($country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active');
103 $i18n->localizeArray($country, array('context' => 'country'));
106 $includeCountry = &$this->addElement('advmultiselect', 'countryLimit',
107 ts('Available Countries') . ' ', $country,
110 'style' => 'width:150px',
111 'class' => 'advmultiselect',
115 $includeCountry->setButtonAttributes('add', array('value' => ts('Add >>')));
116 $includeCountry->setButtonAttributes('remove', array('value' => ts('<< Remove')));
118 $includeState = &$this->addElement('advmultiselect', 'provinceLimit',
119 ts('Available States and Provinces') . ' ', $country,
122 'style' => 'width:150px',
123 'class' => 'advmultiselect',
127 $includeState->setButtonAttributes('add', array('value' => ts('Add >>')));
128 $includeState->setButtonAttributes('remove', array('value' => ts('<< Remove')));
130 $this->addElement('select', 'defaultContactCountry', ts('Default Country'), array('' => ts('- select -')) +
$country);
131 $this->addChainSelect('defaultContactStateProvince', array('label' => ts('Default State/Province')));
133 // we do this only to initialize currencySymbols, kinda hackish but works!
134 $config->defaultCurrencySymbol();
136 $symbol = $config->currencySymbols
;
137 foreach ($symbol as $key => $value) {
138 $this->_currencySymbols
[$key] = "$key";
140 $this->_currencySymbols
[$key] .= " ($value)";
143 $this->addElement('select', 'defaultCurrency', ts('Default Currency'), $this->_currencySymbols
);
145 $includeCurrency = &$this->addElement('advmultiselect', 'currencyLimit',
146 ts('Available Currencies') . ' ', $this->_currencySymbols
,
149 'style' => 'width:150px',
150 'class' => 'advmultiselect',
154 $includeCurrency->setButtonAttributes('add', array('value' => ts('Add >>')));
155 $includeCurrency->setButtonAttributes('remove', array('value' => ts('<< Remove')));
157 $this->addElement('text', 'legacyEncoding', ts('Legacy Encoding'));
158 $this->addElement('text', 'customTranslateFunction', ts('Custom Translate Function'));
159 $this->addElement('text', 'fieldSeparator', ts('Import / Export Field Separator'), array('size' => 2));
161 $this->addFormRule(array('CRM_Admin_Form_Setting_Localization', 'formRule'));
163 parent
::buildQuickForm();
171 public static function formRule($fields) {
173 if (CRM_Utils_Array
::value('monetaryThousandSeparator', $fields) ==
174 CRM_Utils_Array
::value('monetaryDecimalPoint', $fields)
176 $errors['monetaryThousandSeparator'] = ts('Thousands Separator and Decimal Delimiter can not be the same.');
179 if (strlen($fields['monetaryThousandSeparator']) == 0) {
180 $errors['monetaryThousandSeparator'] = ts('Thousands Separator can not be empty. You can use a space character instead.');
183 if (strlen($fields['monetaryThousandSeparator']) > 1) {
184 $errors['monetaryThousandSeparator'] = ts('Thousands Separator can not have more than 1 character.');
187 if (strlen($fields['monetaryDecimalPoint']) > 1) {
188 $errors['monetaryDecimalPoint'] = ts('Decimal Delimiter can not have more than 1 character.');
191 if (trim($fields['customTranslateFunction']) &&
192 !function_exists(trim($fields['customTranslateFunction']))
194 $errors['customTranslateFunction'] = ts('Please define the custom translation function first.');
197 // CRM-7962, CRM-7713, CRM-9004
198 if (!empty($fields['defaultContactCountry']) &&
199 (!empty($fields['countryLimit']) &&
200 (!in_array($fields['defaultContactCountry'], $fields['countryLimit']))
203 $errors['defaultContactCountry'] = ts('Please select a default country that is in the list of available countries.');
206 return empty($errors) ?
TRUE : $errors;
209 public function setDefaultValues() {
210 parent
::setDefaultValues();
213 // retrieve default values for currencyLimit
214 $this->_defaults
['currencyLimit'] = array_keys(CRM_Core_OptionGroup
::values('currencies_enabled'));
216 // CRM-5111: unset these two unconditionally, we don’t want them to stick – ever
217 unset($this->_defaults
['makeMultilingual']);
218 unset($this->_defaults
['makeSinglelingual']);
219 return $this->_defaults
;
222 public function postProcess() {
223 $values = $this->exportValues();
225 // FIXME: stupid QF not submitting unchecked checkboxen…
226 if (!isset($values['inheritLocale'])) {
227 $values['inheritLocale'] = 0;
230 //cache contact fields retaining localized titles
231 //though we changed localization, so reseting cache.
232 CRM_Core_BAO_Cache
::deleteGroup('contact fields');
234 //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache.
235 CRM_Core_BAO_Cache
::deleteGroup('navigation');
237 // we do this only to initialize monetary decimal point and thousand separator
238 $config = CRM_Core_Config
::singleton();
240 // set default Currency Symbol
241 $values['defaultCurrencySymbol'] = $config->defaultCurrencySymbol($values['defaultCurrency']);
243 // save enabled currencies and defaul currency in option group 'currencies_enabled'
245 if (empty($values['currencyLimit'])) {
246 $values['currencyLimit'] = array($values['defaultCurrency']);
248 elseif (!in_array($values['defaultCurrency'],
249 $values['currencyLimit']
252 $values['currencyLimit'][] = $values['defaultCurrency'];
255 // sort so that when we display drop down, weights have right value
256 sort($values['currencyLimit']);
258 // get labels for all the currencies
261 for ($i = 0; $i < count($values['currencyLimit']); $i++
) {
263 'label' => $this->_currencySymbols
[$values['currencyLimit'][$i]],
264 'value' => $values['currencyLimit'][$i],
267 'is_default' => $values['currencyLimit'][$i] == $values['defaultCurrency'],
272 CRM_Core_OptionGroup
::createAssoc('currencies_enabled',
277 // unset currencyLimit so we dont store there
278 unset($values['currencyLimit']);
280 // make the site multi-lang if requested
281 if (!empty($values['makeMultilingual'])) {
282 CRM_Core_I18n_Schema
::makeMultilingual($values['lcMessages']);
283 $values['languageLimit'][$values['lcMessages']] = 1;
284 // make the site single-lang if requested
286 elseif (!empty($values['makeSinglelingual'])) {
287 CRM_Core_I18n_Schema
::makeSinglelingual($values['lcMessages']);
288 $values['languageLimit'] = '';
291 // add a new db locale if the requested language is not yet supported by the db
292 if (!CRM_Utils_Array
::value('makeSinglelingual', $values) and CRM_Utils_Array
::value('addLanguage', $values)) {
293 $domain = new CRM_Core_DAO_Domain();
295 if (!substr_count($domain->locales
, $values['addLanguage'])) {
296 CRM_Core_I18n_Schema
::addLocale($values['addLanguage'], $values['lcMessages']);
298 $values['languageLimit'][$values['addLanguage']] = 1;
301 // if we manipulated the language list, return to the localization admin screen
302 $return = (bool) (CRM_Utils_Array
::value('makeMultilingual', $values) or CRM_Utils_Array
::value('addLanguage', $values));
304 // save all the settings
305 parent
::commonProcess($values);
308 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/setting/localization', 'reset=1'));