version fixes
[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 * $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 * Build the form object.
47 *
48 * @return void
49 */
50 public function buildQuickForm() {
51 $config = CRM_Core_Config::singleton();
52
53 $i18n = CRM_Core_I18n::singleton();
54 CRM_Utils_System::setTitle(ts('Settings - Localization'));
55
56 $locales = CRM_Core_I18n::languages();
57 $warningTitle = json_encode(ts("Warning"));
58 $domain = new CRM_Core_DAO_Domain();
59 $domain->find(TRUE);
60 if ($domain->locales) {
61 // for multi-lingual sites, populate default language drop-down with available languages
62 $lcMessages = array();
63 foreach ($locales as $loc => $lang) {
64 if (substr_count($domain->locales, $loc)) {
65 $lcMessages[$loc] = $lang;
66 }
67 }
68 $this->addElement('select', 'lcMessages', ts('Default Language'), $lcMessages);
69
70 // add language limiter and language adder
71 $this->addCheckBox('languageLimit', ts('Available Languages'), array_flip($lcMessages), NULL, NULL, NULL, NULL, ' &nbsp; ');
72 $this->addElement('select', 'addLanguage', ts('Add Language'), array_merge(array('' => ts('- select -')), array_diff($locales, $lcMessages)));
73
74 // add the ability to return to single language
75 $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).');
76 $this->assign('warning', $warning);
77 $warning = json_encode($warning);
78 $this->addElement('checkbox', 'makeSinglelingual', ts('Return to Single Language'),
79 NULL, array('onChange' => "if (this.checked) CRM.alert($warning, $warningTitle)")
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('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 $warning = json_encode($warning);
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) CRM.alert($warning, $warningTitle)")
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 $this->addChainSelect('defaultContactStateProvince', array('label' => ts('Default State/Province')));
137
138 // we do this only to initialize currencySymbols, kinda hackish but works!
139 $config->defaultCurrencySymbol();
140
141 $symbol = $config->currencySymbols;
142 foreach ($symbol as $key => $value) {
143 $this->_currencySymbols[$key] = "$key";
144 if ($value) {
145 $this->_currencySymbols[$key] .= " ($value)";
146 }
147 }
148 $this->addElement('select', 'defaultCurrency', ts('Default Currency'), $this->_currencySymbols);
149
150 $includeCurrency = &$this->addElement('advmultiselect', 'currencyLimit',
151 ts('Available Currencies') . ' ', $this->_currencySymbols,
152 array(
153 'size' => 5,
154 'style' => 'width:150px',
155 'class' => 'advmultiselect',
156 )
157 );
158
159 $includeCurrency->setButtonAttributes('add', array('value' => ts('Add >>')));
160 $includeCurrency->setButtonAttributes('remove', array('value' => ts('<< Remove')));
161
162 $this->addElement('text', 'legacyEncoding', ts('Legacy Encoding'));
163 $this->addElement('text', 'customTranslateFunction', ts('Custom Translate Function'));
164 $this->addElement('text', 'fieldSeparator', ts('Import / Export Field Separator'), array('size' => 2));
165
166 $this->addFormRule(array('CRM_Admin_Form_Setting_Localization', 'formRule'));
167
168 parent::buildQuickForm();
169 }
170
171 /**
172 * @param $fields
173 *
174 * @return array|bool
175 */
176 public static function formRule($fields) {
177 $errors = array();
178 if (CRM_Utils_Array::value('monetaryThousandSeparator', $fields) ==
179 CRM_Utils_Array::value('monetaryDecimalPoint', $fields)
180 ) {
181 $errors['monetaryThousandSeparator'] = ts('Thousands Separator and Decimal Delimiter can not be the same.');
182 }
183
184 if (strlen($fields['monetaryThousandSeparator']) == 0) {
185 $errors['monetaryThousandSeparator'] = ts('Thousands Separator can not be empty. You can use a space character instead.');
186 }
187
188 if (strlen($fields['monetaryThousandSeparator']) > 1) {
189 $errors['monetaryThousandSeparator'] = ts('Thousands Separator can not have more than 1 character.');
190 }
191
192 if (strlen($fields['monetaryDecimalPoint']) > 1) {
193 $errors['monetaryDecimalPoint'] = ts('Decimal Delimiter can not have more than 1 character.');
194 }
195
196 if (trim($fields['customTranslateFunction']) &&
197 !function_exists(trim($fields['customTranslateFunction']))
198 ) {
199 $errors['customTranslateFunction'] = ts('Please define the custom translation function first.');
200 }
201
202 // CRM-7962, CRM-7713, CRM-9004
203 if (!empty($fields['defaultContactCountry']) &&
204 (!empty($fields['countryLimit']) &&
205 (!in_array($fields['defaultContactCountry'], $fields['countryLimit']))
206 )
207 ) {
208 $errors['defaultContactCountry'] = ts('Please select a default country that is in the list of available countries.');
209 }
210
211 return empty($errors) ? TRUE : $errors;
212 }
213
214 public function setDefaultValues() {
215 parent::setDefaultValues();
216
217 // CRM-1496
218 // retrieve default values for currencyLimit
219 $this->_defaults['currencyLimit'] = array_keys(CRM_Core_OptionGroup::values('currencies_enabled'));
220
221 // CRM-5111: unset these two unconditionally, we don’t want them to stick – ever
222 unset($this->_defaults['makeMultilingual']);
223 unset($this->_defaults['makeSinglelingual']);
224 return $this->_defaults;
225 }
226
227 public function postProcess() {
228 $values = $this->exportValues();
229
230 // FIXME: stupid QF not submitting unchecked checkboxen…
231 if (!isset($values['inheritLocale'])) {
232 $values['inheritLocale'] = 0;
233 }
234
235 //cache contact fields retaining localized titles
236 //though we changed localization, so reseting cache.
237 CRM_Core_BAO_Cache::deleteGroup('contact fields');
238
239 //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache.
240 CRM_Core_BAO_Cache::deleteGroup('navigation');
241
242 // we do this only to initialize monetary decimal point and thousand separator
243 $config = CRM_Core_Config::singleton();
244
245 // set default Currency Symbol
246 $values['defaultCurrencySymbol'] = $config->defaultCurrencySymbol($values['defaultCurrency']);
247
248 // save enabled currencies and defaul currency in option group 'currencies_enabled'
249 // CRM-1496
250 if (empty($values['currencyLimit'])) {
251 $values['currencyLimit'] = array($values['defaultCurrency']);
252 }
253 elseif (!in_array($values['defaultCurrency'],
254 $values['currencyLimit']
255 )
256 ) {
257 $values['currencyLimit'][] = $values['defaultCurrency'];
258 }
259
260 // sort so that when we display drop down, weights have right value
261 sort($values['currencyLimit']);
262
263 // get labels for all the currencies
264 $options = array();
265
266 for ($i = 0; $i < count($values['currencyLimit']); $i++) {
267 $options[] = array(
268 'label' => $this->_currencySymbols[$values['currencyLimit'][$i]],
269 'value' => $values['currencyLimit'][$i],
270 'weight' => $i + 1,
271 'is_active' => 1,
272 'is_default' => $values['currencyLimit'][$i] == $values['defaultCurrency'],
273 );
274 }
275
276 $dontCare = NULL;
277 CRM_Core_OptionGroup::createAssoc('currencies_enabled',
278 $options,
279 $dontCare
280 );
281
282 // unset currencyLimit so we dont store there
283 unset($values['currencyLimit']);
284
285 // make the site multi-lang if requested
286 if (!empty($values['makeMultilingual'])) {
287 CRM_Core_I18n_Schema::makeMultilingual($values['lcMessages']);
288 $values['languageLimit'][$values['lcMessages']] = 1;
289 // make the site single-lang if requested
290 }
291 elseif (!empty($values['makeSinglelingual'])) {
292 CRM_Core_I18n_Schema::makeSinglelingual($values['lcMessages']);
293 $values['languageLimit'] = '';
294 }
295
296 // add a new db locale if the requested language is not yet supported by the db
297 if (!CRM_Utils_Array::value('makeSinglelingual', $values) and CRM_Utils_Array::value('addLanguage', $values)) {
298 $domain = new CRM_Core_DAO_Domain();
299 $domain->find(TRUE);
300 if (!substr_count($domain->locales, $values['addLanguage'])) {
301 CRM_Core_I18n_Schema::addLocale($values['addLanguage'], $values['lcMessages']);
302 }
303 $values['languageLimit'][$values['addLanguage']] = 1;
304 }
305
306 // if we manipulated the language list, return to the localization admin screen
307 $return = (bool) (CRM_Utils_Array::value('makeMultilingual', $values) or CRM_Utils_Array::value('addLanguage', $values));
308
309 // save all the settings
310 parent::commonProcess($values);
311
312 if ($return) {
313 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/setting/localization', 'reset=1'));
314 }
315 }
316
317 }