3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 class CRM_Contact_Page_DedupeFind
extends CRM_Core_Page_Basic
{
36 protected $_cid = NULL;
38 protected $_mainContacts;
47 public function getBAOName() {
48 return 'CRM_Dedupe_BAO_RuleGroup';
55 * (reference) of action links
57 public function &links() {
61 * Browse all rule groups.
65 public function run() {
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);
70 $session = CRM_Core_Session
::singleton();
71 $contactIds = $session->get('selectedSearchContactIds');
72 if ($context == 'search' ||
!empty($contactIds)) {
74 $this->assign('backURL', $session->readUserContext());
77 if ($action & CRM_Core_Action
::RENEW
) {
79 $rgid = CRM_Utils_Request
::retrieve('rgid', 'Positive', $this, FALSE, 0);
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);
88 $urlQry = "reset=1&action=update&rgid={$rgid}";
90 $urlQry .= "&gid={$gid}";
92 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/dedupefind', $urlQry));
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, 75);
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']);
104 if (empty($result['merged']) && empty($result['skipped'])) {
106 if ($mergedCount >= 1) {
107 $message = ts("%1 pairs of duplicates were merged", array(1 => $mergedCount));
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)
115 $message .= ts(" during the batch merge process with safe mode.");
116 CRM_Core_Session
::setStatus($message, ts('Merge Complete'), 'success');
118 $urlQry = "reset=1&action=update&rgid={$rgid}";
120 $urlQry .= "&gid={$gid}";
122 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/dedupefind', $urlQry));
125 $urlQry = "reset=1&action=map&rgid={$rgid}";
127 $urlQry .= "&gid={$gid}";
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.')
138 if ($action & CRM_Core_Action
::UPDATE ||
139 $action & CRM_Core_Action
::BROWSE
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
;
145 //calculate the $contactType
147 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup',
153 $sourceParams = 'snippet=4';
155 $sourceParams .= "&gid={$gid}";
158 $sourceParams .= "&rgid={$rgid}";
160 if ($context == 'conflicts') {
161 $sourceParams .= "&selected=1";
164 $this->assign('sourceUrl', CRM_Utils_System
::url('civicrm/ajax/dedupefind', $sourceParams, FALSE, NULL, FALSE));
166 //reload from cache table
167 $cacheKeyString = "merge $contactType";
168 $cacheKeyString .= $rgid ?
"_{$rgid}" : '_0';
169 $cacheKeyString .= $gid ?
"_{$gid}" : '_0';
171 $stats = CRM_Dedupe_Merger
::getMergeStatsMsg($cacheKeyString);
173 CRM_Core_Session
::setStatus($stats);
174 // reset so we not displaying same message again
175 CRM_Dedupe_Merger
::resetMergeStats($cacheKeyString);
177 $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
178 pn.entity_id2 = de.contact_id2 )";
179 $where = "de.id IS NULL";
180 if ($context == 'conflicts') {
181 $where .= " AND pn.is_selected = 1";
183 $this->_mainContacts
= CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, $join, $where);
184 if (empty($this->_mainContacts
)) {
185 if ($context == 'conflicts') {
186 // if the current screen was intended to list only selected contacts, move back to full dupe list
187 $sourceParams = 'reset=1&action=update';
189 $sourceParams .= "&gid={$gid}";
192 $sourceParams .= "&rgid={$rgid}";
194 CRM_Utils_System
::redirect(CRM_Utils_System
::url(CRM_Utils_System
::currentPath(), $sourceParams));
197 $foundDupes = $this->get("dedupe_dupes_$gid");
199 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($rgid, $gid);
201 $this->set("dedupe_dupes_$gid", $foundDupes);
203 elseif (!empty($contactIds)) {
204 $foundDupes = $this->get("search_dedupe_dupes_$gid");
206 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid, $contactIds);
208 $this->get("search_dedupe_dupes_$gid", $foundDupes);
211 $foundDupes = $this->get('dedupe_dupes');
213 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid);
215 $this->set('dedupe_dupes', $foundDupes);
218 $ruleGroup = new CRM_Dedupe_BAO_RuleGroup();
219 $ruleGroup->id
= $rgid;
220 $ruleGroup->find(TRUE);
222 $session = CRM_Core_Session
::singleton();
223 $session->setStatus(ts('No possible duplicates were found using %1 rule.', array(1 => $ruleGroup->name
)), ts('None Found'), 'info');
224 $url = CRM_Utils_System
::url('civicrm/contact/deduperules', 'reset=1');
225 if ($context == 'search') {
226 $url = $session->readUserContext();
228 CRM_Utils_System
::redirect($url);
232 foreach ($foundDupes as $dupe) {
236 $cidString = implode(', ', array_keys($cids));
237 $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name";
238 $dao = new CRM_Core_DAO();
240 $displayNames = array();
241 while ($dao->fetch()) {
242 $displayNames[$dao->id
] = $dao->display_name
;
245 // FIXME: sort the contacts; $displayName
246 // is already sort_name-sorted, so use that
247 // (also, consider sorting by dupe count first)
248 // lobo - change the sort to by threshold value
249 // so the more likely dupes are sorted first
250 $session = CRM_Core_Session
::singleton();
251 $userId = $session->get('userID');
252 $mainContacts = $permission = array();
254 foreach ($foundDupes as $dupes) {
257 if ($dstID == $userId) {
263 * Eliminate this since it introduces 3 queries PER merge row
264 * and hence is very expensive
266 * if ( !array_key_exists( $srcID, $permission ) ) {
267 * $permission[$srcID] = CRM_Contact_BAO_Contact_Permission::allow( $srcID, CRM_Core_Permission::EDIT );
269 * if ( !array_key_exists( $dstID, $permission ) ) {
270 * $permission[$dstID] = CRM_Contact_BAO_Contact_Permission::allow( $dstID, CRM_Core_Permission::EDIT );
273 * $canMerge = ( $permission[$dstID] && $permission[$srcID] );
277 // we'll do permission checking during the merge process
280 $mainContacts[] = $row = array(
282 'srcName' => $displayNames[$srcID],
284 'dstName' => $displayNames[$dstID],
285 'weight' => $dupes[2],
286 'canMerge' => $canMerge,
289 $data = CRM_Core_DAO
::escapeString(serialize($row));
290 $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) ";
298 $this->_rgid
= $rgid;
299 $this->_mainContacts
= $mainContacts;
301 CRM_Core_BAO_PrevNextCache
::setItem($values);
302 $session = CRM_Core_Session
::singleton();
304 $session->pushUserContext(CRM_Utils_System
::url('civicrm/contact/deduperules',
305 "action=update&rgid={$this->_rgid}&gid={$this->_gid}&cid={$this->_cid}"
309 $session->pushUserContext(CRM_Utils_System
::url('civicrm/contact/dedupefind',
310 "reset=1&action=update&rgid={$this->_rgid}"
322 $this->_rgid
= $rgid;
325 $this->assign('action', $this->action
);
329 $this->action
= CRM_Core_Action
::UPDATE
;
330 $this->edit($this->action
);
331 $this->assign('action', $this->action
);
333 $this->assign('context', $context);
336 return parent
::run();
340 * Browse all rule groups.
344 public function browse() {
345 $this->assign('main_contacts', $this->_mainContacts
);
348 $this->assign('cid', $this->_cid
);
350 if (isset($this->_gid
) ||
$this->_gid
) {
351 $this->assign('gid', $this->_gid
);
353 $this->assign('rgid', $this->_rgid
);
357 * Get name of edit form.
360 * classname of edit form
362 public function editForm() {
363 return 'CRM_Contact_Form_DedupeFind';
367 * Get edit form name.
372 public function editName() {
384 public function userContext($mode = NULL) {
385 return 'civicrm/contact/dedupefind';