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