Merge pull request #17746 from colemanw/apiExpHaving
[civicrm-core.git] / CRM / Dedupe / BAO / Rule.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 * $Id$
17 *
18 */
19
20/**
21 * The CiviCRM duplicate discovery engine is based on an
22 * algorithm designed by David Strauss <david@fourkitchens.com>.
23 */
24class CRM_Dedupe_BAO_Rule extends CRM_Dedupe_DAO_Rule {
25
26 /**
100fef9d 27 * Ids of the contacts to limit the SQL queries (whole-database queries otherwise)
518fa0ee 28 * @var array
6a488035 29 */
518fa0ee 30 public $contactIds = [];
6a488035
TO
31
32 /**
100fef9d 33 * Params to dedupe against (queries against the whole contact set otherwise)
518fa0ee 34 * @var array
6a488035 35 */
518fa0ee 36 public $params = [];
6a488035
TO
37
38 /**
39 * Return the SQL query for the given rule - either for finding matching
40 * pairs of contacts, or for matching against the $params variable (if set).
41 *
a6c01b45
CW
42 * @return string
43 * SQL query performing the search
2cbe6e87 44 *
45 * @throws \CRM_Core_Exception
46 * @throws \CiviCRM_API3_Exception
6a488035 47 */
00be9182 48 public function sql() {
6a488035
TO
49 if ($this->params &&
50 (!array_key_exists($this->rule_table, $this->params) ||
51 !array_key_exists($this->rule_field, $this->params[$this->rule_table])
52 )
53 ) {
54 // if params is present and doesn't have an entry for a field, don't construct the clause.
55 return NULL;
56 }
57
58 // we need to initialise WHERE, ON and USING here, as some table types
59 // extend them; $where is an array of required conditions, $on and
60 // $using are arrays of required field matchings (for substring and
61 // full matches, respectively)
be2fb01f
CW
62 $where = [];
63 $on = ["SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR(t2.{$this->rule_field}, 1, {$this->rule_length})"];
7965a7f1
PN
64
65 $innerJoinClauses = [
66 "t1.{$this->rule_field} IS NOT NULL",
67 "t2.{$this->rule_field} IS NOT NULL",
518fa0ee 68 "t1.{$this->rule_field} = t2.{$this->rule_field}",
7965a7f1 69 ];
692e041d 70 if ($this->getFieldType($this->rule_field) === CRM_Utils_Type::T_DATE) {
7965a7f1
PN
71 $innerJoinClauses[] = "t1.{$this->rule_field} > '1000-01-01'";
72 $innerJoinClauses[] = "t2.{$this->rule_field} > '1000-01-01'";
73 }
74 else {
75 $innerJoinClauses[] = "t1.{$this->rule_field} <> ''";
76 $innerJoinClauses[] = "t2.{$this->rule_field} <> ''";
77 }
6a488035
TO
78
79 switch ($this->rule_table) {
80 case 'civicrm_contact':
81 $id = 'id';
82 //we should restrict by contact type in the first step
83 $sql = "SELECT contact_type FROM civicrm_dedupe_rule_group WHERE id = {$this->dedupe_rule_group_id};";
84 $ct = CRM_Core_DAO::singleValueQuery($sql);
85 if ($this->params) {
86 $where[] = "t1.contact_type = '{$ct}'";
87 }
88 else {
89 $where[] = "t1.contact_type = '{$ct}'";
90 $where[] = "t2.contact_type = '{$ct}'";
91 }
92 break;
93
94 case 'civicrm_address':
6a488035
TO
95 case 'civicrm_email':
96 case 'civicrm_im':
97 case 'civicrm_openid':
98 case 'civicrm_phone':
99 $id = 'contact_id';
100 break;
101
102 case 'civicrm_note':
103 $id = 'entity_id';
104 if ($this->params) {
105 $where[] = "t1.entity_table = 'civicrm_contact'";
106 }
107 else {
108 $where[] = "t1.entity_table = 'civicrm_contact'";
109 $where[] = "t2.entity_table = 'civicrm_contact'";
110 }
111 break;
112
113 default:
114 // custom data tables
115 if (preg_match('/^civicrm_value_/', $this->rule_table) || preg_match('/^custom_value_/', $this->rule_table)) {
116 $id = 'entity_id';
117 }
118 else {
2cbe6e87 119 throw new CRM_Core_Exception("Unsupported rule_table for civicrm_dedupe_rule.id of {$this->id}");
6a488035
TO
120 }
121 break;
122 }
123
124 // build SELECT based on the field names containing contact ids
125 // if there are params provided, id1 should be 0
126 if ($this->params) {
127 $select = "t1.$id id1, {$this->rule_weight} weight";
0226a9f3 128 $subSelect = 'id1, weight';
6a488035
TO
129 }
130 else {
131 $select = "t1.$id id1, t2.$id id2, {$this->rule_weight} weight";
0226a9f3 132 $subSelect = 'id1, id2, weight';
6a488035
TO
133 }
134
135 // build FROM (and WHERE, if it's a parametrised search)
136 // based on whether the rule is about substrings or not
137 if ($this->params) {
138 $from = "{$this->rule_table} t1";
139 $str = 'NULL';
140 if (isset($this->params[$this->rule_table][$this->rule_field])) {
141 $str = CRM_Utils_Type::escape($this->params[$this->rule_table][$this->rule_field], 'String');
142 }
143 if ($this->rule_length) {
144 $where[] = "SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR('$str', 1, {$this->rule_length})";
145 $where[] = "t1.{$this->rule_field} IS NOT NULL";
146 }
147 else {
148 $where[] = "t1.{$this->rule_field} = '$str'";
149 }
150 }
151 else {
152 if ($this->rule_length) {
153 $from = "{$this->rule_table} t1 JOIN {$this->rule_table} t2 ON (" . implode(' AND ', $on) . ")";
154 }
155 else {
7965a7f1 156 $from = "{$this->rule_table} t1 INNER JOIN {$this->rule_table} t2 ON (" . implode(' AND ', $innerJoinClauses) . ")";
6a488035
TO
157 }
158 }
159
160 // finish building WHERE, also limit the results if requested
161 if (!$this->params) {
162 $where[] = "t1.$id < t2.$id";
6a488035 163 }
be61083d 164 $query = "SELECT $select FROM $from WHERE " . implode(' AND ', $where);
6a488035 165 if ($this->contactIds) {
be2fb01f 166 $cids = [];
6a488035
TO
167 foreach ($this->contactIds as $cid) {
168 $cids[] = CRM_Utils_Type::escape($cid, 'Integer');
169 }
170 if (count($cids) == 1) {
be61083d 171 $query .= " AND (t1.$id = {$cids[0]}) UNION $query AND t2.$id = {$cids[0]}";
6a488035
TO
172 }
173 else {
be61083d 174 $query .= " AND t1.$id IN (" . implode(',', $cids) . ")
175 UNION $query AND t2.$id IN (" . implode(',', $cids) . ")";
6a488035 176 }
0226a9f3
AH
177 // The `weight` is ambiguous in the context of the union; put the whole
178 // thing in a subquery.
179 $query = "SELECT $subSelect FROM ($query) subunion";
6a488035
TO
180 }
181
be61083d 182 return $query;
6a488035
TO
183 }
184
185 /**
dc195289 186 * find fields related to a rule group.
6a488035 187 *
389bcebf 188 * @param array $params contains the rule group property to identify rule group
6a488035 189 *
72b3a70c
CW
190 * @return array
191 * rule fields array associated to rule group
6a488035 192 */
00be9182 193 public static function dedupeRuleFields($params) {
353ffa53
TO
194 $rgBao = new CRM_Dedupe_BAO_RuleGroup();
195 $rgBao->used = $params['used'];
6a488035
TO
196 $rgBao->contact_type = $params['contact_type'];
197 $rgBao->find(TRUE);
198
199 $ruleBao = new CRM_Dedupe_BAO_Rule();
200 $ruleBao->dedupe_rule_group_id = $rgBao->id;
201 $ruleBao->find();
be2fb01f 202 $ruleFields = [];
6a488035 203 while ($ruleBao->fetch()) {
0ca748fb
JM
204 $field_name = $ruleBao->rule_field;
205 if ($field_name == 'phone_numeric') {
206 $field_name = 'phone';
207 }
208 $ruleFields[] = $field_name;
6a488035
TO
209 }
210 return $ruleFields;
211 }
212
e0ef6999 213 /**
100fef9d
CW
214 * @param int $cid
215 * @param int $oid
e0ef6999
EM
216 *
217 * @return bool
218 */
00be9182 219 public static function validateContacts($cid, $oid) {
6a488035 220 if (!$cid || !$oid) {
389bcebf 221 return NULL;
6a488035
TO
222 }
223 $exception = new CRM_Dedupe_DAO_Exception();
224 $exception->contact_id1 = $cid;
225 $exception->contact_id2 = $oid;
226 //make sure contact2 > contact1.
227 if ($cid > $oid) {
228 $exception->contact_id1 = $oid;
229 $exception->contact_id2 = $cid;
230 }
231
91768280 232 return !$exception->find(TRUE);
6a488035 233 }
96025800 234
692e041d 235 /**
236 * Get the specification for the given field.
237 *
238 * @param string $fieldName
239 *
240 * @return array
241 * @throws \CiviCRM_API3_Exception
242 */
243 public function getFieldType($fieldName) {
244 $entity = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($this->rule_table));
245 if (!$entity) {
246 // This means we have stored a custom field rather than an entity name in rule_table, figure out the entity.
247 $entity = civicrm_api3('CustomGroup', 'getvalue', ['table_name' => $this->rule_table, 'return' => 'extends']);
248 if (in_array($entity, ['Individual', 'Household', 'Organization'])) {
249 $entity = 'Contact';
250 }
251 $fieldName = 'custom_' . civicrm_api3('CustomField', 'getvalue', ['column_name' => $fieldName, 'return' => 'id']);
252 }
253 $fields = civicrm_api3($entity, 'getfields', ['action' => 'create'])['values'];
254 return $fields[$fieldName]['type'];
255 }
256
6a488035 257}