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