Merge pull request #15933 from civicrm/5.20
[civicrm-core.git] / CRM / Mailing / Form / Optout.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_Optout extends CRM_Core_Form {
18
00be9182 19 public function preProcess() {
87b48098
K
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 ) {
2a7b8221 31 throw new CRM_Core_Exception(ts("Missing input parameters"));
87b48098
K
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) {
2a7b8221 37 throw new CRM_Core_Exception(ts("There was an error in your request"));
87b48098
K
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
00be9182 48 public function buildQuickForm() {
ad03f101 49 CRM_Utils_System::addHTMLHead('<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">');
87b48098
K
50 CRM_Utils_System::setTitle(ts('Please Confirm Your Opt Out'));
51
bc76a7f7
DG
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');
87b48098 54
be2fb01f
CW
55 $buttons = [
56 [
87b48098 57 'type' => 'next',
6d4ba4bb 58 'name' => ts('Opt Out'),
87b48098 59 'isDefault' => TRUE,
be2fb01f
CW
60 ],
61 [
bc76a7f7
DG
62 'type' => 'cancel',
63 'name' => ts('Cancel'),
be2fb01f
CW
64 ],
65 ];
87b48098
K
66
67 $this->addButtons($buttons);
68 }
69
00be9182 70 public function postProcess() {
87b48098
K
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
87b48098
K
77 $job_id = $this->_job_id;
78 $queue_id = $this->_queue_id;
79 $hash = $this->_hash;
80
353ffa53 81 $confirmURL = CRM_Utils_System::url("civicrm/mailing/{$this->_type}", "reset=1&jid={$job_id}&qid={$queue_id}&h={$hash}&confirm=1");
87b48098
K
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',
be2fb01f 93 [1 => $values['email_confirm']]
87b48098
K
94 );
95
481a74f4 96 CRM_Core_Session::setStatus($statusMsg, '', 'success');
87b48098 97 }
4c9b6178 98 elseif ($result == FALSE) {
87b48098 99 // Email address not verified
bc76a7f7 100 $statusMsg = ts('The email address: %1 you have entered does not match the email associated with this opt out request.',
be2fb01f 101 [1 => $values['email_confirm']]
87b48098
K
102 );
103
0563bca3 104 CRM_Core_Session::setStatus($statusMsg, '', 'error');
87b48098
K
105 }
106
107 }
96025800 108
87b48098 109}