Merge pull request #22484 from civicrm/5.46
[civicrm-core.git] / CRM / Admin / Form / WordReplacements.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Admin_Form_WordReplacements extends CRM_Core_Form {
18 protected $_numStrings = 10;
19
20 protected $_stringName = NULL;
21
22 public $unsavedChangesWarn = TRUE;
23
24 /**
25 * Pre process function.
26 */
27 public function preProcess() {
28 // This controller was originally written to CRUD $config->locale_custom_strings,
29 // but that's no longer the canonical store. Re-sync from canonical store to ensure
30 // that we display that latest data. This is inefficient - at some point, we
31 // should rewrite this UI.
32 CRM_Core_BAO_WordReplacement::rebuild(FALSE);
33
34 $this->_soInstance = $_GET['instance'] ?? NULL;
35 $this->assign('soInstance', $this->_soInstance);
36 }
37
38 /**
39 * Set default values.
40 *
41 * @return array
42 */
43 public function setDefaultValues() {
44 if (!empty($this->_defaults)) {
45 return $this->_defaults;
46 }
47
48 $this->_defaults = [];
49
50 $config = CRM_Core_Config::singleton();
51
52 $values = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($config->lcMessages);
53 $i = 1;
54
55 $enableDisable = [
56 1 => 'enabled',
57 0 => 'disabled',
58 ];
59
60 $cardMatch = ['wildcardMatch', 'exactMatch'];
61
62 foreach ($enableDisable as $key => $val) {
63 foreach ($cardMatch as $kc => $vc) {
64 if (!empty($values[$val][$vc])) {
65 foreach ($values[$val][$vc] as $k => $v) {
66 $this->_defaults["enabled"][$i] = $key;
67 $this->_defaults["cb"][$i] = $kc;
68 $this->_defaults["old"][$i] = $k;
69 $this->_defaults["new"][$i] = $v;
70 $i++;
71 }
72 }
73 }
74 }
75
76 return $this->_defaults;
77 }
78
79 /**
80 * Build the form object.
81 */
82 public function buildQuickForm() {
83 $config = CRM_Core_Config::singleton();
84 $values = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($config->lcMessages);
85
86 //CRM-14179
87 $instances = 0;
88 foreach ($values as $valMatchType) {
89 foreach ($valMatchType as $valPairs) {
90 $instances += count($valPairs);
91 }
92 }
93
94 if ($instances > 10) {
95 $this->_numStrings = $instances;
96 }
97
98 $soInstances = range(1, $this->_numStrings, 1);
99 $stringOverrideInstances = [];
100 if ($this->_soInstance) {
101 $soInstances = [$this->_soInstance];
102 }
103 elseif (!empty($_POST['old'])) {
104 $soInstances = $stringOverrideInstances = array_keys($_POST['old']);
105 }
106 elseif (!empty($this->_defaults) && is_array($this->_defaults)) {
107 $stringOverrideInstances = array_keys($this->_defaults['new']);
108 if (count($this->_defaults['old']) > count($this->_defaults['new'])) {
109 $stringOverrideInstances = array_keys($this->_defaults['old']);
110 }
111 }
112 foreach ($soInstances as $instance) {
113 $this->addElement('checkbox', "enabled[$instance]");
114 $this->add('text', "old[$instance]", NULL);
115 $this->add('text', "new[$instance]", NULL);
116 $this->addElement('checkbox', "cb[$instance]");
117 }
118 $this->assign('numStrings', $this->_numStrings);
119 if ($this->_soInstance) {
120 return;
121 }
122
123 $this->assign('stringOverrideInstances', empty($stringOverrideInstances) ? FALSE : $stringOverrideInstances);
124
125 $this->addButtons([
126 [
127 'type' => 'next',
128 'name' => ts('Save'),
129 'isDefault' => TRUE,
130 ],
131 [
132 'type' => 'cancel',
133 'name' => ts('Cancel'),
134 ],
135 ]);
136 $this->addFormRule(['CRM_Admin_Form_WordReplacements', 'formRule'], $this);
137 }
138
139 /**
140 * Global validation rules for the form.
141 *
142 * @param array $values
143 * Posted values of the form.
144 *
145 * @return array
146 * list of errors to be posted back to the form
147 */
148 public static function formRule($values) {
149 $errors = [];
150
151 $oldValues = $values['old'] ?? NULL;
152 $newValues = $values['new'] ?? NULL;
153 $enabled = $values['enabled'] ?? NULL;
154 $exactMatch = $values['cb'] ?? NULL;
155
156 foreach ($oldValues as $k => $v) {
157 if ($v && !$newValues[$k]) {
158 $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word');
159 }
160 elseif (!$v && $newValues[$k]) {
161 $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word');
162 }
163 elseif ((empty($newValues[$k]) && empty($oldValues[$k]))
164 && (!empty($enabled[$k]) || !empty($exactMatch[$k]))
165 ) {
166 $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word');
167 $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word');
168 }
169 }
170
171 return $errors;
172 }
173
174 /**
175 * Process the form submission.
176 */
177 public function postProcess() {
178 $params = $this->controller->exportValues($this->_name);
179 $this->_numStrings = count($params['old']);
180
181 $enabled['exactMatch'] = $enabled['wildcardMatch'] = $disabled['exactMatch'] = $disabled['wildcardMatch'] = [];
182 for ($i = 1; $i <= $this->_numStrings; $i++) {
183 if (!empty($params['new'][$i]) && !empty($params['old'][$i])) {
184 if (isset($params['enabled']) && !empty($params['enabled'][$i])) {
185 if (!empty($params['cb']) && !empty($params['cb'][$i])) {
186 $enabled['exactMatch'] += [$params['old'][$i] => $params['new'][$i]];
187 }
188 else {
189 $enabled['wildcardMatch'] += [$params['old'][$i] => $params['new'][$i]];
190 }
191 }
192 else {
193 if (isset($params['cb']) && is_array($params['cb']) && array_key_exists($i, $params['cb'])) {
194 $disabled['exactMatch'] += [$params['old'][$i] => $params['new'][$i]];
195 }
196 else {
197 $disabled['wildcardMatch'] += [$params['old'][$i] => $params['new'][$i]];
198 }
199 }
200 }
201 }
202
203 $overrides = [
204 'enabled' => $enabled,
205 'disabled' => $disabled,
206 ];
207
208 $config = CRM_Core_Config::singleton();
209 CRM_Core_BAO_WordReplacement::setLocaleCustomStrings($config->lcMessages, $overrides);
210
211 // This controller was originally written to CRUD $config->locale_custom_strings,
212 // but that's no longer the canonical store. Sync changes to canonical store
213 // (civicrm_word_replacement table in the database).
214 // This is inefficient - at some point, we should rewrite this UI.
215 CRM_Core_BAO_WordReplacement::rebuildWordReplacementTable();
216
217 CRM_Core_Session::setStatus("", ts("Settings Saved"), "success");
218 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/options/wordreplacements',
219 "reset=1"
220 ));
221 }
222
223 }