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