Commit | Line | Data |
---|---|---|
63ef778e | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
3435af9a | 4 | | CiviCRM version 4.7 | |
63ef778e | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
63ef778e | 32 | */ |
f931b74c | 33 | class 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) { | |
43 | // Run Everything in the Queue via the Web. | |
44 | $runner->runAllViaWeb(); | |
f931b74c | 45 | } |
46 | else { | |
63ef778e | 47 | CRM_Core_Session::setStatus(ts('Nothing to merge.')); |
48 | } | |
49 | ||
50 | // parent run | |
51 | return parent::run(); | |
52 | } | |
53 | ||
183ec330 | 54 | /** |
55 | * Build a queue of tasks by dividing dupe pairs in batches. | |
56 | */ | |
f931b74c | 57 | public static function getRunner() { |
b639c500 | 58 | $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive'); |
59 | $gid = CRM_Utils_Request::retrieve('gid', 'Positive'); | |
60 | $limit = CRM_Utils_Request::retrieve('limit', 'Positive'); | |
a3d827a7 | 61 | $action = CRM_Utils_Request::retrieve('action', 'String'); |
63ef778e | 62 | $mode = CRM_Utils_Request::retrieve('mode', 'String', CRM_Core_DAO::$_nullObject, FALSE, 'safe'); |
63 | ||
2ae26001 | 64 | $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid); |
63ef778e | 65 | |
b639c500 | 66 | $urlQry = "reset=1&action=update&rgid={$rgid}&gid={$gid}&limit={$limit}"; |
fd630ef9 | 67 | |
68 | if ($mode == 'aggressive' && !CRM_Core_Permission::check('force merge duplicate contacts')) { | |
69 | CRM_Core_Session::setStatus(ts('You do not have permission to force merge duplicate contact records'), ts('Permission Denied'), 'error'); | |
70 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry)); | |
71 | } | |
63ef778e | 72 | // Setup the Queue |
73 | $queue = CRM_Queue_Service::singleton()->create(array( | |
74 | 'name' => $cacheKeyString, | |
75 | 'type' => 'Sql', | |
76 | 'reset' => TRUE, | |
77 | )); | |
78 | ||
79 | $where = NULL; | |
80 | if ($action == CRM_Core_Action::MAP) { | |
81 | $where = "pn.is_selected = 1"; | |
82 | $isSelected = 1; | |
f931b74c | 83 | } |
84 | else { | |
63ef778e | 85 | // else merge all (2) |
f931b74c | 86 | $isSelected = 2; |
63ef778e | 87 | } |
88 | ||
63ef778e | 89 | $total = CRM_Core_BAO_PrevNextCache::getCount($cacheKeyString, NULL, $where); |
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 | ||
f931b74c | 98 | for ($i = 1; $i <= ceil($total / self::BATCHLIMIT); $i++) { |
63ef778e | 99 | $task = new CRM_Queue_Task( |
f931b74c | 100 | array('CRM_Contact_Page_DedupeMerge', 'callBatchMerge'), |
7a555671 | 101 | array($rgid, $gid, $mode, self::BATCHLIMIT, $isSelected), |
f931b74c | 102 | "Processed " . $i * self::BATCHLIMIT . " pair of duplicates out of " . $total |
63ef778e | 103 | ); |
104 | ||
105 | // Add the Task to the Queue | |
106 | $queue->createItem($task); | |
107 | } | |
108 | ||
109 | // Setup the Runner | |
110 | $urlQry .= "&context=conflicts"; | |
111 | $runner = new CRM_Queue_Runner(array( | |
f931b74c | 112 | 'title' => ts('Merging Duplicates..'), |
113 | 'queue' => $queue, | |
114 | 'errorMode' => CRM_Queue_Runner::ERROR_ABORT, | |
115 | 'onEndUrl' => CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry, TRUE, NULL, FALSE), | |
63ef778e | 116 | )); |
117 | ||
118 | return $runner; | |
119 | } | |
120 | ||
121 | /** | |
183ec330 | 122 | * Carry out batch merges. |
54957108 | 123 | * |
124 | * @param \CRM_Queue_TaskContext $ctx | |
125 | * @param int $rgid | |
126 | * @param int $gid | |
127 | * @param string $mode | |
c917824b | 128 | * 'safe' mode or 'force' mode. |
54957108 | 129 | * @param int $batchLimit |
130 | * @param int $isSelected | |
131 | * | |
132 | * @return int | |
63ef778e | 133 | */ |
d13a105a J |
134 | public static function callBatchMerge(CRM_Queue_TaskContext $ctx, $rgid, $gid, $mode = 'safe', $batchLimit, $isSelected) { |
135 | CRM_Dedupe_Merger::batchMerge($rgid, $gid, $mode, $batchLimit, $isSelected); | |
63ef778e | 136 | return CRM_Queue_Task::TASK_SUCCESS; |
137 | } | |
63ef778e | 138 | |
f931b74c | 139 | } |