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