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