Merge pull request #8547 from xurizaemon/patch-2
[civicrm-core.git] / CRM / Contact / Page / DedupeFind.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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
39 /**
40 * Get BAO Name.
41 *
42 * @return string
43 * Classname of BAO.
44 */
45 public function getBAOName() {
46 return 'CRM_Dedupe_BAO_RuleGroup';
47 }
48
49 /**
50 * Get action Links.
51 */
52 public function &links() {
53 }
54
55 /**
56 * Browse all rule groups.
57 */
58 public function run() {
59 $gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0);
60 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 0);
61 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
62 $limit = CRM_Utils_Request::retrieve('limit', 'Integer', $this);
63
64 $session = CRM_Core_Session::singleton();
65 $contactIds = $session->get('selectedSearchContactIds');
66 if ($context == 'search' || !empty($contactIds)) {
67 $context = 'search';
68 $this->assign('backURL', $session->readUserContext());
69 }
70
71 if ($action & CRM_Core_Action::RENEW) {
72 // empty cache
73 $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this, FALSE, 0);
74
75 if ($rgid) {
76 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid));
77 }
78 $urlQry = "reset=1&action=update&rgid={$rgid}";
79 if ($gid) {
80 $urlQry .= "&gid={$gid}";
81 }
82 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
83 }
84 elseif ($action & CRM_Core_Action::MAP) {
85 // do a batch merge if requested
86 $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this, FALSE, 0);
87 $result = CRM_Dedupe_Merger::batchMerge($rgid, $gid, 'safe', TRUE, 75);
88
89 $skippedCount = CRM_Utils_Request::retrieve('skipped', 'Positive', $this, FALSE, 0);
90 $skippedCount = $skippedCount + count($result['skipped']);
91 $mergedCount = CRM_Utils_Request::retrieve('merged', 'Positive', $this, FALSE, 0);
92 $mergedCount = $mergedCount + count($result['merged']);
93
94 if (empty($result['merged']) && empty($result['skipped'])) {
95 $message = '';
96 if ($mergedCount >= 1) {
97 $message = ts("%1 pairs of duplicates were merged", array(1 => $mergedCount));
98 }
99 if ($skippedCount >= 1) {
100 $message = $message ? "{$message} and " : '';
101 $message .= ts("%1 pairs of duplicates were skipped due to conflict",
102 array(1 => $skippedCount)
103 );
104 }
105 $message .= ts(" during the batch merge process with safe mode.");
106 CRM_Core_Session::setStatus($message, ts('Merge Complete'), 'success');
107
108 $urlQry = "reset=1&action=update&rgid={$rgid}";
109 if ($gid) {
110 $urlQry .= "&gid={$gid}";
111 }
112 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
113 }
114 else {
115 $urlQry = "reset=1&action=map&rgid={$rgid}";
116 if ($gid) {
117 $urlQry .= "&gid={$gid}";
118 }
119 $urlQry .= "&skipped={$skippedCount}&merged={$mergedCount}";
120 CRM_Utils_System::jsRedirect(
121 CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry),
122 ts('Batch Merge Task in progress'),
123 ts('The batch merge task is still in progress. This page will be refreshed automatically.')
124 );
125 }
126 }
127
128 if ($action & CRM_Core_Action::UPDATE ||
129 $action & CRM_Core_Action::BROWSE
130 ) {
131 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0);
132 $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this, FALSE, 0);
133 $this->action = CRM_Core_Action::UPDATE;
134
135 $sourceParams = 'snippet=4';
136 if ($gid) {
137 $sourceParams .= "&gid={$gid}";
138 }
139 if ($rgid) {
140 $sourceParams .= "&rgid={$rgid}";
141 }
142 if ($context == 'conflicts') {
143 $sourceParams .= "&selected=1";
144 }
145
146 $this->assign('sourceUrl', CRM_Utils_System::url('civicrm/ajax/dedupefind', $sourceParams, FALSE, NULL, FALSE));
147
148 //reload from cache table
149 $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid);
150
151 $stats = CRM_Dedupe_Merger::getMergeStatsMsg($cacheKeyString);
152 if ($stats) {
153 CRM_Core_Session::setStatus($stats);
154 // reset so we not displaying same message again
155 CRM_Dedupe_Merger::resetMergeStats($cacheKeyString);
156 }
157 $join = CRM_Dedupe_Merger::getJoinOnDedupeTable();
158 $where = "de.id IS NULL";
159 if ($context == 'conflicts') {
160 $where .= " AND pn.is_selected = 1";
161 }
162 $this->_mainContacts = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, $join, $where);
163 if (empty($this->_mainContacts)) {
164 if ($context == 'conflicts') {
165 // if the current screen was intended to list only selected contacts, move back to full dupe list
166 $sourceParams = 'reset=1&action=update';
167 if ($gid) {
168 $sourceParams .= "&gid={$gid}";
169 }
170 if ($rgid) {
171 $sourceParams .= "&rgid={$rgid}";
172 }
173 CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), $sourceParams));
174 }
175 if ($gid) {
176 $foundDupes = $this->get("dedupe_dupes_$gid");
177 if (!$foundDupes) {
178 $foundDupes = CRM_Dedupe_Finder::dupesInGroup($rgid, $gid, $limit);
179 }
180 $this->set("dedupe_dupes_$gid", $foundDupes);
181 }
182 elseif (!empty($contactIds)) {
183 $foundDupes = $this->get("search_dedupe_dupes_$gid");
184 if (!$foundDupes) {
185 $foundDupes = CRM_Dedupe_Finder::dupes($rgid, $contactIds);
186 }
187 $this->get("search_dedupe_dupes_$gid", $foundDupes);
188 }
189 else {
190 $foundDupes = $this->get('dedupe_dupes');
191 if (!$foundDupes) {
192 $foundDupes = CRM_Dedupe_Finder::dupes($rgid, array(), TRUE, $limit);
193 }
194 $this->set('dedupe_dupes', $foundDupes);
195 }
196 if (!$foundDupes) {
197 $ruleGroup = new CRM_Dedupe_BAO_RuleGroup();
198 $ruleGroup->id = $rgid;
199 $ruleGroup->find(TRUE);
200
201 $session = CRM_Core_Session::singleton();
202 $session->setStatus(ts('No possible duplicates were found using %1 rule.', array(1 => $ruleGroup->name)), ts('None Found'), 'info');
203 $url = CRM_Utils_System::url('civicrm/contact/deduperules', 'reset=1');
204 if ($context == 'search') {
205 $url = $session->readUserContext();
206 }
207 CRM_Utils_System::redirect($url);
208 }
209 else {
210 $cids = array();
211 foreach ($foundDupes as $dupe) {
212 $cids[$dupe[0]] = 1;
213 $cids[$dupe[1]] = 1;
214 }
215 $cidString = implode(', ', array_keys($cids));
216 $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name";
217 $dao = new CRM_Core_DAO();
218 $dao->query($sql);
219 $displayNames = array();
220 while ($dao->fetch()) {
221 $displayNames[$dao->id] = $dao->display_name;
222 }
223
224 // FIXME: sort the contacts; $displayName
225 // is already sort_name-sorted, so use that
226 // (also, consider sorting by dupe count first)
227 // lobo - change the sort to by threshold value
228 // so the more likely dupes are sorted first
229 $userId = CRM_Core_Session::singleton()->getLoggedInContactID();
230 $mainContacts = $permission = array();
231
232 foreach ($foundDupes as $dupes) {
233 $srcID = $dupes[0];
234 $dstID = $dupes[1];
235 if ($dstID == $userId) {
236 $srcID = $dupes[1];
237 $dstID = $dupes[0];
238 }
239
240 /***
241 * Eliminate this since it introduces 3 queries PER merge row
242 * and hence is very expensive
243 * CRM-8822
244 * if ( !array_key_exists( $srcID, $permission ) ) {
245 * $permission[$srcID] = CRM_Contact_BAO_Contact_Permission::allow( $srcID, CRM_Core_Permission::EDIT );
246 * }
247 * if ( !array_key_exists( $dstID, $permission ) ) {
248 * $permission[$dstID] = CRM_Contact_BAO_Contact_Permission::allow( $dstID, CRM_Core_Permission::EDIT );
249 * }
250 *
251 * $canMerge = ( $permission[$dstID] && $permission[$srcID] );
252 *
253 */
254
255 // we'll do permission checking during the merge process
256 $canMerge = TRUE;
257
258 $mainContacts[] = $row = array(
259 'srcID' => $srcID,
260 'srcName' => $displayNames[$srcID],
261 'dstID' => $dstID,
262 'dstName' => $displayNames[$dstID],
263 'weight' => $dupes[2],
264 'canMerge' => $canMerge,
265 );
266
267 $data = CRM_Core_DAO::escapeString(serialize($row));
268 $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) ";
269 }
270 if ($cid) {
271 $this->_cid = $cid;
272 }
273 if ($gid) {
274 $this->_gid = $gid;
275 }
276 $this->_rgid = $rgid;
277 $this->_mainContacts = $mainContacts;
278
279 CRM_Core_BAO_PrevNextCache::setItem($values);
280 $session = CRM_Core_Session::singleton();
281 if ($this->_cid) {
282 $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/deduperules',
283 "action=update&rgid={$this->_rgid}&gid={$this->_gid}&cid={$this->_cid}"
284 ));
285 }
286 else {
287 $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/dedupefind',
288 "reset=1&action=update&rgid={$this->_rgid}"
289 ));
290 }
291 }
292 }
293 else {
294 if ($cid) {
295 $this->_cid = $cid;
296 }
297 if ($gid) {
298 $this->_gid = $gid;
299 }
300 $this->_rgid = $rgid;
301 }
302
303 $this->assign('action', $this->action);
304 $this->browse();
305 }
306 else {
307 $this->action = CRM_Core_Action::UPDATE;
308 $this->edit($this->action);
309 $this->assign('action', $this->action);
310 }
311 $this->assign('context', $context);
312
313 // parent run
314 return parent::run();
315 }
316
317 /**
318 * Browse all rule groups.
319 */
320 public function browse() {
321 $this->assign('main_contacts', $this->_mainContacts);
322
323 if ($this->_cid) {
324 $this->assign('cid', $this->_cid);
325 }
326 if (isset($this->_gid) || $this->_gid) {
327 $this->assign('gid', $this->_gid);
328 }
329 $this->assign('rgid', $this->_rgid);
330 }
331
332 /**
333 * Get name of edit form.
334 *
335 * @return string
336 * classname of edit form
337 */
338 public function editForm() {
339 return 'CRM_Contact_Form_DedupeFind';
340 }
341
342 /**
343 * Get edit form name.
344 *
345 * @return string
346 * name of this page
347 */
348 public function editName() {
349 return 'DedupeFind';
350 }
351
352 /**
353 * Get user context.
354 *
355 * @param null $mode
356 *
357 * @return string
358 * user context
359 */
360 public function userContext($mode = NULL) {
361 return 'civicrm/contact/dedupefind';
362 }
363
364 }