Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | */ |
33 | class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic { | |
34 | protected $_cid = NULL; | |
35 | protected $_rgid; | |
36 | protected $_mainContacts; | |
37 | protected $_gid; | |
38 | ||
39 | /** | |
fe482240 | 40 | * Get BAO Name. |
6a488035 | 41 | * |
a6c01b45 CW |
42 | * @return string |
43 | * Classname of BAO. | |
8ef12e64 | 44 | */ |
00be9182 | 45 | public function getBAOName() { |
6a488035 TO |
46 | return 'CRM_Dedupe_BAO_RuleGroup'; |
47 | } | |
48 | ||
49 | /** | |
fe482240 | 50 | * Get action Links. |
6a488035 | 51 | */ |
ce80b209 TO |
52 | public function &links() { |
53 | } | |
6a488035 TO |
54 | |
55 | /** | |
fe482240 | 56 | * Browse all rule groups. |
6a488035 | 57 | */ |
00be9182 | 58 | public function run() { |
353ffa53 TO |
59 | $gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0); |
60 | $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 0); | |
6a488035 TO |
61 | $context = CRM_Utils_Request::retrieve('context', 'String', $this); |
62 | ||
63 | $session = CRM_Core_Session::singleton(); | |
64 | $contactIds = $session->get('selectedSearchContactIds'); | |
65 | if ($context == 'search' || !empty($contactIds)) { | |
66 | $context = 'search'; | |
67 | $this->assign('backURL', $session->readUserContext()); | |
68 | } | |
69 | ||
70 | if ($action & CRM_Core_Action::RENEW) { | |
71 | // empty cache | |
72 | $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this, FALSE, 0); | |
73 | ||
74 | if ($rgid) { | |
2ae26001 | 75 | CRM_Core_BAO_PrevNextCache::deleteItem(NULL, CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid)); |
6a488035 TO |
76 | } |
77 | $urlQry = "reset=1&action=update&rgid={$rgid}"; | |
78 | if ($gid) { | |
79 | $urlQry .= "&gid={$gid}"; | |
80 | } | |
81 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry)); | |
82 | } | |
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); | |
63ef778e | 86 | $result = CRM_Dedupe_Merger::batchMerge($rgid, $gid, 'safe', TRUE, 75); |
6a488035 TO |
87 | |
88 | $skippedCount = CRM_Utils_Request::retrieve('skipped', 'Positive', $this, FALSE, 0); | |
89 | $skippedCount = $skippedCount + count($result['skipped']); | |
353ffa53 TO |
90 | $mergedCount = CRM_Utils_Request::retrieve('merged', 'Positive', $this, FALSE, 0); |
91 | $mergedCount = $mergedCount + count($result['merged']); | |
6a488035 TO |
92 | |
93 | if (empty($result['merged']) && empty($result['skipped'])) { | |
94 | $message = ''; | |
95 | if ($mergedCount >= 1) { | |
96 | $message = ts("%1 pairs of duplicates were merged", array(1 => $mergedCount)); | |
97 | } | |
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) | |
102 | ); | |
103 | } | |
104 | $message .= ts(" during the batch merge process with safe mode."); | |
105 | CRM_Core_Session::setStatus($message, ts('Merge Complete'), 'success'); | |
106 | ||
107 | $urlQry = "reset=1&action=update&rgid={$rgid}"; | |
108 | if ($gid) { | |
109 | $urlQry .= "&gid={$gid}"; | |
110 | } | |
111 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry)); | |
112 | } | |
113 | else { | |
114 | $urlQry = "reset=1&action=map&rgid={$rgid}"; | |
115 | if ($gid) { | |
116 | $urlQry .= "&gid={$gid}"; | |
117 | } | |
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.') | |
123 | ); | |
124 | } | |
125 | } | |
126 | ||
127 | if ($action & CRM_Core_Action::UPDATE || | |
128 | $action & CRM_Core_Action::BROWSE | |
129 | ) { | |
353ffa53 TO |
130 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0); |
131 | $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this, FALSE, 0); | |
6a488035 TO |
132 | $this->action = CRM_Core_Action::UPDATE; |
133 | ||
6a488035 TO |
134 | $sourceParams = 'snippet=4'; |
135 | if ($gid) { | |
136 | $sourceParams .= "&gid={$gid}"; | |
137 | } | |
138 | if ($rgid) { | |
139 | $sourceParams .= "&rgid={$rgid}"; | |
140 | } | |
63ef778e | 141 | if ($context == 'conflicts') { |
142 | $sourceParams .= "&selected=1"; | |
143 | } | |
6a488035 TO |
144 | |
145 | $this->assign('sourceUrl', CRM_Utils_System::url('civicrm/ajax/dedupefind', $sourceParams, FALSE, NULL, FALSE)); | |
146 | ||
147 | //reload from cache table | |
2ae26001 | 148 | $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid); |
6a488035 | 149 | |
63ef778e | 150 | $stats = CRM_Dedupe_Merger::getMergeStatsMsg($cacheKeyString); |
151 | if ($stats) { | |
152 | CRM_Core_Session::setStatus($stats); | |
153 | // reset so we not displaying same message again | |
154 | CRM_Dedupe_Merger::resetMergeStats($cacheKeyString); | |
155 | } | |
2ae26001 | 156 | $join = CRM_Dedupe_Merger::getJoinOnDedupeTable(); |
6a488035 | 157 | $where = "de.id IS NULL"; |
63ef778e | 158 | if ($context == 'conflicts') { |
159 | $where .= " AND pn.is_selected = 1"; | |
160 | } | |
6a488035 TO |
161 | $this->_mainContacts = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, $join, $where); |
162 | if (empty($this->_mainContacts)) { | |
63ef778e | 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'; | |
166 | if ($gid) { | |
167 | $sourceParams .= "&gid={$gid}"; | |
168 | } | |
169 | if ($rgid) { | |
170 | $sourceParams .= "&rgid={$rgid}"; | |
171 | } | |
172 | CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), $sourceParams)); | |
173 | } | |
6a488035 TO |
174 | if ($gid) { |
175 | $foundDupes = $this->get("dedupe_dupes_$gid"); | |
176 | if (!$foundDupes) { | |
177 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($rgid, $gid); | |
178 | } | |
179 | $this->set("dedupe_dupes_$gid", $foundDupes); | |
180 | } | |
181 | elseif (!empty($contactIds)) { | |
182 | $foundDupes = $this->get("search_dedupe_dupes_$gid"); | |
183 | if (!$foundDupes) { | |
184 | $foundDupes = CRM_Dedupe_Finder::dupes($rgid, $contactIds); | |
185 | } | |
186 | $this->get("search_dedupe_dupes_$gid", $foundDupes); | |
187 | } | |
188 | else { | |
189 | $foundDupes = $this->get('dedupe_dupes'); | |
190 | if (!$foundDupes) { | |
191 | $foundDupes = CRM_Dedupe_Finder::dupes($rgid); | |
192 | } | |
193 | $this->set('dedupe_dupes', $foundDupes); | |
194 | } | |
195 | if (!$foundDupes) { | |
196 | $ruleGroup = new CRM_Dedupe_BAO_RuleGroup(); | |
197 | $ruleGroup->id = $rgid; | |
198 | $ruleGroup->find(TRUE); | |
199 | ||
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(); | |
205 | } | |
206 | CRM_Utils_System::redirect($url); | |
207 | } | |
208 | else { | |
209 | $cids = array(); | |
210 | foreach ($foundDupes as $dupe) { | |
211 | $cids[$dupe[0]] = 1; | |
212 | $cids[$dupe[1]] = 1; | |
213 | } | |
214 | $cidString = implode(', ', array_keys($cids)); | |
353ffa53 TO |
215 | $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name"; |
216 | $dao = new CRM_Core_DAO(); | |
6a488035 TO |
217 | $dao->query($sql); |
218 | $displayNames = array(); | |
219 | while ($dao->fetch()) { | |
220 | $displayNames[$dao->id] = $dao->display_name; | |
221 | } | |
222 | ||
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 | |
353ffa53 TO |
228 | $session = CRM_Core_Session::singleton(); |
229 | $userId = $session->get('userID'); | |
6a488035 TO |
230 | $mainContacts = $permission = array(); |
231 | ||
232 | foreach ($foundDupes as $dupes) { | |
233 | $srcID = $dupes[0]; | |
234 | $dstID = $dupes[1]; | |
235 | if ($dstID == $userId) { | |
236 | $srcID = $dupes[1]; | |
237 | $dstID = $dupes[0]; | |
238 | } | |
239 | ||
240 | /*** | |
241 | * Eliminate this since it introduces 3 queries PER merge row | |
242 | * and hence is very expensive | |
243 | * CRM-8822 | |
353ffa53 TO |
244 | * if ( !array_key_exists( $srcID, $permission ) ) { |
245 | * $permission[$srcID] = CRM_Contact_BAO_Contact_Permission::allow( $srcID, CRM_Core_Permission::EDIT ); | |
246 | * } | |
247 | * if ( !array_key_exists( $dstID, $permission ) ) { | |
248 | * $permission[$dstID] = CRM_Contact_BAO_Contact_Permission::allow( $dstID, CRM_Core_Permission::EDIT ); | |
249 | * } | |
250 | * | |
251 | * $canMerge = ( $permission[$dstID] && $permission[$srcID] ); | |
6a488035 TO |
252 | * |
253 | */ | |
254 | ||
255 | // we'll do permission checking during the merge process | |
256 | $canMerge = TRUE; | |
257 | ||
258 | $mainContacts[] = $row = array( | |
259 | 'srcID' => $srcID, | |
260 | 'srcName' => $displayNames[$srcID], | |
261 | 'dstID' => $dstID, | |
262 | 'dstName' => $displayNames[$dstID], | |
263 | 'weight' => $dupes[2], | |
264 | 'canMerge' => $canMerge, | |
265 | ); | |
266 | ||
267 | $data = CRM_Core_DAO::escapeString(serialize($row)); | |
268 | $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) "; | |
269 | } | |
270 | if ($cid) { | |
271 | $this->_cid = $cid; | |
272 | } | |
273 | if ($gid) { | |
274 | $this->_gid = $gid; | |
275 | } | |
276 | $this->_rgid = $rgid; | |
277 | $this->_mainContacts = $mainContacts; | |
278 | ||
279 | CRM_Core_BAO_PrevNextCache::setItem($values); | |
280 | $session = CRM_Core_Session::singleton(); | |
281 | if ($this->_cid) { | |
282 | $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/deduperules', | |
353ffa53 TO |
283 | "action=update&rgid={$this->_rgid}&gid={$this->_gid}&cid={$this->_cid}" |
284 | )); | |
6a488035 TO |
285 | } |
286 | else { | |
287 | $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/dedupefind', | |
353ffa53 TO |
288 | "reset=1&action=update&rgid={$this->_rgid}" |
289 | )); | |
6a488035 TO |
290 | } |
291 | } | |
292 | } | |
293 | else { | |
294 | if ($cid) { | |
295 | $this->_cid = $cid; | |
296 | } | |
297 | if ($gid) { | |
298 | $this->_gid = $gid; | |
299 | } | |
300 | $this->_rgid = $rgid; | |
301 | } | |
302 | ||
303 | $this->assign('action', $this->action); | |
304 | $this->browse(); | |
305 | } | |
306 | else { | |
307 | $this->action = CRM_Core_Action::UPDATE; | |
308 | $this->edit($this->action); | |
309 | $this->assign('action', $this->action); | |
310 | } | |
311 | $this->assign('context', $context); | |
312 | ||
313 | // parent run | |
314 | return parent::run(); | |
315 | } | |
316 | ||
317 | /** | |
fe482240 | 318 | * Browse all rule groups. |
6a488035 | 319 | */ |
00be9182 | 320 | public function browse() { |
6a488035 TO |
321 | $this->assign('main_contacts', $this->_mainContacts); |
322 | ||
323 | if ($this->_cid) { | |
324 | $this->assign('cid', $this->_cid); | |
325 | } | |
326 | if (isset($this->_gid) || $this->_gid) { | |
327 | $this->assign('gid', $this->_gid); | |
328 | } | |
329 | $this->assign('rgid', $this->_rgid); | |
330 | } | |
331 | ||
332 | /** | |
fe482240 | 333 | * Get name of edit form. |
6a488035 | 334 | * |
a6c01b45 CW |
335 | * @return string |
336 | * classname of edit form | |
6a488035 | 337 | */ |
00be9182 | 338 | public function editForm() { |
6a488035 TO |
339 | return 'CRM_Contact_Form_DedupeFind'; |
340 | } | |
341 | ||
342 | /** | |
fe482240 | 343 | * Get edit form name. |
6a488035 | 344 | * |
a6c01b45 CW |
345 | * @return string |
346 | * name of this page | |
6a488035 | 347 | */ |
00be9182 | 348 | public function editName() { |
6a488035 TO |
349 | return 'DedupeFind'; |
350 | } | |
351 | ||
352 | /** | |
fe482240 | 353 | * Get user context. |
6a488035 | 354 | * |
77b97be7 EM |
355 | * @param null $mode |
356 | * | |
a6c01b45 CW |
357 | * @return string |
358 | * user context | |
6a488035 | 359 | */ |
00be9182 | 360 | public function userContext($mode = NULL) { |
6a488035 TO |
361 | return 'civicrm/contact/dedupefind'; |
362 | } | |
96025800 | 363 | |
6a488035 | 364 | } |