3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Admin_Form_WordReplacements
extends CRM_Core_Form
{
18 protected $_numStrings = 10;
20 protected $_stringName = NULL;
22 public $unsavedChangesWarn = TRUE;
25 * Pre process function.
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);
34 $this->_soInstance
= $_GET['instance'] ??
NULL;
35 $this->assign('soInstance', $this->_soInstance
);
43 public function setDefaultValues() {
44 if (!empty($this->_defaults
)) {
45 return $this->_defaults
;
48 $this->_defaults
= [];
50 $config = CRM_Core_Config
::singleton();
52 $values = CRM_Core_BAO_WordReplacement
::getLocaleCustomStrings($config->lcMessages
);
60 $cardMatch = ['wildcardMatch', 'exactMatch'];
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;
76 return $this->_defaults
;
80 * Build the form object.
82 public function buildQuickForm() {
83 $config = CRM_Core_Config
::singleton();
84 $values = CRM_Core_BAO_WordReplacement
::getLocaleCustomStrings($config->lcMessages
);
88 foreach ($values as $valMatchType) {
89 foreach ($valMatchType as $valPairs) {
90 $instances +
= count($valPairs);
94 if ($instances > 10) {
95 $this->_numStrings
= $instances;
98 $soInstances = range(1, $this->_numStrings
, 1);
99 $stringOverrideInstances = [];
100 if ($this->_soInstance
) {
101 $soInstances = [$this->_soInstance
];
103 elseif (!empty($_POST['old'])) {
104 $soInstances = $stringOverrideInstances = array_keys($_POST['old']);
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']);
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]");
118 $this->assign('numStrings', $this->_numStrings
);
119 if ($this->_soInstance
) {
123 $this->assign('stringOverrideInstances', empty($stringOverrideInstances) ?
FALSE : $stringOverrideInstances);
128 'name' => ts('Save'),
133 'name' => ts('Cancel'),
136 $this->addFormRule(['CRM_Admin_Form_WordReplacements', 'formRule'], $this);
140 * Global validation rules for the form.
142 * @param array $values
143 * Posted values of the form.
146 * list of errors to be posted back to the form
148 public static function formRule($values) {
151 $oldValues = $values['old'] ??
NULL;
152 $newValues = $values['new'] ??
NULL;
153 $enabled = $values['enabled'] ??
NULL;
154 $exactMatch = $values['cb'] ??
NULL;
156 foreach ($oldValues as $k => $v) {
157 if ($v && !$newValues[$k]) {
158 $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word');
160 elseif (!$v && $newValues[$k]) {
161 $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word');
163 elseif ((empty($newValues[$k]) && empty($oldValues[$k]))
164 && (!empty($enabled[$k]) ||
!empty($exactMatch[$k]))
166 $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word');
167 $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word');
175 * Process the form submission.
177 public function postProcess() {
178 $params = $this->controller
->exportValues($this->_name
);
179 $this->_numStrings
= count($params['old']);
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]];
189 $enabled['wildcardMatch'] +
= [$params['old'][$i] => $params['new'][$i]];
193 if (isset($params['cb']) && is_array($params['cb']) && array_key_exists($i, $params['cb'])) {
194 $disabled['exactMatch'] +
= [$params['old'][$i] => $params['new'][$i]];
197 $disabled['wildcardMatch'] +
= [$params['old'][$i] => $params['new'][$i]];
204 'enabled' => $enabled,
205 'disabled' => $disabled,
208 $config = CRM_Core_Config
::singleton();
209 CRM_Core_BAO_WordReplacement
::setLocaleCustomStrings($config->lcMessages
, $overrides);
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();
217 CRM_Core_Session
::setStatus("", ts("Settings Saved"), "success");
218 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/options/wordreplacements',