3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for DedupeRules.
21 class CRM_Contact_Form_DedupeRules
extends CRM_Admin_Form
{
22 const RULES_COUNT
= 5;
23 protected $_contactType;
24 protected $_fields = [];
28 * Explicitly declare the entity api name.
30 public function getDefaultEntity() {
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();
44 Civi
::resources()->addScriptFile('civicrm', 'js/crm.dedupeRules.js');
46 $this->_options
= CRM_Core_SelectValues
::getDedupeRuleTypes();
47 $this->_rgid
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, FALSE, 0);
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;
55 elseif (!empty($contactType)) {
56 throw new CRM_Core_Exception('Contact Type is Not valid');
59 $rgDao = new CRM_Dedupe_DAO_DedupeRuleGroup();
60 $rgDao->id
= $this->_rgid
;
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
;
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
;
85 $this->_defaults
['used'] = 'General';
86 $this->assign('ruleUsed', CRM_Core_SelectValues
::getDedupeRuleTypes()['General']);
87 $this->assign('canChangeUsage', TRUE);
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;
97 asort($this->_fields
);
101 * Build the form object.
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']
109 $this->add('hidden', 'used');
110 $reserved = $this->addField('is_reserved', ['label' => ts('Reserved?')]);
111 if (!empty($this->_defaults
['is_reserved'])) {
115 $attributes = ['class' => 'two'];
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')]
121 $this->addField("length_$count", ['entity' => 'Rule', 'name' => 'rule_length'] +
$attributes);
122 $this->addField("weight_$count", ['entity' => 'Rule', 'name' => 'rule_weight'] +
$attributes);
125 $this->addField('threshold', ['label' => ts("Weight Threshold to Consider Contacts 'Matching':")] +
$attributes);
127 $this->assign('contact_type', $this->_contactType
);
129 $this->addFormRule(['CRM_Contact_Form_DedupeRules', 'formRule'], $this);
131 parent
::buildQuickForm();
135 * Global validation rules for the form.
137 * @param array $fields
138 * Posted values of the form.
144 * list of errors to be posted back to the form
146 public static function formRule($fields, $files, $self) {
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"];
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.');
166 if ($actualThreshold < $fields['threshold']) {
167 $errors['threshold'] = ts('Total weight must be greater than or equal to the Weight Threshold.');
171 if (!$fieldSelected) {
172 $errors['_qf_default'] = ts('Please select at least one field.');
175 return empty($errors) ?
TRUE : $errors;
179 * Set default values for the form. MobileProvider that in edit/view mode
180 * the default values are retrieved from the database
189 public function setDefaultValues() {
190 return $this->_defaults
;
194 * Process the form submission.
196 public function postProcess() {
197 $values = $this->exportValues();
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') {
204 UPDATE civicrm_dedupe_rule_group
206 WHERE contact_type = %1
209 1 => [$this->_contactType
, 'String'],
210 2 => [$values['used'], 'String'],
213 CRM_Core_DAO
::executeQuery($query, $queryParams);
216 $rgDao = new CRM_Dedupe_DAO_DedupeRuleGroup();
217 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
218 $rgDao->id
= $this->_rgid
;
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'];
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}";
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');
241 $ruleDao = new CRM_Dedupe_DAO_DedupeRule();
242 $ruleDao->dedupe_rule_group_id
= $rgDao->id
;
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"])) {
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;
265 if (!array_key_exists($table, $tables)) {
266 $tables[$table] = [];
268 $tables[$table][] = $field;
271 // CRM-6245: we must pass table/field/length triples to the createIndexes() call below
273 if (!isset($substrLenghts[$table])) {
274 $substrLenghts[$table] = [];
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);
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
, [
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
;
300 $substrLenghts[$table][$field] = $length;
304 // also create an index for this dedupe rule
306 CRM_Utils_Hook
::dupeQuery($ruleDao, 'dedupeIndexes', $tables);
307 CRM_Core_BAO_SchemaHandler
::createIndexes($tables, 'dedupe_index', $substrLenghts);
309 //need to clear cache of deduped contacts
310 //based on the previous rule
311 $cacheKey = "merge_{$this->_contactType}_{$this->_rgid}_%";
313 CRM_Core_BAO_PrevNextCache
::deleteItem(NULL, $cacheKey);
315 CRM_Core_Session
::setStatus(ts("The rule '%1' has been saved.", [1 => $rgDao->title
]), ts('Saved'), 'success');