dev/core#2820: Deduping produces bad results after a rule change
[civicrm-core.git] / CRM / Contact / Form / DedupeRules.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for DedupeRules.
20 */
21 class CRM_Contact_Form_DedupeRules extends CRM_Admin_Form {
22 const RULES_COUNT = 5;
23 protected $_contactType;
24 protected $_fields = [];
25 protected $_rgid;
26
27 /**
28 * Explicitly declare the entity api name.
29 */
30 public function getDefaultEntity() {
31 return 'RuleGroup';
32 }
33
34 /**
35 * Pre processing.
36 */
37 public function preProcess() {
38 // Ensure user has permission to be here
39 if (!CRM_Core_Permission::check('administer dedupe rules')) {
40 CRM_Utils_System::permissionDenied();
41 CRM_Utils_System::civiExit();
42 }
43 $this->_options = CRM_Core_SelectValues::getDedupeRuleTypes();
44 $this->_rgid = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
45
46 // check if $contactType is valid
47 $contactTypes = civicrm_api3('Contact', 'getOptions', ['field' => "contact_type", 'context' => "validate"]);
48 $contactType = CRM_Utils_Request::retrieve('contact_type', 'String', $this, FALSE, 0);
49 if (!empty($contactTypes['values'][$contactType])) {
50 $this->_contactType = $contactType;
51 }
52 elseif (!empty($contactType)) {
53 throw new CRM_Core_Exception('Contact Type is Not valid');
54 }
55 if ($this->_rgid) {
56 $rgDao = new CRM_Dedupe_DAO_DedupeRuleGroup();
57 $rgDao->id = $this->_rgid;
58 $rgDao->find(TRUE);
59
60 $this->_defaults['threshold'] = $rgDao->threshold;
61 $this->_contactType = $rgDao->contact_type;
62 $this->_defaults['used'] = $rgDao->used;
63 $this->_defaults['title'] = $rgDao->title;
64 $this->_defaults['name'] = $rgDao->name;
65 $this->_defaults['is_reserved'] = $rgDao->is_reserved;
66 $this->assign('isReserved', $rgDao->is_reserved);
67 $this->assign('ruleName', $rgDao->name);
68 $ruleDao = new CRM_Dedupe_DAO_DedupeRule();
69 $ruleDao->dedupe_rule_group_id = $this->_rgid;
70 $ruleDao->find();
71 $count = 0;
72 while ($ruleDao->fetch()) {
73 $this->_defaults["where_$count"] = "{$ruleDao->rule_table}.{$ruleDao->rule_field}";
74 $this->_defaults["length_$count"] = $ruleDao->rule_length;
75 $this->_defaults["weight_$count"] = $ruleDao->rule_weight;
76 $count++;
77 }
78 }
79 $supported = CRM_Dedupe_BAO_DedupeRuleGroup::supportedFields($this->_contactType);
80 if (is_array($supported)) {
81 foreach ($supported as $table => $fields) {
82 foreach ($fields as $field => $title) {
83 $this->_fields["$table.$field"] = $title;
84 }
85 }
86 }
87 asort($this->_fields);
88 }
89
90 /**
91 * Build the form object.
92 */
93 public function buildQuickForm() {
94 $this->addField('title', ['label' => ts('Rule Name')], TRUE);
95 $this->addRule('title', ts('A duplicate matching rule with this name already exists. Please select another name.'),
96 'objectExists', ['CRM_Dedupe_DAO_DedupeRuleGroup', $this->_rgid, 'title']
97 );
98
99 $this->addField('used', ['label' => ts('Usage')], TRUE);
100 $reserved = $this->addField('is_reserved', ['label' => ts('Reserved?')]);
101 if (!empty($this->_defaults['is_reserved'])) {
102 $reserved->freeze();
103 }
104
105 $attributes = ['class' => 'two'];
106
107 for ($count = 0; $count < self::RULES_COUNT; $count++) {
108 $this->add('select', "where_$count", ts('Field'),
109 $this->_fields, FALSE, ['class' => 'crm-select2', 'placeholder' => ts('Select Field')]
110 );
111 $this->addField("length_$count", ['entity' => 'Rule', 'name' => 'rule_length'] + $attributes);
112 $this->addField("weight_$count", ['entity' => 'Rule', 'name' => 'rule_weight'] + $attributes);
113 }
114
115 $this->addField('threshold', ['label' => ts("Weight Threshold to Consider Contacts 'Matching':")] + $attributes);
116
117 $this->assign('contact_type', $this->_contactType);
118
119 $this->addFormRule(['CRM_Contact_Form_DedupeRules', 'formRule'], $this);
120
121 parent::buildQuickForm();
122 }
123
124 /**
125 * Global validation rules for the form.
126 *
127 * @param array $fields
128 * Posted values of the form.
129 *
130 * @param $files
131 * @param $self
132 *
133 * @return array
134 * list of errors to be posted back to the form
135 */
136 public static function formRule($fields, $files, $self) {
137 $errors = [];
138 $fieldSelected = FALSE;
139 $actualThreshold = 0;
140 for ($count = 0; $count < self::RULES_COUNT; $count++) {
141 if (!empty($fields["where_$count"]) || (isset($self->_defaults['is_reserved']) && !empty($self->_defaults["where_$count"]))) {
142 $fieldSelected = TRUE;
143 if (!empty($fields["weight_$count"])) {
144 $actualThreshold += $fields["weight_$count"];
145 }
146 }
147 }
148 if (empty($fields['threshold'])) {
149 // CRM-20607 - Don't validate the threshold of hard-coded rules
150 if (!(CRM_Utils_Array::value('is_reserved', $fields) &&
151 CRM_Utils_File::isIncludable("CRM/Dedupe/BAO/QueryBuilder/{$self->_defaultValues['name']}.php"))) {
152 $errors['threshold'] = ts('Threshold weight cannot be empty or zero.');
153 }
154 }
155 else {
156 if ($actualThreshold < $fields['threshold']) {
157 $errors['threshold'] = ts('Total weight must be greater than or equal to the Weight Threshold.');
158 }
159 }
160
161 if (!$fieldSelected) {
162 $errors['_qf_default'] = ts('Please select at least one field.');
163 }
164
165 return empty($errors) ? TRUE : $errors;
166 }
167
168 /**
169 * Set default values for the form. MobileProvider that in edit/view mode
170 * the default values are retrieved from the database
171 *
172 *
173 * @return array
174 */
175
176 /**
177 * @return array
178 */
179 public function setDefaultValues() {
180 return $this->_defaults;
181 }
182
183 /**
184 * Process the form submission.
185 */
186 public function postProcess() {
187 $values = $this->exportValues();
188
189 //FIXME: Handle logic to replace is_default column by usage
190 // reset used column to General (since there can only
191 // be one 'Supervised' or 'Unsupervised' rule)
192 if ($values['used'] != 'General') {
193 $query = "
194 UPDATE civicrm_dedupe_rule_group
195 SET used = 'General'
196 WHERE contact_type = %1
197 AND used = %2";
198 $queryParams = [
199 1 => [$this->_contactType, 'String'],
200 2 => [$values['used'], 'String'],
201 ];
202
203 CRM_Core_DAO::executeQuery($query, $queryParams);
204 }
205
206 $rgDao = new CRM_Dedupe_DAO_DedupeRuleGroup();
207 if ($this->_action & CRM_Core_Action::UPDATE) {
208 $rgDao->id = $this->_rgid;
209 }
210
211 $rgDao->title = $values['title'];
212 $rgDao->is_reserved = CRM_Utils_Array::value('is_reserved', $values, FALSE);
213 $rgDao->used = $values['used'];
214 $rgDao->contact_type = $this->_contactType;
215 $rgDao->threshold = $values['threshold'];
216 $rgDao->save();
217
218 // make sure name is set only during insert
219 if ($this->_action & CRM_Core_Action::ADD) {
220 // generate name based on title
221 $rgDao->name = CRM_Utils_String::titleToVar($values['title']) . "_{$rgDao->id}";
222 $rgDao->save();
223 }
224
225 // lets skip updating of fields for reserved dedupe group
226 if (!empty($this->_defaults['is_reserved'])) {
227 CRM_Core_Session::setStatus(ts("The rule '%1' has been saved.", [1 => $rgDao->title]), ts('Saved'), 'success');
228 return;
229 }
230
231 $ruleDao = new CRM_Dedupe_DAO_DedupeRule();
232 $ruleDao->dedupe_rule_group_id = $rgDao->id;
233 $ruleDao->delete();
234 $substrLenghts = [];
235
236 $tables = [];
237 $daoObj = new CRM_Core_DAO();
238 $database = $daoObj->database();
239 for ($count = 0; $count < self::RULES_COUNT; $count++) {
240 if (empty($values["where_$count"])) {
241 continue;
242 }
243 list($table, $field) = explode('.', CRM_Utils_Array::value("where_$count", $values));
244 $length = !empty($values["length_$count"]) ? CRM_Utils_Array::value("length_$count", $values) : NULL;
245 $weight = $values["weight_$count"];
246 if ($table and $field) {
247 $ruleDao = new CRM_Dedupe_DAO_DedupeRule();
248 $ruleDao->dedupe_rule_group_id = $rgDao->id;
249 $ruleDao->rule_table = $table;
250 $ruleDao->rule_field = $field;
251 $ruleDao->rule_length = $length;
252 $ruleDao->rule_weight = $weight;
253 $ruleDao->save();
254
255 if (!array_key_exists($table, $tables)) {
256 $tables[$table] = [];
257 }
258 $tables[$table][] = $field;
259 }
260
261 // CRM-6245: we must pass table/field/length triples to the createIndexes() call below
262 if ($length) {
263 if (!isset($substrLenghts[$table])) {
264 $substrLenghts[$table] = [];
265 }
266
267 //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"
268 $schemaQuery = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS
269 WHERE TABLE_SCHEMA = '{$database}' AND
270 TABLE_NAME = '{$table}' AND COLUMN_NAME = '{$field}';";
271 $dao = CRM_Core_DAO::executeQuery($schemaQuery);
272
273 if ($dao->fetch()) {
274 // set the length to null for all the fields where prefix length is not supported. eg. int,tinyint,date,enum etc dataTypes.
275 if ($dao->COLUMN_NAME == $field && !in_array($dao->DATA_TYPE, [
276 'char',
277 'varchar',
278 'binary',
279 'varbinary',
280 'text',
281 'blob',
282 ])) {
283 $length = NULL;
284 }
285 elseif ($dao->COLUMN_NAME == $field && !empty($dao->CHARACTER_MAXIMUM_LENGTH) && ($length > $dao->CHARACTER_MAXIMUM_LENGTH)) {
286 //set the length to CHARACTER_MAXIMUM_LENGTH in case the length provided by the user is greater than the limit
287 $length = $dao->CHARACTER_MAXIMUM_LENGTH;
288 }
289 }
290 $substrLenghts[$table][$field] = $length;
291 }
292 }
293
294 // also create an index for this dedupe rule
295 // CRM-3837
296 CRM_Utils_Hook::dupeQuery($ruleDao, 'dedupeIndexes', $tables);
297 CRM_Core_BAO_SchemaHandler::createIndexes($tables, 'dedupe_index', $substrLenghts);
298
299 //need to clear cache of deduped contacts
300 //based on the previous rule
301 $cacheKey = "merge_{$this->_contactType}_{$this->_rgid}_%";
302
303 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey);
304
305 CRM_Core_Session::setStatus(ts("The rule '%1' has been saved.", [1 => $rgDao->title]), ts('Saved'), 'success');
306 }
307
308 }