3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
37 * This class generates form components for DedupeRules
40 class CRM_Contact_Form_DedupeRules
extends CRM_Admin_Form
{
41 const RULES_COUNT
= 5;
42 protected $_contactType;
43 protected $_defaults = array();
44 protected $_fields = array();
48 * Explicitly declare the entity api name.
50 public function getDefaultEntity() {
59 public function preProcess() {
60 // Ensure user has permission to be here
61 if (!CRM_Core_Permission
::check('administer dedupe rules')) {
62 CRM_Utils_System
::permissionDenied();
63 CRM_Utils_System
::civiExit();
65 $this->_options
= CRM_Core_SelectValues
::getDedupeRuleTypes();
66 $this->_rgid
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, FALSE, 0);
67 $this->_contactType
= CRM_Utils_Request
::retrieve('contact_type', 'String', $this, FALSE, 0);
69 $rgDao = new CRM_Dedupe_DAO_RuleGroup();
70 $rgDao->id
= $this->_rgid
;
73 $this->_defaults
['threshold'] = $rgDao->threshold
;
74 $this->_contactType
= $rgDao->contact_type
;
75 $this->_defaults
['used'] = CRM_Utils_Array
::key($rgDao->used
, $this->_options
);
76 $this->_defaults
['title'] = $rgDao->title
;
77 $this->_defaults
['name'] = $rgDao->name
;
78 $this->_defaults
['is_reserved'] = $rgDao->is_reserved
;
79 $this->assign('isReserved', $rgDao->is_reserved
);
80 $this->assign('ruleName', $rgDao->name
);
81 $ruleDao = new CRM_Dedupe_DAO_Rule();
82 $ruleDao->dedupe_rule_group_id
= $this->_rgid
;
85 while ($ruleDao->fetch()) {
86 $this->_defaults
["where_$count"] = "{$ruleDao->rule_table}.{$ruleDao->rule_field}";
87 $this->_defaults
["length_$count"] = $ruleDao->rule_length
;
88 $this->_defaults
["weight_$count"] = $ruleDao->rule_weight
;
92 $supported = CRM_Dedupe_BAO_RuleGroup
::supportedFields($this->_contactType
);
93 if (is_array($supported)) {
94 foreach ($supported as $table => $fields) {
95 foreach ($fields as $field => $title) {
96 $this->_fields
["$table.$field"] = $title;
100 asort($this->_fields
);
104 * Build the form object.
108 public function buildQuickForm() {
109 $this->addField('title', array('label' => ts('Rule Name')), TRUE);
110 $this->addRule('title', ts('A duplicate matching rule with this name already exists. Please select another name.'),
111 'objectExists', array('CRM_Dedupe_DAO_RuleGroup', $this->_rgid
, 'title')
114 $this->addField('used', array('label' => ts('Usage')), TRUE);
116 $reserved = $this->addField('is_reserved', array('label' => ts('Reserved?')));
117 if (!empty($this->_defaults
['is_reserved'])) {
118 //$reserved->freeze();
119 $disabled = array('disabled' => TRUE);
122 $attributes = array('class' => 'two');
123 if (!empty($disabled)) {
124 $attributes = array_merge($attributes, $disabled);
127 for ($count = 0; $count < self
::RULES_COUNT
; $count++
) {
128 $this->add('select', "where_$count", ts('Field'),
130 NULL => ts('- none -'),
131 ) +
$this->_fields
, FALSE, $disabled
133 $this->addField("length_$count", array('entity' => 'Rule', 'name' => 'rule_length') +
$attributes);
134 $this->addField("weight_$count", array('entity' => 'Rule', 'name' => 'rule_weight') +
$attributes);
137 $this->addField('threshold', array('label' => ts("Weight Threshold to Consider Contacts 'Matching':")) +
$attributes);
139 $this->assign('contact_type', $this->_contactType
);
141 $this->addFormRule(array('CRM_Contact_Form_DedupeRules', 'formRule'), $this);
143 parent
::buildQuickForm();
147 * Global validation rules for the form.
149 * @param array $fields
150 * Posted values of the form.
156 * list of errors to be posted back to the form
158 public static function formRule($fields, $files, $self) {
160 if (!empty($fields['is_reserved'])) {
164 $fieldSelected = FALSE;
165 for ($count = 0; $count < self
::RULES_COUNT
; $count++
) {
166 if (!empty($fields["where_$count"])) {
167 $fieldSelected = TRUE;
172 if (!$fieldSelected) {
173 $errors['_qf_default'] = ts('Please select at least one field.');
176 return empty($errors) ?
TRUE : $errors;
180 * Set default values for the form. MobileProvider that in edit/view mode
181 * the default values are retrieved from the database
189 public function setDefaultValues() {
190 return $this->_defaults
;
194 * Process the form submission.
199 public function postProcess() {
200 $values = $this->exportValues();
202 //FIXME: Handle logic to replace is_default column by usage
203 // reset used column to General (since there can only
204 // be one 'Supervised' or 'Unsupervised' rule)
205 if ($values['used'] != 'General') {
207 UPDATE civicrm_dedupe_rule_group
209 WHERE contact_type = %1
211 $queryParams = array(
212 1 => array($this->_contactType
, 'String'),
213 2 => array($values['used'], 'String'),
216 CRM_Core_DAO
::executeQuery($query, $queryParams);
219 $rgDao = new CRM_Dedupe_DAO_RuleGroup();
220 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
221 $rgDao->id
= $this->_rgid
;
224 $rgDao->title
= $values['title'];
225 $rgDao->is_reserved
= CRM_Utils_Array
::value('is_reserved', $values, FALSE);
226 $rgDao->used
= $values['used'];
227 $rgDao->contact_type
= $this->_contactType
;
228 $rgDao->threshold
= $values['threshold'];
231 // make sure name is set only during insert
232 if ($this->_action
& CRM_Core_Action
::ADD
) {
233 // generate name based on title
234 $rgDao->name
= CRM_Utils_String
::titleToVar($values['title']) . "_{$rgDao->id}";
238 // lets skip updating of fields for reserved dedupe group
239 if ($rgDao->is_reserved
) {
240 CRM_Core_Session
::setStatus(ts("The rule '%1' has been saved.", array(1 => $rgDao->title
)), ts('Saved'), 'success');
244 $ruleDao = new CRM_Dedupe_DAO_Rule();
245 $ruleDao->dedupe_rule_group_id
= $rgDao->id
;
249 $substrLenghts = array();
252 $daoObj = new CRM_Core_DAO();
253 $database = $daoObj->database();
254 for ($count = 0; $count < self
::RULES_COUNT
; $count++
) {
255 if (empty($values["where_$count"])) {
258 list($table, $field) = explode('.', CRM_Utils_Array
::value("where_$count", $values));
259 $length = !empty($values["length_$count"]) ? CRM_Utils_Array
::value("length_$count", $values) : NULL;
260 $weight = $values["weight_$count"];
261 if ($table and $field) {
262 $ruleDao = new CRM_Dedupe_DAO_Rule();
263 $ruleDao->dedupe_rule_group_id
= $rgDao->id
;
264 $ruleDao->rule_table
= $table;
265 $ruleDao->rule_field
= $field;
266 $ruleDao->rule_length
= $length;
267 $ruleDao->rule_weight
= $weight;
271 if (!array_key_exists($table, $tables)) {
272 $tables[$table] = array();
274 $tables[$table][] = $field;
277 // CRM-6245: we must pass table/field/length triples to the createIndexes() call below
279 if (!isset($substrLenghts[$table])) {
280 $substrLenghts[$table] = array();
283 //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"
284 $schemaQuery = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS
285 WHERE TABLE_SCHEMA = '{$database}' AND
286 TABLE_NAME = '{$table}' AND COLUMN_NAME = '{$field}';";
287 $dao = CRM_Core_DAO
::executeQuery($schemaQuery);
290 // set the length to null for all the fields where prefix length is not supported. eg. int,tinyint,date,enum etc dataTypes.
291 if ($dao->COLUMN_NAME
== $field && !in_array($dao->DATA_TYPE
, array(
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
;
307 $substrLenghts[$table][$field] = $length;
311 // also create an index for this dedupe rule
313 CRM_Utils_Hook
::dupeQuery($ruleDao, 'dedupeIndexes', $tables);
314 CRM_Core_BAO_SchemaHandler
::createIndexes($tables, 'dedupe_index', $substrLenghts);
316 //need to clear cache of deduped contacts
317 //based on the previous rule
318 $cacheKey = "merge {$this->_contactType}_{$this->_rgid}_%";
320 CRM_Core_BAO_PrevNextCache
::deleteItem(NULL, $cacheKey);
322 CRM_Core_Session
::setStatus(ts("The rule '%1' has been saved.", array(1 => $rgDao->title
)), ts('Saved'), 'success');