Merge pull request #17192 from jmdh/log_config
[civicrm-core.git] / CRM / Mailing / Form / Unsubscribe.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 class CRM_Mailing_Form_Unsubscribe extends CRM_Core_Form {
18
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
27 public function preProcess() {
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 ) {
39 throw new CRM_Core_Exception(ts('Missing Parameters'));
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) {
45 throw new CRM_Core_Exception(ts("There was an error in your request"));
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 }
63 if (!$groupExist) {
64 $statusMsg = ts('Email: %1 has been successfully unsubscribed from this Mailing List/Group.',
65 [1 => $email]
66 );
67 CRM_Core_Session::setStatus($statusMsg, '', 'error');
68 }
69 $this->assign('groupExist', $groupExist);
70
71 }
72
73 public function buildQuickForm() {
74 CRM_Utils_System::addHTMLHead('<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">');
75 CRM_Utils_System::setTitle(ts('Please Confirm Your Unsubscribe from this Mailing/Group'));
76
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');
79
80 $buttons = [
81 [
82 'type' => 'next',
83 'name' => ts('Unsubscribe'),
84 'isDefault' => TRUE,
85 ],
86 [
87 'type' => 'cancel',
88 'name' => ts('Cancel'),
89 ],
90 ];
91
92 $this->addButtons($buttons);
93 }
94
95 public function postProcess() {
96 $values = $this->exportValues();
97
98 // check if EmailTyped matches Email address
99 $result = CRM_Utils_String::compareStr($this->_email, $values['email_confirm'], TRUE);
100 $job_id = $this->_job_id;
101 $queue_id = $this->_queue_id;
102 $hash = $this->_hash;
103
104 $confirmURL = CRM_Utils_System::url("civicrm/mailing/{$this->_type}", "reset=1&jid={$job_id}&qid={$queue_id}&h={$hash}&confirm=1");
105 $this->assign('confirmURL', $confirmURL);
106 $session = CRM_Core_Session::singleton();
107 $session->pushUserContext($confirmURL);
108
109 if ($result == TRUE) {
110 // Email address verified
111 $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash);
112
113 if (count($groups)) {
114 CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue_id, $groups, FALSE, $job_id);
115 }
116
117 $statusMsg = ts('Email: %1 has been successfully unsubscribed from this Mailing List/Group.',
118 [1 => $values['email_confirm']]
119 );
120
121 CRM_Core_Session::setStatus($statusMsg, '', 'success');
122 }
123 elseif ($result == FALSE) {
124 // Email address not verified
125 $statusMsg = ts('The email address: %1 you have entered does not match the email associated with this unsubscribe request.',
126 [1 => $values['email_confirm']]
127 );
128
129 CRM_Core_Session::setStatus($statusMsg, '', 'error');
130
131 }
132 }
133
134 }