Merge pull request #13615 from civicrm/5.11
[civicrm-core.git] / CRM / Dedupe / BAO / Rule.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
31 * @copyright CiviCRM LLC (c) 2004-2019
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 /**
43 * Ids of the contacts to limit the SQL queries (whole-database queries otherwise)
44 */
45 var $contactIds = array();
46
47 /**
48 * Params to dedupe against (queries against the whole contact set otherwise)
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
57 * SQL query performing the search
58 */
59 public function sql() {
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();
74 $on = array("SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR(t2.{$this->rule_field}, 1, {$this->rule_length})");
75 $entity = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($this->rule_table));
76 $fields = civicrm_api3($entity, 'getfields', ['action' => 'create'])['values'];
77
78 $innerJoinClauses = [
79 "t1.{$this->rule_field} IS NOT NULL",
80 "t2.{$this->rule_field} IS NOT NULL",
81 "t1.{$this->rule_field} = t2.{$this->rule_field}"
82 ];
83 if ($fields[$this->rule_field]['type'] === CRM_Utils_Type::T_DATE) {
84 $innerJoinClauses[] = "t1.{$this->rule_field} > '1000-01-01'";
85 $innerJoinClauses[] = "t2.{$this->rule_field} > '1000-01-01'";
86 }
87 else {
88 $innerJoinClauses[] = "t1.{$this->rule_field} <> ''";
89 $innerJoinClauses[] = "t2.{$this->rule_field} <> ''";
90 }
91
92 switch ($this->rule_table) {
93 case 'civicrm_contact':
94 $id = 'id';
95 //we should restrict by contact type in the first step
96 $sql = "SELECT contact_type FROM civicrm_dedupe_rule_group WHERE id = {$this->dedupe_rule_group_id};";
97 $ct = CRM_Core_DAO::singleValueQuery($sql);
98 if ($this->params) {
99 $where[] = "t1.contact_type = '{$ct}'";
100 }
101 else {
102 $where[] = "t1.contact_type = '{$ct}'";
103 $where[] = "t2.contact_type = '{$ct}'";
104 }
105 break;
106
107 case 'civicrm_address':
108 $id = 'contact_id';
109 $on[] = 't1.location_type_id = t2.location_type_id';
110 $innerJoinClauses[] = ['t1.location_type_id = t2.location_type_id'];
111 if ($this->params['civicrm_address']['location_type_id']) {
112 $locTypeId = CRM_Utils_Type::escape($this->params['civicrm_address']['location_type_id'], 'Integer', FALSE);
113 if ($locTypeId) {
114 $where[] = "t1.location_type_id = $locTypeId";
115 }
116 }
117 break;
118
119 case 'civicrm_email':
120 case 'civicrm_im':
121 case 'civicrm_openid':
122 case 'civicrm_phone':
123 $id = 'contact_id';
124 break;
125
126 case 'civicrm_note':
127 $id = 'entity_id';
128 if ($this->params) {
129 $where[] = "t1.entity_table = 'civicrm_contact'";
130 }
131 else {
132 $where[] = "t1.entity_table = 'civicrm_contact'";
133 $where[] = "t2.entity_table = 'civicrm_contact'";
134 }
135 break;
136
137 default:
138 // custom data tables
139 if (preg_match('/^civicrm_value_/', $this->rule_table) || preg_match('/^custom_value_/', $this->rule_table)) {
140 $id = 'entity_id';
141 }
142 else {
143 CRM_Core_Error::fatal("Unsupported rule_table for civicrm_dedupe_rule.id of {$this->id}");
144 }
145 break;
146 }
147
148 // build SELECT based on the field names containing contact ids
149 // if there are params provided, id1 should be 0
150 if ($this->params) {
151 $select = "t1.$id id1, {$this->rule_weight} weight";
152 $subSelect = 'id1, weight';
153 }
154 else {
155 $select = "t1.$id id1, t2.$id id2, {$this->rule_weight} weight";
156 $subSelect = 'id1, id2, weight';
157 }
158
159 // build FROM (and WHERE, if it's a parametrised search)
160 // based on whether the rule is about substrings or not
161 if ($this->params) {
162 $from = "{$this->rule_table} t1";
163 $str = 'NULL';
164 if (isset($this->params[$this->rule_table][$this->rule_field])) {
165 $str = CRM_Utils_Type::escape($this->params[$this->rule_table][$this->rule_field], 'String');
166 }
167 if ($this->rule_length) {
168 $where[] = "SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR('$str', 1, {$this->rule_length})";
169 $where[] = "t1.{$this->rule_field} IS NOT NULL";
170 }
171 else {
172 $where[] = "t1.{$this->rule_field} = '$str'";
173 }
174 }
175 else {
176 if ($this->rule_length) {
177 $from = "{$this->rule_table} t1 JOIN {$this->rule_table} t2 ON (" . implode(' AND ', $on) . ")";
178 }
179 else {
180 $from = "{$this->rule_table} t1 INNER JOIN {$this->rule_table} t2 ON (" . implode(' AND ', $innerJoinClauses) . ")";
181 }
182 }
183
184 // finish building WHERE, also limit the results if requested
185 if (!$this->params) {
186 $where[] = "t1.$id < t2.$id";
187 }
188 $query = "SELECT $select FROM $from WHERE " . implode(' AND ', $where);
189 if ($this->contactIds) {
190 $cids = array();
191 foreach ($this->contactIds as $cid) {
192 $cids[] = CRM_Utils_Type::escape($cid, 'Integer');
193 }
194 if (count($cids) == 1) {
195 $query .= " AND (t1.$id = {$cids[0]}) UNION $query AND t2.$id = {$cids[0]}";
196 }
197 else {
198 $query .= " AND t1.$id IN (" . implode(',', $cids) . ")
199 UNION $query AND t2.$id IN (" . implode(',', $cids) . ")";
200 }
201 // The `weight` is ambiguous in the context of the union; put the whole
202 // thing in a subquery.
203 $query = "SELECT $subSelect FROM ($query) subunion";
204 }
205
206 return $query;
207 }
208
209 /**
210 * find fields related to a rule group.
211 *
212 * @param array $params contains the rule group property to identify rule group
213 *
214 * @return array
215 * rule fields array associated to rule group
216 */
217 public static function dedupeRuleFields($params) {
218 $rgBao = new CRM_Dedupe_BAO_RuleGroup();
219 $rgBao->used = $params['used'];
220 $rgBao->contact_type = $params['contact_type'];
221 $rgBao->find(TRUE);
222
223 $ruleBao = new CRM_Dedupe_BAO_Rule();
224 $ruleBao->dedupe_rule_group_id = $rgBao->id;
225 $ruleBao->find();
226 $ruleFields = array();
227 while ($ruleBao->fetch()) {
228 $ruleFields[] = $ruleBao->rule_field;
229 }
230 return $ruleFields;
231 }
232
233 /**
234 * @param int $cid
235 * @param int $oid
236 *
237 * @return bool
238 */
239 public static function validateContacts($cid, $oid) {
240 if (!$cid || !$oid) {
241 return NULL;
242 }
243 $exception = new CRM_Dedupe_DAO_Exception();
244 $exception->contact_id1 = $cid;
245 $exception->contact_id2 = $oid;
246 //make sure contact2 > contact1.
247 if ($cid > $oid) {
248 $exception->contact_id1 = $oid;
249 $exception->contact_id2 = $cid;
250 }
251
252 return $exception->find(TRUE) ? FALSE : TRUE;
253 }
254
255 }