Merge pull request #9860 from JMAConsulting/CRM-20058
[civicrm-core.git] / CRM / Contact / Page / DedupeFind.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic {
34 protected $_cid = NULL;
35 protected $_rgid;
36 protected $_mainContacts;
37 protected $_gid;
92241006 38 protected $action;
6a488035
TO
39
40 /**
fe482240 41 * Get BAO Name.
6a488035 42 *
a6c01b45
CW
43 * @return string
44 * Classname of BAO.
8ef12e64 45 */
00be9182 46 public function getBAOName() {
6a488035
TO
47 return 'CRM_Dedupe_BAO_RuleGroup';
48 }
49
50 /**
fe482240 51 * Get action Links.
6a488035 52 */
ce80b209
TO
53 public function &links() {
54 }
6a488035
TO
55
56 /**
fe482240 57 * Browse all rule groups.
6a488035 58 */
00be9182 59 public function run() {
353ffa53
TO
60 $gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0);
61 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 0);
6a488035 62 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
4c8b4719 63 $limit = CRM_Utils_Request::retrieve('limit', 'Integer', $this);
dc6285d5 64 $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive');
4a8e09b1
JM
65 $urlQry = array(
66 'reset' => 1,
67 'rgid' => $rgid,
68 'gid' => $gid,
83c2e820 69 'limit' => $limit,
4a8e09b1 70 );
4c7de212 71 $this->assign('urlQuery', CRM_Utils_System::makeQueryString($urlQry));
6a488035
TO
72
73 $session = CRM_Core_Session::singleton();
74 $contactIds = $session->get('selectedSearchContactIds');
75 if ($context == 'search' || !empty($contactIds)) {
76 $context = 'search';
77 $this->assign('backURL', $session->readUserContext());
78 }
79
80 if ($action & CRM_Core_Action::RENEW) {
81 // empty cache
6a488035
TO
82
83 if ($rgid) {
2ae26001 84 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid));
6a488035 85 }
4a8e09b1
JM
86 $urlQry['action'] = 'update';
87 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
6a488035
TO
88 }
89 elseif ($action & CRM_Core_Action::MAP) {
90 // do a batch merge if requested
d13a105a 91 $result = CRM_Dedupe_Merger::batchMerge($rgid, $gid, 'safe', 75);
6a488035
TO
92
93 $skippedCount = CRM_Utils_Request::retrieve('skipped', 'Positive', $this, FALSE, 0);
94 $skippedCount = $skippedCount + count($result['skipped']);
353ffa53
TO
95 $mergedCount = CRM_Utils_Request::retrieve('merged', 'Positive', $this, FALSE, 0);
96 $mergedCount = $mergedCount + count($result['merged']);
6a488035
TO
97
98 if (empty($result['merged']) && empty($result['skipped'])) {
99 $message = '';
100 if ($mergedCount >= 1) {
101 $message = ts("%1 pairs of duplicates were merged", array(1 => $mergedCount));
102 }
103 if ($skippedCount >= 1) {
104 $message = $message ? "{$message} and " : '';
105 $message .= ts("%1 pairs of duplicates were skipped due to conflict",
106 array(1 => $skippedCount)
107 );
108 }
109 $message .= ts(" during the batch merge process with safe mode.");
110 CRM_Core_Session::setStatus($message, ts('Merge Complete'), 'success');
4a8e09b1
JM
111 $urlQry['action'] = 'update';
112 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
6a488035
TO
113 }
114 else {
4a8e09b1 115 $urlQry['action'] = 'map';
83c2e820 116 $urlQry['skipped'] = $skippedCount;
4a8e09b1 117 $urlQry['merged'] = $mergedCount;
6a488035
TO
118 CRM_Utils_System::jsRedirect(
119 CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry),
120 ts('Batch Merge Task in progress'),
121 ts('The batch merge task is still in progress. This page will be refreshed automatically.')
122 );
123 }
124 }
125
126 if ($action & CRM_Core_Action::UPDATE ||
127 $action & CRM_Core_Action::BROWSE
128 ) {
353ffa53 129 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0);
6a488035
TO
130 $this->action = CRM_Core_Action::UPDATE;
131
4a8e09b1 132 $urlQry['snippet'] = 4;
63ef778e 133 if ($context == 'conflicts') {
4a8e09b1 134 $urlQry['selected'] = 1;
63ef778e 135 }
6a488035 136
dc6285d5 137 $this->assign('sourceUrl', CRM_Utils_System::url('civicrm/ajax/dedupefind', $urlQry, FALSE, NULL, FALSE));
6a488035
TO
138
139 //reload from cache table
2ae26001 140 $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid);
6a488035 141
63ef778e 142 $stats = CRM_Dedupe_Merger::getMergeStatsMsg($cacheKeyString);
143 if ($stats) {
144 CRM_Core_Session::setStatus($stats);
145 // reset so we not displaying same message again
146 CRM_Dedupe_Merger::resetMergeStats($cacheKeyString);
147 }
2ae26001 148 $join = CRM_Dedupe_Merger::getJoinOnDedupeTable();
6a488035 149 $where = "de.id IS NULL";
63ef778e 150 if ($context == 'conflicts') {
151 $where .= " AND pn.is_selected = 1";
152 }
6a488035
TO
153 $this->_mainContacts = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, $join, $where);
154 if (empty($this->_mainContacts)) {
63ef778e 155 if ($context == 'conflicts') {
156 // if the current screen was intended to list only selected contacts, move back to full dupe list
4a8e09b1 157 $urlQry['action'] = 'update';
9893cfd2 158 unset($urlQry['snippet']);
4a8e09b1 159 CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), $urlQry));
63ef778e 160 }
6a488035
TO
161 if ($gid) {
162 $foundDupes = $this->get("dedupe_dupes_$gid");
163 if (!$foundDupes) {
4c8b4719 164 $foundDupes = CRM_Dedupe_Finder::dupesInGroup($rgid, $gid, $limit);
6a488035
TO
165 }
166 $this->set("dedupe_dupes_$gid", $foundDupes);
167 }
168 elseif (!empty($contactIds)) {
169 $foundDupes = $this->get("search_dedupe_dupes_$gid");
170 if (!$foundDupes) {
171 $foundDupes = CRM_Dedupe_Finder::dupes($rgid, $contactIds);
172 }
92241006 173 $this->set("search_dedupe_dupes_$gid", $foundDupes);
6a488035
TO
174 }
175 else {
176 $foundDupes = $this->get('dedupe_dupes');
177 if (!$foundDupes) {
4c8b4719 178 $foundDupes = CRM_Dedupe_Finder::dupes($rgid, array(), TRUE, $limit);
6a488035
TO
179 }
180 $this->set('dedupe_dupes', $foundDupes);
181 }
182 if (!$foundDupes) {
183 $ruleGroup = new CRM_Dedupe_BAO_RuleGroup();
184 $ruleGroup->id = $rgid;
185 $ruleGroup->find(TRUE);
186
187 $session = CRM_Core_Session::singleton();
188 $session->setStatus(ts('No possible duplicates were found using %1 rule.', array(1 => $ruleGroup->name)), ts('None Found'), 'info');
189 $url = CRM_Utils_System::url('civicrm/contact/deduperules', 'reset=1');
190 if ($context == 'search') {
191 $url = $session->readUserContext();
192 }
193 CRM_Utils_System::redirect($url);
194 }
195 else {
1719073d 196 $mainContacts = CRM_Dedupe_Finder::parseAndStoreDupePairs($foundDupes, $cacheKeyString);
6a488035 197
6a488035
TO
198 if ($cid) {
199 $this->_cid = $cid;
200 }
201 if ($gid) {
202 $this->_gid = $gid;
203 }
204 $this->_rgid = $rgid;
205 $this->_mainContacts = $mainContacts;
206
6a488035 207 $session = CRM_Core_Session::singleton();
4a8e09b1 208 $urlQry['action'] = 'update';
6a488035 209 if ($this->_cid) {
4a8e09b1 210 $urlQry['cid'] = $this->_cid;
6a488035 211 $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/deduperules',
4a8e09b1 212 $urlQry
353ffa53 213 ));
6a488035
TO
214 }
215 else {
216 $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/dedupefind',
4a8e09b1 217 $urlQry
353ffa53 218 ));
6a488035
TO
219 }
220 }
221 }
222 else {
223 if ($cid) {
224 $this->_cid = $cid;
225 }
226 if ($gid) {
227 $this->_gid = $gid;
228 }
229 $this->_rgid = $rgid;
230 }
231
232 $this->assign('action', $this->action);
233 $this->browse();
234 }
235 else {
236 $this->action = CRM_Core_Action::UPDATE;
237 $this->edit($this->action);
238 $this->assign('action', $this->action);
239 }
240 $this->assign('context', $context);
241
242 // parent run
243 return parent::run();
244 }
245
246 /**
fe482240 247 * Browse all rule groups.
6a488035 248 */
00be9182 249 public function browse() {
6a488035
TO
250 $this->assign('main_contacts', $this->_mainContacts);
251
252 if ($this->_cid) {
253 $this->assign('cid', $this->_cid);
254 }
255 if (isset($this->_gid) || $this->_gid) {
256 $this->assign('gid', $this->_gid);
257 }
258 $this->assign('rgid', $this->_rgid);
259 }
260
261 /**
fe482240 262 * Get name of edit form.
6a488035 263 *
a6c01b45
CW
264 * @return string
265 * classname of edit form
6a488035 266 */
00be9182 267 public function editForm() {
6a488035
TO
268 return 'CRM_Contact_Form_DedupeFind';
269 }
270
271 /**
fe482240 272 * Get edit form name.
6a488035 273 *
a6c01b45
CW
274 * @return string
275 * name of this page
6a488035 276 */
00be9182 277 public function editName() {
6a488035
TO
278 return 'DedupeFind';
279 }
280
281 /**
fe482240 282 * Get user context.
6a488035 283 *
77b97be7
EM
284 * @param null $mode
285 *
a6c01b45
CW
286 * @return string
287 * user context
6a488035 288 */
00be9182 289 public function userContext($mode = NULL) {
6a488035
TO
290 return 'civicrm/contact/dedupefind';
291 }
96025800 292
6a488035 293}