Code styling fixes
[civicrm-core.git] / CRM / Contact / Page / DedupeMerge.php
CommitLineData
63ef778e 1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
f931b74c 35class CRM_Contact_Page_DedupeMerge extends CRM_Core_Page {
63ef778e 36
37 const BATCHLIMIT = 2;
38
39 /**
40 * Browse all rule groups
41 *
42 * @return void
43 * @access public
44 */
f931b74c 45 public function run() {
63ef778e 46 $runner = self::getRunner();
47 if ($runner) {
48 // Run Everything in the Queue via the Web.
49 $runner->runAllViaWeb();
f931b74c 50 }
51 else {
63ef778e 52 CRM_Core_Session::setStatus(ts('Nothing to merge.'));
53 }
54
55 // parent run
56 return parent::run();
57 }
58
f931b74c 59 public static function getRunner() {
63ef778e 60 $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this, FALSE, 0);
61 $gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0);
62 $action = CRM_Utils_Request::retrieve('action', 'String', CRM_Core_DAO::$_nullObject);
63 $mode = CRM_Utils_Request::retrieve('mode', 'String', CRM_Core_DAO::$_nullObject, FALSE, 'safe');
64
65 $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
66 $cacheKeyString = "merge {$contactType}";
67 $cacheKeyString .= $rgid ? "_{$rgid}" : '_0';
68 $cacheKeyString .= $gid ? "_{$gid}" : '_0';
69
fd630ef9 70 $urlQry = "reset=1&action=update&rgid={$rgid}";
71 $urlQry = $gid ? ($urlQry . "&gid={$gid}") : $urlQry;
72
73 if ($mode == 'aggressive' && !CRM_Core_Permission::check('force merge duplicate contacts')) {
74 CRM_Core_Session::setStatus(ts('You do not have permission to force merge duplicate contact records'), ts('Permission Denied'), 'error');
75 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
76 }
63ef778e 77 // Setup the Queue
78 $queue = CRM_Queue_Service::singleton()->create(array(
79 'name' => $cacheKeyString,
80 'type' => 'Sql',
81 'reset' => TRUE,
82 ));
83
84 $where = NULL;
85 if ($action == CRM_Core_Action::MAP) {
86 $where = "pn.is_selected = 1";
87 $isSelected = 1;
f931b74c 88 }
89 else {
63ef778e 90 // else merge all (2)
f931b74c 91 $isSelected = 2;
63ef778e 92 }
93
63ef778e 94 $total = CRM_Core_BAO_PrevNextCache::getCount($cacheKeyString, NULL, $where);
95 if ($total <= 0) {
96 // Nothing to do.
97 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry));
98 }
99
100 // reset merge stats, so we compute new stats
101 CRM_Dedupe_Merger::resetMergeStats($cacheKeyString);
102
f931b74c 103 for ($i = 1; $i <= ceil($total / self::BATCHLIMIT); $i++) {
63ef778e 104 $task = new CRM_Queue_Task(
f931b74c 105 array('CRM_Contact_Page_DedupeMerge', 'callBatchMerge'),
63ef778e 106 array($rgid, $gid, $mode, TRUE, self::BATCHLIMIT, $isSelected),
f931b74c 107 "Processed " . $i * self::BATCHLIMIT . " pair of duplicates out of " . $total
63ef778e 108 );
109
110 // Add the Task to the Queue
111 $queue->createItem($task);
112 }
113
114 // Setup the Runner
115 $urlQry .= "&context=conflicts";
116 $runner = new CRM_Queue_Runner(array(
f931b74c 117 'title' => ts('Merging Duplicates..'),
118 'queue' => $queue,
119 'errorMode' => CRM_Queue_Runner::ERROR_ABORT,
120 'onEndUrl' => CRM_Utils_System::url('civicrm/contact/dedupefind', $urlQry, TRUE, NULL, FALSE),
63ef778e 121 ));
122
123 return $runner;
124 }
125
126 /**
127 * Collect Mailchimp data into temporary working table.
128 */
f931b74c 129 public static function callBatchMerge(CRM_Queue_TaskContext $ctx, $rgid, $gid = NULL, $mode = 'safe', $autoFlip = TRUE, $batchLimit = 1, $isSelected = 2) {
63ef778e 130 $result = CRM_Dedupe_Merger::batchMerge($rgid, $gid, $mode, $autoFlip, $batchLimit, $isSelected);
131
132 return CRM_Queue_Task::TASK_SUCCESS;
133 }
63ef778e 134
f931b74c 135}