Merge pull request #17866 from colemanw/customFix
[civicrm-core.git] / CRM / Contact / Page / DedupeMerge.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Contact_Page_DedupeMerge extends CRM_Core_Page {
18
19 const BATCHLIMIT = 2;
20
21 /**
22 * Browse batch merges.
23 */
24 public function run() {
25 $runner = self::getRunner();
26 if ($runner) {
27 $runner->runAllViaWeb();
28 }
29 else {
30 CRM_Core_Session::setStatus(ts('Nothing to merge.'));
31 }
32 return parent::run();
33 }
34
35 /**
36 * Build a queue of tasks by dividing dupe pairs in batches.
37 */
38 public static function getRunner() {
39
40 $rgid = CRM_Utils_Request::retrieveValue('rgid', 'Positive');
41 $gid = CRM_Utils_Request::retrieveValue('gid', 'Positive');
42 $limit = CRM_Utils_Request::retrieveValue('limit', 'Positive');
43 $action = CRM_Utils_Request::retrieveValue('action', 'String');
44 $mode = CRM_Utils_Request::retrieveValue('mode', 'String', 'safe');
45 $criteria = CRM_Utils_Request::retrieve('criteria', 'Json', $null, FALSE, '{}');
46
47 $urlQry = [
48 'reset' => 1,
49 'action' => 'update',
50 'rgid' => $rgid,
51 'gid' => $gid,
52 'limit' => $limit,
53 'criteria' => $criteria,
54 ];
55
56 $criteria = json_decode($criteria, TRUE);
57 $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria, TRUE, $limit);
58
59 if ($mode === 'aggressive' && !CRM_Core_Permission::check('force merge duplicate contacts')) {
60 CRM_Core_Session::setStatus(ts('You do not have permission to force merge duplicate contact records'), ts('Permission Denied'), 'error');
61 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
62 }
63 // Setup the Queue
64 $queue = CRM_Queue_Service::singleton()->create([
65 'name' => $cacheKeyString,
66 'type' => 'Sql',
67 'reset' => TRUE,
68 ]);
69
70 $where = NULL;
71 $onlyProcessSelected = ($action == CRM_Core_Action::MAP) ? 1 : 0;
72
73 $total = CRM_Core_BAO_PrevNextCache::getCount($cacheKeyString, NULL, ($onlyProcessSelected ? "pn.is_selected = 1" : NULL));
74 if ($total <= 0) {
75 // Nothing to do.
76 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
77 }
78
79 // reset merge stats, so we compute new stats
80 CRM_Dedupe_Merger::resetMergeStats($cacheKeyString);
81
82 for ($i = 1; $i <= ceil($total / self::BATCHLIMIT); $i++) {
83 $task = new CRM_Queue_Task(
84 ['CRM_Contact_Page_DedupeMerge', 'callBatchMerge'],
85 [$rgid, $gid, $mode, self::BATCHLIMIT, $onlyProcessSelected, $criteria, $limit],
86 "Processed " . $i * self::BATCHLIMIT . " pair of duplicates out of " . $total
87 );
88
89 // Add the Task to the Queue
90 $queue->createItem($task);
91 }
92
93 // Setup the Runner
94 $urlQry['context'] = "conflicts";
95 if ($onlyProcessSelected) {
96 $urlQry['selected'] = 1;
97 }
98 $runner = new CRM_Queue_Runner([
99 'title' => ts('Merging Duplicates..'),
100 'queue' => $queue,
101 'errorMode' => CRM_Queue_Runner::ERROR_ABORT,
102 'onEndUrl' => CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry, TRUE, NULL, FALSE),
103 ]);
104
105 return $runner;
106 }
107
108 /**
109 * Carry out batch merges.
110 *
111 * @param \CRM_Queue_TaskContext $ctx
112 * @param int $rgid
113 * @param int $gid
114 * @param string $mode
115 * 'safe' mode or 'force' mode.
116 * @param int $batchLimit
117 * @param int $isSelected
118 * @param array $criteria
119 * @param int $searchLimit
120 *
121 * @return int
122 *
123 * @throws \CRM_Core_Exception
124 * @throws \CiviCRM_API3_Exception
125 * @throws \API_Exception
126 */
127 public static function callBatchMerge(CRM_Queue_TaskContext $ctx, $rgid, $gid, $mode = 'safe', $batchLimit, $isSelected, $criteria, $searchLimit) {
128 CRM_Dedupe_Merger::batchMerge($rgid, $gid, $mode, $batchLimit, $isSelected, $criteria, TRUE, FALSE, $searchLimit);
129 return CRM_Queue_Task::TASK_SUCCESS;
130 }
131
132 }