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);
62 $limit = CRM_Utils_Request
::retrieve('limit', 'Integer', $this);
64 $session = CRM_Core_Session
::singleton();
65 $contactIds = $session->get('selectedSearchContactIds');
66 if ($context == 'search' ||
!empty($contactIds)) {
68 $this->assign('backURL', $session->readUserContext());
71 if ($action & CRM_Core_Action
::RENEW
) {
73 $rgid = CRM_Utils_Request
::retrieve('rgid', 'Positive', $this, FALSE, 0);
76 CRM_Core_BAO_PrevNextCache
::deleteItem(NULL, CRM_Dedupe_Merger
::getMergeCacheKeyString($rgid, $gid));
78 $urlQry = "reset=1&action=update&rgid={$rgid}";
80 $urlQry .= "&gid={$gid}";
82 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/dedupefind', $urlQry));
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);
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']);
94 if (empty($result['merged']) && empty($result['skipped'])) {
96 if ($mergedCount >= 1) {
97 $message = ts("%1 pairs of duplicates were merged", array(1 => $mergedCount));
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)
105 $message .= ts(" during the batch merge process with safe mode.");
106 CRM_Core_Session
::setStatus($message, ts('Merge Complete'), 'success');
108 $urlQry = "reset=1&action=update&rgid={$rgid}";
110 $urlQry .= "&gid={$gid}";
112 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/dedupefind', $urlQry));
115 $urlQry = "reset=1&action=map&rgid={$rgid}";
117 $urlQry .= "&gid={$gid}";
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.')
128 if ($action & CRM_Core_Action
::UPDATE ||
129 $action & CRM_Core_Action
::BROWSE
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
;
135 $sourceParams = 'snippet=4';
137 $sourceParams .= "&gid={$gid}";
140 $sourceParams .= "&rgid={$rgid}";
142 if ($context == 'conflicts') {
143 $sourceParams .= "&selected=1";
146 $this->assign('sourceUrl', CRM_Utils_System
::url('civicrm/ajax/dedupefind', $sourceParams, FALSE, NULL, FALSE));
148 //reload from cache table
149 $cacheKeyString = CRM_Dedupe_Merger
::getMergeCacheKeyString($rgid, $gid);
151 $stats = CRM_Dedupe_Merger
::getMergeStatsMsg($cacheKeyString);
153 CRM_Core_Session
::setStatus($stats);
154 // reset so we not displaying same message again
155 CRM_Dedupe_Merger
::resetMergeStats($cacheKeyString);
157 $join = CRM_Dedupe_Merger
::getJoinOnDedupeTable();
158 $where = "de.id IS NULL";
159 if ($context == 'conflicts') {
160 $where .= " AND pn.is_selected = 1";
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';
168 $sourceParams .= "&gid={$gid}";
171 $sourceParams .= "&rgid={$rgid}";
173 CRM_Utils_System
::redirect(CRM_Utils_System
::url(CRM_Utils_System
::currentPath(), $sourceParams));
176 $foundDupes = $this->get("dedupe_dupes_$gid");
178 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($rgid, $gid, $limit);
180 $this->set("dedupe_dupes_$gid", $foundDupes);
182 elseif (!empty($contactIds)) {
183 $foundDupes = $this->get("search_dedupe_dupes_$gid");
185 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid, $contactIds);
187 $this->get("search_dedupe_dupes_$gid", $foundDupes);
190 $foundDupes = $this->get('dedupe_dupes');
192 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid, array(), TRUE, $limit);
194 $this->set('dedupe_dupes', $foundDupes);
197 $ruleGroup = new CRM_Dedupe_BAO_RuleGroup();
198 $ruleGroup->id
= $rgid;
199 $ruleGroup->find(TRUE);
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();
207 CRM_Utils_System
::redirect($url);
211 foreach ($foundDupes as $dupe) {
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();
219 $displayNames = array();
220 while ($dao->fetch()) {
221 $displayNames[$dao->id
] = $dao->display_name
;
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();
232 foreach ($foundDupes as $dupes) {
235 if ($dstID == $userId) {
241 * Eliminate this since it introduces 3 queries PER merge row
242 * and hence is very expensive
244 * if ( !array_key_exists( $srcID, $permission ) ) {
245 * $permission[$srcID] = CRM_Contact_BAO_Contact_Permission::allow( $srcID, CRM_Core_Permission::EDIT );
247 * if ( !array_key_exists( $dstID, $permission ) ) {
248 * $permission[$dstID] = CRM_Contact_BAO_Contact_Permission::allow( $dstID, CRM_Core_Permission::EDIT );
251 * $canMerge = ( $permission[$dstID] && $permission[$srcID] );
255 // we'll do permission checking during the merge process
258 $mainContacts[] = $row = array(
260 'srcName' => $displayNames[$srcID],
262 'dstName' => $displayNames[$dstID],
263 'weight' => $dupes[2],
264 'canMerge' => $canMerge,
267 $data = CRM_Core_DAO
::escapeString(serialize($row));
268 $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) ";
276 $this->_rgid
= $rgid;
277 $this->_mainContacts
= $mainContacts;
279 CRM_Core_BAO_PrevNextCache
::setItem($values);
280 $session = CRM_Core_Session
::singleton();
282 $session->pushUserContext(CRM_Utils_System
::url('civicrm/contact/deduperules',
283 "action=update&rgid={$this->_rgid}&gid={$this->_gid}&cid={$this->_cid}"
287 $session->pushUserContext(CRM_Utils_System
::url('civicrm/contact/dedupefind',
288 "reset=1&action=update&rgid={$this->_rgid}"
300 $this->_rgid
= $rgid;
303 $this->assign('action', $this->action
);
307 $this->action
= CRM_Core_Action
::UPDATE
;
308 $this->edit($this->action
);
309 $this->assign('action', $this->action
);
311 $this->assign('context', $context);
314 return parent
::run();
318 * Browse all rule groups.
320 public function browse() {
321 $this->assign('main_contacts', $this->_mainContacts
);
324 $this->assign('cid', $this->_cid
);
326 if (isset($this->_gid
) ||
$this->_gid
) {
327 $this->assign('gid', $this->_gid
);
329 $this->assign('rgid', $this->_rgid
);
333 * Get name of edit form.
336 * classname of edit form
338 public function editForm() {
339 return 'CRM_Contact_Form_DedupeFind';
343 * Get edit form name.
348 public function editName() {
360 public function userContext($mode = NULL) {
361 return 'civicrm/contact/dedupefind';