Merge pull request #14018 from seamuslee001/financial_extension_export_cxn_dashlet
[civicrm-core.git] / CRM / Mailing / Form / Optout.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33 class CRM_Mailing_Form_Optout extends CRM_Core_Form {
34
35 public function preProcess() {
36
37 $this->_type = 'optout';
38
39 $this->_job_id = $job_id = CRM_Utils_Request::retrieve('jid', 'Integer', $this);
40 $this->_queue_id = $queue_id = CRM_Utils_Request::retrieve('qid', 'Integer', $this);
41 $this->_hash = $hash = CRM_Utils_Request::retrieve('h', 'String', $this);
42
43 if (!$job_id ||
44 !$queue_id ||
45 !$hash
46 ) {
47 CRM_Core_Error::fatal(ts("Missing input parameters"));
48 }
49
50 // verify that the three numbers above match
51 $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
52 if (!$q) {
53 CRM_Core_Error::fatal(ts("There was an error in your request"));
54 }
55
56 list($displayName, $email) = CRM_Mailing_Event_BAO_Queue::getContactInfo($queue_id);
57 $this->assign('display_name', $displayName);
58 $emailMasked = CRM_Utils_String::maskEmail($email);
59 $this->assign('email_masked', $emailMasked);
60 $this->assign('email', $email);
61 $this->_email = $email;
62 }
63
64 public function buildQuickForm() {
65 CRM_Utils_System::addHTMLHead('<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">');
66 CRM_Utils_System::setTitle(ts('Please Confirm Your Opt Out'));
67
68 $this->add('text', 'email_confirm', ts('Verify email address to opt out:'));
69 $this->addRule('email_confirm', ts('Email address is required to opt out.'), 'required');
70
71 $buttons = [
72 [
73 'type' => 'next',
74 'name' => ts('Opt Out'),
75 'isDefault' => TRUE,
76 ],
77 [
78 'type' => 'cancel',
79 'name' => ts('Cancel'),
80 ],
81 ];
82
83 $this->addButtons($buttons);
84 }
85
86 public function postProcess() {
87
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
93 $job_id = $this->_job_id;
94 $queue_id = $this->_queue_id;
95 $hash = $this->_hash;
96
97 $confirmURL = CRM_Utils_System::url("civicrm/mailing/{$this->_type}", "reset=1&jid={$job_id}&qid={$queue_id}&h={$hash}&confirm=1");
98 $this->assign('confirmURL', $confirmURL);
99 $session = CRM_Core_Session::singleton();
100 $session->pushUserContext($confirmURL);
101
102 if ($result == TRUE) {
103 // Email address verified
104 if (CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_domain($job_id, $queue_id, $hash)) {
105 CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue_id, NULL, TRUE, $job_id);
106 }
107
108 $statusMsg = ts('Email: %1 has been successfully opted out',
109 [1 => $values['email_confirm']]
110 );
111
112 CRM_Core_Session::setStatus($statusMsg, '', 'success');
113 }
114 elseif ($result == FALSE) {
115 // Email address not verified
116 $statusMsg = ts('The email address: %1 you have entered does not match the email associated with this opt out request.',
117 [1 => $values['email_confirm']]
118 );
119
120 CRM_Core_Session::setStatus($statusMsg, '', 'error');
121 }
122
123 }
124
125 }