Merge pull request #15843 from totten/master-simplehead
[civicrm-core.git] / CRM / Contact / Page / DedupeFind.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic {
18 protected $_cid = NULL;
19 protected $_rgid;
20 protected $_mainContacts;
21 protected $_gid;
92241006 22 protected $action;
bb22928b 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 }
6a488035
TO
40
41 /**
fe482240 42 * Get BAO Name.
6a488035 43 *
a6c01b45
CW
44 * @return string
45 * Classname of BAO.
8ef12e64 46 */
00be9182 47 public function getBAOName() {
6a488035
TO
48 return 'CRM_Dedupe_BAO_RuleGroup';
49 }
50
51 /**
fe482240 52 * Get action Links.
6a488035 53 */
ce80b209
TO
54 public function &links() {
55 }
6a488035 56
bb22928b 57 /**
58 * Initialize properties from input.
59 */
60 protected function initialize() {
61 $this->selected = CRM_Utils_Request::retrieveValue('selected', 'Boolean');
62 }
63
6a488035 64 /**
fe482240 65 * Browse all rule groups.
6a488035 66 */
00be9182 67 public function run() {
bb22928b 68 $this->initialize();
353ffa53
TO
69 $gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0);
70 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 0);
edc80cda 71 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
4c8b4719 72 $limit = CRM_Utils_Request::retrieve('limit', 'Integer', $this);
28c64535 73 $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this);
5721d85e 74 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0);
9f54f049 75
76 $criteria = CRM_Utils_Request::retrieve('criteria', 'Json', $this, FALSE, '{}');
88251439 77 $this->assign('criteria', $criteria);
78
5721d85e 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
be2fb01f 88 $urlQry = [
4a8e09b1
JM
89 'reset' => 1,
90 'rgid' => $rgid,
91 'gid' => $gid,
997a03fe 92 'limit' => (int) $limit,
88251439 93 'criteria' => $criteria,
be2fb01f 94 ];
4c7de212 95 $this->assign('urlQuery', CRM_Utils_System::makeQueryString($urlQry));
bb22928b 96 $this->assign('isSelected', $this->isSelected());
88251439 97 $criteria = json_decode($criteria, TRUE);
997a03fe 98 $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria, TRUE, $limit);
9d2f6d53 99 $this->assign('cacheKey', $cacheKeyString);
6a488035 100
d4c035a2 101 if ($context == 'search') {
6a488035 102 $context = 'search';
d4c035a2 103 $this->assign('backURL', CRM_Core_Session::singleton()->readUserContext());
6a488035
TO
104 }
105
106 if ($action & CRM_Core_Action::RENEW) {
107 // empty cache
6a488035 108 if ($rgid) {
9d2f6d53 109 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKeyString);
6a488035 110 }
4a8e09b1
JM
111 $urlQry['action'] = 'update';
112 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
6a488035
TO
113 }
114 elseif ($action & CRM_Core_Action::MAP) {
115 // do a batch merge if requested
997a03fe 116 $result = CRM_Dedupe_Merger::batchMerge($rgid, $gid, 'safe', 75, 2, $criteria, TRUE, NULL, $limit);
6a488035
TO
117
118 $skippedCount = CRM_Utils_Request::retrieve('skipped', 'Positive', $this, FALSE, 0);
119 $skippedCount = $skippedCount + count($result['skipped']);
353ffa53
TO
120 $mergedCount = CRM_Utils_Request::retrieve('merged', 'Positive', $this, FALSE, 0);
121 $mergedCount = $mergedCount + count($result['merged']);
6a488035
TO
122
123 if (empty($result['merged']) && empty($result['skipped'])) {
124 $message = '';
125 if ($mergedCount >= 1) {
be2fb01f 126 $message = ts("%1 pairs of duplicates were merged", [1 => $mergedCount]);
6a488035
TO
127 }
128 if ($skippedCount >= 1) {
129 $message = $message ? "{$message} and " : '';
130 $message .= ts("%1 pairs of duplicates were skipped due to conflict",
be2fb01f 131 [1 => $skippedCount]
6a488035
TO
132 );
133 }
134 $message .= ts(" during the batch merge process with safe mode.");
135 CRM_Core_Session::setStatus($message, ts('Merge Complete'), 'success');
4a8e09b1
JM
136 $urlQry['action'] = 'update';
137 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
6a488035
TO
138 }
139 else {
4a8e09b1 140 $urlQry['action'] = 'map';
83c2e820 141 $urlQry['skipped'] = $skippedCount;
4a8e09b1 142 $urlQry['merged'] = $mergedCount;
6a488035
TO
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 ) {
6a488035
TO
154 $this->action = CRM_Core_Action::UPDATE;
155
4a8e09b1 156 $urlQry['snippet'] = 4;
6a488035 157
97a322ac 158 $this->assign('sourceUrl', CRM_Utils_System::url('civicrm/ajax/dedupefind', $urlQry, FALSE, NULL, FALSE));
6a488035 159
3743fd3d 160 $stats = CRM_Dedupe_Merger::getMergeStats($cacheKeyString);
63ef778e 161 if ($stats) {
3743fd3d
CW
162 $message = CRM_Dedupe_Merger::getMergeStatsMsg($stats);
163 $status = empty($stats['skipped']) ? 'success' : 'alert';
be2fb01f 164 CRM_Core_Session::setStatus($message, ts('Batch Complete'), $status, ['expires' => 0]);
63ef778e 165 // reset so we not displaying same message again
166 CRM_Dedupe_Merger::resetMergeStats($cacheKeyString);
167 }
5721d85e 168
9c5fe572 169 $this->_mainContacts = CRM_Dedupe_Merger::getDuplicatePairs($rgid, $gid, !$isConflictMode, 0, $this->isSelected(), $isConflictMode, $criteria, TRUE, $limit);
5721d85e 170
6a488035 171 if (empty($this->_mainContacts)) {
5721d85e 172 if ($isConflictMode) {
63ef778e 173 // if the current screen was intended to list only selected contacts, move back to full dupe list
4a8e09b1 174 $urlQry['action'] = 'update';
9893cfd2 175 unset($urlQry['snippet']);
4a8e09b1 176 CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), $urlQry));
63ef778e 177 }
be2fb01f
CW
178 $ruleGroupName = civicrm_api3('RuleGroup', 'getvalue', ['id' => $rgid, 'return' => 'name']);
179 CRM_Core_Session::singleton()->setStatus(ts('No possible duplicates were found using %1 rule.', [1 => $ruleGroupName]), ts('None Found'), 'info');
5721d85e 180 $url = CRM_Utils_System::url('civicrm/contact/deduperules', 'reset=1');
181 if ($context == 'search') {
182 $url = CRM_Core_Session::singleton()->readUserContext();
6a488035 183 }
5721d85e 184 CRM_Utils_System::redirect($url);
6a488035
TO
185 }
186 else {
5721d85e 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 ));
6a488035 193 }
5721d85e 194 else {
195 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/contact/dedupefind',
196 $urlQry
197 ));
6a488035 198 }
6a488035
TO
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
6a488035
TO
211 return parent::run();
212 }
213
214 /**
fe482240 215 * Browse all rule groups.
6a488035 216 */
00be9182 217 public function browse() {
6a488035
TO
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 /**
fe482240 230 * Get name of edit form.
6a488035 231 *
a6c01b45
CW
232 * @return string
233 * classname of edit form
6a488035 234 */
00be9182 235 public function editForm() {
6a488035
TO
236 return 'CRM_Contact_Form_DedupeFind';
237 }
238
239 /**
fe482240 240 * Get edit form name.
6a488035 241 *
a6c01b45
CW
242 * @return string
243 * name of this page
6a488035 244 */
00be9182 245 public function editName() {
6a488035
TO
246 return 'DedupeFind';
247 }
248
249 /**
fe482240 250 * Get user context.
6a488035 251 *
77b97be7
EM
252 * @param null $mode
253 *
a6c01b45
CW
254 * @return string
255 * user context
6a488035 256 */
00be9182 257 public function userContext($mode = NULL) {
6a488035
TO
258 return 'civicrm/contact/dedupefind';
259 }
96025800 260
6a488035 261}