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