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 CRM_Core_BAO_PrevNextCache
::deleteItem(NULL, CRM_Dedupe_Merger
::getMergeCacheKeyString($rgid, $gid));
77 $urlQry = "reset=1&action=update&rgid={$rgid}";
79 $urlQry .= "&gid={$gid}";
81 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/dedupefind', $urlQry));
83 elseif ($action & CRM_Core_Action
::MAP
) {
84 // do a batch merge if requested
85 $rgid = CRM_Utils_Request
::retrieve('rgid', 'Positive', $this, FALSE, 0);
86 $result = CRM_Dedupe_Merger
::batchMerge($rgid, $gid, 'safe', TRUE, 75);
88 $skippedCount = CRM_Utils_Request
::retrieve('skipped', 'Positive', $this, FALSE, 0);
89 $skippedCount = $skippedCount +
count($result['skipped']);
90 $mergedCount = CRM_Utils_Request
::retrieve('merged', 'Positive', $this, FALSE, 0);
91 $mergedCount = $mergedCount +
count($result['merged']);
93 if (empty($result['merged']) && empty($result['skipped'])) {
95 if ($mergedCount >= 1) {
96 $message = ts("%1 pairs of duplicates were merged", array(1 => $mergedCount));
98 if ($skippedCount >= 1) {
99 $message = $message ?
"{$message} and " : '';
100 $message .= ts("%1 pairs of duplicates were skipped due to conflict",
101 array(1 => $skippedCount)
104 $message .= ts(" during the batch merge process with safe mode.");
105 CRM_Core_Session
::setStatus($message, ts('Merge Complete'), 'success');
107 $urlQry = "reset=1&action=update&rgid={$rgid}";
109 $urlQry .= "&gid={$gid}";
111 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/dedupefind', $urlQry));
114 $urlQry = "reset=1&action=map&rgid={$rgid}";
116 $urlQry .= "&gid={$gid}";
118 $urlQry .= "&skipped={$skippedCount}&merged={$mergedCount}";
119 CRM_Utils_System
::jsRedirect(
120 CRM_Utils_System
::url('civicrm/contact/dedupefind', $urlQry),
121 ts('Batch Merge Task in progress'),
122 ts('The batch merge task is still in progress. This page will be refreshed automatically.')
127 if ($action & CRM_Core_Action
::UPDATE ||
128 $action & CRM_Core_Action
::BROWSE
130 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this, FALSE, 0);
131 $rgid = CRM_Utils_Request
::retrieve('rgid', 'Positive', $this, FALSE, 0);
132 $this->action
= CRM_Core_Action
::UPDATE
;
134 $sourceParams = 'snippet=4';
136 $sourceParams .= "&gid={$gid}";
139 $sourceParams .= "&rgid={$rgid}";
141 if ($context == 'conflicts') {
142 $sourceParams .= "&selected=1";
145 $this->assign('sourceUrl', CRM_Utils_System
::url('civicrm/ajax/dedupefind', $sourceParams, FALSE, NULL, FALSE));
147 //reload from cache table
148 $cacheKeyString = CRM_Dedupe_Merger
::getMergeCacheKeyString($rgid, $gid);
150 $stats = CRM_Dedupe_Merger
::getMergeStatsMsg($cacheKeyString);
152 CRM_Core_Session
::setStatus($stats);
153 // reset so we not displaying same message again
154 CRM_Dedupe_Merger
::resetMergeStats($cacheKeyString);
156 $join = CRM_Dedupe_Merger
::getJoinOnDedupeTable();
157 $where = "de.id IS NULL";
158 if ($context == 'conflicts') {
159 $where .= " AND pn.is_selected = 1";
161 $this->_mainContacts
= CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, $join, $where);
162 if (empty($this->_mainContacts
)) {
163 if ($context == 'conflicts') {
164 // if the current screen was intended to list only selected contacts, move back to full dupe list
165 $sourceParams = 'reset=1&action=update';
167 $sourceParams .= "&gid={$gid}";
170 $sourceParams .= "&rgid={$rgid}";
172 CRM_Utils_System
::redirect(CRM_Utils_System
::url(CRM_Utils_System
::currentPath(), $sourceParams));
175 $foundDupes = $this->get("dedupe_dupes_$gid");
177 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($rgid, $gid);
179 $this->set("dedupe_dupes_$gid", $foundDupes);
181 elseif (!empty($contactIds)) {
182 $foundDupes = $this->get("search_dedupe_dupes_$gid");
184 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid, $contactIds);
186 $this->get("search_dedupe_dupes_$gid", $foundDupes);
189 $foundDupes = $this->get('dedupe_dupes');
191 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid);
193 $this->set('dedupe_dupes', $foundDupes);
196 $ruleGroup = new CRM_Dedupe_BAO_RuleGroup();
197 $ruleGroup->id
= $rgid;
198 $ruleGroup->find(TRUE);
200 $session = CRM_Core_Session
::singleton();
201 $session->setStatus(ts('No possible duplicates were found using %1 rule.', array(1 => $ruleGroup->name
)), ts('None Found'), 'info');
202 $url = CRM_Utils_System
::url('civicrm/contact/deduperules', 'reset=1');
203 if ($context == 'search') {
204 $url = $session->readUserContext();
206 CRM_Utils_System
::redirect($url);
210 foreach ($foundDupes as $dupe) {
214 $cidString = implode(', ', array_keys($cids));
215 $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name";
216 $dao = new CRM_Core_DAO();
218 $displayNames = array();
219 while ($dao->fetch()) {
220 $displayNames[$dao->id
] = $dao->display_name
;
223 // FIXME: sort the contacts; $displayName
224 // is already sort_name-sorted, so use that
225 // (also, consider sorting by dupe count first)
226 // lobo - change the sort to by threshold value
227 // so the more likely dupes are sorted first
228 $session = CRM_Core_Session
::singleton();
229 $userId = $session->get('userID');
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';