Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | class CRM_Mailing_Form_Subscribe extends CRM_Core_Form { | |
34 | protected $_groupID = NULL; | |
35 | ||
00be9182 | 36 | public function preProcess() { |
6a488035 TO |
37 | parent::preProcess(); |
38 | $this->_groupID = CRM_Utils_Request::retrieve('gid', 'Integer', $this, | |
39 | FALSE, NULL, 'REQUEST' | |
40 | ); | |
41 | ||
42 | // ensure that there is a destination, if not set the destination to the | |
43 | // referrer string | |
44 | if (!$this->controller->getDestination()) { | |
45 | $this->controller->setDestination(NULL, TRUE); | |
46 | } | |
47 | ||
6a488035 TO |
48 | if ($this->_groupID) { |
49 | $groupTypeCondition = CRM_Contact_BAO_Group::groupTypeCondition('Mailing'); | |
50 | ||
51 | // make sure requested qroup is accessible and exists | |
52 | $query = " | |
53 | SELECT title, description | |
54 | FROM civicrm_group | |
03e04002 | 55 | WHERE id={$this->_groupID} |
6a488035 TO |
56 | AND visibility != 'User and User Admin Only' |
57 | AND $groupTypeCondition"; | |
58 | ||
33621c4f | 59 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
60 | if ($dao->fetch()) { |
61 | $this->assign('groupName', $dao->title); | |
be2fb01f | 62 | CRM_Utils_System::setTitle(ts('Subscribe to Mailing List - %1', [1 => $dao->title])); |
6a488035 TO |
63 | } |
64 | else { | |
65 | CRM_Core_Error::statusBounce("The specified group is not configured for this action OR The group doesn't exist."); | |
66 | } | |
67 | ||
68 | $this->assign('single', TRUE); | |
69 | } | |
70 | else { | |
71 | $this->assign('single', FALSE); | |
72 | CRM_Utils_System::setTitle(ts('Mailing List Subscription')); | |
73 | } | |
74 | } | |
75 | ||
76 | /** | |
fe482240 | 77 | * Build the form object. |
6a488035 TO |
78 | */ |
79 | public function buildQuickForm() { | |
80 | // add the email address | |
81 | $this->add('text', | |
82 | 'email', | |
83 | ts('Email'), | |
84 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', | |
85 | 'email' | |
86 | ), | |
87 | TRUE | |
88 | ); | |
f289559e | 89 | $this->addRule('email', ts("Please enter a valid email address."), 'email'); |
6a488035 TO |
90 | |
91 | if (!$this->_groupID) { | |
92 | // create a selector box of all public groups | |
93 | $groupTypeCondition = CRM_Contact_BAO_Group::groupTypeCondition('Mailing'); | |
94 | ||
95 | $query = " | |
96 | SELECT id, title, description | |
97 | FROM civicrm_group | |
98 | WHERE ( saved_search_id = 0 | |
99 | OR saved_search_id IS NULL ) | |
100 | AND visibility != 'User and User Admin Only' | |
101 | AND $groupTypeCondition | |
102 | ORDER BY title"; | |
33621c4f | 103 | $dao = CRM_Core_DAO::executeQuery($query); |
be2fb01f | 104 | $rows = []; |
6a488035 | 105 | while ($dao->fetch()) { |
be2fb01f | 106 | $row = []; |
353ffa53 TO |
107 | $row['id'] = $dao->id; |
108 | $row['title'] = $dao->title; | |
6a488035 | 109 | $row['description'] = $dao->description; |
353ffa53 | 110 | $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $row['id']; |
6a488035 TO |
111 | $this->addElement('checkbox', |
112 | $row['checkbox'], | |
113 | NULL, NULL | |
114 | ); | |
115 | $rows[] = $row; | |
116 | } | |
117 | if (empty($rows)) { | |
118 | CRM_Core_Error::fatal(ts('There are no public mailing list groups to display.')); | |
119 | } | |
120 | $this->assign('rows', $rows); | |
be2fb01f | 121 | $this->addFormRule(['CRM_Mailing_Form_Subscribe', 'formRule']); |
6a488035 TO |
122 | } |
123 | ||
124 | $addCaptcha = TRUE; | |
125 | ||
71fc6ea4 DG |
126 | // if recaptcha is not configured, then dont add it |
127 | // CRM-11316 Only enable ReCAPTCHA for anonymous visitors | |
6a488035 | 128 | $config = CRM_Core_Config::singleton(); |
353ffa53 | 129 | $session = CRM_Core_Session::singleton(); |
71fc6ea4 | 130 | $contactID = $session->get('userID'); |
e0ef6999 | 131 | |
6a488035 | 132 | if (empty($config->recaptchaPublicKey) || |
71fc6ea4 | 133 | empty($config->recaptchaPrivateKey) || |
353ffa53 TO |
134 | $contactID |
135 | ) { | |
6a488035 TO |
136 | $addCaptcha = FALSE; |
137 | } | |
138 | else { | |
25606795 SB |
139 | // If this is POST request and came from a block, |
140 | // lets add recaptcha only if already present. | |
141 | // Gross hack for now. | |
6a488035 TO |
142 | if (!empty($_POST) && |
143 | !array_key_exists('recaptcha_challenge_field', $_POST) | |
144 | ) { | |
145 | $addCaptcha = FALSE; | |
146 | } | |
147 | } | |
148 | ||
149 | if ($addCaptcha) { | |
150 | // add captcha | |
151 | $captcha = CRM_Utils_ReCAPTCHA::singleton(); | |
152 | $captcha->add($this); | |
71fc6ea4 | 153 | $this->assign('isCaptcha', TRUE); |
6a488035 TO |
154 | } |
155 | ||
be2fb01f | 156 | $this->addButtons([ |
7e8c8317 SL |
157 | [ |
158 | 'type' => 'next', | |
159 | 'name' => ts('Subscribe'), | |
160 | 'isDefault' => TRUE, | |
161 | ], | |
162 | [ | |
163 | 'type' => 'cancel', | |
164 | 'name' => ts('Cancel'), | |
165 | ], | |
166 | ]); | |
6a488035 TO |
167 | } |
168 | ||
e0ef6999 EM |
169 | /** |
170 | * @param $fields | |
171 | * | |
172 | * @return array|bool | |
173 | */ | |
00be9182 | 174 | public static function formRule($fields) { |
6a488035 TO |
175 | foreach ($fields as $name => $dontCare) { |
176 | if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { | |
177 | return TRUE; | |
178 | } | |
179 | } | |
be2fb01f | 180 | return ['_qf_default' => 'Please select one or more mailing lists.']; |
6a488035 TO |
181 | } |
182 | ||
6a488035 TO |
183 | public function postProcess() { |
184 | $params = $this->controller->exportValues($this->_name); | |
185 | ||
be2fb01f | 186 | $groups = []; |
6a488035 TO |
187 | if ($this->_groupID) { |
188 | $groups[] = $this->_groupID; | |
189 | } | |
190 | else { | |
191 | foreach ($params as $name => $dontCare) { | |
192 | if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { | |
193 | $groups[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN); | |
194 | } | |
195 | } | |
196 | } | |
197 | ||
198 | CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($groups, $params); | |
199 | } | |
96025800 | 200 | |
6a488035 | 201 | } |