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