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