Merge pull request #15808 from civicrm/5.20
[civicrm-core.git] / CRM / Admin / Form / WordReplacements.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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-2020
32 */
33 class CRM_Admin_Form_WordReplacements extends CRM_Core_Form {
34 protected $_numStrings = 10;
35
36 protected $_stringName = NULL;
37
38 public $unsavedChangesWarn = TRUE;
39
40 /**
41 * Pre process function.
42 */
43 public function preProcess() {
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.
48 CRM_Core_BAO_WordReplacement::rebuild(FALSE);
49
50 $this->_soInstance = CRM_Utils_Array::value('instance', $_GET);
51 $this->assign('soInstance', $this->_soInstance);
52 }
53
54 /**
55 * Set default values.
56 *
57 * @return array
58 */
59 public function setDefaultValues() {
60 if (!empty($this->_defaults)) {
61 return $this->_defaults;
62 }
63
64 $this->_defaults = [];
65
66 $config = CRM_Core_Config::singleton();
67
68 $values = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($config->lcMessages);
69 $i = 1;
70
71 $enableDisable = [
72 1 => 'enabled',
73 0 => 'disabled',
74 ];
75
76 $cardMatch = ['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 return $this->_defaults;
93 }
94
95 /**
96 * Build the form object.
97 */
98 public function buildQuickForm() {
99 $config = CRM_Core_Config::singleton();
100 $values = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($config->lcMessages);
101
102 //CRM-14179
103 $instances = 0;
104 foreach ($values as $valMatchType) {
105 foreach ($valMatchType as $valPairs) {
106 $instances += count($valPairs);
107 }
108 }
109
110 if ($instances > 10) {
111 $this->_numStrings = $instances;
112 }
113
114 $soInstances = range(1, $this->_numStrings, 1);
115 $stringOverrideInstances = [];
116 if ($this->_soInstance) {
117 $soInstances = [$this->_soInstance];
118 }
119 elseif (!empty($_POST['old'])) {
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('text', "old[$instance]", NULL);
131 $this->add('text', "new[$instance]", NULL);
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([
142 [
143 'type' => 'next',
144 'name' => ts('Save'),
145 'isDefault' => TRUE,
146 ],
147 [
148 'type' => 'cancel',
149 'name' => ts('Cancel'),
150 ],
151 ]);
152 $this->addFormRule(['CRM_Admin_Form_WordReplacements', 'formRule'], $this);
153 }
154
155 /**
156 * Global validation rules for the form.
157 *
158 * @param array $values
159 * Posted values of the form.
160 *
161 * @return array
162 * list of errors to be posted back to the form
163 */
164 public static function formRule($values) {
165 $errors = [];
166
167 $oldValues = CRM_Utils_Array::value('old', $values);
168 $newValues = CRM_Utils_Array::value('new', $values);
169 $enabled = CRM_Utils_Array::value('enabled', $values);
170 $exactMatch = CRM_Utils_Array::value('cb', $values);
171
172 foreach ($oldValues as $k => $v) {
173 if ($v && !$newValues[$k]) {
174 $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word');
175 }
176 elseif (!$v && $newValues[$k]) {
177 $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word');
178 }
179 elseif ((empty($newValues[$k]) && empty($oldValues[$k]))
180 && (!empty($enabled[$k]) || !empty($exactMatch[$k]))
181 ) {
182 $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word');
183 $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word');
184 }
185 }
186
187 return $errors;
188 }
189
190 /**
191 * Process the form submission.
192 */
193 public function postProcess() {
194 $params = $this->controller->exportValues($this->_name);
195 $this->_numStrings = count($params['old']);
196
197 $enabled['exactMatch'] = $enabled['wildcardMatch'] = $disabled['exactMatch'] = $disabled['wildcardMatch'] = [];
198 for ($i = 1; $i <= $this->_numStrings; $i++) {
199 if (!empty($params['new'][$i]) && !empty($params['old'][$i])) {
200 if (isset($params['enabled']) && !empty($params['enabled'][$i])) {
201 if (!empty($params['cb']) && !empty($params['cb'][$i])) {
202 $enabled['exactMatch'] += [$params['old'][$i] => $params['new'][$i]];
203 }
204 else {
205 $enabled['wildcardMatch'] += [$params['old'][$i] => $params['new'][$i]];
206 }
207 }
208 else {
209 if (isset($params['cb']) && is_array($params['cb']) && array_key_exists($i, $params['cb'])) {
210 $disabled['exactMatch'] += [$params['old'][$i] => $params['new'][$i]];
211 }
212 else {
213 $disabled['wildcardMatch'] += [$params['old'][$i] => $params['new'][$i]];
214 }
215 }
216 }
217 }
218
219 $overrides = [
220 'enabled' => $enabled,
221 'disabled' => $disabled,
222 ];
223
224 $config = CRM_Core_Config::singleton();
225 CRM_Core_BAO_WordReplacement::setLocaleCustomStrings($config->lcMessages, $overrides);
226
227 // This controller was originally written to CRUD $config->locale_custom_strings,
228 // but that's no longer the canonical store. Sync changes to canonical store
229 // (civicrm_word_replacement table in the database).
230 // This is inefficient - at some point, we should rewrite this UI.
231 CRM_Core_BAO_WordReplacement::rebuildWordReplacementTable();
232
233 CRM_Core_Session::setStatus("", ts("Settings Saved"), "success");
234 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/options/wordreplacements',
235 "reset=1"
236 ));
237 }
238
239 }