Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
66f9e52b | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 | 32 | * $Id$ |
6a488035 TO |
33 | */ |
34 | class CRM_Admin_Form_WordReplacements extends CRM_Core_Form { | |
35 | protected $_numStrings = 10; | |
36 | ||
37 | protected $_stringName = NULL; | |
38 | ||
39 | protected $_defaults = NULL; | |
40 | ||
d24c529a CW |
41 | public $unsavedChangesWarn = TRUE; |
42 | ||
1ca4ca9f EM |
43 | /** |
44 | * Pre process function. | |
45 | */ | |
00be9182 | 46 | public function preProcess() { |
0f65e834 TO |
47 | // This controller was originally written to CRUD $config->locale_custom_strings, |
48 | // but that's no longer the canonical store. Re-sync from canonical store to ensure | |
49 | // that we display that latest data. This is inefficient - at some point, we | |
50 | // should rewrite this UI. | |
9762f6ff | 51 | CRM_Core_BAO_WordReplacement::rebuild(FALSE); |
0f65e834 | 52 | |
6a488035 TO |
53 | $this->_soInstance = CRM_Utils_Array::value('instance', $_GET); |
54 | $this->assign('soInstance', $this->_soInstance); | |
6a488035 TO |
55 | } |
56 | ||
e0ef6999 | 57 | /** |
1ca4ca9f EM |
58 | * Set default values. |
59 | * | |
e0ef6999 EM |
60 | * @return array |
61 | */ | |
6a488035 TO |
62 | public function setDefaultValues() { |
63 | if ($this->_defaults !== NULL) { | |
64 | return $this->_defaults; | |
65 | } | |
66 | ||
67 | $this->_defaults = array(); | |
68 | ||
69 | $config = CRM_Core_Config::singleton(); | |
70 | ||
71 | $values = $config->localeCustomStrings[$config->lcMessages]; | |
72 | $i = 1; | |
73 | ||
74 | $enableDisable = array( | |
75 | 1 => 'enabled', | |
76 | 0 => 'disabled', | |
77 | ); | |
78 | ||
79 | $cardMatch = array('wildcardMatch', 'exactMatch'); | |
80 | ||
81 | foreach ($enableDisable as $key => $val) { | |
82 | foreach ($cardMatch as $kc => $vc) { | |
83 | if (!empty($values[$val][$vc])) { | |
84 | foreach ($values[$val][$vc] as $k => $v) { | |
85 | $this->_defaults["enabled"][$i] = $key; | |
86 | $this->_defaults["cb"][$i] = $kc; | |
87 | $this->_defaults["old"][$i] = $k; | |
88 | $this->_defaults["new"][$i] = $v; | |
89 | $i++; | |
90 | } | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | $name = $this->_stringName = "custom_string_override_{$config->lcMessages}"; | |
96 | if (isset($config->$name) && | |
97 | is_array($config->$name) | |
98 | ) { | |
99 | $this->_numStrings = 1; | |
100 | foreach ($config->$name as $old => $newValues) { | |
101 | $this->_numStrings++; | |
102 | $this->_numStrings += 9; | |
103 | } | |
104 | } | |
105 | else { | |
106 | $this->_numStrings = 10; | |
107 | } | |
108 | ||
109 | return $this->_defaults; | |
110 | } | |
111 | ||
112 | /** | |
eceb18cc | 113 | * Build the form object. |
6a488035 TO |
114 | */ |
115 | public function buildQuickForm() { | |
353ffa53 TO |
116 | $config = CRM_Core_Config::singleton(); |
117 | $values = $config->localeCustomStrings[$config->lcMessages]; | |
6e557bb3 BS |
118 | |
119 | //CRM-14179 | |
120 | $instances = 0; | |
481a74f4 TO |
121 | foreach ($values as $valMatchType) { |
122 | foreach ($valMatchType as $valPairs) { | |
6e557bb3 BS |
123 | $instances += count($valPairs); |
124 | } | |
125 | } | |
126 | ||
6a488035 TO |
127 | if ($instances > 10) { |
128 | $this->_numStrings = $instances; | |
129 | } | |
130 | ||
131 | $soInstances = range(1, $this->_numStrings, 1); | |
132 | $stringOverrideInstances = array(); | |
133 | if ($this->_soInstance) { | |
134 | $soInstances = array($this->_soInstance); | |
135 | } | |
a7488080 | 136 | elseif (!empty($_POST['old'])) { |
6a488035 TO |
137 | $soInstances = $stringOverrideInstances = array_keys($_POST['old']); |
138 | } | |
139 | elseif (!empty($this->_defaults) && is_array($this->_defaults)) { | |
140 | $stringOverrideInstances = array_keys($this->_defaults['new']); | |
141 | if (count($this->_defaults['old']) > count($this->_defaults['new'])) { | |
142 | $stringOverrideInstances = array_keys($this->_defaults['old']); | |
143 | } | |
144 | } | |
145 | foreach ($soInstances as $instance) { | |
146 | $this->addElement('checkbox', "enabled[$instance]"); | |
147 | $this->add('textarea', "old[$instance]", NULL, array('rows' => 1, 'cols' => 40)); | |
148 | $this->add('textarea', "new[$instance]", NULL, array('rows' => 1, 'cols' => 40)); | |
149 | $this->addElement('checkbox', "cb[$instance]"); | |
150 | } | |
151 | $this->assign('numStrings', $this->_numStrings); | |
152 | if ($this->_soInstance) { | |
153 | return; | |
154 | } | |
155 | ||
156 | $this->assign('stringOverrideInstances', empty($stringOverrideInstances) ? FALSE : $stringOverrideInstances); | |
157 | ||
158 | $this->addButtons(array( | |
159 | array( | |
160 | 'type' => 'next', | |
161 | 'name' => ts('Save'), | |
162 | 'isDefault' => TRUE, | |
163 | ), | |
164 | array( | |
165 | 'type' => 'cancel', | |
166 | 'name' => ts('Cancel'), | |
167 | ), | |
168 | ) | |
169 | ); | |
170 | $this->addFormRule(array('CRM_Admin_Form_WordReplacements', 'formRule'), $this); | |
171 | } | |
172 | ||
173 | /** | |
eceb18cc | 174 | * Global validation rules for the form. |
6a488035 | 175 | * |
5173bd95 TO |
176 | * @param array $values |
177 | * Posted values of the form. | |
6a488035 | 178 | * |
a6c01b45 CW |
179 | * @return array |
180 | * list of errors to be posted back to the form | |
6a488035 | 181 | */ |
00be9182 | 182 | public static function formRule($values) { |
6a488035 TO |
183 | $errors = array(); |
184 | ||
353ffa53 TO |
185 | $oldValues = CRM_Utils_Array::value('old', $values); |
186 | $newValues = CRM_Utils_Array::value('new', $values); | |
187 | $enabled = CRM_Utils_Array::value('enabled', $values); | |
6a488035 TO |
188 | $exactMatch = CRM_Utils_Array::value('cb', $values); |
189 | ||
190 | foreach ($oldValues as $k => $v) { | |
191 | if ($v && !$newValues[$k]) { | |
192 | $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word'); | |
193 | } | |
194 | elseif (!$v && $newValues[$k]) { | |
195 | $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word'); | |
196 | } | |
8cc574cf CW |
197 | elseif ((empty($newValues[$k]) && empty($oldValues[$k])) |
198 | && (!empty($enabled[$k]) || !empty($exactMatch[$k])) | |
6a488035 TO |
199 | ) { |
200 | $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word'); | |
201 | $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word'); | |
202 | } | |
203 | } | |
204 | ||
205 | return $errors; | |
206 | } | |
207 | ||
208 | /** | |
eceb18cc | 209 | * Process the form submission. |
6a488035 TO |
210 | */ |
211 | public function postProcess() { | |
212 | $params = $this->controller->exportValues($this->_name); | |
608e6658 | 213 | $this->_numStrings = count($params['old']); |
6a488035 TO |
214 | |
215 | $enabled['exactMatch'] = $enabled['wildcardMatch'] = $disabled['exactMatch'] = $disabled['wildcardMatch'] = array(); | |
216 | for ($i = 1; $i <= $this->_numStrings; $i++) { | |
8cc574cf CW |
217 | if (!empty($params['new'][$i]) && !empty($params['old'][$i])) { |
218 | if (isset($params['enabled']) && !empty($params['enabled'][$i])) { | |
219 | if (!empty($params['cb']) && !empty($params['cb'][$i])) { | |
6a488035 TO |
220 | $enabled['exactMatch'] += array($params['old'][$i] => $params['new'][$i]); |
221 | } | |
222 | else { | |
223 | $enabled['wildcardMatch'] += array($params['old'][$i] => $params['new'][$i]); | |
224 | } | |
225 | } | |
226 | else { | |
227 | if (isset($params['cb']) && is_array($params['cb']) && array_key_exists($i, $params['cb'])) { | |
228 | $disabled['exactMatch'] += array($params['old'][$i] => $params['new'][$i]); | |
229 | } | |
230 | else { | |
231 | $disabled['wildcardMatch'] += array($params['old'][$i] => $params['new'][$i]); | |
232 | } | |
233 | } | |
234 | } | |
235 | } | |
236 | ||
237 | $overrides = array( | |
238 | 'enabled' => $enabled, | |
239 | 'disabled' => $disabled, | |
240 | ); | |
241 | ||
242 | $config = CRM_Core_Config::singleton(); | |
243 | ||
244 | $domain = new CRM_Core_DAO_Domain(); | |
245 | $domain->find(TRUE); | |
246 | ||
247 | if ($domain->locales && $config->localeCustomStrings) { | |
248 | // for multilingual | |
249 | $addReplacements = $config->localeCustomStrings; | |
250 | $addReplacements[$config->lcMessages] = $overrides; | |
251 | $stringOverride = serialize($addReplacements); | |
252 | } | |
253 | else { | |
254 | // for single language | |
255 | $stringOverride = serialize(array($config->lcMessages => $overrides)); | |
256 | } | |
257 | ||
258 | $params = array('locale_custom_strings' => $stringOverride); | |
259 | $id = CRM_Core_Config::domainID(); | |
260 | ||
261 | $wordReplacementSettings = CRM_Core_BAO_Domain::edit($params, $id); | |
262 | ||
263 | if ($wordReplacementSettings) { | |
0f65e834 TO |
264 | // This controller was originally written to CRUD $config->locale_custom_strings, |
265 | // but that's no longer the canonical store. Sync changes to canonical store. | |
266 | // This is inefficient - at some point, we should rewrite this UI. | |
267 | CRM_Core_BAO_WordReplacement::rebuildWordReplacementTable(); | |
6a488035 TO |
268 | |
269 | CRM_Core_Session::setStatus("", ts("Settings Saved"), "success"); | |
270 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/options/wordreplacements', | |
353ffa53 TO |
271 | "reset=1" |
272 | )); | |
6a488035 TO |
273 | } |
274 | } | |
1ca4ca9f | 275 | |
6a488035 | 276 | } |