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