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