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