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