(NFC) Update CRM/Contact to match new coder style
[civicrm-core.git] / CRM / Contact / Page / DedupeMerge.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33 class CRM_Contact_Page_DedupeMerge extends CRM_Core_Page {
34
35 const BATCHLIMIT = 2;
36
37 /**
38 * Browse batch merges.
39 */
40 public function run() {
41 $runner = self::getRunner();
42 if ($runner) {
43 $runner->runAllViaWeb();
44 }
45 else {
46 CRM_Core_Session::setStatus(ts('Nothing to merge.'));
47 }
48 return parent::run();
49 }
50
51 /**
52 * Build a queue of tasks by dividing dupe pairs in batches.
53 */
54 public static function getRunner() {
55
56 $rgid = CRM_Utils_Request::retrieveValue('rgid', 'Positive');
57 $gid = CRM_Utils_Request::retrieveValue('gid', 'Positive');
58 $limit = CRM_Utils_Request::retrieveValue('limit', 'Positive');
59 $action = CRM_Utils_Request::retrieveValue('action', 'String');
60 $mode = CRM_Utils_Request::retrieveValue('mode', 'String', 'safe');
61 $criteria = CRM_Utils_Request::retrieve('criteria', 'Json', $null, FALSE, '{}');
62
63 $urlQry = [
64 'reset' => 1,
65 'action' => 'update',
66 'rgid' => $rgid,
67 'gid' => $gid,
68 'limit' => $limit,
69 'criteria' => $criteria,
70 ];
71
72 $criteria = json_decode($criteria, TRUE);
73 $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria);
74
75 if ($mode == 'aggressive' && !CRM_Core_Permission::check('force merge duplicate contacts')) {
76 CRM_Core_Session::setStatus(ts('You do not have permission to force merge duplicate contact records'), ts('Permission Denied'), 'error');
77 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
78 }
79 // Setup the Queue
80 $queue = CRM_Queue_Service::singleton()->create([
81 'name' => $cacheKeyString,
82 'type' => 'Sql',
83 'reset' => TRUE,
84 ]);
85
86 $where = NULL;
87 $onlyProcessSelected = ($action == CRM_Core_Action::MAP) ? 1 : 0;
88
89 $total = CRM_Core_BAO_PrevNextCache::getCount($cacheKeyString, NULL, ($onlyProcessSelected ? "pn.is_selected = 1" : NULL));
90 if ($total <= 0) {
91 // Nothing to do.
92 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
93 }
94
95 // reset merge stats, so we compute new stats
96 CRM_Dedupe_Merger::resetMergeStats($cacheKeyString);
97
98 for ($i = 1; $i <= ceil($total / self::BATCHLIMIT); $i++) {
99 $task = new CRM_Queue_Task(
100 ['CRM_Contact_Page_DedupeMerge', 'callBatchMerge'],
101 [$rgid, $gid, $mode, self::BATCHLIMIT, $onlyProcessSelected, $criteria],
102 "Processed " . $i * self::BATCHLIMIT . " pair of duplicates out of " . $total
103 );
104
105 // Add the Task to the Queue
106 $queue->createItem($task);
107 }
108
109 // Setup the Runner
110 $urlQry['context'] = "conflicts";
111 if ($onlyProcessSelected) {
112 $urlQry['selected'] = 1;
113 }
114 $runner = new CRM_Queue_Runner([
115 'title' => ts('Merging Duplicates..'),
116 'queue' => $queue,
117 'errorMode' => CRM_Queue_Runner::ERROR_ABORT,
118 'onEndUrl' => CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry, TRUE, NULL, FALSE),
119 ]);
120
121 return $runner;
122 }
123
124 /**
125 * Carry out batch merges.
126 *
127 * @param \CRM_Queue_TaskContext $ctx
128 * @param int $rgid
129 * @param int $gid
130 * @param string $mode
131 * 'safe' mode or 'force' mode.
132 * @param int $batchLimit
133 * @param int $isSelected
134 * @param array $criteria
135 *
136 * @return int
137 */
138 public static function callBatchMerge(CRM_Queue_TaskContext $ctx, $rgid, $gid, $mode = 'safe', $batchLimit, $isSelected, $criteria) {
139 CRM_Dedupe_Merger::batchMerge($rgid, $gid, $mode, $batchLimit, $isSelected, $criteria, TRUE, FALSE);
140 return CRM_Queue_Task::TASK_SUCCESS;
141 }
142
143 }