3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
34 class CRM_Admin_Form_WordReplacements
extends CRM_Core_Form
{
35 protected $_numStrings = 10;
37 protected $_stringName = NULL;
39 public $unsavedChangesWarn = TRUE;
42 * Pre process function.
44 public function preProcess() {
45 // This controller was originally written to CRUD $config->locale_custom_strings,
46 // but that's no longer the canonical store. Re-sync from canonical store to ensure
47 // that we display that latest data. This is inefficient - at some point, we
48 // should rewrite this UI.
49 CRM_Core_BAO_WordReplacement
::rebuild(FALSE);
51 $this->_soInstance
= CRM_Utils_Array
::value('instance', $_GET);
52 $this->assign('soInstance', $this->_soInstance
);
60 public function setDefaultValues() {
61 if ($this->_defaults
!== NULL) {
62 return $this->_defaults
;
65 $this->_defaults
= array();
67 $config = CRM_Core_Config
::singleton();
69 $values = $config->localeCustomStrings
[$config->lcMessages
];
72 $enableDisable = array(
77 $cardMatch = array('wildcardMatch', 'exactMatch');
79 foreach ($enableDisable as $key => $val) {
80 foreach ($cardMatch as $kc => $vc) {
81 if (!empty($values[$val][$vc])) {
82 foreach ($values[$val][$vc] as $k => $v) {
83 $this->_defaults
["enabled"][$i] = $key;
84 $this->_defaults
["cb"][$i] = $kc;
85 $this->_defaults
["old"][$i] = $k;
86 $this->_defaults
["new"][$i] = $v;
93 $name = $this->_stringName
= "custom_string_override_{$config->lcMessages}";
94 if (isset($config->$name) &&
95 is_array($config->$name)
97 $this->_numStrings
= 1;
98 foreach ($config->$name as $old => $newValues) {
100 $this->_numStrings +
= 9;
104 $this->_numStrings
= 10;
107 return $this->_defaults
;
111 * Build the form object.
113 public function buildQuickForm() {
114 $config = CRM_Core_Config
::singleton();
115 $values = $config->localeCustomStrings
[$config->lcMessages
];
119 foreach ($values as $valMatchType) {
120 foreach ($valMatchType as $valPairs) {
121 $instances +
= count($valPairs);
125 if ($instances > 10) {
126 $this->_numStrings
= $instances;
129 $soInstances = range(1, $this->_numStrings
, 1);
130 $stringOverrideInstances = array();
131 if ($this->_soInstance
) {
132 $soInstances = array($this->_soInstance
);
134 elseif (!empty($_POST['old'])) {
135 $soInstances = $stringOverrideInstances = array_keys($_POST['old']);
137 elseif (!empty($this->_defaults
) && is_array($this->_defaults
)) {
138 $stringOverrideInstances = array_keys($this->_defaults
['new']);
139 if (count($this->_defaults
['old']) > count($this->_defaults
['new'])) {
140 $stringOverrideInstances = array_keys($this->_defaults
['old']);
143 foreach ($soInstances as $instance) {
144 $this->addElement('checkbox', "enabled[$instance]");
145 $this->add('textarea', "old[$instance]", NULL, array('rows' => 1, 'cols' => 40));
146 $this->add('textarea', "new[$instance]", NULL, array('rows' => 1, 'cols' => 40));
147 $this->addElement('checkbox', "cb[$instance]");
149 $this->assign('numStrings', $this->_numStrings
);
150 if ($this->_soInstance
) {
154 $this->assign('stringOverrideInstances', empty($stringOverrideInstances) ?
FALSE : $stringOverrideInstances);
156 $this->addButtons(array(
159 'name' => ts('Save'),
164 'name' => ts('Cancel'),
168 $this->addFormRule(array('CRM_Admin_Form_WordReplacements', 'formRule'), $this);
172 * Global validation rules for the form.
174 * @param array $values
175 * Posted values of the form.
178 * list of errors to be posted back to the form
180 public static function formRule($values) {
183 $oldValues = CRM_Utils_Array
::value('old', $values);
184 $newValues = CRM_Utils_Array
::value('new', $values);
185 $enabled = CRM_Utils_Array
::value('enabled', $values);
186 $exactMatch = CRM_Utils_Array
::value('cb', $values);
188 foreach ($oldValues as $k => $v) {
189 if ($v && !$newValues[$k]) {
190 $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word');
192 elseif (!$v && $newValues[$k]) {
193 $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word');
195 elseif ((empty($newValues[$k]) && empty($oldValues[$k]))
196 && (!empty($enabled[$k]) ||
!empty($exactMatch[$k]))
198 $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word');
199 $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word');
207 * Process the form submission.
209 public function postProcess() {
210 $params = $this->controller
->exportValues($this->_name
);
211 $this->_numStrings
= count($params['old']);
213 $enabled['exactMatch'] = $enabled['wildcardMatch'] = $disabled['exactMatch'] = $disabled['wildcardMatch'] = array();
214 for ($i = 1; $i <= $this->_numStrings
; $i++
) {
215 if (!empty($params['new'][$i]) && !empty($params['old'][$i])) {
216 if (isset($params['enabled']) && !empty($params['enabled'][$i])) {
217 if (!empty($params['cb']) && !empty($params['cb'][$i])) {
218 $enabled['exactMatch'] +
= array($params['old'][$i] => $params['new'][$i]);
221 $enabled['wildcardMatch'] +
= array($params['old'][$i] => $params['new'][$i]);
225 if (isset($params['cb']) && is_array($params['cb']) && array_key_exists($i, $params['cb'])) {
226 $disabled['exactMatch'] +
= array($params['old'][$i] => $params['new'][$i]);
229 $disabled['wildcardMatch'] +
= array($params['old'][$i] => $params['new'][$i]);
236 'enabled' => $enabled,
237 'disabled' => $disabled,
240 $config = CRM_Core_Config
::singleton();
242 $domain = new CRM_Core_DAO_Domain();
245 if ($domain->locales
&& $config->localeCustomStrings
) {
247 $addReplacements = $config->localeCustomStrings
;
248 $addReplacements[$config->lcMessages
] = $overrides;
249 $stringOverride = serialize($addReplacements);
252 // for single language
253 $stringOverride = serialize(array($config->lcMessages
=> $overrides));
256 $params = array('locale_custom_strings' => $stringOverride);
257 $id = CRM_Core_Config
::domainID();
259 $wordReplacementSettings = CRM_Core_BAO_Domain
::edit($params, $id);
261 if ($wordReplacementSettings) {
262 // This controller was originally written to CRUD $config->locale_custom_strings,
263 // but that's no longer the canonical store. Sync changes to canonical store.
264 // This is inefficient - at some point, we should rewrite this UI.
265 CRM_Core_BAO_WordReplacement
::rebuildWordReplacementTable();
267 CRM_Core_Session
::setStatus("", ts("Settings Saved"), "success");
268 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/options/wordreplacements',