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