3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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, TRUE);
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}";
161 $this->assign('sourceUrl', CRM_Utils_System
::url('civicrm/ajax/dedupefind', $sourceParams, FALSE, NULL, FALSE));
163 //reload from cache table
164 $cacheKeyString = "merge $contactType";
165 $cacheKeyString .= $rgid ?
"_{$rgid}" : '_0';
166 $cacheKeyString .= $gid ?
"_{$gid}" : '_0';
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
)) {
174 $foundDupes = $this->get("dedupe_dupes_$gid");
176 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($rgid, $gid);
178 $this->set("dedupe_dupes_$gid", $foundDupes);
180 elseif (!empty($contactIds)) {
181 $foundDupes = $this->get("search_dedupe_dupes_$gid");
183 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid, $contactIds);
185 $this->get("search_dedupe_dupes_$gid", $foundDupes);
188 $foundDupes = $this->get('dedupe_dupes');
190 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid);
192 $this->set('dedupe_dupes', $foundDupes);
195 $ruleGroup = new CRM_Dedupe_BAO_RuleGroup();
196 $ruleGroup->id
= $rgid;
197 $ruleGroup->find(TRUE);
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();
205 CRM_Utils_System
::redirect($url);
209 foreach ($foundDupes as $dupe) {
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();
217 $displayNames = array();
218 while ($dao->fetch()) {
219 $displayNames[$dao->id
] = $dao->display_name
;
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();
231 foreach ($foundDupes as $dupes) {
234 if ($dstID == $userId) {
240 * Eliminate this since it introduces 3 queries PER merge row
241 * and hence is very expensive
243 * if ( !array_key_exists( $srcID, $permission ) ) {
244 * $permission[$srcID] = CRM_Contact_BAO_Contact_Permission::allow( $srcID, CRM_Core_Permission::EDIT );
246 * if ( !array_key_exists( $dstID, $permission ) ) {
247 * $permission[$dstID] = CRM_Contact_BAO_Contact_Permission::allow( $dstID, CRM_Core_Permission::EDIT );
250 * $canMerge = ( $permission[$dstID] && $permission[$srcID] );
254 // we'll do permission checking during the merge process
257 $mainContacts[] = $row = array(
259 'srcName' => $displayNames[$srcID],
261 'dstName' => $displayNames[$dstID],
262 'weight' => $dupes[2],
263 'canMerge' => $canMerge,
266 $data = CRM_Core_DAO
::escapeString(serialize($row));
267 $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) ";
275 $this->_rgid
= $rgid;
276 $this->_mainContacts
= $mainContacts;
278 CRM_Core_BAO_PrevNextCache
::setItem($values);
279 $session = CRM_Core_Session
::singleton();
281 $session->pushUserContext(CRM_Utils_System
::url('civicrm/contact/deduperules',
282 "action=update&rgid={$this->_rgid}&gid={$this->_gid}&cid={$this->_cid}"
286 $session->pushUserContext(CRM_Utils_System
::url('civicrm/contact/dedupefind',
287 "reset=1&action=update&rgid={$this->_rgid}"
299 $this->_rgid
= $rgid;
302 $this->assign('action', $this->action
);
306 $this->action
= CRM_Core_Action
::UPDATE
;
307 $this->edit($this->action
);
308 $this->assign('action', $this->action
);
310 $this->assign('context', $context);
313 return parent
::run();
317 * Browse all rule groups.
321 public function browse() {
322 $this->assign('main_contacts', $this->_mainContacts
);
325 $this->assign('cid', $this->_cid
);
327 if (isset($this->_gid
) ||
$this->_gid
) {
328 $this->assign('gid', $this->_gid
);
330 $this->assign('rgid', $this->_rgid
);
334 * Get name of edit form.
337 * classname of edit form
339 public function editForm() {
340 return 'CRM_Contact_Form_DedupeFind';
344 * Get edit form name.
349 public function editName() {
361 public function userContext($mode = NULL) {
362 return 'civicrm/contact/dedupefind';