Merge pull request #16715 from mattwire/cancelsubscriptiongeneratetext
[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
00be9182 19 public function preProcess() {
87b48098
K
20
21 $this->_type = 'unsubscribe';
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 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 $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash, TRUE);
48 $this->assign('groups', $groups);
49 $groupExist = NULL;
50 foreach ($groups as $key => $value) {
51 if ($value) {
52 $groupExist = TRUE;
53 }
54 }
32077cfb 55 if (!$groupExist) {
56 $statusMsg = ts('Email: %1 has been successfully unsubscribed from this Mailing List/Group.',
be2fb01f 57 [1 => $email]
32077cfb 58 );
0563bca3 59 CRM_Core_Session::setStatus($statusMsg, '', 'error');
32077cfb 60 }
87b48098
K
61 $this->assign('groupExist', $groupExist);
62
63 }
64
00be9182 65 public function buildQuickForm() {
ad03f101 66 CRM_Utils_System::addHTMLHead('<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">');
87b48098
K
67 CRM_Utils_System::setTitle(ts('Please Confirm Your Unsubscribe from this Mailing/Group'));
68
bc76a7f7
DG
69 $this->add('text', 'email_confirm', ts('Verify email address to unsubscribe:'));
70 $this->addRule('email_confirm', ts('Email address is required to unsubscribe.'), 'required');
87b48098 71
be2fb01f
CW
72 $buttons = [
73 [
87b48098 74 'type' => 'next',
8109f693 75 'name' => ts('Unsubscribe'),
87b48098 76 'isDefault' => TRUE,
be2fb01f
CW
77 ],
78 [
bc76a7f7
DG
79 'type' => 'cancel',
80 'name' => ts('Cancel'),
be2fb01f
CW
81 ],
82 ];
87b48098
K
83
84 $this->addButtons($buttons);
85 }
86
00be9182 87 public function postProcess() {
87b48098
K
88 $values = $this->exportValues();
89
90 // check if EmailTyped matches Email address
91 $result = CRM_Utils_String::compareStr($this->_email, $values['email_confirm'], TRUE);
87b48098
K
92 $job_id = $this->_job_id;
93 $queue_id = $this->_queue_id;
94 $hash = $this->_hash;
95
353ffa53 96 $confirmURL = CRM_Utils_System::url("civicrm/mailing/{$this->_type}", "reset=1&jid={$job_id}&qid={$queue_id}&h={$hash}&confirm=1");
87b48098
K
97 $this->assign('confirmURL', $confirmURL);
98 $session = CRM_Core_Session::singleton();
99 $session->pushUserContext($confirmURL);
100
101 if ($result == TRUE) {
102 // Email address verified
87b48098 103 $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash);
0563bca3 104
87b48098
K
105 if (count($groups)) {
106 CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue_id, $groups, FALSE, $job_id);
107 }
108
bc76a7f7 109 $statusMsg = ts('Email: %1 has been successfully unsubscribed from this Mailing List/Group.',
be2fb01f 110 [1 => $values['email_confirm']]
87b48098
K
111 );
112
481a74f4 113 CRM_Core_Session::setStatus($statusMsg, '', 'success');
87b48098 114 }
4c9b6178 115 elseif ($result == FALSE) {
87b48098 116 // Email address not verified
bc76a7f7 117 $statusMsg = ts('The email address: %1 you have entered does not match the email associated with this unsubscribe request.',
be2fb01f 118 [1 => $values['email_confirm']]
87b48098
K
119 );
120
0563bca3 121 CRM_Core_Session::setStatus($statusMsg, '', 'error');
87b48098
K
122
123 }
124
125 }
96025800 126
87b48098 127}