Merge pull request #14503 from pradpnayak/ReportFix
[civicrm-core.git] / api / v3 / Dedupe.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 * 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 CRM_Utils_Array::value('check_permissions', $params, [])
111 ));
112 return civicrm_api3_create_success($stats);
113 }
114
115 /**
116 * Adjust Metadata for Create action.
117 *
118 * The metadata is used for setting defaults, documentation & validation.
119 *
120 * @param array $params
121 * Array of parameters determined by getfields.
122 */
123 function _civicrm_api3_dedupe_getstatistics_spec(&$params) {
124 $params['rule_group_id'] = [
125 'title' => ts('Rule Group ID'),
126 'api.required' => TRUE,
127 'type' => CRM_Utils_Type::T_INT,
128 ];
129 $params['group_id'] = [
130 'title' => ts('Group ID'),
131 'api.required' => FALSE,
132 'type' => CRM_Utils_Type::T_INT,
133 ];
134 $params['criteria'] = [
135 'title' => ts('Criteria'),
136 'description' => ts('Dedupe search criteria, as parsable by v3 Contact.get api'),
137 ];
138
139 }
140
141 /**
142 * Get the duplicate contacts for the supplied parameters.
143 *
144 * @param array $params
145 *
146 * @return array
147 * @throws \CiviCRM_API3_Exception
148 * @throws \API_Exception
149 * @throws \CRM_Core_Exception
150 */
151 function civicrm_api3_dedupe_getduplicates($params) {
152 $options = _civicrm_api3_get_options_from_params($params);
153 $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));
154 return civicrm_api3_create_success($dupePairs);
155 }
156
157 /**
158 * Adjust Metadata for getduplicates action..
159 *
160 * The metadata is used for setting defaults, documentation & validation.
161 *
162 * @param array $params
163 * Array of parameters determined by getfields.
164 */
165 function _civicrm_api3_dedupe_getduplicates_spec(&$params) {
166 $params['rule_group_id'] = [
167 'title' => ts('Rule Group ID'),
168 'api.required' => TRUE,
169 'type' => CRM_Utils_Type::T_INT,
170 ];
171 $params['criteria'] = [
172 'title' => ts('Criteria'),
173 'description' => ts("Dedupe search criteria, as parsable by v3 Contact.get api, keyed by Contact. Eg.['Contact' => ['id' => ['BETWEEN' => [1, 2000]], 'group' => 34]"),
174 'api.default' => [],
175 ];
176 $spec['search_limit'] = [
177 'title' => ts('Number of contacts to look for matches for.'),
178 'type' => CRM_Utils_Type::T_INT,
179 'api.default' => (int) Civi::settings()->get('dedupe_default_limit'),
180 ];
181
182 }