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