3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 protected $_defaults = NULL;
41 public $unsavedChangesWarn = TRUE;
44 * Pre process function.
46 public function preProcess() {
47 // This controller was originally written to CRUD $config->locale_custom_strings,
48 // but that's no longer the canonical store. Re-sync from canonical store to ensure
49 // that we display that latest data. This is inefficient - at some point, we
50 // should rewrite this UI.
51 CRM_Core_BAO_WordReplacement
::rebuild(FALSE);
53 $this->_soInstance
= CRM_Utils_Array
::value('instance', $_GET);
54 $this->assign('soInstance', $this->_soInstance
);
62 public function setDefaultValues() {
63 if ($this->_defaults
!== NULL) {
64 return $this->_defaults
;
67 $this->_defaults
= array();
69 $config = CRM_Core_Config
::singleton();
71 $values = $config->localeCustomStrings
[$config->lcMessages
];
74 $enableDisable = array(
79 $cardMatch = array('wildcardMatch', 'exactMatch');
81 foreach ($enableDisable as $key => $val) {
82 foreach ($cardMatch as $kc => $vc) {
83 if (!empty($values[$val][$vc])) {
84 foreach ($values[$val][$vc] as $k => $v) {
85 $this->_defaults
["enabled"][$i] = $key;
86 $this->_defaults
["cb"][$i] = $kc;
87 $this->_defaults
["old"][$i] = $k;
88 $this->_defaults
["new"][$i] = $v;
95 $name = $this->_stringName
= "custom_string_override_{$config->lcMessages}";
96 if (isset($config->$name) &&
97 is_array($config->$name)
99 $this->_numStrings
= 1;
100 foreach ($config->$name as $old => $newValues) {
101 $this->_numStrings++
;
102 $this->_numStrings +
= 9;
106 $this->_numStrings
= 10;
109 return $this->_defaults
;
113 * Build the form object.
115 public function buildQuickForm() {
116 $config = CRM_Core_Config
::singleton();
117 $values = $config->localeCustomStrings
[$config->lcMessages
];
121 foreach ($values as $valMatchType) {
122 foreach ($valMatchType as $valPairs) {
123 $instances +
= count($valPairs);
127 if ($instances > 10) {
128 $this->_numStrings
= $instances;
131 $soInstances = range(1, $this->_numStrings
, 1);
132 $stringOverrideInstances = array();
133 if ($this->_soInstance
) {
134 $soInstances = array($this->_soInstance
);
136 elseif (!empty($_POST['old'])) {
137 $soInstances = $stringOverrideInstances = array_keys($_POST['old']);
139 elseif (!empty($this->_defaults
) && is_array($this->_defaults
)) {
140 $stringOverrideInstances = array_keys($this->_defaults
['new']);
141 if (count($this->_defaults
['old']) > count($this->_defaults
['new'])) {
142 $stringOverrideInstances = array_keys($this->_defaults
['old']);
145 foreach ($soInstances as $instance) {
146 $this->addElement('checkbox', "enabled[$instance]");
147 $this->add('textarea', "old[$instance]", NULL, array('rows' => 1, 'cols' => 40));
148 $this->add('textarea', "new[$instance]", NULL, array('rows' => 1, 'cols' => 40));
149 $this->addElement('checkbox', "cb[$instance]");
151 $this->assign('numStrings', $this->_numStrings
);
152 if ($this->_soInstance
) {
156 $this->assign('stringOverrideInstances', empty($stringOverrideInstances) ?
FALSE : $stringOverrideInstances);
158 $this->addButtons(array(
161 'name' => ts('Save'),
166 'name' => ts('Cancel'),
170 $this->addFormRule(array('CRM_Admin_Form_WordReplacements', 'formRule'), $this);
174 * Global validation rules for the form.
176 * @param array $values
177 * Posted values of the form.
180 * list of errors to be posted back to the form
182 public static function formRule($values) {
185 $oldValues = CRM_Utils_Array
::value('old', $values);
186 $newValues = CRM_Utils_Array
::value('new', $values);
187 $enabled = CRM_Utils_Array
::value('enabled', $values);
188 $exactMatch = CRM_Utils_Array
::value('cb', $values);
190 foreach ($oldValues as $k => $v) {
191 if ($v && !$newValues[$k]) {
192 $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word');
194 elseif (!$v && $newValues[$k]) {
195 $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word');
197 elseif ((empty($newValues[$k]) && empty($oldValues[$k]))
198 && (!empty($enabled[$k]) ||
!empty($exactMatch[$k]))
200 $errors['old[' . $k . ']'] = ts('Please Enter the value for Original Word');
201 $errors['new[' . $k . ']'] = ts('Please Enter the value for Replacement Word');
209 * Process the form submission.
211 public function postProcess() {
212 $params = $this->controller
->exportValues($this->_name
);
213 $this->_numStrings
= count($params['old']);
215 $enabled['exactMatch'] = $enabled['wildcardMatch'] = $disabled['exactMatch'] = $disabled['wildcardMatch'] = array();
216 for ($i = 1; $i <= $this->_numStrings
; $i++
) {
217 if (!empty($params['new'][$i]) && !empty($params['old'][$i])) {
218 if (isset($params['enabled']) && !empty($params['enabled'][$i])) {
219 if (!empty($params['cb']) && !empty($params['cb'][$i])) {
220 $enabled['exactMatch'] +
= array($params['old'][$i] => $params['new'][$i]);
223 $enabled['wildcardMatch'] +
= array($params['old'][$i] => $params['new'][$i]);
227 if (isset($params['cb']) && is_array($params['cb']) && array_key_exists($i, $params['cb'])) {
228 $disabled['exactMatch'] +
= array($params['old'][$i] => $params['new'][$i]);
231 $disabled['wildcardMatch'] +
= array($params['old'][$i] => $params['new'][$i]);
238 'enabled' => $enabled,
239 'disabled' => $disabled,
242 $config = CRM_Core_Config
::singleton();
244 $domain = new CRM_Core_DAO_Domain();
247 if ($domain->locales
&& $config->localeCustomStrings
) {
249 $addReplacements = $config->localeCustomStrings
;
250 $addReplacements[$config->lcMessages
] = $overrides;
251 $stringOverride = serialize($addReplacements);
254 // for single language
255 $stringOverride = serialize(array($config->lcMessages
=> $overrides));
258 $params = array('locale_custom_strings' => $stringOverride);
259 $id = CRM_Core_Config
::domainID();
261 $wordReplacementSettings = CRM_Core_BAO_Domain
::edit($params, $id);
263 if ($wordReplacementSettings) {
264 // This controller was originally written to CRUD $config->locale_custom_strings,
265 // but that's no longer the canonical store. Sync changes to canonical store.
266 // This is inefficient - at some point, we should rewrite this UI.
267 CRM_Core_BAO_WordReplacement
::rebuildWordReplacementTable();
269 CRM_Core_Session
::setStatus("", ts("Settings Saved"), "success");
270 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/options/wordreplacements',