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