Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
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 | */ | |
40 | class CRM_Dedupe_BAO_Rule extends CRM_Dedupe_DAO_Rule { | |
41 | ||
42 | /** | |
100fef9d | 43 | * Ids of the contacts to limit the SQL queries (whole-database queries otherwise) |
6a488035 TO |
44 | */ |
45 | var $contactIds = array(); | |
46 | ||
47 | /** | |
100fef9d | 48 | * Params to dedupe against (queries against the whole contact set otherwise) |
6a488035 TO |
49 | */ |
50 | var $params = array(); | |
51 | ||
52 | /** | |
53 | * Return the SQL query for the given rule - either for finding matching | |
54 | * pairs of contacts, or for matching against the $params variable (if set). | |
55 | * | |
a6c01b45 CW |
56 | * @return string |
57 | * SQL query performing the search | |
6a488035 | 58 | */ |
00be9182 | 59 | public function sql() { |
6a488035 TO |
60 | if ($this->params && |
61 | (!array_key_exists($this->rule_table, $this->params) || | |
62 | !array_key_exists($this->rule_field, $this->params[$this->rule_table]) | |
63 | ) | |
64 | ) { | |
65 | // if params is present and doesn't have an entry for a field, don't construct the clause. | |
66 | return NULL; | |
67 | } | |
68 | ||
69 | // we need to initialise WHERE, ON and USING here, as some table types | |
70 | // extend them; $where is an array of required conditions, $on and | |
71 | // $using are arrays of required field matchings (for substring and | |
72 | // full matches, respectively) | |
73 | $where = array(); | |
353ffa53 | 74 | $on = array("SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR(t2.{$this->rule_field}, 1, {$this->rule_length})"); |
6a488035 TO |
75 | $using = array($this->rule_field); |
76 | ||
77 | switch ($this->rule_table) { | |
78 | case 'civicrm_contact': | |
79 | $id = 'id'; | |
80 | //we should restrict by contact type in the first step | |
81 | $sql = "SELECT contact_type FROM civicrm_dedupe_rule_group WHERE id = {$this->dedupe_rule_group_id};"; | |
82 | $ct = CRM_Core_DAO::singleValueQuery($sql); | |
83 | if ($this->params) { | |
84 | $where[] = "t1.contact_type = '{$ct}'"; | |
85 | } | |
86 | else { | |
87 | $where[] = "t1.contact_type = '{$ct}'"; | |
88 | $where[] = "t2.contact_type = '{$ct}'"; | |
89 | } | |
90 | break; | |
91 | ||
92 | case 'civicrm_address': | |
353ffa53 TO |
93 | $id = 'contact_id'; |
94 | $on[] = 't1.location_type_id = t2.location_type_id'; | |
6a488035 TO |
95 | $using[] = 'location_type_id'; |
96 | if ($this->params['civicrm_address']['location_type_id']) { | |
97 | $locTypeId = CRM_Utils_Type::escape($this->params['civicrm_address']['location_type_id'], 'Integer', FALSE); | |
98 | if ($locTypeId) { | |
99 | $where[] = "t1.location_type_id = $locTypeId"; | |
100 | } | |
101 | } | |
102 | break; | |
103 | ||
104 | case 'civicrm_email': | |
105 | case 'civicrm_im': | |
106 | case 'civicrm_openid': | |
107 | case 'civicrm_phone': | |
108 | $id = 'contact_id'; | |
109 | break; | |
110 | ||
111 | case 'civicrm_note': | |
112 | $id = 'entity_id'; | |
113 | if ($this->params) { | |
114 | $where[] = "t1.entity_table = 'civicrm_contact'"; | |
115 | } | |
116 | else { | |
117 | $where[] = "t1.entity_table = 'civicrm_contact'"; | |
118 | $where[] = "t2.entity_table = 'civicrm_contact'"; | |
119 | } | |
120 | break; | |
121 | ||
122 | default: | |
123 | // custom data tables | |
124 | if (preg_match('/^civicrm_value_/', $this->rule_table) || preg_match('/^custom_value_/', $this->rule_table)) { | |
125 | $id = 'entity_id'; | |
126 | } | |
127 | else { | |
128 | CRM_Core_Error::fatal("Unsupported rule_table for civicrm_dedupe_rule.id of {$this->id}"); | |
129 | } | |
130 | break; | |
131 | } | |
132 | ||
133 | // build SELECT based on the field names containing contact ids | |
134 | // if there are params provided, id1 should be 0 | |
135 | if ($this->params) { | |
136 | $select = "t1.$id id1, {$this->rule_weight} weight"; | |
0226a9f3 | 137 | $subSelect = 'id1, weight'; |
6a488035 TO |
138 | } |
139 | else { | |
140 | $select = "t1.$id id1, t2.$id id2, {$this->rule_weight} weight"; | |
0226a9f3 | 141 | $subSelect = 'id1, id2, weight'; |
6a488035 TO |
142 | } |
143 | ||
144 | // build FROM (and WHERE, if it's a parametrised search) | |
145 | // based on whether the rule is about substrings or not | |
146 | if ($this->params) { | |
147 | $from = "{$this->rule_table} t1"; | |
148 | $str = 'NULL'; | |
149 | if (isset($this->params[$this->rule_table][$this->rule_field])) { | |
150 | $str = CRM_Utils_Type::escape($this->params[$this->rule_table][$this->rule_field], 'String'); | |
151 | } | |
152 | if ($this->rule_length) { | |
153 | $where[] = "SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR('$str', 1, {$this->rule_length})"; | |
154 | $where[] = "t1.{$this->rule_field} IS NOT NULL"; | |
161bb30b | 155 | $where[] = "t1.{$this->rule_field} <> ''"; |
6a488035 TO |
156 | } |
157 | else { | |
158 | $where[] = "t1.{$this->rule_field} = '$str'"; | |
159 | } | |
160 | } | |
161 | else { | |
162 | if ($this->rule_length) { | |
163 | $from = "{$this->rule_table} t1 JOIN {$this->rule_table} t2 ON (" . implode(' AND ', $on) . ")"; | |
164 | } | |
165 | else { | |
166 | $from = "{$this->rule_table} t1 JOIN {$this->rule_table} t2 USING (" . implode(', ', $using) . ")"; | |
167 | } | |
168 | } | |
169 | ||
170 | // finish building WHERE, also limit the results if requested | |
171 | if (!$this->params) { | |
172 | $where[] = "t1.$id < t2.$id"; | |
173 | $where[] = "t1.{$this->rule_field} IS NOT NULL"; | |
161bb30b | 174 | $where[] = "t1.{$this->rule_field} <> ''"; |
6a488035 | 175 | } |
be61083d | 176 | $query = "SELECT $select FROM $from WHERE " . implode(' AND ', $where); |
6a488035 TO |
177 | if ($this->contactIds) { |
178 | $cids = array(); | |
179 | foreach ($this->contactIds as $cid) { | |
180 | $cids[] = CRM_Utils_Type::escape($cid, 'Integer'); | |
181 | } | |
182 | if (count($cids) == 1) { | |
be61083d | 183 | $query .= " AND (t1.$id = {$cids[0]}) UNION $query AND t2.$id = {$cids[0]}"; |
6a488035 TO |
184 | } |
185 | else { | |
be61083d | 186 | $query .= " AND t1.$id IN (" . implode(',', $cids) . ") |
187 | UNION $query AND t2.$id IN (" . implode(',', $cids) . ")"; | |
6a488035 | 188 | } |
0226a9f3 AH |
189 | // The `weight` is ambiguous in the context of the union; put the whole |
190 | // thing in a subquery. | |
191 | $query = "SELECT $subSelect FROM ($query) subunion"; | |
6a488035 TO |
192 | } |
193 | ||
be61083d | 194 | return $query; |
6a488035 TO |
195 | } |
196 | ||
197 | /** | |
dc195289 | 198 | * find fields related to a rule group. |
6a488035 | 199 | * |
389bcebf | 200 | * @param array $params contains the rule group property to identify rule group |
6a488035 | 201 | * |
72b3a70c CW |
202 | * @return array |
203 | * rule fields array associated to rule group | |
6a488035 | 204 | */ |
00be9182 | 205 | public static function dedupeRuleFields($params) { |
353ffa53 TO |
206 | $rgBao = new CRM_Dedupe_BAO_RuleGroup(); |
207 | $rgBao->used = $params['used']; | |
6a488035 TO |
208 | $rgBao->contact_type = $params['contact_type']; |
209 | $rgBao->find(TRUE); | |
210 | ||
211 | $ruleBao = new CRM_Dedupe_BAO_Rule(); | |
212 | $ruleBao->dedupe_rule_group_id = $rgBao->id; | |
213 | $ruleBao->find(); | |
214 | $ruleFields = array(); | |
215 | while ($ruleBao->fetch()) { | |
216 | $ruleFields[] = $ruleBao->rule_field; | |
217 | } | |
218 | return $ruleFields; | |
219 | } | |
220 | ||
e0ef6999 | 221 | /** |
100fef9d CW |
222 | * @param int $cid |
223 | * @param int $oid | |
e0ef6999 EM |
224 | * |
225 | * @return bool | |
226 | */ | |
00be9182 | 227 | public static function validateContacts($cid, $oid) { |
6a488035 | 228 | if (!$cid || !$oid) { |
389bcebf | 229 | return NULL; |
6a488035 TO |
230 | } |
231 | $exception = new CRM_Dedupe_DAO_Exception(); | |
232 | $exception->contact_id1 = $cid; | |
233 | $exception->contact_id2 = $oid; | |
234 | //make sure contact2 > contact1. | |
235 | if ($cid > $oid) { | |
236 | $exception->contact_id1 = $oid; | |
237 | $exception->contact_id2 = $cid; | |
238 | } | |
239 | ||
240 | return $exception->find(TRUE) ? FALSE : TRUE; | |
241 | } | |
96025800 | 242 | |
6a488035 | 243 | } |