Commit | Line | Data |
---|---|---|
87b48098 K |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
87b48098 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
87b48098 K |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
87b48098 K |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
87b48098 K |
32 | */ |
33 | class CRM_Mailing_Form_Unsubscribe extends CRM_Core_Form { | |
34 | ||
00be9182 | 35 | public function preProcess() { |
87b48098 K |
36 | |
37 | $this->_type = 'unsubscribe'; | |
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 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 | $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash, TRUE); | |
64 | $this->assign('groups', $groups); | |
65 | $groupExist = NULL; | |
66 | foreach ($groups as $key => $value) { | |
67 | if ($value) { | |
68 | $groupExist = TRUE; | |
69 | } | |
70 | } | |
32077cfb | 71 | if (!$groupExist) { |
72 | $statusMsg = ts('Email: %1 has been successfully unsubscribed from this Mailing List/Group.', | |
73 | array(1 => $email) | |
74 | ); | |
481a74f4 | 75 | CRM_Core_Session::setStatus($statusMsg, '', 'fail'); |
32077cfb | 76 | } |
87b48098 K |
77 | $this->assign('groupExist', $groupExist); |
78 | ||
79 | } | |
80 | ||
00be9182 | 81 | public function buildQuickForm() { |
ad03f101 | 82 | CRM_Utils_System::addHTMLHead('<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">'); |
87b48098 K |
83 | CRM_Utils_System::setTitle(ts('Please Confirm Your Unsubscribe from this Mailing/Group')); |
84 | ||
bc76a7f7 DG |
85 | $this->add('text', 'email_confirm', ts('Verify email address to unsubscribe:')); |
86 | $this->addRule('email_confirm', ts('Email address is required to unsubscribe.'), 'required'); | |
87b48098 K |
87 | |
88 | $buttons = array( | |
87b48098 K |
89 | array( |
90 | 'type' => 'next', | |
8109f693 | 91 | 'name' => ts('Unsubscribe'), |
87b48098 K |
92 | 'isDefault' => TRUE, |
93 | ), | |
bc76a7f7 DG |
94 | array( |
95 | 'type' => 'cancel', | |
96 | 'name' => ts('Cancel'), | |
97 | ), | |
87b48098 K |
98 | ); |
99 | ||
100 | $this->addButtons($buttons); | |
101 | } | |
102 | ||
00be9182 | 103 | public function postProcess() { |
87b48098 K |
104 | $values = $this->exportValues(); |
105 | ||
106 | // check if EmailTyped matches Email address | |
107 | $result = CRM_Utils_String::compareStr($this->_email, $values['email_confirm'], TRUE); | |
87b48098 K |
108 | $job_id = $this->_job_id; |
109 | $queue_id = $this->_queue_id; | |
110 | $hash = $this->_hash; | |
111 | ||
353ffa53 | 112 | $confirmURL = CRM_Utils_System::url("civicrm/mailing/{$this->_type}", "reset=1&jid={$job_id}&qid={$queue_id}&h={$hash}&confirm=1"); |
87b48098 K |
113 | $this->assign('confirmURL', $confirmURL); |
114 | $session = CRM_Core_Session::singleton(); | |
115 | $session->pushUserContext($confirmURL); | |
116 | ||
117 | if ($result == TRUE) { | |
118 | // Email address verified | |
119 | ||
120 | $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash); | |
121 | if (count($groups)) { | |
122 | CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue_id, $groups, FALSE, $job_id); | |
123 | } | |
124 | ||
bc76a7f7 | 125 | $statusMsg = ts('Email: %1 has been successfully unsubscribed from this Mailing List/Group.', |
87b48098 K |
126 | array(1 => $values['email_confirm']) |
127 | ); | |
128 | ||
481a74f4 | 129 | CRM_Core_Session::setStatus($statusMsg, '', 'success'); |
87b48098 | 130 | } |
4c9b6178 | 131 | elseif ($result == FALSE) { |
87b48098 K |
132 | // Email address not verified |
133 | ||
bc76a7f7 | 134 | $statusMsg = ts('The email address: %1 you have entered does not match the email associated with this unsubscribe request.', |
87b48098 K |
135 | array(1 => $values['email_confirm']) |
136 | ); | |
137 | ||
353ffa53 | 138 | CRM_Core_Session::setStatus($statusMsg, '', 'fail'); |
87b48098 K |
139 | |
140 | } | |
141 | ||
142 | } | |
96025800 | 143 | |
87b48098 | 144 | } |