Merge remote-tracking branch 'upstream/4.3' into 4.3-4.4-2013-11-03-17-29-16
[civicrm-core.git] / CRM / Admin / Form / Setting / Localization.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for Localization
38 *
39 */
40 class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting {
41 // use this variable to store mappings that we compute in buildForm and also
42 // use in postProcess (CRM-1496)
43 protected $_currencySymbols;
44
45 /**
46 * Function to build the form
47 *
48 * @return None
49 * @access public
50 */
51 public function buildQuickForm() {
52 $config = CRM_Core_Config::singleton();
53
54 $i18n = CRM_Core_I18n::singleton();
55 CRM_Utils_System::setTitle(ts('Settings - Localization'));
56
57 $locales = CRM_Core_I18n::languages();
58
59 $domain = new CRM_Core_DAO_Domain();
60 $domain->find(TRUE);
61 if ($domain->locales) {
62 // for multi-lingual sites, populate default language drop-down with available languages
63 $lcMessages = array();
64 foreach ($locales as $loc => $lang) {
65 if (substr_count($domain->locales, $loc)) {
66 $lcMessages[$loc] = $lang;
67 }
68 }
69 $this->addElement('select', 'lcMessages', ts('Default Language'), $lcMessages);
70
71 // add language limiter and language adder
72 $this->addCheckBox('languageLimit', ts('Available Languages'), array_flip($lcMessages), NULL, NULL, NULL, NULL, ' &nbsp; ');
73 $this->addElement('select', 'addLanguage', ts('Add Language'), array_merge(array('' => ts('- select -')), array_diff($locales, $lcMessages)));
74
75 // add the ability to return to single language
76 $warning = ts('WARNING: 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).');
77 $this->assign('warning', $warning);
78 $this->addElement('checkbox', 'makeSinglelingual', ts('Return to Single Language'),
79 NULL, array('onChange' => "if (this.checked) alert('$warning')")
80 );
81 }
82 else {
83 // for single-lingual sites, populate default language drop-down with all languages
84 $this->addElement('select', 'lcMessages', ts('Default Language'), $locales);
85
86 $warning = ts('WARNING: 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.');
87 $this->assign('warning', $warning);
88
89 $validTriggerPermission = CRM_Core_DAO::checkTriggerViewPermission(TRUE);
90
91 if ($validTriggerPermission &&
92 !$config->logging
93 ) {
94 $this->addElement('checkbox', 'makeMultilingual', ts('Enable Multiple Languages'),
95 NULL, array('onChange' => "if (this.checked) alert('$warning')")
96 );
97 }
98 }
99
100 $this->addElement('checkbox', 'inheritLocale', ts('Inherit CMS Language'));
101 $this->addElement('text', 'monetaryThousandSeparator', ts('Thousands Separator'), array('size' => 2));
102 $this->addElement('text', 'monetaryDecimalPoint', ts('Decimal Delimiter'), array('size' => 2));
103 $this->addElement('text', 'moneyformat', ts('Monetary Amount Display'));
104 $this->addElement('text', 'moneyvalueformat', ts('Monetary Value Display'));
105
106 $country = array();
107 CRM_Core_PseudoConstant::populate($country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active');
108 $i18n->localizeArray($country, array('context' => 'country'));
109 asort($country);
110
111 $includeCountry = &$this->addElement('advmultiselect', 'countryLimit',
112 ts('Available Countries') . ' ', $country,
113 array(
114 'size' => 5,
115 'style' => 'width:150px',
116 'class' => 'advmultiselect',
117 )
118 );
119
120 $includeCountry->setButtonAttributes('add', array('value' => ts('Add >>')));
121 $includeCountry->setButtonAttributes('remove', array('value' => ts('<< Remove')));
122
123 $includeState = &$this->addElement('advmultiselect', 'provinceLimit',
124 ts('Available States and Provinces') . ' ', $country,
125 array(
126 'size' => 5,
127 'style' => 'width:150px',
128 'class' => 'advmultiselect',
129 )
130 );
131
132 $includeState->setButtonAttributes('add', array('value' => ts('Add >>')));
133 $includeState->setButtonAttributes('remove', array('value' => ts('<< Remove')));
134
135 $this->addElement('select', 'defaultContactCountry', ts('Default Country'), array('' => ts('- select -')) + $country);
136
137 /***Default State/Province***/
138 $stateCountryMap = array();
139 $stateCountryMap[] = array(
140 'state_province' => 'defaultContactStateProvince',
141 'country' => 'defaultContactCountry',
142 );
143
144 $countryDefault = isset($this->_submitValues['defaultContactCountry']) ? $this->_submitValues['defaultContactCountry'] : $config->defaultContactCountry;
145
146 if ($countryDefault) {
147 $selectStateProvinceOptions = array('' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvinceForCountry($countryDefault);
148 }
149 else {
150 $selectStateProvinceOptions = array('' => ts('- select a country -'));
151 }
152
153 $i18n->localizeArray($selectStateProvinceOptions, array('context' => 'state_province'));
154 asort($selectStateProvinceOptions);
155
156 $this->addElement('select', 'defaultContactStateProvince', ts('Default State/Province'), $selectStateProvinceOptions);
157
158 // state country js
159 CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap);
160
161 $defaults = array();
162 CRM_Core_BAO_Address::fixAllStateSelects($form, $defaults);
163
164 // we do this only to initialize currencySymbols, kinda hackish but works!
165 $config->defaultCurrencySymbol();
166
167 $symbol = $config->currencySymbols;
168 foreach ($symbol as $key => $value) {
169 $this->_currencySymbols[$key] = "$key";
170 if ($value) {
171 $this->_currencySymbols[$key] .= " ($value)";
172 }
173 }
174 $this->addElement('select', 'defaultCurrency', ts('Default Currency'), $this->_currencySymbols);
175
176 $includeCurrency = &$this->addElement('advmultiselect', 'currencyLimit',
177 ts('Available Currencies') . ' ', $this->_currencySymbols,
178 array(
179 'size' => 5,
180 'style' => 'width:150px',
181 'class' => 'advmultiselect',
182 )
183 );
184
185 $includeCurrency->setButtonAttributes('add', array('value' => ts('Add >>')));
186 $includeCurrency->setButtonAttributes('remove', array('value' => ts('<< Remove')));
187
188 $this->addElement('text', 'legacyEncoding', ts('Legacy Encoding'));
189 $this->addElement('text', 'customTranslateFunction', ts('Custom Translate Function'));
190 $this->addElement('text', 'fieldSeparator', ts('Import / Export Field Separator'), array('size' => 2));
191
192 $this->addFormRule(array('CRM_Admin_Form_Setting_Localization', 'formRule'));
193
194 parent::buildQuickForm();
195 }
196
197 static function formRule($fields) {
198 $errors = array();
199 if (CRM_Utils_Array::value('monetaryThousandSeparator', $fields) ==
200 CRM_Utils_Array::value('monetaryDecimalPoint', $fields)
201 ) {
202 $errors['monetaryThousandSeparator'] = ts('Thousands Separator and Decimal Delimiter can not be the same.');
203 }
204
205 if (strlen($fields['monetaryThousandSeparator']) == 0) {
206 $errors['monetaryThousandSeparator'] = ts('Thousands Separator can not be empty. You can use a space character instead.');
207 }
208
209 if (strlen($fields['monetaryThousandSeparator']) > 1) {
210 $errors['monetaryThousandSeparator'] = ts('Thousands Separator can not have more than 1 character.');
211 }
212
213 if (strlen($fields['monetaryDecimalPoint']) > 1) {
214 $errors['monetaryDecimalPoint'] = ts('Decimal Delimiter can not have more than 1 character.');
215 }
216
217 if (trim($fields['customTranslateFunction']) &&
218 !function_exists(trim($fields['customTranslateFunction']))
219 ) {
220 $errors['customTranslateFunction'] = ts('Please define the custom translation function first.');
221 }
222
223 // CRM-7962, CRM-7713, CRM-9004
224 if (!empty($fields['defaultContactCountry']) &&
225 (CRM_Utils_Array::value('countryLimit', $fields) &&
226 (!in_array($fields['defaultContactCountry'], $fields['countryLimit']))
227 )
228 ) {
229 $errors['defaultContactCountry'] = ts('Please select a default country that is in the list of available countries.');
230 }
231
232 return empty($errors) ? TRUE : $errors;
233 }
234
235 function setDefaultValues() {
236 parent::setDefaultValues();
237
238 // CRM-1496
239 // retrieve default values for currencyLimit
240 $this->_defaults['currencyLimit'] = array_keys(CRM_Core_OptionGroup::values('currencies_enabled'));
241
242 // CRM-5111: unset these two unconditionally, we don’t want them to stick – ever
243 unset($this->_defaults['makeMultilingual']);
244 unset($this->_defaults['makeSinglelingual']);
245 return $this->_defaults;
246 }
247
248 public function postProcess() {
249 $values = $this->exportValues();
250
251 // FIXME: stupid QF not submitting unchecked checkboxen…
252 if (!isset($values['inheritLocale'])) {
253 $values['inheritLocale'] = 0;
254 }
255
256 //cache contact fields retaining localized titles
257 //though we changed localization, so reseting cache.
258 CRM_Core_BAO_Cache::deleteGroup('contact fields');
259
260 //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache.
261 CRM_Core_BAO_Cache::deleteGroup('navigation');
262
263 // we do this only to initialize monetary decimal point and thousand separator
264 $config = CRM_Core_Config::singleton();
265
266 // set default Currency Symbol
267 $values['defaultCurrencySymbol'] = $config->defaultCurrencySymbol($values['defaultCurrency']);
268
269 // save enabled currencies and defaul currency in option group 'currencies_enabled'
270 // CRM-1496
271 if (empty($values['currencyLimit'])) {
272 $values['currencyLimit'] = array($values['defaultCurrency']);
273 }
274 elseif (!in_array($values['defaultCurrency'],
275 $values['currencyLimit']
276 )) {
277 $values['currencyLimit'][] = $values['defaultCurrency'];
278 }
279
280 // sort so that when we display drop down, weights have right value
281 sort($values['currencyLimit']);
282
283 // get labels for all the currencies
284 $options = array();
285
286 for ($i = 0; $i < count($values['currencyLimit']); $i++) {
287 $options[] = array(
288 'label' => $this->_currencySymbols[$values['currencyLimit'][$i]],
289 'value' => $values['currencyLimit'][$i],
290 'weight' => $i + 1,
291 'is_active' => 1,
292 'is_default' => $values['currencyLimit'][$i] == $values['defaultCurrency'],
293 );
294 }
295
296 $dontCare = NULL;
297 CRM_Core_OptionGroup::createAssoc('currencies_enabled',
298 $options,
299 $dontCare
300 );
301
302 // unset currencyLimit so we dont store there
303 unset($values['currencyLimit']);
304
305 // make the site multi-lang if requested
306 if (CRM_Utils_Array::value('makeMultilingual', $values)) {
307 CRM_Core_I18n_Schema::makeMultilingual($values['lcMessages']);
308 $values['languageLimit'][$values['lcMessages']] = 1;
309 // make the site single-lang if requested
310 }
311 elseif (CRM_Utils_Array::value('makeSinglelingual', $values)) {
312 CRM_Core_I18n_Schema::makeSinglelingual($values['lcMessages']);
313 $values['languageLimit'] = '';
314 }
315
316 // add a new db locale if the requested language is not yet supported by the db
317 if (!CRM_Utils_Array::value('makeSinglelingual', $values) and CRM_Utils_Array::value('addLanguage', $values)) {
318 $domain = new CRM_Core_DAO_Domain();
319 $domain->find(TRUE);
320 if (!substr_count($domain->locales, $values['addLanguage'])) {
321 CRM_Core_I18n_Schema::addLocale($values['addLanguage'], $values['lcMessages']);
322 }
323 $values['languageLimit'][$values['addLanguage']] = 1;
324 }
325
326 // if we manipulated the language list, return to the localization admin screen
327 $return = (bool)(CRM_Utils_Array::value('makeMultilingual', $values) or CRM_Utils_Array::value('addLanguage', $values));
328
329 // save all the settings
330 parent::commonProcess($values);
331
332 if ($return) {
333 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/setting/localization', 'reset=1'));
334 }
335 }
336 }
337