Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
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 { | |
41 | CONST RULES_COUNT = 5; | |
42 | protected $_contactType; | |
43 | protected $_defaults = array(); | |
44 | protected $_fields = array(); | |
45 | protected $_rgid; | |
46 | ||
47 | /** | |
48 | * Function to pre processing | |
49 | * | |
50 | * @return None | |
51 | * @access public | |
52 | */ | |
53 | 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 = array(ts('Unsupervised'), ts('Supervised'), ts('General')); | |
60 | $this->_rgid = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0); | |
61 | $this->_contactType = CRM_Utils_Request::retrieve('contact_type', 'String', $this, FALSE, 0); | |
62 | if ($this->_rgid) { | |
63 | $rgDao = new CRM_Dedupe_DAO_RuleGroup(); | |
64 | $rgDao->id = $this->_rgid; | |
65 | $rgDao->find(TRUE); | |
66 | ||
67 | $this->_defaults['threshold'] = $rgDao->threshold; | |
68 | $this->_contactType = $rgDao->contact_type; | |
69 | $this->_defaults['used'] = CRM_Utils_Array::key($rgDao->used, $this->_options); | |
70 | $this->_defaults['title'] = $rgDao->title; | |
71 | $this->_defaults['name'] = $rgDao->name; | |
72 | $this->_defaults['is_reserved'] = $rgDao->is_reserved; | |
73 | $this->assign('isReserved', $rgDao->is_reserved); | |
74 | $this->assign('ruleName', $rgDao->name); | |
75 | $ruleDao = new CRM_Dedupe_DAO_Rule(); | |
76 | $ruleDao->dedupe_rule_group_id = $this->_rgid; | |
77 | $ruleDao->find(); | |
78 | $count = 0; | |
79 | while ($ruleDao->fetch()) { | |
80 | $this->_defaults["where_$count"] = "{$ruleDao->rule_table}.{$ruleDao->rule_field}"; | |
81 | $this->_defaults["length_$count"] = $ruleDao->rule_length; | |
82 | $this->_defaults["weight_$count"] = $ruleDao->rule_weight; | |
83 | $count++; | |
84 | } | |
85 | } | |
86 | $supported = CRM_Dedupe_BAO_RuleGroup::supportedFields($this->_contactType); | |
87 | if (is_array($supported)) { | |
88 | foreach ($supported as $table => $fields) { | |
89 | foreach ($fields as $field => $title) { | |
90 | $this->_fields["$table.$field"] = $title; | |
91 | } | |
92 | } | |
93 | } | |
94 | asort($this->_fields); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Function to build the form | |
99 | * | |
100 | * @return None | |
101 | * @access public | |
102 | */ | |
103 | public function buildQuickForm() { | |
104 | $foo = CRM_Core_DAO::getAttribute('CRM_Dedupe_DAO_Rule', 'title'); | |
105 | ||
106 | $this->add('text', 'title', ts('Rule Name'), array('maxlength' => 255, 'class' => 'huge'), TRUE); | |
107 | $this->addRule('title', ts('A duplicate matching rule with this name already exists. Please select another name.'), | |
108 | 'objectExists', array('CRM_Dedupe_DAO_RuleGroup', $this->_rgid, 'title') | |
109 | ); | |
110 | ||
111 | $this->addRadio('used', ts('Usage'), $this->_options); | |
112 | ||
113 | $disabled = array(); | |
114 | $reserved = $this->add('checkbox', 'is_reserved', ts('Reserved?')); | |
115 | if (CRM_Utils_Array::value('is_reserved', $this->_defaults)) { | |
116 | $reserved->freeze(); | |
117 | $disabled = array('disabled' => TRUE); | |
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 -')) + $this->_fields, FALSE, $disabled | |
129 | ); | |
130 | $this->add('text', "length_$count", ts('Length'), $attributes); | |
131 | $this->add('text', "weight_$count", ts('Weight'), $attributes); | |
132 | } | |
133 | ||
134 | $this->add('text', 'threshold', ts("Weight Threshold to Consider Contacts 'Matching':"), $attributes); | |
135 | $this->addButtons(array( | |
136 | array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE), | |
137 | array('type' => 'cancel', 'name' => ts('Cancel')), | |
138 | )); | |
139 | ||
140 | $this->assign('contact_type', $this->_contactType); | |
141 | ||
142 | $this->addFormRule(array('CRM_Contact_Form_DedupeRules', 'formRule'), $this); | |
143 | } | |
144 | ||
145 | /** | |
146 | * global validation rules for the form | |
147 | * | |
148 | * @param array $fields posted values of the form | |
149 | * | |
150 | * @return array list of errors to be posted back to the form | |
151 | * @static | |
152 | * @access public | |
153 | */ | |
154 | static function formRule($fields, $files, $self) { | |
155 | $errors = array(); | |
156 | if (CRM_Utils_Array::value('is_reserved', $fields)) { | |
157 | return TRUE; | |
158 | } | |
159 | ||
160 | $fieldSelected = FALSE; | |
161 | for ($count = 0; $count < self::RULES_COUNT; $count++) { | |
162 | if (CRM_Utils_Array::value("where_$count", $fields)) { | |
163 | $fieldSelected = TRUE; | |
164 | break; | |
165 | } | |
166 | } | |
167 | ||
168 | if (!$fieldSelected) { | |
169 | $errors['_qf_default'] = ts('Please select at least one field.'); | |
170 | } | |
171 | ||
172 | return empty($errors) ? TRUE : $errors; | |
173 | } | |
174 | ||
175 | function setDefaultValues() { | |
176 | return $this->_defaults; | |
177 | } | |
178 | ||
179 | /** | |
180 | * Function to process the form | |
181 | * | |
182 | * @access public | |
183 | * | |
184 | * @return None | |
185 | */ | |
186 | public function postProcess() { | |
187 | $values = $this->exportValues(); | |
188 | ||
189 | //FIXME: Handle logic to replace is_default column by usage | |
190 | $used = CRM_Utils_Array::value('used', $values, FALSE); | |
191 | // reset used column to General (since there can only | |
192 | // be one 'Supervised' or 'Unsupervised' rule) | |
193 | if ($this->_options[$used] != 'General') { | |
194 | $query = " | |
195 | UPDATE civicrm_dedupe_rule_group | |
196 | SET used = 'General' | |
197 | WHERE contact_type = %1 | |
198 | AND used = %2"; | |
199 | $queryParams = array(1 => array($this->_contactType, 'String'), | |
200 | 2 => array($this->_options[$used], 'String'), | |
201 | ); | |
202 | ||
203 | CRM_Core_DAO::executeQuery($query, $queryParams); | |
204 | } | |
205 | ||
206 | $rgDao = new CRM_Dedupe_DAO_RuleGroup(); | |
207 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
208 | $rgDao->id = $this->_rgid; | |
209 | } | |
210 | ||
211 | $rgDao->title = $values['title']; | |
212 | $rgDao->is_reserved = CRM_Utils_Array::value('is_reserved', $values, FALSE); | |
213 | $rgDao->used = $this->_options[$values['used']]; | |
214 | $rgDao->contact_type = $this->_contactType; | |
215 | $rgDao->threshold = $values['threshold']; | |
216 | $rgDao->save(); | |
217 | ||
218 | // make sure name is set only during insert | |
219 | if ($this->_action & CRM_Core_Action::ADD) { | |
220 | // generate name based on title | |
221 | $rgDao->name = CRM_Utils_String::titleToVar($values['title']) . "_{$rgDao->id}"; | |
222 | $rgDao->save(); | |
223 | } | |
224 | ||
225 | // lets skip updating of fields for reserved dedupe group | |
226 | if ($rgDao->is_reserved) { | |
227 | CRM_Core_Session::setStatus(ts("The rule '%1' has been saved.", array(1 => $rgDao->title)), ts('Saved'), 'success'); | |
228 | return; | |
229 | } | |
230 | ||
231 | $ruleDao = new CRM_Dedupe_DAO_Rule(); | |
232 | $ruleDao->dedupe_rule_group_id = $rgDao->id; | |
233 | $ruleDao->delete(); | |
234 | $ruleDao->free(); | |
235 | ||
236 | $substrLenghts = array(); | |
237 | ||
238 | $tables = array(); | |
239 | for ($count = 0; $count < self::RULES_COUNT; $count++) { | |
240 | if (!CRM_Utils_Array::value("where_$count", $values)) { | |
241 | continue; | |
242 | } | |
243 | list($table, $field) = explode('.', CRM_Utils_Array::value("where_$count", $values)); | |
244 | $length = CRM_Utils_Array::value("length_$count", $values) ? CRM_Utils_Array::value("length_$count", $values) : NULL; | |
245 | $weight = $values["weight_$count"]; | |
246 | if ($table and $field) { | |
247 | $ruleDao = new CRM_Dedupe_DAO_Rule(); | |
248 | $ruleDao->dedupe_rule_group_id = $rgDao->id; | |
249 | $ruleDao->rule_table = $table; | |
250 | $ruleDao->rule_field = $field; | |
251 | $ruleDao->rule_length = $length; | |
252 | $ruleDao->rule_weight = $weight; | |
253 | $ruleDao->save(); | |
254 | $ruleDao->free(); | |
255 | ||
256 | if (!array_key_exists($table, $tables)) { | |
257 | $tables[$table] = array(); | |
258 | } | |
259 | $tables[$table][] = $field; | |
260 | } | |
261 | ||
262 | // CRM-6245: we must pass table/field/length triples to the createIndexes() call below | |
263 | if ($length) { | |
264 | if (!isset($substrLenghts[$table])) { | |
265 | $substrLenghts[$table] = array(); | |
266 | } | |
267 | $substrLenghts[$table][$field] = $length; | |
268 | } | |
269 | } | |
270 | ||
271 | // also create an index for this dedupe rule | |
272 | // CRM-3837 | |
273 | CRM_Core_BAO_SchemaHandler::createIndexes($tables, 'dedupe_index', $substrLenghts); | |
274 | ||
275 | //need to clear cache of deduped contacts | |
276 | //based on the previous rule | |
277 | $cacheKey = "merge {$this->_contactType}_{$this->_rgid}_%"; | |
278 | ||
279 | CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey); | |
280 | ||
281 | CRM_Core_Session::setStatus(ts("The rule '%1' has been saved.", array(1 => $rgDao->title)), ts('Saved'), 'success'); | |
282 | } | |
283 | } | |
284 |