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