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