Merge pull request #18063 from civicrm/5.28
[civicrm-core.git] / CRM / Mailing / Form / Optout.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_Optout extends CRM_Core_Form {
18
19 public function preProcess() {
20
21 $this->_type = 'optout';
22
23 $this->_job_id = $job_id = CRM_Utils_Request::retrieve('jid', 'Integer', $this);
24 $this->_queue_id = $queue_id = CRM_Utils_Request::retrieve('qid', 'Integer', $this);
25 $this->_hash = $hash = CRM_Utils_Request::retrieve('h', 'String', $this);
26
27 if (!$job_id ||
28 !$queue_id ||
29 !$hash
30 ) {
31 throw new CRM_Core_Exception(ts("Missing input parameters"));
32 }
33
34 // verify that the three numbers above match
35 $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
36 if (!$q) {
37 throw new CRM_Core_Exception(ts("There was an error in your request"));
38 }
39
40 list($displayName, $email) = CRM_Mailing_Event_BAO_Queue::getContactInfo($queue_id);
41 $this->assign('display_name', $displayName);
42 $emailMasked = CRM_Utils_String::maskEmail($email);
43 $this->assign('email_masked', $emailMasked);
44 $this->assign('email', $email);
45 $this->_email = $email;
46 }
47
48 public function buildQuickForm() {
49 CRM_Utils_System::addHTMLHead('<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">');
50 CRM_Utils_System::setTitle(ts('Please Confirm Your Opt Out'));
51
52 $this->add('text', 'email_confirm', ts('Verify email address to opt out:'));
53 $this->addRule('email_confirm', ts('Email address is required to opt out.'), 'required');
54
55 $buttons = [
56 [
57 'type' => 'next',
58 'name' => ts('Opt Out'),
59 'isDefault' => TRUE,
60 ],
61 [
62 'type' => 'cancel',
63 'name' => ts('Cancel'),
64 ],
65 ];
66
67 $this->addButtons($buttons);
68 }
69
70 public function postProcess() {
71
72 $values = $this->exportValues();
73
74 // check if EmailTyped matches Email address
75 $result = CRM_Utils_String::compareStr($this->_email, $values['email_confirm'], TRUE);
76
77 $job_id = $this->_job_id;
78 $queue_id = $this->_queue_id;
79 $hash = $this->_hash;
80
81 $confirmURL = CRM_Utils_System::url("civicrm/mailing/{$this->_type}", "reset=1&jid={$job_id}&qid={$queue_id}&h={$hash}&confirm=1");
82 $this->assign('confirmURL', $confirmURL);
83 $session = CRM_Core_Session::singleton();
84 $session->pushUserContext($confirmURL);
85
86 if ($result == TRUE) {
87 // Email address verified
88 if (CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_domain($job_id, $queue_id, $hash)) {
89 CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue_id, NULL, TRUE, $job_id);
90 }
91
92 $statusMsg = ts('Email: %1 has been successfully opted out',
93 [1 => $values['email_confirm']]
94 );
95
96 CRM_Core_Session::setStatus($statusMsg, '', 'success');
97 }
98 elseif ($result == FALSE) {
99 // Email address not verified
100 $statusMsg = ts('The email address: %1 you have entered does not match the email associated with this opt out request.',
101 [1 => $values['email_confirm']]
102 );
103
104 CRM_Core_Session::setStatus($statusMsg, '', 'error');
105 }
106
107 }
108
109 }