Merge pull request #23655 from colemanw/searchKitFixJoinAgain
[civicrm-core.git] / CRM / Mailing / Form / Unsubscribe.php
CommitLineData
87b48098
K
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
87b48098 5 | |
bc77d7c0
TO
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 |
87b48098 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
87b48098
K
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
87b48098
K
16 */
17class CRM_Mailing_Form_Unsubscribe extends CRM_Core_Form {
18
5184f1b6
RLAR
19 /**
20 * Prevent people double-submitting the form (e.g. by double-clicking).
21 * https://lab.civicrm.org/dev/core/-/issues/1773
22 *
23 * @var bool
24 */
25 public $submitOnce = TRUE;
26
ec436db2
MW
27 /**
28 * @var int
29 */
30 private $_job_id;
31
32 /**
33 * @var int
34 */
35 private $_queue_id;
87b48098 36
ec436db2
MW
37 /**
38 * @var string
39 */
40 private $_hash;
41
42 /**
43 * @var string
44 */
45 private $_email;
87b48098 46
ec436db2 47 public function preProcess() {
87b48098
K
48 $this->_job_id = $job_id = CRM_Utils_Request::retrieve('jid', 'Integer', $this);
49 $this->_queue_id = $queue_id = CRM_Utils_Request::retrieve('qid', 'Integer', $this);
50 $this->_hash = $hash = CRM_Utils_Request::retrieve('h', 'String', $this);
0bb34e30 51 $isConfirm = CRM_Utils_Request::retrieveValue('confirm', 'Boolean', FALSE, FALSE, 'GET');
87b48098 52
ec436db2 53 if (!$job_id || !$queue_id || !$hash) {
2a7b8221 54 throw new CRM_Core_Exception(ts('Missing Parameters'));
87b48098
K
55 }
56
57 // verify that the three numbers above match
58 $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
59 if (!$q) {
2a7b8221 60 throw new CRM_Core_Exception(ts("There was an error in your request"));
87b48098
K
61 }
62
63 list($displayName, $email) = CRM_Mailing_Event_BAO_Queue::getContactInfo($queue_id);
64 $this->assign('display_name', $displayName);
65 $emailMasked = CRM_Utils_String::maskEmail($email);
66 $this->assign('email_masked', $emailMasked);
67 $this->assign('email', $email);
68 $this->_email = $email;
69
70 $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash, TRUE);
71 $this->assign('groups', $groups);
72 $groupExist = NULL;
ec436db2 73 foreach ($groups as $value) {
87b48098
K
74 if ($value) {
75 $groupExist = TRUE;
76 }
77 }
0bb34e30
MW
78 if (!$groupExist && !$isConfirm) {
79 $statusMsg = ts('%1 has already been unsubscribed.', [1 => $email]);
0563bca3 80 CRM_Core_Session::setStatus($statusMsg, '', 'error');
32077cfb 81 }
87b48098 82 $this->assign('groupExist', $groupExist);
87b48098
K
83 }
84
00be9182 85 public function buildQuickForm() {
ad03f101 86 CRM_Utils_System::addHTMLHead('<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">');
94fd9d17 87 $this->setTitle(ts('Unsubscribe Confirmation'));
87b48098 88
be2fb01f
CW
89 $buttons = [
90 [
87b48098 91 'type' => 'next',
8109f693 92 'name' => ts('Unsubscribe'),
87b48098 93 'isDefault' => TRUE,
be2fb01f
CW
94 ],
95 [
bc76a7f7
DG
96 'type' => 'cancel',
97 'name' => ts('Cancel'),
be2fb01f
CW
98 ],
99 ];
87b48098
K
100
101 $this->addButtons($buttons);
102 }
103
00be9182 104 public function postProcess() {
ec436db2 105 $confirmURL = CRM_Utils_System::url("civicrm/mailing/unsubscribe", "reset=1&jid={$this->_job_id}&qid={$this->_queue_id}&h={$this->_hash}&confirm=1");
87b48098 106 $this->assign('confirmURL', $confirmURL);
ec436db2 107 CRM_Core_Session::singleton()->pushUserContext($confirmURL);
87b48098 108
ec436db2
MW
109 // Email address verified
110 $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($this->_job_id, $this->_queue_id, $this->_hash);
0563bca3 111
ec436db2
MW
112 if (count($groups)) {
113 CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($this->_queue_id, $groups, FALSE, $this->_job_id);
87b48098 114 }
87b48098 115
0bb34e30 116 $statusMsg = ts('%1 has been unsubscribed successfully.', [1 => $this->_email]);
ec436db2 117 CRM_Core_Session::setStatus($statusMsg, '', 'success');
87b48098 118 }
96025800 119
87b48098 120}