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