Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
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; | |
92241006 | 38 | protected $action; |
bb22928b | 39 | /** |
40 | * Only display selected. | |
41 | * | |
42 | * @var bool | |
43 | */ | |
44 | protected $selected; | |
45 | ||
46 | /** | |
47 | * Get isSelected value. | |
48 | * | |
49 | * This needs to be an integer of 0 or 1 or NULL for no filter. | |
50 | * | |
51 | * @return bool|NULL | |
52 | */ | |
53 | public function isSelected() { | |
54 | return ($this->selected === NULL) ? NULL : (int) $this->selected; | |
55 | } | |
6a488035 TO |
56 | |
57 | /** | |
fe482240 | 58 | * Get BAO Name. |
6a488035 | 59 | * |
a6c01b45 CW |
60 | * @return string |
61 | * Classname of BAO. | |
8ef12e64 | 62 | */ |
00be9182 | 63 | public function getBAOName() { |
6a488035 TO |
64 | return 'CRM_Dedupe_BAO_RuleGroup'; |
65 | } | |
66 | ||
67 | /** | |
fe482240 | 68 | * Get action Links. |
6a488035 | 69 | */ |
ce80b209 TO |
70 | public function &links() { |
71 | } | |
6a488035 | 72 | |
bb22928b | 73 | /** |
74 | * Initialize properties from input. | |
75 | */ | |
76 | protected function initialize() { | |
77 | $this->selected = CRM_Utils_Request::retrieveValue('selected', 'Boolean'); | |
78 | } | |
79 | ||
6a488035 | 80 | /** |
fe482240 | 81 | * Browse all rule groups. |
6a488035 | 82 | */ |
00be9182 | 83 | public function run() { |
bb22928b | 84 | $this->initialize(); |
353ffa53 TO |
85 | $gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0); |
86 | $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 0); | |
6a488035 | 87 | $context = CRM_Utils_Request::retrieve('context', 'String', $this); |
4c8b4719 | 88 | $limit = CRM_Utils_Request::retrieve('limit', 'Integer', $this); |
28c64535 | 89 | $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this); |
5721d85e | 90 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0); |
9f54f049 | 91 | |
92 | $criteria = CRM_Utils_Request::retrieve('criteria', 'Json', $this, FALSE, '{}'); | |
88251439 | 93 | $this->assign('criteria', $criteria); |
94 | ||
5721d85e | 95 | $isConflictMode = ($context == 'conflicts'); |
96 | if ($cid) { | |
97 | $this->_cid = $cid; | |
98 | } | |
99 | if ($gid) { | |
100 | $this->_gid = $gid; | |
101 | } | |
102 | $this->_rgid = $rgid; | |
103 | ||
4a8e09b1 JM |
104 | $urlQry = array( |
105 | 'reset' => 1, | |
106 | 'rgid' => $rgid, | |
107 | 'gid' => $gid, | |
83c2e820 | 108 | 'limit' => $limit, |
88251439 | 109 | 'criteria' => $criteria, |
4a8e09b1 | 110 | ); |
4c7de212 | 111 | $this->assign('urlQuery', CRM_Utils_System::makeQueryString($urlQry)); |
bb22928b | 112 | $this->assign('isSelected', $this->isSelected()); |
88251439 | 113 | $criteria = json_decode($criteria, TRUE); |
9d2f6d53 | 114 | $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria); |
115 | $this->assign('cacheKey', $cacheKeyString); | |
6a488035 | 116 | |
d4c035a2 | 117 | if ($context == 'search') { |
6a488035 | 118 | $context = 'search'; |
d4c035a2 | 119 | $this->assign('backURL', CRM_Core_Session::singleton()->readUserContext()); |
6a488035 TO |
120 | } |
121 | ||
122 | if ($action & CRM_Core_Action::RENEW) { | |
123 | // empty cache | |
6a488035 | 124 | if ($rgid) { |
9d2f6d53 | 125 | CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKeyString); |
6a488035 | 126 | } |
4a8e09b1 JM |
127 | $urlQry['action'] = 'update'; |
128 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry)); | |
6a488035 TO |
129 | } |
130 | elseif ($action & CRM_Core_Action::MAP) { | |
131 | // do a batch merge if requested | |
5721d85e | 132 | $result = CRM_Dedupe_Merger::batchMerge($rgid, $gid, 'safe', 75, 2, $criteria); |
6a488035 TO |
133 | |
134 | $skippedCount = CRM_Utils_Request::retrieve('skipped', 'Positive', $this, FALSE, 0); | |
135 | $skippedCount = $skippedCount + count($result['skipped']); | |
353ffa53 TO |
136 | $mergedCount = CRM_Utils_Request::retrieve('merged', 'Positive', $this, FALSE, 0); |
137 | $mergedCount = $mergedCount + count($result['merged']); | |
6a488035 TO |
138 | |
139 | if (empty($result['merged']) && empty($result['skipped'])) { | |
140 | $message = ''; | |
141 | if ($mergedCount >= 1) { | |
142 | $message = ts("%1 pairs of duplicates were merged", array(1 => $mergedCount)); | |
143 | } | |
144 | if ($skippedCount >= 1) { | |
145 | $message = $message ? "{$message} and " : ''; | |
146 | $message .= ts("%1 pairs of duplicates were skipped due to conflict", | |
147 | array(1 => $skippedCount) | |
148 | ); | |
149 | } | |
150 | $message .= ts(" during the batch merge process with safe mode."); | |
151 | CRM_Core_Session::setStatus($message, ts('Merge Complete'), 'success'); | |
4a8e09b1 JM |
152 | $urlQry['action'] = 'update'; |
153 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry)); | |
6a488035 TO |
154 | } |
155 | else { | |
4a8e09b1 | 156 | $urlQry['action'] = 'map'; |
83c2e820 | 157 | $urlQry['skipped'] = $skippedCount; |
4a8e09b1 | 158 | $urlQry['merged'] = $mergedCount; |
6a488035 TO |
159 | CRM_Utils_System::jsRedirect( |
160 | CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry), | |
161 | ts('Batch Merge Task in progress'), | |
162 | ts('The batch merge task is still in progress. This page will be refreshed automatically.') | |
163 | ); | |
164 | } | |
165 | } | |
166 | ||
167 | if ($action & CRM_Core_Action::UPDATE || | |
168 | $action & CRM_Core_Action::BROWSE | |
169 | ) { | |
6a488035 TO |
170 | $this->action = CRM_Core_Action::UPDATE; |
171 | ||
4a8e09b1 | 172 | $urlQry['snippet'] = 4; |
6a488035 | 173 | |
97a322ac | 174 | $this->assign('sourceUrl', CRM_Utils_System::url('civicrm/ajax/dedupefind', $urlQry, FALSE, NULL, FALSE)); |
6a488035 | 175 | |
3743fd3d | 176 | $stats = CRM_Dedupe_Merger::getMergeStats($cacheKeyString); |
63ef778e | 177 | if ($stats) { |
3743fd3d CW |
178 | $message = CRM_Dedupe_Merger::getMergeStatsMsg($stats); |
179 | $status = empty($stats['skipped']) ? 'success' : 'alert'; | |
180 | CRM_Core_Session::setStatus($message, ts('Batch Complete'), $status, array('expires' => 0)); | |
63ef778e | 181 | // reset so we not displaying same message again |
182 | CRM_Dedupe_Merger::resetMergeStats($cacheKeyString); | |
183 | } | |
5721d85e | 184 | |
917acf6f | 185 | $this->_mainContacts = CRM_Dedupe_Merger::getDuplicatePairs($rgid, $gid, !$isConflictMode, 0, $this->isSelected(), '', $isConflictMode, $criteria, TRUE, $limit); |
5721d85e | 186 | |
6a488035 | 187 | if (empty($this->_mainContacts)) { |
5721d85e | 188 | if ($isConflictMode) { |
63ef778e | 189 | // if the current screen was intended to list only selected contacts, move back to full dupe list |
4a8e09b1 | 190 | $urlQry['action'] = 'update'; |
9893cfd2 | 191 | unset($urlQry['snippet']); |
4a8e09b1 | 192 | CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), $urlQry)); |
63ef778e | 193 | } |
5721d85e | 194 | $ruleGroupName = civicrm_api3('RuleGroup', 'getvalue', array('id' => $rgid, 'return' => 'name')); |
195 | CRM_Core_Session::singleton()->setStatus(ts('No possible duplicates were found using %1 rule.', array(1 => $ruleGroupName)), ts('None Found'), 'info'); | |
196 | $url = CRM_Utils_System::url('civicrm/contact/deduperules', 'reset=1'); | |
197 | if ($context == 'search') { | |
198 | $url = CRM_Core_Session::singleton()->readUserContext(); | |
6a488035 | 199 | } |
5721d85e | 200 | CRM_Utils_System::redirect($url); |
6a488035 TO |
201 | } |
202 | else { | |
5721d85e | 203 | $urlQry['action'] = 'update'; |
204 | if ($this->_cid) { | |
205 | $urlQry['cid'] = $this->_cid; | |
206 | CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/contact/deduperules', | |
207 | $urlQry | |
208 | )); | |
6a488035 | 209 | } |
5721d85e | 210 | else { |
211 | CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/contact/dedupefind', | |
212 | $urlQry | |
213 | )); | |
6a488035 | 214 | } |
6a488035 TO |
215 | } |
216 | ||
217 | $this->assign('action', $this->action); | |
218 | $this->browse(); | |
219 | } | |
220 | else { | |
221 | $this->action = CRM_Core_Action::UPDATE; | |
222 | $this->edit($this->action); | |
223 | $this->assign('action', $this->action); | |
224 | } | |
225 | $this->assign('context', $context); | |
226 | ||
6a488035 TO |
227 | return parent::run(); |
228 | } | |
229 | ||
230 | /** | |
fe482240 | 231 | * Browse all rule groups. |
6a488035 | 232 | */ |
00be9182 | 233 | public function browse() { |
6a488035 TO |
234 | $this->assign('main_contacts', $this->_mainContacts); |
235 | ||
236 | if ($this->_cid) { | |
237 | $this->assign('cid', $this->_cid); | |
238 | } | |
239 | if (isset($this->_gid) || $this->_gid) { | |
240 | $this->assign('gid', $this->_gid); | |
241 | } | |
242 | $this->assign('rgid', $this->_rgid); | |
243 | } | |
244 | ||
245 | /** | |
fe482240 | 246 | * Get name of edit form. |
6a488035 | 247 | * |
a6c01b45 CW |
248 | * @return string |
249 | * classname of edit form | |
6a488035 | 250 | */ |
00be9182 | 251 | public function editForm() { |
6a488035 TO |
252 | return 'CRM_Contact_Form_DedupeFind'; |
253 | } | |
254 | ||
255 | /** | |
fe482240 | 256 | * Get edit form name. |
6a488035 | 257 | * |
a6c01b45 CW |
258 | * @return string |
259 | * name of this page | |
6a488035 | 260 | */ |
00be9182 | 261 | public function editName() { |
6a488035 TO |
262 | return 'DedupeFind'; |
263 | } | |
264 | ||
265 | /** | |
fe482240 | 266 | * Get user context. |
6a488035 | 267 | * |
77b97be7 EM |
268 | * @param null $mode |
269 | * | |
a6c01b45 CW |
270 | * @return string |
271 | * user context | |
6a488035 | 272 | */ |
00be9182 | 273 | public function userContext($mode = NULL) { |
6a488035 TO |
274 | return 'civicrm/contact/dedupefind'; |
275 | } | |
96025800 | 276 | |
6a488035 | 277 | } |