3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * The CiviCRM duplicate discovery engine is based on an
38 * algorithm designed by David Strauss <david@fourkitchens.com>.
40 class CRM_Dedupe_Finder
{
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.
49 * Contact ids to limit the search to.
51 * @return array array of (cid1, cid2, weight) dupe triples
53 public static function dupes($rgid, $cids = array()) {
54 $rgBao = new CRM_Dedupe_BAO_RuleGroup();
56 $rgBao->contactIds
= $cids;
57 if (!$rgBao->find(TRUE)) {
58 CRM_Core_Error
::fatal("Dedupe rule not found for selected contacts");
62 $dao = new CRM_Core_DAO();
63 $dao->query($rgBao->thresholdQuery());
65 while ($dao->fetch()) {
66 $dupes[] = array($dao->id1
, $dao->id2
, $dao->weight
);
68 $dao->query($rgBao->tableDropQuery());
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
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.
83 * @param array $params
84 * Array of params of the form $params[$table][$field] == $value.
85 * @param string $ctype
86 * Contact type to match against.
88 * Dedupe rule group usage ('Unsupervised' or 'Supervised' or 'General').
89 * @param array $except
90 * Array of contacts that shouldn't be considered dupes.
91 * @param int $ruleGroupID
92 * The id of the dedupe rule we should be using.
94 * @return array matching contact ids
96 static function dupesByParams(
99 $used = 'Unsupervised',
103 // If $params is empty there is zero reason to proceed.
110 $rgBao = new CRM_Dedupe_BAO_RuleGroup();
111 $rgBao->id
= $ruleGroupID;
112 $rgBao->contact_type
= $ctype;
113 if ($rgBao->find(TRUE)) {
119 $rgBao = new CRM_Dedupe_BAO_RuleGroup();
120 $rgBao->contact_type
= $ctype;
121 $rgBao->used
= $used;
122 if (!$rgBao->find(TRUE)) {
123 CRM_Core_Error
::fatal("$used rule for $ctype does not exist");
126 $params['check_permission'] = CRM_Utils_Array
::value('check_permission', $params, TRUE);
128 $rgBao->params
= $params;
130 $dao = new CRM_Core_DAO();
131 $dao->query($rgBao->thresholdQuery($params['check_permission']));
133 while ($dao->fetch()) {
134 if (isset($dao->id
) && $dao->id
) {
138 $dao->query($rgBao->tableDropQuery());
139 return array_diff($dupes, $except);
143 * Return a contact_id-keyed array of arrays of possible dupes in the given group.
148 * Contact group id (currently, works only with non-smart groups).
150 * @return array array of (cid1, cid2, weight) dupe triples
152 public static function dupesInGroup($rgid, $gid) {
153 $cids = array_keys(CRM_Contact_BAO_Group
::getMember($gid));
155 return self
::dupes($rgid, $cids);
161 * Return dupes of a given contact, using the default rule group (of a provided usage).
164 * Contact id of the given contact.
165 * @param string $used
166 * Dedupe rule group usage ('Unsupervised' or 'Supervised' or 'General').
167 * @param string $ctype
168 * Contact type of the given contact.
170 * @return array array of dupe contact_ids
172 public static function dupesOfContact($cid, $used = 'Unsupervised', $ctype = NULL) {
173 // if not provided, fetch the contact type from the database
175 $dao = new CRM_Contact_DAO_Contact();
177 if (!$dao->find(TRUE)) {
178 CRM_Core_Error
::fatal("contact id of $cid does not exist");
180 $ctype = $dao->contact_type
;
182 $rgBao = new CRM_Dedupe_BAO_RuleGroup();
183 $rgBao->used
= $used;
184 $rgBao->contact_type
= $ctype;
185 if (!$rgBao->find(TRUE)) {
186 CRM_Core_Error
::fatal("$used rule for $ctype does not exist");
188 $dupes = self
::dupes($rgBao->id
, array($cid));
190 // get the dupes for this cid
192 foreach ($dupes as $dupe) {
193 if ($dupe[0] == $cid) {
194 $result[] = $dupe[1];
196 elseif ($dupe[1] == $cid) {
197 $result[] = $dupe[0];
204 * A hackish function needed to massage CRM_Contact_Form_$ctype::formRule()
205 * object into a valid $params array for dedupe
207 * @param array $fields
208 * Contact structure from formRule().
209 * @param string $ctype
210 * Contact type of the given contact.
212 * @return array valid $params array for dedupe
214 public static function formatParams($fields, $ctype) {
216 CRM_Utils_Array
::flatten($fields, $flat);
218 // FIXME: This may no longer be necessary - check inputs
219 $replace_these = array(
220 'individual_prefix' => 'prefix_id',
221 'individual_suffix' => 'suffix_id',
222 'gender' => 'gender_id',
224 foreach (array('individual_suffix', 'individual_prefix', 'gender') as $name) {
225 if (!empty($fields[$name])) {
226 $flat[$replace_these[$name]] = $flat[$name];
231 // handle {birth,deceased}_date
233 'birth_date', 'deceased_date') as $date) {
234 if (!empty($fields[$date])) {
235 $flat[$date] = $fields[$date];
236 if (is_array($flat[$date])) {
237 $flat[$date] = CRM_Utils_Date
::format($flat[$date]);
239 $flat[$date] = CRM_Utils_Date
::processDate($flat[$date]);
243 if (!empty($flat['contact_source'])) {
244 $flat['source'] = $flat['contact_source'];
245 unset($flat['contact_source']);
248 // handle preferred_communication_method
249 if (!empty($fields['preferred_communication_method'])) {
250 $methods = array_intersect($fields['preferred_communication_method'], array('1'));
251 $methods = array_keys($methods);
254 $flat['preferred_communication_method'] = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $methods) . CRM_Core_DAO
::VALUE_SEPARATOR
;
258 // handle custom data
259 $tree = CRM_Core_BAO_CustomGroup
::getTree($ctype, CRM_Core_DAO
::$_nullObject, NULL, -1);
260 CRM_Core_BAO_CustomGroup
::postProcess($tree, $fields, TRUE);
261 foreach ($tree as $key => $cg) {
265 foreach ($cg['fields'] as $cf) {
266 $flat[$cf['column_name']] = CRM_Utils_Array
::value('data', $cf['customValue']);
270 // if the key is dotted, keep just the last part of it
271 foreach ($flat as $key => $value) {
272 if (substr_count($key, '.')) {
273 $last = explode('.', $key);
274 $last = array_pop($last);
275 // make sure the first occurence is kept, not the last
276 if (!isset($flat[$last])) {
277 $flat[$last] = $value;
283 // drop the -digit (and -Primary, for CRM-3902) postfixes (so event registration's $flat['email-5'] becomes $flat['email'])
284 // FIXME: CRM-5026 should be fixed here; the below clobbers all address info; we should split off address fields and match
285 // the -digit to civicrm_address.location_type_id and -Primary to civicrm_address.is_primary
286 foreach ($flat as $key => $value) {
288 if (preg_match('/(.*)-(\d+|Primary)$/', $key, $matches)) {
289 $flat[$matches[1]] = $value;
295 $supportedFields = CRM_Dedupe_BAO_RuleGroup
::supportedFields($ctype);
296 if (is_array($supportedFields)) {
297 foreach ($supportedFields as $table => $fields) {
298 if ($table == 'civicrm_address') {
299 // for matching on civicrm_address fields, we also need the location_type_id
300 $fields['location_type_id'] = '';
301 // FIXME: we also need to do some hacking for id and name fields, see CRM-3902’s comments
303 'address_name' => 'name',
304 'country' => 'country_id',
305 'state_province' => 'state_province_id',
306 'county' => 'county_id',
308 foreach ($fixes as $orig => $target) {
309 if (!empty($flat[$orig])) {
310 $params[$table][$target] = $flat[$orig];
314 foreach ($fields as $field => $title) {
315 if (!empty($flat[$field])) {
316 $params[$table][$field] = $flat[$field];