Merge pull request #15156 from eileenmcnaughton/dedupe5
[civicrm-core.git] / CRM / Dedupe / BAO / QueryBuilder.php
CommitLineData
6a488035 1<?php
4c6ce474
EM
2/**
3 * Class CRM_Dedupe_BAO_QueryBuilder
4 */
6a488035 5class CRM_Dedupe_BAO_QueryBuilder {
518fa0ee 6
e0ef6999
EM
7 /**
8 * @param $rg
9 * @param string $strID1
10 * @param string $strID2
11 *
12 * @return string
13 */
d58a19a1 14 public static function internalFilters($rg, $strID1 = 'contact1.id', $strID2 = 'contact2.id') {
6a488035
TO
15 // Add a contact id filter for dedupe by group requests and add logic
16 // to remove duplicate results with opposing orders, i.e. 1,2 and 2,1
481a74f4 17 if (!empty($rg->contactIds)) {
d58a19a1 18 $cids = implode(',', $rg->contactIds);
6a488035
TO
19 return "($strID1 IN ($cids) AND ( $strID2 NOT IN ($cids) OR ($strID2 IN ($cids) AND $strID1 < $strID2) ))";
20 }
21 else {
22 return "($strID1 < $strID2)";
23 }
24 }
96025800 25
c232f005 26 /**
27 * If a contact list is specified then adjust the query to ensure one contact is in that list.
28 *
29 * Doing an OR join here will lead to a server-killing unindexed query. However, a union will
30 * perform better.
31 *
32 * @param array $contactList
33 * @param string $query
34 * @param string $strID1
35 * @param string $strID2
36 *
37 * @return string
38 */
39 protected static function filterQueryByContactList(array $contactList, $query, $strID1 = 'contact1.id', $strID2 = 'contact2.id') {
40 if (empty($contactList)) {
41 return $query . " AND ($strID1 < $strID2)";
42 }
43 $contactIDs = implode(',', $contactList);
44 return "$query AND $strID1 IN ($contactIDs) AND $strID1 > $strID2
45 UNION $query AND $strID1 > $strID2 AND $strID2 IN ($contactIDs) AND $strID1 NOT IN ($contactIDs)
46 ";
47
48 }
49
353ffa53 50}