CRM-16119: Fix incorrect usages of ts().
[civicrm-core.git] / CRM / Contact / Form / DedupeRules.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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for DedupeRules
38 *
39 */
40class CRM_Contact_Form_DedupeRules extends CRM_Admin_Form {
7da04cde 41 const RULES_COUNT = 5;
6a488035
TO
42 protected $_contactType;
43 protected $_defaults = array();
44 protected $_fields = array();
45 protected $_rgid;
46
47 /**
fe482240 48 * Pre processing.
6a488035 49 *
355ba699 50 * @return void
8ef12e64 51 */
00be9182 52 public function preProcess() {
6a488035
TO
53 // Ensure user has permission to be here
54 if (!CRM_Core_Permission::check('administer dedupe rules')) {
55 CRM_Utils_System::permissionDenied();
56 CRM_Utils_System::civiExit();
57 }
77d0b1f8 58 $this->_options = CRM_Core_SelectValues::getDedupeRuleTypes();
6a488035
TO
59 $this->_rgid = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
60 $this->_contactType = CRM_Utils_Request::retrieve('contact_type', 'String', $this, FALSE, 0);
61 if ($this->_rgid) {
62 $rgDao = new CRM_Dedupe_DAO_RuleGroup();
63 $rgDao->id = $this->_rgid;
64 $rgDao->find(TRUE);
65
66 $this->_defaults['threshold'] = $rgDao->threshold;
67 $this->_contactType = $rgDao->contact_type;
68 $this->_defaults['used'] = CRM_Utils_Array::key($rgDao->used, $this->_options);
69 $this->_defaults['title'] = $rgDao->title;
70 $this->_defaults['name'] = $rgDao->name;
71 $this->_defaults['is_reserved'] = $rgDao->is_reserved;
72 $this->assign('isReserved', $rgDao->is_reserved);
73 $this->assign('ruleName', $rgDao->name);
74 $ruleDao = new CRM_Dedupe_DAO_Rule();
75 $ruleDao->dedupe_rule_group_id = $this->_rgid;
76 $ruleDao->find();
77 $count = 0;
78 while ($ruleDao->fetch()) {
79 $this->_defaults["where_$count"] = "{$ruleDao->rule_table}.{$ruleDao->rule_field}";
80 $this->_defaults["length_$count"] = $ruleDao->rule_length;
81 $this->_defaults["weight_$count"] = $ruleDao->rule_weight;
82 $count++;
83 }
84 }
85 $supported = CRM_Dedupe_BAO_RuleGroup::supportedFields($this->_contactType);
86 if (is_array($supported)) {
87 foreach ($supported as $table => $fields) {
88 foreach ($fields as $field => $title) {
89 $this->_fields["$table.$field"] = $title;
90 }
91 }
92 }
93 asort($this->_fields);
94 }
95
96 /**
fe482240 97 * Build the form object.
6a488035 98 *
355ba699 99 * @return void
6a488035
TO
100 */
101 public function buildQuickForm() {
102 $foo = CRM_Core_DAO::getAttribute('CRM_Dedupe_DAO_Rule', 'title');
103
104 $this->add('text', 'title', ts('Rule Name'), array('maxlength' => 255, 'class' => 'huge'), TRUE);
105 $this->addRule('title', ts('A duplicate matching rule with this name already exists. Please select another name.'),
106 'objectExists', array('CRM_Dedupe_DAO_RuleGroup', $this->_rgid, 'title')
107 );
108
407cd1e9 109 $this->addRadio('used', ts('Usage'), $this->_options, NULL, NULL, TRUE);
6a488035
TO
110
111 $disabled = array();
112 $reserved = $this->add('checkbox', 'is_reserved', ts('Reserved?'));
a7488080 113 if (!empty($this->_defaults['is_reserved'])) {
6a488035
TO
114 $reserved->freeze();
115 $disabled = array('disabled' => TRUE);
116 }
117
118 $attributes = array('class' => 'two');
119 if (!empty($disabled)) {
120 $attributes = array_merge($attributes, $disabled);
121 }
122
123 for ($count = 0; $count < self::RULES_COUNT; $count++) {
124 $this->add('select', "where_$count", ts('Field'),
125 array(
21dfd5f5 126 NULL => ts('- none -'),
6432bd67 127 ) + $this->_fields, FALSE, $disabled
6a488035
TO
128 );
129 $this->add('text', "length_$count", ts('Length'), $attributes);
130 $this->add('text', "weight_$count", ts('Weight'), $attributes);
131 }
132
133 $this->add('text', 'threshold', ts("Weight Threshold to Consider Contacts 'Matching':"), $attributes);
6a488035
TO
134
135 $this->assign('contact_type', $this->_contactType);
136
137 $this->addFormRule(array('CRM_Contact_Form_DedupeRules', 'formRule'), $this);
a1f552a9
CW
138
139 parent::buildQuickForm();
6a488035
TO
140 }
141
142 /**
fe482240 143 * Global validation rules for the form.
6a488035 144 *
77c5b619
TO
145 * @param array $fields
146 * Posted values of the form.
6a488035 147 *
da6b46f4
EM
148 * @param $files
149 * @param $self
150 *
a6c01b45
CW
151 * @return array
152 * list of errors to be posted back to the form
6a488035 153 */
00be9182 154 public static function formRule($fields, $files, $self) {
6a488035 155 $errors = array();
a7488080 156 if (!empty($fields['is_reserved'])) {
6a488035
TO
157 return TRUE;
158 }
159
160 $fieldSelected = FALSE;
161 for ($count = 0; $count < self::RULES_COUNT; $count++) {
a7488080 162 if (!empty($fields["where_$count"])) {
6a488035
TO
163 $fieldSelected = TRUE;
164 break;
165 }
166 }
167
168 if (!$fieldSelected) {
169 $errors['_qf_default'] = ts('Please select at least one field.');
170 }
171
172 return empty($errors) ? TRUE : $errors;
173 }
174
86538308 175 /**
c490a46a 176 * Set default values for the form. MobileProvider that in edit/view mode
86538308
EM
177 * the default values are retrieved from the database
178 *
86538308
EM
179 *
180 * @return array
181 */
182 /**
183 * @return array
184 */
00be9182 185 public function setDefaultValues() {
6a488035
TO
186 return $this->_defaults;
187 }
188
189 /**
fe482240 190 * Process the form submission.
6a488035 191 *
6a488035 192 *
355ba699 193 * @return void
6a488035
TO
194 */
195 public function postProcess() {
196 $values = $this->exportValues();
8ef12e64 197
6a488035 198 //FIXME: Handle logic to replace is_default column by usage
6a488035
TO
199 // reset used column to General (since there can only
200 // be one 'Supervised' or 'Unsupervised' rule)
77d0b1f8 201 if ($values['used'] != 'General') {
6a488035 202 $query = "
8ef12e64 203UPDATE civicrm_dedupe_rule_group
6a488035 204 SET used = 'General'
8ef12e64 205 WHERE contact_type = %1
6a488035 206 AND used = %2";
77d0b1f8 207 $queryParams = array(
208 1 => array($this->_contactType, 'String'),
03390e26 209 2 => array($values['used'], 'String'),
6a488035
TO
210 );
211
212 CRM_Core_DAO::executeQuery($query, $queryParams);
213 }
214
215 $rgDao = new CRM_Dedupe_DAO_RuleGroup();
216 if ($this->_action & CRM_Core_Action::UPDATE) {
217 $rgDao->id = $this->_rgid;
218 }
219
353ffa53
TO
220 $rgDao->title = $values['title'];
221 $rgDao->is_reserved = CRM_Utils_Array::value('is_reserved', $values, FALSE);
222 $rgDao->used = $values['used'];
6a488035 223 $rgDao->contact_type = $this->_contactType;
353ffa53 224 $rgDao->threshold = $values['threshold'];
6a488035
TO
225 $rgDao->save();
226
227 // make sure name is set only during insert
228 if ($this->_action & CRM_Core_Action::ADD) {
229 // generate name based on title
230 $rgDao->name = CRM_Utils_String::titleToVar($values['title']) . "_{$rgDao->id}";
231 $rgDao->save();
232 }
233
234 // lets skip updating of fields for reserved dedupe group
235 if ($rgDao->is_reserved) {
236 CRM_Core_Session::setStatus(ts("The rule '%1' has been saved.", array(1 => $rgDao->title)), ts('Saved'), 'success');
237 return;
238 }
239
240 $ruleDao = new CRM_Dedupe_DAO_Rule();
241 $ruleDao->dedupe_rule_group_id = $rgDao->id;
242 $ruleDao->delete();
243 $ruleDao->free();
244
245 $substrLenghts = array();
246
247 $tables = array();
b0c27559
RN
248 $daoObj = new CRM_Core_DAO();
249 $database = $daoObj->database();
6a488035 250 for ($count = 0; $count < self::RULES_COUNT; $count++) {
a7488080 251 if (empty($values["where_$count"])) {
6a488035
TO
252 continue;
253 }
254 list($table, $field) = explode('.', CRM_Utils_Array::value("where_$count", $values));
0d8afee2 255 $length = !empty($values["length_$count"]) ? CRM_Utils_Array::value("length_$count", $values) : NULL;
6a488035
TO
256 $weight = $values["weight_$count"];
257 if ($table and $field) {
258 $ruleDao = new CRM_Dedupe_DAO_Rule();
259 $ruleDao->dedupe_rule_group_id = $rgDao->id;
260 $ruleDao->rule_table = $table;
261 $ruleDao->rule_field = $field;
262 $ruleDao->rule_length = $length;
263 $ruleDao->rule_weight = $weight;
264 $ruleDao->save();
265 $ruleDao->free();
266
267 if (!array_key_exists($table, $tables)) {
268 $tables[$table] = array();
269 }
270 $tables[$table][] = $field;
271 }
272
273 // CRM-6245: we must pass table/field/length triples to the createIndexes() call below
274 if ($length) {
275 if (!isset($substrLenghts[$table])) {
276 $substrLenghts[$table] = array();
277 }
f41b3f7e
RN
278
279 //CRM-13417 to avoid fatal error "Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys, 1089"
f41b3f7e
RN
280 $schemaQuery = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS
281 WHERE TABLE_SCHEMA = '{$database}' AND
282 TABLE_NAME = '{$table}' AND COLUMN_NAME = '{$field}';";
283 $dao = CRM_Core_DAO::executeQuery($schemaQuery);
284
b0c27559 285 if ($dao->fetch()) {
f41b3f7e 286 // set the length to null for all the fields where prefix length is not supported. eg. int,tinyint,date,enum etc dataTypes.
353ffa53
TO
287 if ($dao->COLUMN_NAME == $field && !in_array($dao->DATA_TYPE, array(
288 'char',
289 'varchar',
290 'binary',
291 'varbinary',
292 'text',
af9b09df 293 'blob',
353ffa53
TO
294 ))
295 ) {
f41b3f7e
RN
296 $length = NULL;
297 }
298 elseif ($dao->COLUMN_NAME == $field && !empty($dao->CHARACTER_MAXIMUM_LENGTH) && ($length > $dao->CHARACTER_MAXIMUM_LENGTH)) {
299 //set the length to CHARACTER_MAXIMUM_LENGTH in case the length provided by the user is greater than the limit
300 $length = $dao->CHARACTER_MAXIMUM_LENGTH;
301 }
302 }
6a488035
TO
303 $substrLenghts[$table][$field] = $length;
304 }
305 }
306
307 // also create an index for this dedupe rule
308 // CRM-3837
1cf05c3e 309 CRM_Utils_Hook::dupeQuery($ruleDao, 'dedupeIndexes', $tables);
6a488035
TO
310 CRM_Core_BAO_SchemaHandler::createIndexes($tables, 'dedupe_index', $substrLenghts);
311
312 //need to clear cache of deduped contacts
313 //based on the previous rule
314 $cacheKey = "merge {$this->_contactType}_{$this->_rgid}_%";
315
316 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey);
317
318 CRM_Core_Session::setStatus(ts("The rule '%1' has been saved.", array(1 => $rgDao->title)), ts('Saved'), 'success');
319 }
96025800 320
6a488035 321}