Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic { | |
36 | protected $_cid = NULL; | |
37 | protected $_rgid; | |
38 | protected $_mainContacts; | |
39 | protected $_gid; | |
40 | ||
41 | /** | |
42 | * Get BAO Name | |
43 | * | |
44 | * @return string Classname of BAO. | |
8ef12e64 | 45 | */ |
00be9182 | 46 | public function getBAOName() { |
6a488035 TO |
47 | return 'CRM_Dedupe_BAO_RuleGroup'; |
48 | } | |
49 | ||
50 | /** | |
51 | * Get action Links | |
52 | * | |
53 | * @return array (reference) of action links | |
54 | */ | |
00be9182 | 55 | public function &links() {} |
6a488035 TO |
56 | |
57 | /** | |
58 | * Browse all rule groups | |
59 | * | |
60 | * @return void | |
6a488035 | 61 | */ |
00be9182 | 62 | public function run() { |
6a488035 TO |
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); | |
66 | ||
67 | $session = CRM_Core_Session::singleton(); | |
68 | $contactIds = $session->get('selectedSearchContactIds'); | |
69 | if ($context == 'search' || !empty($contactIds)) { | |
70 | $context = 'search'; | |
71 | $this->assign('backURL', $session->readUserContext()); | |
72 | } | |
73 | ||
74 | if ($action & CRM_Core_Action::RENEW) { | |
75 | // empty cache | |
76 | $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this, FALSE, 0); | |
77 | ||
78 | if ($rgid) { | |
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); | |
84 | } | |
85 | $urlQry = "reset=1&action=update&rgid={$rgid}"; | |
86 | if ($gid) { | |
87 | $urlQry .= "&gid={$gid}"; | |
88 | } | |
89 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry)); | |
90 | } | |
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); | |
95 | ||
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']); | |
100 | ||
101 | if (empty($result['merged']) && empty($result['skipped'])) { | |
102 | $message = ''; | |
103 | if ($mergedCount >= 1) { | |
104 | $message = ts("%1 pairs of duplicates were merged", array(1 => $mergedCount)); | |
105 | } | |
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) | |
110 | ); | |
111 | } | |
112 | $message .= ts(" during the batch merge process with safe mode."); | |
113 | CRM_Core_Session::setStatus($message, ts('Merge Complete'), 'success'); | |
114 | ||
115 | $urlQry = "reset=1&action=update&rgid={$rgid}"; | |
116 | if ($gid) { | |
117 | $urlQry .= "&gid={$gid}"; | |
118 | } | |
119 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry)); | |
120 | } | |
121 | else { | |
122 | $urlQry = "reset=1&action=map&rgid={$rgid}"; | |
123 | if ($gid) { | |
124 | $urlQry .= "&gid={$gid}"; | |
125 | } | |
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.') | |
131 | ); | |
132 | } | |
133 | } | |
134 | ||
135 | if ($action & CRM_Core_Action::UPDATE || | |
136 | $action & CRM_Core_Action::BROWSE | |
137 | ) { | |
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; | |
141 | ||
142 | //calculate the $contactType | |
143 | if ($rgid) { | |
144 | $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', | |
145 | $rgid, | |
146 | 'contact_type' | |
147 | ); | |
148 | } | |
149 | ||
150 | $sourceParams = 'snippet=4'; | |
151 | if ($gid) { | |
152 | $sourceParams .= "&gid={$gid}"; | |
153 | } | |
154 | if ($rgid) { | |
155 | $sourceParams .= "&rgid={$rgid}"; | |
156 | } | |
157 | ||
158 | $this->assign('sourceUrl', CRM_Utils_System::url('civicrm/ajax/dedupefind', $sourceParams, FALSE, NULL, FALSE)); | |
159 | ||
160 | //reload from cache table | |
161 | $cacheKeyString = "merge $contactType"; | |
162 | $cacheKeyString .= $rgid ? "_{$rgid}" : '_0'; | |
163 | $cacheKeyString .= $gid ? "_{$gid}" : '_0'; | |
164 | ||
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)) { | |
170 | if ($gid) { | |
171 | $foundDupes = $this->get("dedupe_dupes_$gid"); | |
172 | if (!$foundDupes) { | |
173 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($rgid, $gid); | |
174 | } | |
175 | $this->set("dedupe_dupes_$gid", $foundDupes); | |
176 | } | |
177 | elseif (!empty($contactIds)) { | |
178 | $foundDupes = $this->get("search_dedupe_dupes_$gid"); | |
179 | if (!$foundDupes) { | |
180 | $foundDupes = CRM_Dedupe_Finder::dupes($rgid, $contactIds); | |
181 | } | |
182 | $this->get("search_dedupe_dupes_$gid", $foundDupes); | |
183 | } | |
184 | else { | |
185 | $foundDupes = $this->get('dedupe_dupes'); | |
186 | if (!$foundDupes) { | |
187 | $foundDupes = CRM_Dedupe_Finder::dupes($rgid); | |
188 | } | |
189 | $this->set('dedupe_dupes', $foundDupes); | |
190 | } | |
191 | if (!$foundDupes) { | |
192 | $ruleGroup = new CRM_Dedupe_BAO_RuleGroup(); | |
193 | $ruleGroup->id = $rgid; | |
194 | $ruleGroup->find(TRUE); | |
195 | ||
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(); | |
201 | } | |
202 | CRM_Utils_System::redirect($url); | |
203 | } | |
204 | else { | |
205 | $cids = array(); | |
206 | foreach ($foundDupes as $dupe) { | |
207 | $cids[$dupe[0]] = 1; | |
208 | $cids[$dupe[1]] = 1; | |
209 | } | |
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(); | |
213 | $dao->query($sql); | |
214 | $displayNames = array(); | |
215 | while ($dao->fetch()) { | |
216 | $displayNames[$dao->id] = $dao->display_name; | |
217 | } | |
218 | ||
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(); | |
227 | ||
228 | foreach ($foundDupes as $dupes) { | |
229 | $srcID = $dupes[0]; | |
230 | $dstID = $dupes[1]; | |
231 | if ($dstID == $userId) { | |
232 | $srcID = $dupes[1]; | |
233 | $dstID = $dupes[0]; | |
234 | } | |
235 | ||
236 | /*** | |
237 | * Eliminate this since it introduces 3 queries PER merge row | |
238 | * and hence is very expensive | |
239 | * CRM-8822 | |
240 | if ( !array_key_exists( $srcID, $permission ) ) { | |
241 | $permission[$srcID] = CRM_Contact_BAO_Contact_Permission::allow( $srcID, CRM_Core_Permission::EDIT ); | |
242 | } | |
243 | if ( !array_key_exists( $dstID, $permission ) ) { | |
244 | $permission[$dstID] = CRM_Contact_BAO_Contact_Permission::allow( $dstID, CRM_Core_Permission::EDIT ); | |
245 | } | |
246 | ||
247 | $canMerge = ( $permission[$dstID] && $permission[$srcID] ); | |
248 | * | |
249 | */ | |
250 | ||
251 | // we'll do permission checking during the merge process | |
252 | $canMerge = TRUE; | |
253 | ||
254 | $mainContacts[] = $row = array( | |
255 | 'srcID' => $srcID, | |
256 | 'srcName' => $displayNames[$srcID], | |
257 | 'dstID' => $dstID, | |
258 | 'dstName' => $displayNames[$dstID], | |
259 | 'weight' => $dupes[2], | |
260 | 'canMerge' => $canMerge, | |
261 | ); | |
262 | ||
263 | $data = CRM_Core_DAO::escapeString(serialize($row)); | |
264 | $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) "; | |
265 | } | |
266 | if ($cid) { | |
267 | $this->_cid = $cid; | |
268 | } | |
269 | if ($gid) { | |
270 | $this->_gid = $gid; | |
271 | } | |
272 | $this->_rgid = $rgid; | |
273 | $this->_mainContacts = $mainContacts; | |
274 | ||
275 | CRM_Core_BAO_PrevNextCache::setItem($values); | |
276 | $session = CRM_Core_Session::singleton(); | |
277 | if ($this->_cid) { | |
278 | $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/deduperules', | |
279 | "action=update&rgid={$this->_rgid}&gid={$this->_gid}&cid={$this->_cid}" | |
280 | )); | |
281 | } | |
282 | else { | |
283 | $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/dedupefind', | |
284 | "reset=1&action=update&rgid={$this->_rgid}" | |
285 | )); | |
286 | } | |
287 | } | |
288 | } | |
289 | else { | |
290 | if ($cid) { | |
291 | $this->_cid = $cid; | |
292 | } | |
293 | if ($gid) { | |
294 | $this->_gid = $gid; | |
295 | } | |
296 | $this->_rgid = $rgid; | |
297 | } | |
298 | ||
299 | $this->assign('action', $this->action); | |
300 | $this->browse(); | |
301 | } | |
302 | else { | |
303 | $this->action = CRM_Core_Action::UPDATE; | |
304 | $this->edit($this->action); | |
305 | $this->assign('action', $this->action); | |
306 | } | |
307 | $this->assign('context', $context); | |
308 | ||
309 | // parent run | |
310 | return parent::run(); | |
311 | } | |
312 | ||
313 | /** | |
314 | * Browse all rule groups | |
315 | * | |
316 | * @return void | |
6a488035 | 317 | */ |
00be9182 | 318 | public function browse() { |
6a488035 TO |
319 | $this->assign('main_contacts', $this->_mainContacts); |
320 | ||
321 | if ($this->_cid) { | |
322 | $this->assign('cid', $this->_cid); | |
323 | } | |
324 | if (isset($this->_gid) || $this->_gid) { | |
325 | $this->assign('gid', $this->_gid); | |
326 | } | |
327 | $this->assign('rgid', $this->_rgid); | |
328 | } | |
329 | ||
330 | /** | |
331 | * Get name of edit form | |
332 | * | |
333 | * @return string classname of edit form | |
334 | */ | |
00be9182 | 335 | public function editForm() { |
6a488035 TO |
336 | return 'CRM_Contact_Form_DedupeFind'; |
337 | } | |
338 | ||
339 | /** | |
340 | * Get edit form name | |
341 | * | |
342 | * @return string name of this page | |
343 | */ | |
00be9182 | 344 | public function editName() { |
6a488035 TO |
345 | return 'DedupeFind'; |
346 | } | |
347 | ||
348 | /** | |
349 | * Get user context | |
350 | * | |
77b97be7 EM |
351 | * @param null $mode |
352 | * | |
6a488035 TO |
353 | * @return string user context |
354 | */ | |
00be9182 | 355 | public function userContext($mode = NULL) { |
6a488035 TO |
356 | return 'civicrm/contact/dedupefind'; |
357 | } | |
358 | } |