Merge pull request #11986 from eileenmcnaughton/test
[civicrm-core.git] / CRM / Contact / Page / DedupeMerge.php
CommitLineData
63ef778e 1<?php
2/*
3 +--------------------------------------------------------------------+
3435af9a 4 | CiviCRM version 4.7 |
63ef778e 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
63ef778e 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 +--------------------------------------------------------------------+
f931b74c 26 */
63ef778e 27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
63ef778e 32 */
f931b74c 33class CRM_Contact_Page_DedupeMerge extends CRM_Core_Page {
63ef778e 34
35 const BATCHLIMIT = 2;
36
37 /**
183ec330 38 * Browse batch merges.
63ef778e 39 */
f931b74c 40 public function run() {
63ef778e 41 $runner = self::getRunner();
42 if ($runner) {
63ef778e 43 $runner->runAllViaWeb();
f931b74c 44 }
45 else {
63ef778e 46 CRM_Core_Session::setStatus(ts('Nothing to merge.'));
47 }
63ef778e 48 return parent::run();
49 }
50
183ec330 51 /**
52 * Build a queue of tasks by dividing dupe pairs in batches.
53 */
f931b74c 54 public static function getRunner() {
9f54f049 55
5e4ccea5 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');
9f54f049 61 $criteria = CRM_Utils_Request::retrieve('criteria', 'Json', $null, FALSE, '{}');
63ef778e 62
b160ca47 63 $urlQry = array(
64 'reset' => 1,
65 'action' => 'update',
66 'rgid' => $rgid,
67 'gid' => $gid,
5e4ccea5 68 'limit' => $limit,
9f54f049 69 'criteria' => $criteria,
b160ca47 70 );
fd630ef9 71
9f54f049 72 $criteria = json_decode($criteria, TRUE);
73 $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria);
74
fd630ef9 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 }
63ef778e 79 // Setup the Queue
80 $queue = CRM_Queue_Service::singleton()->create(array(
81 'name' => $cacheKeyString,
82 'type' => 'Sql',
83 'reset' => TRUE,
84 ));
85
86 $where = NULL;
87 if ($action == CRM_Core_Action::MAP) {
88 $where = "pn.is_selected = 1";
89 $isSelected = 1;
f931b74c 90 }
91 else {
63ef778e 92 // else merge all (2)
f931b74c 93 $isSelected = 2;
63ef778e 94 }
95
63ef778e 96 $total = CRM_Core_BAO_PrevNextCache::getCount($cacheKeyString, NULL, $where);
97 if ($total <= 0) {
98 // Nothing to do.
99 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
100 }
101
102 // reset merge stats, so we compute new stats
103 CRM_Dedupe_Merger::resetMergeStats($cacheKeyString);
104
f931b74c 105 for ($i = 1; $i <= ceil($total / self::BATCHLIMIT); $i++) {
63ef778e 106 $task = new CRM_Queue_Task(
f931b74c 107 array('CRM_Contact_Page_DedupeMerge', 'callBatchMerge'),
9f54f049 108 array($rgid, $gid, $mode, self::BATCHLIMIT, $isSelected, $criteria),
f931b74c 109 "Processed " . $i * self::BATCHLIMIT . " pair of duplicates out of " . $total
63ef778e 110 );
111
112 // Add the Task to the Queue
113 $queue->createItem($task);
114 }
115
116 // Setup the Runner
b160ca47 117 $urlQry['context'] = "conflicts";
63ef778e 118 $runner = new CRM_Queue_Runner(array(
f931b74c 119 'title' => ts('Merging Duplicates..'),
120 'queue' => $queue,
121 'errorMode' => CRM_Queue_Runner::ERROR_ABORT,
122 'onEndUrl' => CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry, TRUE, NULL, FALSE),
63ef778e 123 ));
124
125 return $runner;
126 }
127
128 /**
183ec330 129 * Carry out batch merges.
54957108 130 *
131 * @param \CRM_Queue_TaskContext $ctx
132 * @param int $rgid
133 * @param int $gid
134 * @param string $mode
c917824b 135 * 'safe' mode or 'force' mode.
54957108 136 * @param int $batchLimit
137 * @param int $isSelected
9f54f049 138 * @param array $criteria
54957108 139 *
140 * @return int
63ef778e 141 */
9f54f049 142 public static function callBatchMerge(CRM_Queue_TaskContext $ctx, $rgid, $gid, $mode = 'safe', $batchLimit, $isSelected, $criteria) {
143 CRM_Dedupe_Merger::batchMerge($rgid, $gid, $mode, $batchLimit, $isSelected, $criteria);
63ef778e 144 return CRM_Queue_Task::TASK_SUCCESS;
145 }
63ef778e 146
f931b74c 147}