Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
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 | 59 | public function setDefaultValues() { |
234d8f09 | 60 | if (!empty($this->_defaults)) { |
6a488035 TO |
61 | return $this->_defaults; |
62 | } | |
63 | ||
64 | $this->_defaults = array(); | |
65 | ||
66 | $config = CRM_Core_Config::singleton(); | |
67 | ||
234d8f09 | 68 | $values = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($config->lcMessages); |
6a488035 TO |
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 | ||
6a488035 TO |
92 | return $this->_defaults; |
93 | } | |
94 | ||
95 | /** | |
eceb18cc | 96 | * Build the form object. |
6a488035 TO |
97 | */ |
98 | public function buildQuickForm() { | |
353ffa53 | 99 | $config = CRM_Core_Config::singleton(); |
234d8f09 | 100 | $values = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($config->lcMessages); |
6e557bb3 BS |
101 | |
102 | //CRM-14179 | |
103 | $instances = 0; | |
481a74f4 TO |
104 | foreach ($values as $valMatchType) { |
105 | foreach ($valMatchType as $valPairs) { | |
6e557bb3 BS |
106 | $instances += count($valPairs); |
107 | } | |
108 | } | |
109 | ||
6a488035 TO |
110 | if ($instances > 10) { |
111 | $this->_numStrings = $instances; | |
112 | } | |
113 | ||
114 | $soInstances = range(1, $this->_numStrings, 1); | |
115 | $stringOverrideInstances = array(); | |
116 | if ($this->_soInstance) { | |
117 | $soInstances = array($this->_soInstance); | |
118 | } | |
a7488080 | 119 | elseif (!empty($_POST['old'])) { |
6a488035 TO |
120 | $soInstances = $stringOverrideInstances = array_keys($_POST['old']); |
121 | } | |
122 | elseif (!empty($this->_defaults) && is_array($this->_defaults)) { | |
123 | $stringOverrideInstances = array_keys($this->_defaults['new']); | |
124 | if (count($this->_defaults['old']) > count($this->_defaults['new'])) { | |
125 | $stringOverrideInstances = array_keys($this->_defaults['old']); | |
126 | } | |
127 | } | |
128 | foreach ($soInstances as $instance) { | |
129 | $this->addElement('checkbox', "enabled[$instance]"); | |
130 | $this->add('textarea', "old[$instance]", NULL, array('rows' => 1, 'cols' => 40)); | |
131 | $this->add('textarea', "new[$instance]", NULL, array('rows' => 1, 'cols' => 40)); | |
132 | $this->addElement('checkbox', "cb[$instance]"); | |
133 | } | |
134 | $this->assign('numStrings', $this->_numStrings); | |
135 | if ($this->_soInstance) { | |
136 | return; | |
137 | } | |
138 | ||
139 | $this->assign('stringOverrideInstances', empty($stringOverrideInstances) ? FALSE : $stringOverrideInstances); | |
140 | ||
141 | $this->addButtons(array( | |
142 | array( | |
143 | 'type' => 'next', | |
144 | 'name' => ts('Save'), | |
145 | 'isDefault' => TRUE, | |
146 | ), | |
147 | array( | |
148 | 'type' => 'cancel', | |
149 | 'name' => ts('Cancel'), | |
150 | ), | |
151 | ) | |
152 | ); | |
153 | $this->addFormRule(array('CRM_Admin_Form_WordReplacements', 'formRule'), $this); | |
154 | } | |
155 | ||
156 | /** | |
eceb18cc | 157 | * Global validation rules for the form. |
6a488035 | 158 | * |
5173bd95 TO |
159 | * @param array $values |
160 | * Posted values of the form. | |
6a488035 | 161 | * |
a6c01b45 CW |
162 | * @return array |
163 | * list of errors to be posted back to the form | |
6a488035 | 164 | */ |
00be9182 | 165 | public static function formRule($values) { |
6a488035 TO |
166 | $errors = array(); |
167 | ||
353ffa53 TO |
168 | $oldValues = CRM_Utils_Array::value('old', $values); |
169 | $newValues = CRM_Utils_Array::value('new', $values); | |
170 | $enabled = CRM_Utils_Array::value('enabled', $values); | |
6a488035 TO |
171 | $exactMatch = CRM_Utils_Array::value('cb', $values); |
172 | ||
173 | foreach ($oldValues as $k => $v) { | |
174 | if ($v && !$newValues[$k]) { | |
175 | $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word'); | |
176 | } | |
177 | elseif (!$v && $newValues[$k]) { | |
178 | $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word'); | |
179 | } | |
8cc574cf CW |
180 | elseif ((empty($newValues[$k]) && empty($oldValues[$k])) |
181 | && (!empty($enabled[$k]) || !empty($exactMatch[$k])) | |
6a488035 TO |
182 | ) { |
183 | $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word'); | |
184 | $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word'); | |
185 | } | |
186 | } | |
187 | ||
188 | return $errors; | |
189 | } | |
190 | ||
191 | /** | |
eceb18cc | 192 | * Process the form submission. |
6a488035 TO |
193 | */ |
194 | public function postProcess() { | |
195 | $params = $this->controller->exportValues($this->_name); | |
608e6658 | 196 | $this->_numStrings = count($params['old']); |
6a488035 TO |
197 | |
198 | $enabled['exactMatch'] = $enabled['wildcardMatch'] = $disabled['exactMatch'] = $disabled['wildcardMatch'] = array(); | |
199 | for ($i = 1; $i <= $this->_numStrings; $i++) { | |
8cc574cf CW |
200 | if (!empty($params['new'][$i]) && !empty($params['old'][$i])) { |
201 | if (isset($params['enabled']) && !empty($params['enabled'][$i])) { | |
202 | if (!empty($params['cb']) && !empty($params['cb'][$i])) { | |
6a488035 TO |
203 | $enabled['exactMatch'] += array($params['old'][$i] => $params['new'][$i]); |
204 | } | |
205 | else { | |
206 | $enabled['wildcardMatch'] += array($params['old'][$i] => $params['new'][$i]); | |
207 | } | |
208 | } | |
209 | else { | |
210 | if (isset($params['cb']) && is_array($params['cb']) && array_key_exists($i, $params['cb'])) { | |
211 | $disabled['exactMatch'] += array($params['old'][$i] => $params['new'][$i]); | |
212 | } | |
213 | else { | |
214 | $disabled['wildcardMatch'] += array($params['old'][$i] => $params['new'][$i]); | |
215 | } | |
216 | } | |
217 | } | |
218 | } | |
219 | ||
220 | $overrides = array( | |
221 | 'enabled' => $enabled, | |
222 | 'disabled' => $disabled, | |
223 | ); | |
224 | ||
225 | $config = CRM_Core_Config::singleton(); | |
234d8f09 | 226 | CRM_Core_BAO_WordReplacement::setLocaleCustomStrings($config->lcMessages, $overrides); |
6a488035 | 227 | |
234d8f09 | 228 | // This controller was originally written to CRUD $config->locale_custom_strings, |
b37b9bef JV |
229 | // but that's no longer the canonical store. Sync changes to canonical store |
230 | // (civicrm_word_replacement table in the database). | |
234d8f09 TO |
231 | // This is inefficient - at some point, we should rewrite this UI. |
232 | CRM_Core_BAO_WordReplacement::rebuildWordReplacementTable(); | |
233 | ||
234 | CRM_Core_Session::setStatus("", ts("Settings Saved"), "success"); | |
235 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/options/wordreplacements', | |
236 | "reset=1" | |
237 | )); | |
6a488035 | 238 | } |
1ca4ca9f | 239 | |
6a488035 | 240 | } |