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