Merge pull request #23508 from eileenmcnaughton/import_subkid
[civicrm-core.git] / CRM / Admin / Form / WordReplacements.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
66f9e52b 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Admin_Form_WordReplacements extends CRM_Core_Form {
18 protected $_numStrings = 10;
19
20 protected $_stringName = NULL;
21
d24c529a
CW
22 public $unsavedChangesWarn = TRUE;
23
1ca4ca9f
EM
24 /**
25 * Pre process function.
26 */
00be9182 27 public function preProcess() {
0f65e834
TO
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.
9762f6ff 32 CRM_Core_BAO_WordReplacement::rebuild(FALSE);
0f65e834 33
9c1bc317 34 $this->_soInstance = $_GET['instance'] ?? NULL;
6a488035 35 $this->assign('soInstance', $this->_soInstance);
6a488035
TO
36 }
37
e0ef6999 38 /**
1ca4ca9f
EM
39 * Set default values.
40 *
e0ef6999
EM
41 * @return array
42 */
6a488035 43 public function setDefaultValues() {
234d8f09 44 if (!empty($this->_defaults)) {
6a488035
TO
45 return $this->_defaults;
46 }
47
be2fb01f 48 $this->_defaults = [];
6a488035
TO
49
50 $config = CRM_Core_Config::singleton();
51
234d8f09 52 $values = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($config->lcMessages);
6a488035
TO
53 $i = 1;
54
be2fb01f 55 $enableDisable = [
6a488035
TO
56 1 => 'enabled',
57 0 => 'disabled',
be2fb01f 58 ];
6a488035 59
be2fb01f 60 $cardMatch = ['wildcardMatch', 'exactMatch'];
6a488035
TO
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
6a488035
TO
76 return $this->_defaults;
77 }
78
79 /**
eceb18cc 80 * Build the form object.
6a488035
TO
81 */
82 public function buildQuickForm() {
353ffa53 83 $config = CRM_Core_Config::singleton();
234d8f09 84 $values = CRM_Core_BAO_WordReplacement::getLocaleCustomStrings($config->lcMessages);
6e557bb3
BS
85
86 //CRM-14179
87 $instances = 0;
481a74f4
TO
88 foreach ($values as $valMatchType) {
89 foreach ($valMatchType as $valPairs) {
6e557bb3
BS
90 $instances += count($valPairs);
91 }
92 }
93
6a488035
TO
94 if ($instances > 10) {
95 $this->_numStrings = $instances;
96 }
97
98 $soInstances = range(1, $this->_numStrings, 1);
be2fb01f 99 $stringOverrideInstances = [];
6a488035 100 if ($this->_soInstance) {
be2fb01f 101 $soInstances = [$this->_soInstance];
6a488035 102 }
a7488080 103 elseif (!empty($_POST['old'])) {
6a488035
TO
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]");
69e1be3a
SL
114 $this->add('text', "old[$instance]", NULL);
115 $this->add('text', "new[$instance]", NULL);
6a488035
TO
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
be2fb01f 125 $this->addButtons([
0d48f1cc
TO
126 [
127 'type' => 'next',
128 'name' => ts('Save'),
129 'isDefault' => TRUE,
130 ],
131 [
132 'type' => 'cancel',
133 'name' => ts('Cancel'),
134 ],
135 ]);
be2fb01f 136 $this->addFormRule(['CRM_Admin_Form_WordReplacements', 'formRule'], $this);
6a488035
TO
137 }
138
139 /**
eceb18cc 140 * Global validation rules for the form.
6a488035 141 *
5173bd95
TO
142 * @param array $values
143 * Posted values of the form.
6a488035 144 *
a6c01b45
CW
145 * @return array
146 * list of errors to be posted back to the form
6a488035 147 */
00be9182 148 public static function formRule($values) {
be2fb01f 149 $errors = [];
6a488035 150
9c1bc317
CW
151 $oldValues = $values['old'] ?? NULL;
152 $newValues = $values['new'] ?? NULL;
153 $enabled = $values['enabled'] ?? NULL;
154 $exactMatch = $values['cb'] ?? NULL;
6a488035
TO
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 }
8cc574cf
CW
163 elseif ((empty($newValues[$k]) && empty($oldValues[$k]))
164 && (!empty($enabled[$k]) || !empty($exactMatch[$k]))
6a488035
TO
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 /**
eceb18cc 175 * Process the form submission.
6a488035
TO
176 */
177 public function postProcess() {
178 $params = $this->controller->exportValues($this->_name);
608e6658 179 $this->_numStrings = count($params['old']);
6a488035 180
be2fb01f 181 $enabled['exactMatch'] = $enabled['wildcardMatch'] = $disabled['exactMatch'] = $disabled['wildcardMatch'] = [];
6a488035 182 for ($i = 1; $i <= $this->_numStrings; $i++) {
8cc574cf
CW
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])) {
be2fb01f 186 $enabled['exactMatch'] += [$params['old'][$i] => $params['new'][$i]];
6a488035
TO
187 }
188 else {
be2fb01f 189 $enabled['wildcardMatch'] += [$params['old'][$i] => $params['new'][$i]];
6a488035
TO
190 }
191 }
192 else {
193 if (isset($params['cb']) && is_array($params['cb']) && array_key_exists($i, $params['cb'])) {
be2fb01f 194 $disabled['exactMatch'] += [$params['old'][$i] => $params['new'][$i]];
6a488035
TO
195 }
196 else {
be2fb01f 197 $disabled['wildcardMatch'] += [$params['old'][$i] => $params['new'][$i]];
6a488035
TO
198 }
199 }
200 }
201 }
202
be2fb01f 203 $overrides = [
6a488035
TO
204 'enabled' => $enabled,
205 'disabled' => $disabled,
be2fb01f 206 ];
6a488035
TO
207
208 $config = CRM_Core_Config::singleton();
234d8f09 209 CRM_Core_BAO_WordReplacement::setLocaleCustomStrings($config->lcMessages, $overrides);
6a488035 210
234d8f09 211 // This controller was originally written to CRUD $config->locale_custom_strings,
b37b9bef
JV
212 // but that's no longer the canonical store. Sync changes to canonical store
213 // (civicrm_word_replacement table in the database).
234d8f09
TO
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 ));
6a488035 221 }
1ca4ca9f 222
6a488035 223}