Merge pull request #534 from yashodha/CRM-12410
[civicrm-core.git] / CRM / Dedupe / Finder.php
CommitLineData
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 * The CiviCRM duplicate discovery engine is based on an
38 * algorithm designed by David Strauss <david@fourkitchens.com>.
39 */
40class CRM_Dedupe_Finder {
41
42 /**
43 * Return a contact_id-keyed array of arrays of possible dupes
44 * (of the key contact_id) - limited to dupes of $cids if provided.
45 *
46 * @param int $rgid rule group id
47 * @param array $cids contact ids to limit the search to
48 *
49 * @return array array of (cid1, cid2, weight) dupe triples
50 */
51 static function dupes($rgid, $cids = array(
52 )) {
53 $rgBao = new CRM_Dedupe_BAO_RuleGroup();
54 $rgBao->id = $rgid;
55 $rgBao->contactIds = $cids;
56 if (!$rgBao->find(TRUE)) {
57 //FIXME:
58 CRM_Core_Error::fatal("$level rule for $ctype does not exist");
59 }
60
61 $rgBao->fillTable();
62 $dao = new CRM_Core_DAO();
63 $dao->query($rgBao->thresholdQuery());
64 $dupes = array();
65 while ($dao->fetch()) {
66 $dupes[] = array($dao->id1, $dao->id2, $dao->weight);
67 }
68 $dao->query($rgBao->tableDropQuery());
69
70 return $dupes;
71 }
72
73 /**
74 * Return an array of possible dupes, based on the provided array of
75 * params, using the default rule group for the given contact type and
76 * usage.
77 *
78 * check_permission is a boolean flag to indicate if permission should be considered.
79 * default is to always check permissioning but public pages for example might not want
80 * permission to be checked for anonymous users. Refer CRM-6211. We might be beaking
81 * Multi-Site dedupe for public pages.
82 *
83 * @param array $params array of params of the form $params[$table][$field] == $value
84 * @param string $ctype contact type to match against
85 * @param string $used dedupe rule group usage ('Unsupervised' or 'Supervised' or 'General')
86 * @param array $except array of contacts that shouldn't be considered dupes
87 * @param int $ruleGroupID the id of the dedupe rule we should be using
88 *
89 * @return array matching contact ids
90 */
91 static function dupesByParams($params,
92 $ctype,
93 $used = 'Unsupervised',
94 $except = array(),
95 $ruleGroupID = NULL
96 ) {
97 // If $params is empty there is zero reason to proceed.
98 if (!$params) {
99 return array();
100 }
101
102 $foundByID = FALSE;
103 if ($ruleGroupID) {
104 $rgBao = new CRM_Dedupe_BAO_RuleGroup();
105 $rgBao->id = $ruleGroupID;
106 $rgBao->contact_type = $ctype;
107 if ($rgBao->find(TRUE)) {
108 $foundByID = TRUE;
109 }
110 }
111
112 if (!$foundByID) {
113 $rgBao = new CRM_Dedupe_BAO_RuleGroup();
114 $rgBao->contact_type = $ctype;
115 $rgBao->used = $used;
116 if (!$rgBao->find(TRUE)) {
117 CRM_Core_Error::fatal("$used rule for $ctype does not exist");
118 }
119 }
120 $params['check_permission'] = CRM_Utils_Array::value('check_permission', $params, TRUE);
121
122 $rgBao->params = $params;
123 $rgBao->fillTable();
124 $dao = new CRM_Core_DAO();
125 $dao->query($rgBao->thresholdQuery($params['check_permission']));
126 $dupes = array();
127 while ($dao->fetch()) {
128 if (isset($dao->id) && $dao->id) {
129 $dupes[] = $dao->id;
130 }
131 }
132 $dao->query($rgBao->tableDropQuery());
133 return array_diff($dupes, $except);
134 }
135
136 /**
137 * Return a contact_id-keyed array of arrays of possible dupes in the given group.
138 *
139 * @param int $rgid rule group id
140 * @param int $gid contact group id (currently, works only with non-smart groups)
141 *
142 * @return array array of (cid1, cid2, weight) dupe triples
143 */
144 static function dupesInGroup($rgid, $gid) {
145 $cids = array_keys(CRM_Contact_BAO_Group::getMember($gid));
146 if ( !empty($cids) ) {
147 return self::dupes($rgid, $cids);
148 }
149 return array();
150 }
151
152 /**
153 * Return dupes of a given contact, using the default rule group (of a provided usage).
154 *
155 * @param int $cid contact id of the given contact
156 * @param string $used dedupe rule group usage ('Unsupervised' or 'Supervised' or 'General')
157 * @param string $ctype contact type of the given contact
158 *
159 * @return array array of dupe contact_ids
160 */
161 static function dupesOfContact($cid, $used = 'Unsupervised', $ctype = NULL) {
162 // if not provided, fetch the contact type from the database
163 if (!$ctype) {
164 $dao = new CRM_Contact_DAO_Contact();
165 $dao->id = $cid;
166 if (!$dao->find(TRUE)) {
167 CRM_Core_Error::fatal("contact id of $cid does not exist");
168 }
169 $ctype = $dao->contact_type;
170 }
171 $rgBao = new CRM_Dedupe_BAO_RuleGroup();
172 $rgBao->used = $used;
173 $rgBao->contact_type = $ctype;
174 if (!$rgBao->find(TRUE)) {
175 CRM_Core_Error::fatal("$used rule for $ctype does not exist");
176 }
177 $dupes = self::dupes($rgBao->id, array($cid));
178
179 // get the dupes for this cid
180 $result = array();
181 foreach ($dupes as $dupe) {
182 if ($dupe[0] == $cid) {
183 $result[] = $dupe[1];
184 }
185 elseif ($dupe[1] == $cid) {
186 $result[] = $dupe[0];
187 }
188 }
189 return $result;
190 }
191
192 /**
193 * A hackish function needed to massage CRM_Contact_Form_$ctype::formRule()
194 * object into a valid $params array for dedupe
195 *
196 * @param array $fields contact structure from formRule()
197 * @param string $ctype contact type of the given contact
198 *
199 * @return array valid $params array for dedupe
200 */
201 static function formatParams($fields, $ctype) {
202 $flat = array();
203 CRM_Utils_Array::flatten($fields, $flat);
204
205 $replace_these = array(
206 'individual_prefix' => 'prefix_id',
207 'individual_suffix' => 'suffix_id',
208 'gender' => 'gender_id',
209 );
210 //handle for individual_suffix, individual_prefix, gender
211 foreach (array(
212 'individual_suffix', 'individual_prefix', 'gender') as $name) {
213 if (CRM_Utils_Array::value($name, $fields)) {
214 $flat[$replace_these[$name]] = $flat[$name];
215 unset($flat[$name]);
216 }
217 }
218
219 // handle {birth,deceased}_date
220 foreach (array(
221 'birth_date', 'deceased_date') as $date) {
222 if (CRM_Utils_Array::value($date, $fields)) {
223 $flat[$date] = $fields[$date];
224 if (is_array($flat[$date])) {
225 $flat[$date] = CRM_Utils_Date::format($flat[$date]);
226 }
227 $flat[$date] = CRM_Utils_Date::processDate($flat[$date]);
228 }
229 }
230
231 if (CRM_Utils_Array::value('contact_source', $flat)) {
232 $flat['source'] = $flat['contact_source'];
233 unset($flat['contact_source']);
234 }
235
236 // handle preferred_communication_method
237 if (array_key_exists('preferred_communication_method', $fields)) {
238 $methods = array_intersect($fields['preferred_communication_method'], array('1'));
239 $methods = array_keys($methods);
240 sort($methods);
241 if ($methods) {
242 $flat['preferred_communication_method'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $methods) . CRM_Core_DAO::VALUE_SEPARATOR;
243 }
244 }
245
246 // handle custom data
247 $tree = CRM_Core_BAO_CustomGroup::getTree($ctype, CRM_Core_DAO::$_nullObject, NULL, -1);
248 CRM_Core_BAO_CustomGroup::postProcess($tree, $fields, TRUE);
249 foreach ($tree as $key => $cg) {
250 if (!is_int($key)) {
251 continue;
252 }
253 foreach ($cg['fields'] as $cf) {
254 $flat[$cf['column_name']] = CRM_Utils_Array::value('data', $cf['customValue']);
255 }
256 }
257
258 // if the key is dotted, keep just the last part of it
259 foreach ($flat as $key => $value) {
260 if (substr_count($key, '.')) {
261 $last = explode('.', $key);
262 $last = array_pop($last);
263 // make sure the first occurence is kept, not the last
264 if (!isset($flat[$last])) {
265 $flat[$last] = $value;
266 }
267 unset($flat[$key]);
268 }
269 }
270
271 // drop the -digit (and -Primary, for CRM-3902) postfixes (so event registration's $flat['email-5'] becomes $flat['email'])
272 // FIXME: CRM-5026 should be fixed here; the below clobbers all address info; we should split off address fields and match
273 // the -digit to civicrm_address.location_type_id and -Primary to civicrm_address.is_primary
274 foreach ($flat as $key => $value) {
275 $matches = array();
276 if (preg_match('/(.*)-(\d+|Primary)$/', $key, $matches)) {
277 $flat[$matches[1]] = $value;
278 unset($flat[$key]);
279 }
280 }
281
282 $params = array();
283 $supportedFields = CRM_Dedupe_BAO_RuleGroup::supportedFields($ctype);
284 if (is_array($supportedFields)) {
285 foreach ($supportedFields as $table => $fields) {
286 if ($table == 'civicrm_address') {
287 // for matching on civicrm_address fields, we also need the location_type_id
288 $fields['location_type_id'] = '';
289 // FIXME: we also need to do some hacking for id and name fields, see CRM-3902’s comments
290 $fixes = array(
291 'address_name' => 'name', 'country' => 'country_id',
292 'state_province' => 'state_province_id', 'county' => 'county_id',
293 );
294 foreach ($fixes as $orig => $target) {
295 if (CRM_Utils_Array::value($orig, $flat)) {
296 $params[$table][$target] = $flat[$orig];
297 }
298 }
299 }
300 foreach ($fields as $field => $title) {
301 if (CRM_Utils_Array::value($field, $flat)) {
302 $params[$table][$field] = $flat[$field];
303 }
304 }
305 }
306 }
307 return $params;
308 }
309}
310