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