Merge pull request #18033 from eileenmcnaughton/cont_id
[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
00be9182 27 public function preProcess() {
87b48098
K
28
29 $this->_type = 'unsubscribe';
30
31 $this->_job_id = $job_id = CRM_Utils_Request::retrieve('jid', 'Integer', $this);
32 $this->_queue_id = $queue_id = CRM_Utils_Request::retrieve('qid', 'Integer', $this);
33 $this->_hash = $hash = CRM_Utils_Request::retrieve('h', 'String', $this);
34
35 if (!$job_id ||
36 !$queue_id ||
37 !$hash
38 ) {
2a7b8221 39 throw new CRM_Core_Exception(ts('Missing Parameters'));
87b48098
K
40 }
41
42 // verify that the three numbers above match
43 $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
44 if (!$q) {
2a7b8221 45 throw new CRM_Core_Exception(ts("There was an error in your request"));
87b48098
K
46 }
47
48 list($displayName, $email) = CRM_Mailing_Event_BAO_Queue::getContactInfo($queue_id);
49 $this->assign('display_name', $displayName);
50 $emailMasked = CRM_Utils_String::maskEmail($email);
51 $this->assign('email_masked', $emailMasked);
52 $this->assign('email', $email);
53 $this->_email = $email;
54
55 $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash, TRUE);
56 $this->assign('groups', $groups);
57 $groupExist = NULL;
58 foreach ($groups as $key => $value) {
59 if ($value) {
60 $groupExist = TRUE;
61 }
62 }
32077cfb 63 if (!$groupExist) {
64 $statusMsg = ts('Email: %1 has been successfully unsubscribed from this Mailing List/Group.',
be2fb01f 65 [1 => $email]
32077cfb 66 );
0563bca3 67 CRM_Core_Session::setStatus($statusMsg, '', 'error');
32077cfb 68 }
87b48098
K
69 $this->assign('groupExist', $groupExist);
70
71 }
72
00be9182 73 public function buildQuickForm() {
ad03f101 74 CRM_Utils_System::addHTMLHead('<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">');
87b48098
K
75 CRM_Utils_System::setTitle(ts('Please Confirm Your Unsubscribe from this Mailing/Group'));
76
bc76a7f7
DG
77 $this->add('text', 'email_confirm', ts('Verify email address to unsubscribe:'));
78 $this->addRule('email_confirm', ts('Email address is required to unsubscribe.'), 'required');
87b48098 79
be2fb01f
CW
80 $buttons = [
81 [
87b48098 82 'type' => 'next',
8109f693 83 'name' => ts('Unsubscribe'),
87b48098 84 'isDefault' => TRUE,
be2fb01f
CW
85 ],
86 [
bc76a7f7
DG
87 'type' => 'cancel',
88 'name' => ts('Cancel'),
be2fb01f
CW
89 ],
90 ];
87b48098
K
91
92 $this->addButtons($buttons);
93 }
94
00be9182 95 public function postProcess() {
87b48098
K
96 $values = $this->exportValues();
97
98 // check if EmailTyped matches Email address
99 $result = CRM_Utils_String::compareStr($this->_email, $values['email_confirm'], TRUE);
87b48098
K
100 $job_id = $this->_job_id;
101 $queue_id = $this->_queue_id;
102 $hash = $this->_hash;
103
353ffa53 104 $confirmURL = CRM_Utils_System::url("civicrm/mailing/{$this->_type}", "reset=1&jid={$job_id}&qid={$queue_id}&h={$hash}&confirm=1");
87b48098
K
105 $this->assign('confirmURL', $confirmURL);
106 $session = CRM_Core_Session::singleton();
107 $session->pushUserContext($confirmURL);
108
109 if ($result == TRUE) {
110 // Email address verified
87b48098 111 $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash);
0563bca3 112
87b48098
K
113 if (count($groups)) {
114 CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue_id, $groups, FALSE, $job_id);
115 }
116
bc76a7f7 117 $statusMsg = ts('Email: %1 has been successfully unsubscribed from this Mailing List/Group.',
be2fb01f 118 [1 => $values['email_confirm']]
87b48098
K
119 );
120
481a74f4 121 CRM_Core_Session::setStatus($statusMsg, '', 'success');
87b48098 122 }
4c9b6178 123 elseif ($result == FALSE) {
87b48098 124 // Email address not verified
bc76a7f7 125 $statusMsg = ts('The email address: %1 you have entered does not match the email associated with this unsubscribe request.',
be2fb01f 126 [1 => $values['email_confirm']]
87b48098
K
127 );
128
0563bca3 129 CRM_Core_Session::setStatus($statusMsg, '', 'error');
87b48098
K
130
131 }
87b48098 132 }
96025800 133
87b48098 134}