Merge pull request #15595 from eileenmcnaughton/dedupe3
[civicrm-core.git] / api / v3 / Dedupe.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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 * This api exposes CiviCRM dedupe functionality.
30 *
31 * @package CiviCRM_APIv3
32 */
33
34 /**
35 * Get rows for any cached attempted merges on the passed criteria.
36 *
37 * @param array $params
38 *
39 * @return array
40 * @throws \API_Exception
41 */
42 function civicrm_api3_dedupe_get($params) {
43 $sql = CRM_Utils_SQL_Select::fragment();
44 $sql->where(['merge_data_restriction' => "cachekey LIKE 'merge_%'"]);
45
46 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'PrevNextCache', 'get');
47 $result = _civicrm_api3_basic_get('CRM_Core_BAO_PrevNextCache', $params, FALSE, 'PrevNextCache', $sql);
48
49 if ($options['is_count']) {
50 return civicrm_api3_create_success($result, $params, 'PrevNextCache', 'get');
51 }
52 foreach ($result as $index => $values) {
53 if (isset($values['data']) && !empty($values['data'])) {
54 $result[$index]['data'] = CRM_Core_DAO::unSerializeField($values['data'], CRM_Core_DAO::SERIALIZE_PHP);
55 }
56 }
57 return civicrm_api3_create_success($result, $params, 'PrevNextCache');
58 }
59
60 /**
61 * Get rows for getting dedupe cache records.
62 *
63 * @param array $params
64 */
65 function _civicrm_api3_dedupe_get_spec(&$params) {
66 $params = CRM_Core_DAO_PrevNextCache::fields();
67 $params['id']['api.aliases'] = ['dedupe_id'];
68 }
69
70 /**
71 * Delete rows for any cached attempted merges on the passed criteria.
72 *
73 * @param array $params
74 *
75 * @return array
76 *
77 * @throws \API_Exception
78 * @throws \Civi\API\Exception\UnauthorizedException
79 */
80 function civicrm_api3_dedupe_delete($params) {
81 return _civicrm_api3_basic_delete('CRM_Core_BAO_PrevNextCache', $params);
82 }
83
84 /**
85 * Get the statistics for any cached attempted merges on the passed criteria.
86 *
87 * @param array $params
88 *
89 * @return array
90 * @throws \API_Exception
91 * @throws \Civi\API\Exception\UnauthorizedException
92 */
93 function civicrm_api3_dedupe_create($params) {
94 return _civicrm_api3_basic_create('CRM_Core_BAO_PrevNextCache', $params, 'PrevNextCache');
95 }
96
97 /**
98 * Get the statistics for any cached attempted merges on the passed criteria.
99 *
100 * @param array $params
101 *
102 * @return array
103 * @throws \CiviCRM_API3_Exception
104 */
105 function civicrm_api3_dedupe_getstatistics($params) {
106 $stats = CRM_Dedupe_Merger::getMergeStats(CRM_Dedupe_Merger::getMergeCacheKeyString(
107 $params['rule_group_id'],
108 CRM_Utils_Array::value('group_id', $params),
109 CRM_Utils_Array::value('criteria', $params, []),
110 !empty($params['check_permissions']),
111 CRM_Utils_Array::value('search_limit', $params, 0)
112 ));
113 return civicrm_api3_create_success($stats);
114 }
115
116 /**
117 * Adjust Metadata for Create action.
118 *
119 * The metadata is used for setting defaults, documentation & validation.
120 *
121 * @param array $params
122 * Array of parameters determined by getfields.
123 */
124 function _civicrm_api3_dedupe_getstatistics_spec(&$params) {
125 $params['rule_group_id'] = [
126 'title' => ts('Rule Group ID'),
127 'api.required' => TRUE,
128 'type' => CRM_Utils_Type::T_INT,
129 ];
130 $params['group_id'] = [
131 'title' => ts('Group ID'),
132 'api.required' => FALSE,
133 'type' => CRM_Utils_Type::T_INT,
134 ];
135 $params['criteria'] = [
136 'title' => ts('Criteria'),
137 'description' => ts('Dedupe search criteria, as parsable by v3 Contact.get api'),
138 ];
139 $spec['search_limit'] = [
140 'title' => ts('Number of contacts to look for matches for.'),
141 'type' => CRM_Utils_Type::T_INT,
142 'api.default' => (int) Civi::settings()->get('dedupe_default_limit'),
143 ];
144
145 }
146
147 /**
148 * Get the duplicate contacts for the supplied parameters.
149 *
150 * @param array $params
151 *
152 * @return array
153 * @throws \CiviCRM_API3_Exception
154 * @throws \API_Exception
155 * @throws \CRM_Core_Exception
156 */
157 function civicrm_api3_dedupe_getduplicates($params) {
158 $options = _civicrm_api3_get_options_from_params($params);
159 $dupePairs = CRM_Dedupe_Merger::getDuplicatePairs($params['rule_group_id'], NULL, TRUE, $options['limit'], FALSE, TRUE, $params['criteria'], CRM_Utils_Array::value('check_permissions', $params), CRM_Utils_Array::value('search_limit', $params, 0), CRM_Utils_Array::value('is_force_new_search', $params));
160 return civicrm_api3_create_success($dupePairs);
161 }
162
163 /**
164 * Adjust Metadata for getduplicates action..
165 *
166 * The metadata is used for setting defaults, documentation & validation.
167 *
168 * @param array $params
169 * Array of parameters determined by getfields.
170 */
171 function _civicrm_api3_dedupe_getduplicates_spec(&$params) {
172 $params['rule_group_id'] = [
173 'title' => ts('Rule Group ID'),
174 'api.required' => TRUE,
175 'type' => CRM_Utils_Type::T_INT,
176 ];
177 $params['criteria'] = [
178 'title' => ts('Criteria'),
179 'description' => ts("Dedupe search criteria, as parsable by v3 Contact.get api, keyed by Contact. Eg.['Contact' => ['id' => ['BETWEEN' => [1, 2000]], 'group' => 34]"),
180 'api.default' => [],
181 ];
182 $spec['search_limit'] = [
183 'title' => ts('Number of contacts to look for matches for.'),
184 'type' => CRM_Utils_Type::T_INT,
185 'api.default' => (int) Civi::settings()->get('dedupe_default_limit'),
186 ];
187 $spec['is_force_new_search'] = [
188 'title' => ts('Force a new search, refreshing any cached search'),
189 'type' => CRM_Utils_Type::T_BOOLEAN,
190 ];
191
192 }