Update copyright date for 2020
[civicrm-core.git] / CRM / Contact / Page / DedupeFind.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 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2020
32 */
33 class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic {
34 protected $_cid = NULL;
35 protected $_rgid;
36 protected $_mainContacts;
37 protected $_gid;
38 protected $action;
39 /**
40 * Only display selected.
41 *
42 * @var bool
43 */
44 protected $selected;
45
46 /**
47 * Get isSelected value.
48 *
49 * This needs to be an integer of 0 or 1 or NULL for no filter.
50 *
51 * @return bool|NULL
52 */
53 public function isSelected() {
54 return ($this->selected === NULL) ? NULL : (int) $this->selected;
55 }
56
57 /**
58 * Get BAO Name.
59 *
60 * @return string
61 * Classname of BAO.
62 */
63 public function getBAOName() {
64 return 'CRM_Dedupe_BAO_RuleGroup';
65 }
66
67 /**
68 * Get action Links.
69 */
70 public function &links() {
71 }
72
73 /**
74 * Initialize properties from input.
75 */
76 protected function initialize() {
77 $this->selected = CRM_Utils_Request::retrieveValue('selected', 'Boolean');
78 }
79
80 /**
81 * Browse all rule groups.
82 */
83 public function run() {
84 $this->initialize();
85 $gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0);
86 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 0);
87 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
88 $limit = CRM_Utils_Request::retrieve('limit', 'Integer', $this);
89 $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this);
90 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0);
91
92 $criteria = CRM_Utils_Request::retrieve('criteria', 'Json', $this, FALSE, '{}');
93 $this->assign('criteria', $criteria);
94
95 $isConflictMode = ($context == 'conflicts');
96 if ($cid) {
97 $this->_cid = $cid;
98 }
99 if ($gid) {
100 $this->_gid = $gid;
101 }
102 $this->_rgid = $rgid;
103
104 $urlQry = [
105 'reset' => 1,
106 'rgid' => $rgid,
107 'gid' => $gid,
108 'limit' => (int) $limit,
109 'criteria' => $criteria,
110 ];
111 $this->assign('urlQuery', CRM_Utils_System::makeQueryString($urlQry));
112 $this->assign('isSelected', $this->isSelected());
113 $criteria = json_decode($criteria, TRUE);
114 $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria, TRUE, $limit);
115 $this->assign('cacheKey', $cacheKeyString);
116
117 if ($context == 'search') {
118 $context = 'search';
119 $this->assign('backURL', CRM_Core_Session::singleton()->readUserContext());
120 }
121
122 if ($action & CRM_Core_Action::RENEW) {
123 // empty cache
124 if ($rgid) {
125 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKeyString);
126 }
127 $urlQry['action'] = 'update';
128 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
129 }
130 elseif ($action & CRM_Core_Action::MAP) {
131 // do a batch merge if requested
132 $result = CRM_Dedupe_Merger::batchMerge($rgid, $gid, 'safe', 75, 2, $criteria, TRUE, NULL, $limit);
133
134 $skippedCount = CRM_Utils_Request::retrieve('skipped', 'Positive', $this, FALSE, 0);
135 $skippedCount = $skippedCount + count($result['skipped']);
136 $mergedCount = CRM_Utils_Request::retrieve('merged', 'Positive', $this, FALSE, 0);
137 $mergedCount = $mergedCount + count($result['merged']);
138
139 if (empty($result['merged']) && empty($result['skipped'])) {
140 $message = '';
141 if ($mergedCount >= 1) {
142 $message = ts("%1 pairs of duplicates were merged", [1 => $mergedCount]);
143 }
144 if ($skippedCount >= 1) {
145 $message = $message ? "{$message} and " : '';
146 $message .= ts("%1 pairs of duplicates were skipped due to conflict",
147 [1 => $skippedCount]
148 );
149 }
150 $message .= ts(" during the batch merge process with safe mode.");
151 CRM_Core_Session::setStatus($message, ts('Merge Complete'), 'success');
152 $urlQry['action'] = 'update';
153 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
154 }
155 else {
156 $urlQry['action'] = 'map';
157 $urlQry['skipped'] = $skippedCount;
158 $urlQry['merged'] = $mergedCount;
159 CRM_Utils_System::jsRedirect(
160 CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry),
161 ts('Batch Merge Task in progress'),
162 ts('The batch merge task is still in progress. This page will be refreshed automatically.')
163 );
164 }
165 }
166
167 if ($action & CRM_Core_Action::UPDATE ||
168 $action & CRM_Core_Action::BROWSE
169 ) {
170 $this->action = CRM_Core_Action::UPDATE;
171
172 $urlQry['snippet'] = 4;
173
174 $this->assign('sourceUrl', CRM_Utils_System::url('civicrm/ajax/dedupefind', $urlQry, FALSE, NULL, FALSE));
175
176 $stats = CRM_Dedupe_Merger::getMergeStats($cacheKeyString);
177 if ($stats) {
178 $message = CRM_Dedupe_Merger::getMergeStatsMsg($stats);
179 $status = empty($stats['skipped']) ? 'success' : 'alert';
180 CRM_Core_Session::setStatus($message, ts('Batch Complete'), $status, ['expires' => 0]);
181 // reset so we not displaying same message again
182 CRM_Dedupe_Merger::resetMergeStats($cacheKeyString);
183 }
184
185 $this->_mainContacts = CRM_Dedupe_Merger::getDuplicatePairs($rgid, $gid, !$isConflictMode, 0, $this->isSelected(), $isConflictMode, $criteria, TRUE, $limit);
186
187 if (empty($this->_mainContacts)) {
188 if ($isConflictMode) {
189 // if the current screen was intended to list only selected contacts, move back to full dupe list
190 $urlQry['action'] = 'update';
191 unset($urlQry['snippet']);
192 CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), $urlQry));
193 }
194 $ruleGroupName = civicrm_api3('RuleGroup', 'getvalue', ['id' => $rgid, 'return' => 'name']);
195 CRM_Core_Session::singleton()->setStatus(ts('No possible duplicates were found using %1 rule.', [1 => $ruleGroupName]), ts('None Found'), 'info');
196 $url = CRM_Utils_System::url('civicrm/contact/deduperules', 'reset=1');
197 if ($context == 'search') {
198 $url = CRM_Core_Session::singleton()->readUserContext();
199 }
200 CRM_Utils_System::redirect($url);
201 }
202 else {
203 $urlQry['action'] = 'update';
204 if ($this->_cid) {
205 $urlQry['cid'] = $this->_cid;
206 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/contact/deduperules',
207 $urlQry
208 ));
209 }
210 else {
211 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/contact/dedupefind',
212 $urlQry
213 ));
214 }
215 }
216
217 $this->assign('action', $this->action);
218 $this->browse();
219 }
220 else {
221 $this->action = CRM_Core_Action::UPDATE;
222 $this->edit($this->action);
223 $this->assign('action', $this->action);
224 }
225 $this->assign('context', $context);
226
227 return parent::run();
228 }
229
230 /**
231 * Browse all rule groups.
232 */
233 public function browse() {
234 $this->assign('main_contacts', $this->_mainContacts);
235
236 if ($this->_cid) {
237 $this->assign('cid', $this->_cid);
238 }
239 if (isset($this->_gid) || $this->_gid) {
240 $this->assign('gid', $this->_gid);
241 }
242 $this->assign('rgid', $this->_rgid);
243 }
244
245 /**
246 * Get name of edit form.
247 *
248 * @return string
249 * classname of edit form
250 */
251 public function editForm() {
252 return 'CRM_Contact_Form_DedupeFind';
253 }
254
255 /**
256 * Get edit form name.
257 *
258 * @return string
259 * name of this page
260 */
261 public function editName() {
262 return 'DedupeFind';
263 }
264
265 /**
266 * Get user context.
267 *
268 * @param null $mode
269 *
270 * @return string
271 * user context
272 */
273 public function userContext($mode = NULL) {
274 return 'civicrm/contact/dedupefind';
275 }
276
277 }