phpcs - Fix error, "CONST keyword must be lowercase; expected const but found CONST"
[civicrm-core.git] / CRM / Admin / Form / WordReplacements.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Admin_Form_WordReplacements extends CRM_Core_Form {
36 protected $_numStrings = 10;
37
38 protected $_stringName = NULL;
39
40 protected $_defaults = NULL;
41
42 function preProcess() {
0f65e834
TO
43 // This controller was originally written to CRUD $config->locale_custom_strings,
44 // but that's no longer the canonical store. Re-sync from canonical store to ensure
45 // that we display that latest data. This is inefficient - at some point, we
46 // should rewrite this UI.
9762f6ff 47 CRM_Core_BAO_WordReplacement::rebuild(FALSE);
0f65e834 48
6a488035
TO
49 $this->_soInstance = CRM_Utils_Array::value('instance', $_GET);
50 $this->assign('soInstance', $this->_soInstance);
51 $breadCrumbUrl = CRM_Utils_System::url('civicrm/admin/options/wordreplacements',
52 "reset=1"
53 );
54 $breadCrumb = array(array('title' => ts('Word Replacements'),
55 'url' => $breadCrumbUrl,
56 ));
57 CRM_Utils_System::appendBreadCrumb($breadCrumb);
58 }
59
e0ef6999
EM
60 /**
61 * @return array
62 */
6a488035
TO
63 public function setDefaultValues() {
64 if ($this->_defaults !== NULL) {
65 return $this->_defaults;
66 }
67
68 $this->_defaults = array();
69
70 $config = CRM_Core_Config::singleton();
71
72 $values = $config->localeCustomStrings[$config->lcMessages];
73 $i = 1;
74
75 $enableDisable = array(
76 1 => 'enabled',
77 0 => 'disabled',
78 );
79
80 $cardMatch = array('wildcardMatch', 'exactMatch');
81
82 foreach ($enableDisable as $key => $val) {
83 foreach ($cardMatch as $kc => $vc) {
84 if (!empty($values[$val][$vc])) {
85 foreach ($values[$val][$vc] as $k => $v) {
86 $this->_defaults["enabled"][$i] = $key;
87 $this->_defaults["cb"][$i] = $kc;
88 $this->_defaults["old"][$i] = $k;
89 $this->_defaults["new"][$i] = $v;
90 $i++;
91 }
92 }
93 }
94 }
95
96 $name = $this->_stringName = "custom_string_override_{$config->lcMessages}";
97 if (isset($config->$name) &&
98 is_array($config->$name)
99 ) {
100 $this->_numStrings = 1;
101 foreach ($config->$name as $old => $newValues) {
102 $this->_numStrings++;
103 $this->_numStrings += 9;
104 }
105 }
106 else {
107 $this->_numStrings = 10;
108 }
109
110 return $this->_defaults;
111 }
112
113 /**
c490a46a 114 * Build the form object
6a488035 115 *
355ba699 116 * @return void
6a488035
TO
117 * @access public
118 */
119 public function buildQuickForm() {
120 $config = CRM_Core_Config::singleton();
121 $values = $config->localeCustomStrings[$config->lcMessages];
6e557bb3
BS
122
123 //CRM-14179
124 $instances = 0;
125 foreach ( $values as $valMatchType ) {
126 foreach ( $valMatchType as $valPairs ) {
127 $instances += count($valPairs);
128 }
129 }
130
6a488035
TO
131 if ($instances > 10) {
132 $this->_numStrings = $instances;
133 }
134
135 $soInstances = range(1, $this->_numStrings, 1);
136 $stringOverrideInstances = array();
137 if ($this->_soInstance) {
138 $soInstances = array($this->_soInstance);
139 }
a7488080 140 elseif (!empty($_POST['old'])) {
6a488035
TO
141 $soInstances = $stringOverrideInstances = array_keys($_POST['old']);
142 }
143 elseif (!empty($this->_defaults) && is_array($this->_defaults)) {
144 $stringOverrideInstances = array_keys($this->_defaults['new']);
145 if (count($this->_defaults['old']) > count($this->_defaults['new'])) {
146 $stringOverrideInstances = array_keys($this->_defaults['old']);
147 }
148 }
149 foreach ($soInstances as $instance) {
150 $this->addElement('checkbox', "enabled[$instance]");
151 $this->add('textarea', "old[$instance]", NULL, array('rows' => 1, 'cols' => 40));
152 $this->add('textarea', "new[$instance]", NULL, array('rows' => 1, 'cols' => 40));
153 $this->addElement('checkbox', "cb[$instance]");
154 }
155 $this->assign('numStrings', $this->_numStrings);
156 if ($this->_soInstance) {
157 return;
158 }
159
160 $this->assign('stringOverrideInstances', empty($stringOverrideInstances) ? FALSE : $stringOverrideInstances);
161
162 $this->addButtons(array(
163 array(
164 'type' => 'next',
165 'name' => ts('Save'),
166 'isDefault' => TRUE,
167 ),
168 array(
169 'type' => 'cancel',
170 'name' => ts('Cancel'),
171 ),
172 )
173 );
174 $this->addFormRule(array('CRM_Admin_Form_WordReplacements', 'formRule'), $this);
175 }
176
177 /**
100fef9d 178 * Global validation rules for the form
6a488035
TO
179 *
180 * @param array $values posted values of the form
181 *
182 * @return array list of errors to be posted back to the form
183 * @static
184 * @access public
185 */
186 static function formRule($values) {
187 $errors = array();
188
189 $oldValues = CRM_Utils_Array::value('old', $values);
190 $newValues = CRM_Utils_Array::value('new', $values);
191 $enabled = CRM_Utils_Array::value('enabled', $values);
192 $exactMatch = CRM_Utils_Array::value('cb', $values);
193
194 foreach ($oldValues as $k => $v) {
195 if ($v && !$newValues[$k]) {
196 $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word');
197 }
198 elseif (!$v && $newValues[$k]) {
199 $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word');
200 }
8cc574cf
CW
201 elseif ((empty($newValues[$k]) && empty($oldValues[$k]))
202 && (!empty($enabled[$k]) || !empty($exactMatch[$k]))
6a488035
TO
203 ) {
204 $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word');
205 $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word');
206 }
207 }
208
209 return $errors;
210 }
211
212 /**
c490a46a 213 * Process the form submission
6a488035
TO
214 *
215 * @access public
216 *
355ba699 217 * @return void
6a488035
TO
218 */
219 public function postProcess() {
220 $params = $this->controller->exportValues($this->_name);
221 $this->_numStrings = sizeof($params['old']);
222
223 $enabled['exactMatch'] = $enabled['wildcardMatch'] = $disabled['exactMatch'] = $disabled['wildcardMatch'] = array();
224 for ($i = 1; $i <= $this->_numStrings; $i++) {
8cc574cf
CW
225 if (!empty($params['new'][$i]) && !empty($params['old'][$i])) {
226 if (isset($params['enabled']) && !empty($params['enabled'][$i])) {
227 if (!empty($params['cb']) && !empty($params['cb'][$i])) {
6a488035
TO
228 $enabled['exactMatch'] += array($params['old'][$i] => $params['new'][$i]);
229 }
230 else {
231 $enabled['wildcardMatch'] += array($params['old'][$i] => $params['new'][$i]);
232 }
233 }
234 else {
235 if (isset($params['cb']) && is_array($params['cb']) && array_key_exists($i, $params['cb'])) {
236 $disabled['exactMatch'] += array($params['old'][$i] => $params['new'][$i]);
237 }
238 else {
239 $disabled['wildcardMatch'] += array($params['old'][$i] => $params['new'][$i]);
240 }
241 }
242 }
243 }
244
245 $overrides = array(
246 'enabled' => $enabled,
247 'disabled' => $disabled,
248 );
249
250 $config = CRM_Core_Config::singleton();
251
252 $domain = new CRM_Core_DAO_Domain();
253 $domain->find(TRUE);
254
255 if ($domain->locales && $config->localeCustomStrings) {
256 // for multilingual
257 $addReplacements = $config->localeCustomStrings;
258 $addReplacements[$config->lcMessages] = $overrides;
259 $stringOverride = serialize($addReplacements);
260 }
261 else {
262 // for single language
263 $stringOverride = serialize(array($config->lcMessages => $overrides));
264 }
265
266 $params = array('locale_custom_strings' => $stringOverride);
267 $id = CRM_Core_Config::domainID();
268
269 $wordReplacementSettings = CRM_Core_BAO_Domain::edit($params, $id);
270
271 if ($wordReplacementSettings) {
0f65e834
TO
272 // This controller was originally written to CRUD $config->locale_custom_strings,
273 // but that's no longer the canonical store. Sync changes to canonical store.
274 // This is inefficient - at some point, we should rewrite this UI.
275 CRM_Core_BAO_WordReplacement::rebuildWordReplacementTable();
6a488035
TO
276
277 CRM_Core_Session::setStatus("", ts("Settings Saved"), "success");
278 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/options/wordreplacements',
279 "reset=1"
280 ));
281 }
282 }
283}
284