3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Mailing_Form_Unsubscribe
extends CRM_Core_Form
{
20 * Prevent people double-submitting the form (e.g. by double-clicking).
21 * https://lab.civicrm.org/dev/core/-/issues/1773
25 public $submitOnce = TRUE;
27 public function preProcess() {
29 $this->_type
= 'unsubscribe';
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);
39 throw new CRM_Core_Exception(ts('Missing Parameters'));
42 // verify that the three numbers above match
43 $q = CRM_Mailing_Event_BAO_Queue
::verify($job_id, $queue_id, $hash);
45 throw new CRM_Core_Exception(ts("There was an error in your request"));
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;
55 $groups = CRM_Mailing_Event_BAO_Unsubscribe
::unsub_from_mailing($job_id, $queue_id, $hash, TRUE);
56 $this->assign('groups', $groups);
58 foreach ($groups as $key => $value) {
64 $statusMsg = ts('%1 has been unsubscribed.',
67 CRM_Core_Session
::setStatus($statusMsg, '', 'error');
69 $this->assign('groupExist', $groupExist);
73 public function buildQuickForm() {
74 CRM_Utils_System
::addHTMLHead('<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">');
75 $this->setTitle(ts('Unsubscribe Confirmation'));
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');
83 'name' => ts('Unsubscribe'),
88 'name' => ts('Cancel'),
92 $this->addButtons($buttons);
95 public function postProcess() {
96 $values = $this->exportValues();
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
;
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);
109 if ($result == TRUE) {
110 // Email address verified
111 $groups = CRM_Mailing_Event_BAO_Unsubscribe
::unsub_from_mailing($job_id, $queue_id, $hash);
113 if (count($groups)) {
114 CRM_Mailing_Event_BAO_Unsubscribe
::send_unsub_response($queue_id, $groups, FALSE, $job_id);
117 $statusMsg = ts('%1 is unsubscribed.',
118 [1 => $values['email_confirm']]
121 CRM_Core_Session
::setStatus($statusMsg, '', 'success');
123 elseif ($result == FALSE) {
124 // Email address not verified
125 $statusMsg = ts('%1 is not associated with this unsubscribe request.',
126 [1 => $values['email_confirm']]
129 CRM_Core_Session
::setStatus($statusMsg, '', 'error');