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