dev/core#1405 Strip any spaces from Option Group name parameter and remove the name...
[civicrm-core.git] / CRM / Contact / Page / DedupeMerge.php
CommitLineData
63ef778e 1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
63ef778e 5 | |
bc77d7c0
TO
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 |
63ef778e 9 +--------------------------------------------------------------------+
f931b74c 10 */
63ef778e 11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
63ef778e 16 */
f931b74c 17class CRM_Contact_Page_DedupeMerge extends CRM_Core_Page {
63ef778e 18
19 const BATCHLIMIT = 2;
20
21 /**
183ec330 22 * Browse batch merges.
63ef778e 23 */
f931b74c 24 public function run() {
63ef778e 25 $runner = self::getRunner();
26 if ($runner) {
63ef778e 27 $runner->runAllViaWeb();
f931b74c 28 }
29 else {
63ef778e 30 CRM_Core_Session::setStatus(ts('Nothing to merge.'));
31 }
63ef778e 32 return parent::run();
33 }
34
183ec330 35 /**
36 * Build a queue of tasks by dividing dupe pairs in batches.
37 */
f931b74c 38 public static function getRunner() {
9f54f049 39
5e4ccea5 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');
9f54f049 45 $criteria = CRM_Utils_Request::retrieve('criteria', 'Json', $null, FALSE, '{}');
63ef778e 46
be2fb01f 47 $urlQry = [
b160ca47 48 'reset' => 1,
49 'action' => 'update',
50 'rgid' => $rgid,
51 'gid' => $gid,
5e4ccea5 52 'limit' => $limit,
9f54f049 53 'criteria' => $criteria,
be2fb01f 54 ];
fd630ef9 55
9f54f049 56 $criteria = json_decode($criteria, TRUE);
997a03fe 57 $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria, TRUE, $limit);
9f54f049 58
fd630ef9 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 }
63ef778e 63 // Setup the Queue
be2fb01f 64 $queue = CRM_Queue_Service::singleton()->create([
63ef778e 65 'name' => $cacheKeyString,
66 'type' => 'Sql',
67 'reset' => TRUE,
be2fb01f 68 ]);
63ef778e 69
70 $where = NULL;
bb22928b 71 $onlyProcessSelected = ($action == CRM_Core_Action::MAP) ? 1 : 0;
63ef778e 72
69078420 73 $total = CRM_Core_BAO_PrevNextCache::getCount($cacheKeyString, NULL, ($onlyProcessSelected ? "pn.is_selected = 1" : NULL));
63ef778e 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
f931b74c 82 for ($i = 1; $i <= ceil($total / self::BATCHLIMIT); $i++) {
69078420 83 $task = new CRM_Queue_Task(
be2fb01f 84 ['CRM_Contact_Page_DedupeMerge', 'callBatchMerge'],
997a03fe 85 [$rgid, $gid, $mode, self::BATCHLIMIT, $onlyProcessSelected, $criteria, $limit],
f931b74c 86 "Processed " . $i * self::BATCHLIMIT . " pair of duplicates out of " . $total
63ef778e 87 );
88
89 // Add the Task to the Queue
90 $queue->createItem($task);
91 }
92
93 // Setup the Runner
b160ca47 94 $urlQry['context'] = "conflicts";
bb22928b 95 if ($onlyProcessSelected) {
96 $urlQry['selected'] = 1;
97 }
be2fb01f 98 $runner = new CRM_Queue_Runner([
f931b74c 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),
be2fb01f 103 ]);
63ef778e 104
105 return $runner;
106 }
107
108 /**
183ec330 109 * Carry out batch merges.
54957108 110 *
111 * @param \CRM_Queue_TaskContext $ctx
112 * @param int $rgid
113 * @param int $gid
114 * @param string $mode
c917824b 115 * 'safe' mode or 'force' mode.
54957108 116 * @param int $batchLimit
117 * @param int $isSelected
9f54f049 118 * @param array $criteria
997a03fe 119 * @param int $searchLimit
54957108 120 *
121 * @return int
997a03fe 122 *
123 * @throws \CRM_Core_Exception
124 * @throws \CiviCRM_API3_Exception
63ef778e 125 */
997a03fe 126 public static function callBatchMerge(CRM_Queue_TaskContext $ctx, $rgid, $gid, $mode = 'safe', $batchLimit, $isSelected, $criteria, $searchLimit) {
127 CRM_Dedupe_Merger::batchMerge($rgid, $gid, $mode, $batchLimit, $isSelected, $criteria, TRUE, FALSE, $searchLimit);
63ef778e 128 return CRM_Queue_Task::TASK_SUCCESS;
129 }
63ef778e 130
f931b74c 131}