3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
35 class CRM_Admin_Form_WordReplacements
extends CRM_Core_Form
{
36 protected $_numStrings = 10;
38 protected $_stringName = NULL;
40 protected $_defaults = NULL;
42 function preProcess() {
43 $this->_soInstance
= CRM_Utils_Array
::value('instance', $_GET);
44 $this->assign('soInstance', $this->_soInstance
);
45 $breadCrumbUrl = CRM_Utils_System
::url('civicrm/admin/options/wordreplacements',
48 $breadCrumb = array(array('title' => ts('Word Replacements'),
49 'url' => $breadCrumbUrl,
51 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
54 public function setDefaultValues() {
55 if ($this->_defaults
!== NULL) {
56 return $this->_defaults
;
59 $this->_defaults
= array();
61 $config = CRM_Core_Config
::singleton();
63 $values = $config->localeCustomStrings
[$config->lcMessages
];
66 $enableDisable = array(
71 $cardMatch = array('wildcardMatch', 'exactMatch');
73 foreach ($enableDisable as $key => $val) {
74 foreach ($cardMatch as $kc => $vc) {
75 if (!empty($values[$val][$vc])) {
76 foreach ($values[$val][$vc] as $k => $v) {
77 $this->_defaults
["enabled"][$i] = $key;
78 $this->_defaults
["cb"][$i] = $kc;
79 $this->_defaults
["old"][$i] = $k;
80 $this->_defaults
["new"][$i] = $v;
87 $name = $this->_stringName
= "custom_string_override_{$config->lcMessages}";
88 if (isset($config->$name) &&
89 is_array($config->$name)
91 $this->_numStrings
= 1;
92 foreach ($config->$name as $old => $newValues) {
94 $this->_numStrings +
= 9;
98 $this->_numStrings
= 10;
101 return $this->_defaults
;
105 * Function to actually build the form
110 public function buildQuickForm() {
111 $config = CRM_Core_Config
::singleton();
112 $values = $config->localeCustomStrings
[$config->lcMessages
];
113 $instances = (count($values, COUNT_RECURSIVE
) - 6);
114 if ($instances > 10) {
115 $this->_numStrings
= $instances;
118 $soInstances = range(1, $this->_numStrings
, 1);
119 $stringOverrideInstances = array();
120 if ($this->_soInstance
) {
121 $soInstances = array($this->_soInstance
);
123 elseif (CRM_Utils_Array
::value('old', $_POST)) {
124 $soInstances = $stringOverrideInstances = array_keys($_POST['old']);
126 elseif (!empty($this->_defaults
) && is_array($this->_defaults
)) {
127 $stringOverrideInstances = array_keys($this->_defaults
['new']);
128 if (count($this->_defaults
['old']) > count($this->_defaults
['new'])) {
129 $stringOverrideInstances = array_keys($this->_defaults
['old']);
132 foreach ($soInstances as $instance) {
133 $this->addElement('checkbox', "enabled[$instance]");
134 $this->add('textarea', "old[$instance]", NULL, array('rows' => 1, 'cols' => 40));
135 $this->add('textarea', "new[$instance]", NULL, array('rows' => 1, 'cols' => 40));
136 $this->addElement('checkbox', "cb[$instance]");
138 $this->assign('numStrings', $this->_numStrings
);
139 if ($this->_soInstance
) {
143 $this->assign('stringOverrideInstances', empty($stringOverrideInstances) ?
FALSE : $stringOverrideInstances);
145 $this->addButtons(array(
148 'name' => ts('Save'),
153 'name' => ts('Cancel'),
157 $this->addFormRule(array('CRM_Admin_Form_WordReplacements', 'formRule'), $this);
161 * global validation rules for the form
163 * @param array $values posted values of the form
165 * @return array list of errors to be posted back to the form
169 static function formRule($values) {
172 $oldValues = CRM_Utils_Array
::value('old', $values);
173 $newValues = CRM_Utils_Array
::value('new', $values);
174 $enabled = CRM_Utils_Array
::value('enabled', $values);
175 $exactMatch = CRM_Utils_Array
::value('cb', $values);
177 foreach ($oldValues as $k => $v) {
178 if ($v && !$newValues[$k]) {
179 $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word');
181 elseif (!$v && $newValues[$k]) {
182 $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word');
184 elseif ((!CRM_Utils_Array
::value($k, $newValues) && !CRM_Utils_Array
::value($k, $oldValues))
185 && (CRM_Utils_Array
::value($k, $enabled) || CRM_Utils_Array
::value($k, $exactMatch))
187 $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word');
188 $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word');
196 * Function to process the form
202 public function postProcess() {
203 $params = $this->controller
->exportValues($this->_name
);
204 $this->_numStrings
= sizeof($params['old']);
206 $enabled['exactMatch'] = $enabled['wildcardMatch'] = $disabled['exactMatch'] = $disabled['wildcardMatch'] = array();
207 for ($i = 1; $i <= $this->_numStrings
; $i++
) {
208 if (CRM_Utils_Array
::value($i, $params['new']) &&
209 CRM_Utils_Array
::value($i, $params['old'])
211 if (isset($params['enabled']) && CRM_Utils_Array
::value($i, $params['enabled'])) {
212 if (CRM_Utils_Array
::value('cb', $params) &&
213 CRM_Utils_Array
::value($i, $params['cb'])
215 $enabled['exactMatch'] +
= array($params['old'][$i] => $params['new'][$i]);
218 $enabled['wildcardMatch'] +
= array($params['old'][$i] => $params['new'][$i]);
222 if (isset($params['cb']) && is_array($params['cb']) && array_key_exists($i, $params['cb'])) {
223 $disabled['exactMatch'] +
= array($params['old'][$i] => $params['new'][$i]);
226 $disabled['wildcardMatch'] +
= array($params['old'][$i] => $params['new'][$i]);
233 'enabled' => $enabled,
234 'disabled' => $disabled,
237 $config = CRM_Core_Config
::singleton();
239 $domain = new CRM_Core_DAO_Domain();
242 if ($domain->locales
&& $config->localeCustomStrings
) {
244 $addReplacements = $config->localeCustomStrings
;
245 $addReplacements[$config->lcMessages
] = $overrides;
246 $stringOverride = serialize($addReplacements);
249 // for single language
250 $stringOverride = serialize(array($config->lcMessages
=> $overrides));
253 $params = array('locale_custom_strings' => $stringOverride);
254 $id = CRM_Core_Config
::domainID();
256 $wordReplacementSettings = CRM_Core_BAO_Domain
::edit($params, $id);
258 if ($wordReplacementSettings) {
260 CRM_Core_BAO_Navigation
::resetNavigation();
261 // Clear js string cache
262 CRM_Core_Resources
::singleton()->flushStrings();
264 CRM_Core_Session
::setStatus("", ts("Settings Saved"), "success");
265 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/options/wordreplacements',