2555693399a3e18343551e2e78afff90e6fe8dc2
[civicrm-core.git] / CRM / Contact / Page / DedupeFind.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
31 * @copyright CiviCRM LLC (c) 2004-2016
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 /**
40 * Get BAO Name.
41 *
42 * @return string
43 * Classname of BAO.
44 */
45 public function getBAOName() {
46 return 'CRM_Dedupe_BAO_RuleGroup';
47 }
48
49 /**
50 * Get action Links.
51 */
52 public function &links() {
53 }
54
55 /**
56 * Browse all rule groups.
57 */
58 public function run() {
59 $gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0);
60 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 0);
61 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
62 $limit = CRM_Utils_Request::retrieve('limit', 'Integer', $this);
63 $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive');
64 $urlQry = "reset=1&rgid={$rgid}&gid={$gid}&limit={$limit}";
65 $this->assign('urlQuery', $urlQry);
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
77 if ($rgid) {
78 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid));
79 }
80 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry . "&action=update"));
81 }
82 elseif ($action & CRM_Core_Action::MAP) {
83 // do a batch merge if requested
84 $result = CRM_Dedupe_Merger::batchMerge($rgid, $gid, 'safe', TRUE, 75);
85
86 $skippedCount = CRM_Utils_Request::retrieve('skipped', 'Positive', $this, FALSE, 0);
87 $skippedCount = $skippedCount + count($result['skipped']);
88 $mergedCount = CRM_Utils_Request::retrieve('merged', 'Positive', $this, FALSE, 0);
89 $mergedCount = $mergedCount + count($result['merged']);
90
91 if (empty($result['merged']) && empty($result['skipped'])) {
92 $message = '';
93 if ($mergedCount >= 1) {
94 $message = ts("%1 pairs of duplicates were merged", array(1 => $mergedCount));
95 }
96 if ($skippedCount >= 1) {
97 $message = $message ? "{$message} and " : '';
98 $message .= ts("%1 pairs of duplicates were skipped due to conflict",
99 array(1 => $skippedCount)
100 );
101 }
102 $message .= ts(" during the batch merge process with safe mode.");
103 CRM_Core_Session::setStatus($message, ts('Merge Complete'), 'success');
104 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry . "&action=update"));
105 }
106 else {
107 $urlQry .= "&action=map&skipped={$skippedCount}&merged={$mergedCount}";
108 CRM_Utils_System::jsRedirect(
109 CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry),
110 ts('Batch Merge Task in progress'),
111 ts('The batch merge task is still in progress. This page will be refreshed automatically.')
112 );
113 }
114 }
115
116 if ($action & CRM_Core_Action::UPDATE ||
117 $action & CRM_Core_Action::BROWSE
118 ) {
119 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0);
120 $this->action = CRM_Core_Action::UPDATE;
121
122 $urlQry .= '&snippet=4';
123 if ($context == 'conflicts') {
124 $urlQry .= "&selected=1";
125 }
126
127 $this->assign('sourceUrl', CRM_Utils_System::url('civicrm/ajax/dedupefind', $urlQry, FALSE, NULL, FALSE));
128
129 //reload from cache table
130 $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid);
131
132 $stats = CRM_Dedupe_Merger::getMergeStatsMsg($cacheKeyString);
133 if ($stats) {
134 CRM_Core_Session::setStatus($stats);
135 // reset so we not displaying same message again
136 CRM_Dedupe_Merger::resetMergeStats($cacheKeyString);
137 }
138 $join = CRM_Dedupe_Merger::getJoinOnDedupeTable();
139 $where = "de.id IS NULL";
140 if ($context == 'conflicts') {
141 $where .= " AND pn.is_selected = 1";
142 }
143 $this->_mainContacts = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, $join, $where);
144 if (empty($this->_mainContacts)) {
145 if ($context == 'conflicts') {
146 // if the current screen was intended to list only selected contacts, move back to full dupe list
147 CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), $urlQry . '&action=update'));
148 }
149 if ($gid) {
150 $foundDupes = $this->get("dedupe_dupes_$gid");
151 if (!$foundDupes) {
152 $foundDupes = CRM_Dedupe_Finder::dupesInGroup($rgid, $gid, $limit);
153 }
154 $this->set("dedupe_dupes_$gid", $foundDupes);
155 }
156 elseif (!empty($contactIds)) {
157 $foundDupes = $this->get("search_dedupe_dupes_$gid");
158 if (!$foundDupes) {
159 $foundDupes = CRM_Dedupe_Finder::dupes($rgid, $contactIds);
160 }
161 $this->get("search_dedupe_dupes_$gid", $foundDupes);
162 }
163 else {
164 $foundDupes = $this->get('dedupe_dupes');
165 if (!$foundDupes) {
166 $foundDupes = CRM_Dedupe_Finder::dupes($rgid, array(), TRUE, $limit);
167 }
168 $this->set('dedupe_dupes', $foundDupes);
169 }
170 if (!$foundDupes) {
171 $ruleGroup = new CRM_Dedupe_BAO_RuleGroup();
172 $ruleGroup->id = $rgid;
173 $ruleGroup->find(TRUE);
174
175 $session = CRM_Core_Session::singleton();
176 $session->setStatus(ts('No possible duplicates were found using %1 rule.', array(1 => $ruleGroup->name)), ts('None Found'), 'info');
177 $url = CRM_Utils_System::url('civicrm/contact/deduperules', 'reset=1');
178 if ($context == 'search') {
179 $url = $session->readUserContext();
180 }
181 CRM_Utils_System::redirect($url);
182 }
183 else {
184 $mainContacts = CRM_Dedupe_Finder::parseAndStoreDupePairs($foundDupes, $cacheKeyString);
185
186 if ($cid) {
187 $this->_cid = $cid;
188 }
189 if ($gid) {
190 $this->_gid = $gid;
191 }
192 $this->_rgid = $rgid;
193 $this->_mainContacts = $mainContacts;
194
195 $session = CRM_Core_Session::singleton();
196 if ($this->_cid) {
197 $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/deduperules',
198 $urlQry . "&action=update&cid={$this->_cid}"
199 ));
200 }
201 else {
202 $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/dedupefind',
203 $urlQry . "&action=update"
204 ));
205 }
206 }
207 }
208 else {
209 if ($cid) {
210 $this->_cid = $cid;
211 }
212 if ($gid) {
213 $this->_gid = $gid;
214 }
215 $this->_rgid = $rgid;
216 }
217
218 $this->assign('action', $this->action);
219 $this->browse();
220 }
221 else {
222 $this->action = CRM_Core_Action::UPDATE;
223 $this->edit($this->action);
224 $this->assign('action', $this->action);
225 }
226 $this->assign('context', $context);
227
228 // parent run
229 return parent::run();
230 }
231
232 /**
233 * Browse all rule groups.
234 */
235 public function browse() {
236 $this->assign('main_contacts', $this->_mainContacts);
237
238 if ($this->_cid) {
239 $this->assign('cid', $this->_cid);
240 }
241 if (isset($this->_gid) || $this->_gid) {
242 $this->assign('gid', $this->_gid);
243 }
244 $this->assign('rgid', $this->_rgid);
245 }
246
247 /**
248 * Get name of edit form.
249 *
250 * @return string
251 * classname of edit form
252 */
253 public function editForm() {
254 return 'CRM_Contact_Form_DedupeFind';
255 }
256
257 /**
258 * Get edit form name.
259 *
260 * @return string
261 * name of this page
262 */
263 public function editName() {
264 return 'DedupeFind';
265 }
266
267 /**
268 * Get user context.
269 *
270 * @param null $mode
271 *
272 * @return string
273 * user context
274 */
275 public function userContext($mode = NULL) {
276 return 'civicrm/contact/dedupefind';
277 }
278
279 }