3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2016
33 class CRM_Contact_Page_DedupeFind
extends CRM_Core_Page_Basic
{
34 protected $_cid = NULL;
36 protected $_mainContacts;
45 public function getBAOName() {
46 return 'CRM_Dedupe_BAO_RuleGroup';
52 public function &links() {
56 * Browse all rule groups.
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);
63 $session = CRM_Core_Session
::singleton();
64 $contactIds = $session->get('selectedSearchContactIds');
65 if ($context == 'search' ||
!empty($contactIds)) {
67 $this->assign('backURL', $session->readUserContext());
70 if ($action & CRM_Core_Action
::RENEW
) {
72 $rgid = CRM_Utils_Request
::retrieve('rgid', 'Positive', $this, FALSE, 0);
75 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
76 $cacheKeyString = "merge $contactType";
77 $cacheKeyString .= $rgid ?
"_{$rgid}" : '_0';
78 $cacheKeyString .= $gid ?
"_{$gid}" : '_0';
79 CRM_Core_BAO_PrevNextCache
::deleteItem(NULL, $cacheKeyString);
81 $urlQry = "reset=1&action=update&rgid={$rgid}";
83 $urlQry .= "&gid={$gid}";
85 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/dedupefind', $urlQry));
87 elseif ($action & CRM_Core_Action
::MAP
) {
88 // do a batch merge if requested
89 $rgid = CRM_Utils_Request
::retrieve('rgid', 'Positive', $this, FALSE, 0);
90 $result = CRM_Dedupe_Merger
::batchMerge($rgid, $gid, 'safe', TRUE, 75);
92 $skippedCount = CRM_Utils_Request
::retrieve('skipped', 'Positive', $this, FALSE, 0);
93 $skippedCount = $skippedCount +
count($result['skipped']);
94 $mergedCount = CRM_Utils_Request
::retrieve('merged', 'Positive', $this, FALSE, 0);
95 $mergedCount = $mergedCount +
count($result['merged']);
97 if (empty($result['merged']) && empty($result['skipped'])) {
99 if ($mergedCount >= 1) {
100 $message = ts("%1 pairs of duplicates were merged", array(1 => $mergedCount));
102 if ($skippedCount >= 1) {
103 $message = $message ?
"{$message} and " : '';
104 $message .= ts("%1 pairs of duplicates were skipped due to conflict",
105 array(1 => $skippedCount)
108 $message .= ts(" during the batch merge process with safe mode.");
109 CRM_Core_Session
::setStatus($message, ts('Merge Complete'), 'success');
111 $urlQry = "reset=1&action=update&rgid={$rgid}";
113 $urlQry .= "&gid={$gid}";
115 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/dedupefind', $urlQry));
118 $urlQry = "reset=1&action=map&rgid={$rgid}";
120 $urlQry .= "&gid={$gid}";
122 $urlQry .= "&skipped={$skippedCount}&merged={$mergedCount}";
123 CRM_Utils_System
::jsRedirect(
124 CRM_Utils_System
::url('civicrm/contact/dedupefind', $urlQry),
125 ts('Batch Merge Task in progress'),
126 ts('The batch merge task is still in progress. This page will be refreshed automatically.')
131 if ($action & CRM_Core_Action
::UPDATE ||
132 $action & CRM_Core_Action
::BROWSE
134 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this, FALSE, 0);
135 $rgid = CRM_Utils_Request
::retrieve('rgid', 'Positive', $this, FALSE, 0);
136 $this->action
= CRM_Core_Action
::UPDATE
;
138 //calculate the $contactType
140 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup',
146 $sourceParams = 'snippet=4';
148 $sourceParams .= "&gid={$gid}";
151 $sourceParams .= "&rgid={$rgid}";
153 if ($context == 'conflicts') {
154 $sourceParams .= "&selected=1";
157 $this->assign('sourceUrl', CRM_Utils_System
::url('civicrm/ajax/dedupefind', $sourceParams, FALSE, NULL, FALSE));
159 //reload from cache table
160 $cacheKeyString = "merge $contactType";
161 $cacheKeyString .= $rgid ?
"_{$rgid}" : '_0';
162 $cacheKeyString .= $gid ?
"_{$gid}" : '_0';
164 $stats = CRM_Dedupe_Merger
::getMergeStatsMsg($cacheKeyString);
166 CRM_Core_Session
::setStatus($stats);
167 // reset so we not displaying same message again
168 CRM_Dedupe_Merger
::resetMergeStats($cacheKeyString);
170 $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
171 pn.entity_id2 = de.contact_id2 )";
172 $where = "de.id IS NULL";
173 if ($context == 'conflicts') {
174 $where .= " AND pn.is_selected = 1";
176 $this->_mainContacts
= CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, $join, $where);
177 if (empty($this->_mainContacts
)) {
178 if ($context == 'conflicts') {
179 // if the current screen was intended to list only selected contacts, move back to full dupe list
180 $sourceParams = 'reset=1&action=update';
182 $sourceParams .= "&gid={$gid}";
185 $sourceParams .= "&rgid={$rgid}";
187 CRM_Utils_System
::redirect(CRM_Utils_System
::url(CRM_Utils_System
::currentPath(), $sourceParams));
190 $foundDupes = $this->get("dedupe_dupes_$gid");
192 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($rgid, $gid);
194 $this->set("dedupe_dupes_$gid", $foundDupes);
196 elseif (!empty($contactIds)) {
197 $foundDupes = $this->get("search_dedupe_dupes_$gid");
199 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid, $contactIds);
201 $this->get("search_dedupe_dupes_$gid", $foundDupes);
204 $foundDupes = $this->get('dedupe_dupes');
206 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid);
208 $this->set('dedupe_dupes', $foundDupes);
211 $ruleGroup = new CRM_Dedupe_BAO_RuleGroup();
212 $ruleGroup->id
= $rgid;
213 $ruleGroup->find(TRUE);
215 $session = CRM_Core_Session
::singleton();
216 $session->setStatus(ts('No possible duplicates were found using %1 rule.', array(1 => $ruleGroup->name
)), ts('None Found'), 'info');
217 $url = CRM_Utils_System
::url('civicrm/contact/deduperules', 'reset=1');
218 if ($context == 'search') {
219 $url = $session->readUserContext();
221 CRM_Utils_System
::redirect($url);
225 foreach ($foundDupes as $dupe) {
229 $cidString = implode(', ', array_keys($cids));
230 $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name";
231 $dao = new CRM_Core_DAO();
233 $displayNames = array();
234 while ($dao->fetch()) {
235 $displayNames[$dao->id
] = $dao->display_name
;
238 // FIXME: sort the contacts; $displayName
239 // is already sort_name-sorted, so use that
240 // (also, consider sorting by dupe count first)
241 // lobo - change the sort to by threshold value
242 // so the more likely dupes are sorted first
243 $session = CRM_Core_Session
::singleton();
244 $userId = $session->get('userID');
245 $mainContacts = $permission = array();
247 foreach ($foundDupes as $dupes) {
250 if ($dstID == $userId) {
256 * Eliminate this since it introduces 3 queries PER merge row
257 * and hence is very expensive
259 * if ( !array_key_exists( $srcID, $permission ) ) {
260 * $permission[$srcID] = CRM_Contact_BAO_Contact_Permission::allow( $srcID, CRM_Core_Permission::EDIT );
262 * if ( !array_key_exists( $dstID, $permission ) ) {
263 * $permission[$dstID] = CRM_Contact_BAO_Contact_Permission::allow( $dstID, CRM_Core_Permission::EDIT );
266 * $canMerge = ( $permission[$dstID] && $permission[$srcID] );
270 // we'll do permission checking during the merge process
273 $mainContacts[] = $row = array(
275 'srcName' => $displayNames[$srcID],
277 'dstName' => $displayNames[$dstID],
278 'weight' => $dupes[2],
279 'canMerge' => $canMerge,
282 $data = CRM_Core_DAO
::escapeString(serialize($row));
283 $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) ";
291 $this->_rgid
= $rgid;
292 $this->_mainContacts
= $mainContacts;
294 CRM_Core_BAO_PrevNextCache
::setItem($values);
295 $session = CRM_Core_Session
::singleton();
297 $session->pushUserContext(CRM_Utils_System
::url('civicrm/contact/deduperules',
298 "action=update&rgid={$this->_rgid}&gid={$this->_gid}&cid={$this->_cid}"
302 $session->pushUserContext(CRM_Utils_System
::url('civicrm/contact/dedupefind',
303 "reset=1&action=update&rgid={$this->_rgid}"
315 $this->_rgid
= $rgid;
318 $this->assign('action', $this->action
);
322 $this->action
= CRM_Core_Action
::UPDATE
;
323 $this->edit($this->action
);
324 $this->assign('action', $this->action
);
326 $this->assign('context', $context);
329 return parent
::run();
333 * Browse all rule groups.
335 public function browse() {
336 $this->assign('main_contacts', $this->_mainContacts
);
339 $this->assign('cid', $this->_cid
);
341 if (isset($this->_gid
) ||
$this->_gid
) {
342 $this->assign('gid', $this->_gid
);
344 $this->assign('rgid', $this->_rgid
);
348 * Get name of edit form.
351 * classname of edit form
353 public function editForm() {
354 return 'CRM_Contact_Form_DedupeFind';
358 * Get edit form name.
363 public function editName() {
375 public function userContext($mode = NULL) {
376 return 'civicrm/contact/dedupefind';