Merge pull request #18907 from alifrumin/2139
[civicrm-core.git] / CRM / Contact / Form / Task / Merge.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class provides the functionality to Merge contacts.
20 */
21 class CRM_Contact_Form_Task_Merge extends CRM_Contact_Form_Task {
22
23 /**
24 * Build all the data structures needed to build the form.
25 */
26 public function preProcess() {
27 parent::preProcess();
28 $statusMsg = NULL;
29 $contactIds = [];
30 if (is_array($this->_contactIds)) {
31 $contactIds = array_unique($this->_contactIds);
32 }
33 if (count($contactIds) != 2) {
34 $statusMsg = ts('Merge operation requires selecting two contacts.');
35 }
36
37 // do check for same contact type.
38 $contactTypes = [];
39 if (!$statusMsg) {
40 $sql = "SELECT contact_type FROM civicrm_contact WHERE id IN (" . implode(',', $contactIds) . ")";
41 $contact = CRM_Core_DAO::executeQuery($sql);
42 while ($contact->fetch()) {
43 $contactTypes[$contact->contact_type] = TRUE;
44 if (count($contactTypes) > 1) {
45 break;
46 }
47 }
48 if (count($contactTypes) > 1) {
49 $statusMsg = ts('Selected records must all be the same contact type (i.e. all Individuals).');
50 }
51 }
52 if ($statusMsg) {
53 CRM_Core_Error::statusBounce($statusMsg);
54 }
55
56 // redirect to merge form directly.
57 $cid = $contactIds[0];
58 $oid = $contactIds[1];
59
60 //don't allow to delete logged in user.
61 $session = CRM_Core_Session::singleton();
62 if ($oid == $session->get('userID')) {
63 $oid = $cid;
64 $cid = $session->get('userID');
65 }
66
67 $url = CRM_Utils_System::url('civicrm/contact/merge', "reset=1&cid={$cid}&oid={$oid}");
68
69 // redirect to merge page.
70 CRM_Utils_System::redirect($url);
71 }
72
73 }