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;
44 * @return string Classname of BAO.
46 public function getBAOName() {
47 return 'CRM_Dedupe_BAO_RuleGroup';
53 * @return array (reference) of action links
55 public function &links() {}
58 * Browse all rule groups
62 public function run() {
63 $gid = CRM_Utils_Request
::retrieve('gid', 'Positive', $this, FALSE, 0);
64 $action = CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 0);
65 $context = CRM_Utils_Request
::retrieve('context', 'String', $this);
67 $session = CRM_Core_Session
::singleton();
68 $contactIds = $session->get('selectedSearchContactIds');
69 if ($context == 'search' ||
!empty($contactIds)) {
71 $this->assign('backURL', $session->readUserContext());
74 if ($action & CRM_Core_Action
::RENEW
) {
76 $rgid = CRM_Utils_Request
::retrieve('rgid', 'Positive', $this, FALSE, 0);
79 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
80 $cacheKeyString = "merge $contactType";
81 $cacheKeyString .= $rgid ?
"_{$rgid}" : '_0';
82 $cacheKeyString .= $gid ?
"_{$gid}" : '_0';
83 CRM_Core_BAO_PrevNextCache
::deleteItem(NULL, $cacheKeyString);
85 $urlQry = "reset=1&action=update&rgid={$rgid}";
87 $urlQry .= "&gid={$gid}";
89 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/dedupefind', $urlQry));
91 elseif ($action & CRM_Core_Action
::MAP
) {
92 // do a batch merge if requested
93 $rgid = CRM_Utils_Request
::retrieve('rgid', 'Positive', $this, FALSE, 0);
94 $result = CRM_Dedupe_Merger
::batchMerge($rgid, $gid, 'safe', TRUE, TRUE);
96 $skippedCount = CRM_Utils_Request
::retrieve('skipped', 'Positive', $this, FALSE, 0);
97 $skippedCount = $skippedCount +
count($result['skipped']);
98 $mergedCount = CRM_Utils_Request
::retrieve('merged', 'Positive', $this, FALSE, 0);
99 $mergedCount = $mergedCount +
count($result['merged']);
101 if (empty($result['merged']) && empty($result['skipped'])) {
103 if ($mergedCount >= 1) {
104 $message = ts("%1 pairs of duplicates were merged", array(1 => $mergedCount));
106 if ($skippedCount >= 1) {
107 $message = $message ?
"{$message} and " : '';
108 $message .= ts("%1 pairs of duplicates were skipped due to conflict",
109 array(1 => $skippedCount)
112 $message .= ts(" during the batch merge process with safe mode.");
113 CRM_Core_Session
::setStatus($message, ts('Merge Complete'), 'success');
115 $urlQry = "reset=1&action=update&rgid={$rgid}";
117 $urlQry .= "&gid={$gid}";
119 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/dedupefind', $urlQry));
122 $urlQry = "reset=1&action=map&rgid={$rgid}";
124 $urlQry .= "&gid={$gid}";
126 $urlQry .= "&skipped={$skippedCount}&merged={$mergedCount}";
127 CRM_Utils_System
::jsRedirect(
128 CRM_Utils_System
::url('civicrm/contact/dedupefind', $urlQry),
129 ts('Batch Merge Task in progress'),
130 ts('The batch merge task is still in progress. This page will be refreshed automatically.')
135 if ($action & CRM_Core_Action
::UPDATE ||
136 $action & CRM_Core_Action
::BROWSE
138 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this, FALSE, 0);
139 $rgid = CRM_Utils_Request
::retrieve('rgid', 'Positive', $this, FALSE, 0);
140 $this->action
= CRM_Core_Action
::UPDATE
;
142 //calculate the $contactType
144 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup',
150 $sourceParams = 'snippet=4';
152 $sourceParams .= "&gid={$gid}";
155 $sourceParams .= "&rgid={$rgid}";
158 $this->assign('sourceUrl', CRM_Utils_System
::url('civicrm/ajax/dedupefind', $sourceParams, FALSE, NULL, FALSE));
160 //reload from cache table
161 $cacheKeyString = "merge $contactType";
162 $cacheKeyString .= $rgid ?
"_{$rgid}" : '_0';
163 $cacheKeyString .= $gid ?
"_{$gid}" : '_0';
165 $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
166 pn.entity_id2 = de.contact_id2 )";
167 $where = "de.id IS NULL";
168 $this->_mainContacts
= CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, $join, $where);
169 if (empty($this->_mainContacts
)) {
171 $foundDupes = $this->get("dedupe_dupes_$gid");
173 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($rgid, $gid);
175 $this->set("dedupe_dupes_$gid", $foundDupes);
177 elseif (!empty($contactIds)) {
178 $foundDupes = $this->get("search_dedupe_dupes_$gid");
180 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid, $contactIds);
182 $this->get("search_dedupe_dupes_$gid", $foundDupes);
185 $foundDupes = $this->get('dedupe_dupes');
187 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid);
189 $this->set('dedupe_dupes', $foundDupes);
192 $ruleGroup = new CRM_Dedupe_BAO_RuleGroup();
193 $ruleGroup->id
= $rgid;
194 $ruleGroup->find(TRUE);
196 $session = CRM_Core_Session
::singleton();
197 $session->setStatus(ts('No possible duplicates were found using %1 rule.', array(1 => $ruleGroup->name
)), ts('None Found'), 'info');
198 $url = CRM_Utils_System
::url('civicrm/contact/deduperules', 'reset=1');
199 if ($context == 'search') {
200 $url = $session->readUserContext();
202 CRM_Utils_System
::redirect($url);
206 foreach ($foundDupes as $dupe) {
210 $cidString = implode(', ', array_keys($cids));
211 $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name";
212 $dao = new CRM_Core_DAO();
214 $displayNames = array();
215 while ($dao->fetch()) {
216 $displayNames[$dao->id
] = $dao->display_name
;
219 // FIXME: sort the contacts; $displayName
220 // is already sort_name-sorted, so use that
221 // (also, consider sorting by dupe count first)
222 // lobo - change the sort to by threshold value
223 // so the more likely dupes are sorted first
224 $session = CRM_Core_Session
::singleton();
225 $userId = $session->get('userID');
226 $mainContacts = $permission = array();
228 foreach ($foundDupes as $dupes) {
231 if ($dstID == $userId) {
237 * Eliminate this since it introduces 3 queries PER merge row
238 * and hence is very expensive
240 if ( !array_key_exists( $srcID, $permission ) ) {
241 $permission[$srcID] = CRM_Contact_BAO_Contact_Permission::allow( $srcID, CRM_Core_Permission::EDIT );
243 if ( !array_key_exists( $dstID, $permission ) ) {
244 $permission[$dstID] = CRM_Contact_BAO_Contact_Permission::allow( $dstID, CRM_Core_Permission::EDIT );
247 $canMerge = ( $permission[$dstID] && $permission[$srcID] );
251 // we'll do permission checking during the merge process
254 $mainContacts[] = $row = array(
256 'srcName' => $displayNames[$srcID],
258 'dstName' => $displayNames[$dstID],
259 'weight' => $dupes[2],
260 'canMerge' => $canMerge,
263 $data = CRM_Core_DAO
::escapeString(serialize($row));
264 $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) ";
272 $this->_rgid
= $rgid;
273 $this->_mainContacts
= $mainContacts;
275 CRM_Core_BAO_PrevNextCache
::setItem($values);
276 $session = CRM_Core_Session
::singleton();
278 $session->pushUserContext(CRM_Utils_System
::url('civicrm/contact/deduperules',
279 "action=update&rgid={$this->_rgid}&gid={$this->_gid}&cid={$this->_cid}"
283 $session->pushUserContext(CRM_Utils_System
::url('civicrm/contact/dedupefind',
284 "reset=1&action=update&rgid={$this->_rgid}"
296 $this->_rgid
= $rgid;
299 $this->assign('action', $this->action
);
303 $this->action
= CRM_Core_Action
::UPDATE
;
304 $this->edit($this->action
);
305 $this->assign('action', $this->action
);
307 $this->assign('context', $context);
310 return parent
::run();
314 * Browse all rule groups
318 public function browse() {
319 $this->assign('main_contacts', $this->_mainContacts
);
322 $this->assign('cid', $this->_cid
);
324 if (isset($this->_gid
) ||
$this->_gid
) {
325 $this->assign('gid', $this->_gid
);
327 $this->assign('rgid', $this->_rgid
);
331 * Get name of edit form
333 * @return string classname of edit form
335 public function editForm() {
336 return 'CRM_Contact_Form_DedupeFind';
342 * @return string name of this page
344 public function editName() {
353 * @return string user context
355 public function userContext($mode = NULL) {
356 return 'civicrm/contact/dedupefind';