Merge pull request #4667 from eileenmcnaughton/whitespace
[civicrm-core.git] / CRM / Mailing / Form / Subscribe.php
CommitLineData
6a488035
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
06b69b18 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36class CRM_Mailing_Form_Subscribe extends CRM_Core_Form {
37 protected $_groupID = NULL;
38
39 function preProcess() {
40 parent::preProcess();
41 $this->_groupID = CRM_Utils_Request::retrieve('gid', 'Integer', $this,
42 FALSE, NULL, 'REQUEST'
43 );
44
45 // ensure that there is a destination, if not set the destination to the
46 // referrer string
47 if (!$this->controller->getDestination()) {
48 $this->controller->setDestination(NULL, TRUE);
49 }
50
51
52 if ($this->_groupID) {
53 $groupTypeCondition = CRM_Contact_BAO_Group::groupTypeCondition('Mailing');
54
55 // make sure requested qroup is accessible and exists
56 $query = "
57SELECT title, description
58 FROM civicrm_group
03e04002 59 WHERE id={$this->_groupID}
6a488035
TO
60 AND visibility != 'User and User Admin Only'
61 AND $groupTypeCondition";
62
63 $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
64 if ($dao->fetch()) {
65 $this->assign('groupName', $dao->title);
66 CRM_Utils_System::setTitle(ts('Subscribe to Mailing List - %1', array(1 => $dao->title)));
67 }
68 else {
69 CRM_Core_Error::statusBounce("The specified group is not configured for this action OR The group doesn't exist.");
70 }
71
72 $this->assign('single', TRUE);
73 }
74 else {
75 $this->assign('single', FALSE);
76 CRM_Utils_System::setTitle(ts('Mailing List Subscription'));
77 }
78 }
79
80 /**
c490a46a 81 * Build the form object
6a488035 82 *
355ba699 83 * @return void
6a488035
TO
84 * @access public
85 */
86 public function buildQuickForm() {
87 // add the email address
88 $this->add('text',
89 'email',
90 ts('Email'),
91 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email',
92 'email'
93 ),
94 TRUE
95 );
f289559e 96 $this->addRule('email', ts("Please enter a valid email address."), 'email');
6a488035
TO
97
98 if (!$this->_groupID) {
99 // create a selector box of all public groups
100 $groupTypeCondition = CRM_Contact_BAO_Group::groupTypeCondition('Mailing');
101
102 $query = "
103SELECT id, title, description
104 FROM civicrm_group
105 WHERE ( saved_search_id = 0
106 OR saved_search_id IS NULL )
107 AND visibility != 'User and User Admin Only'
108 AND $groupTypeCondition
109ORDER BY title";
110 $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
111 $rows = array();
112 while ($dao->fetch()) {
113 $row = array();
114 $row['id'] = $dao->id;
115 $row['title'] = $dao->title;
116 $row['description'] = $dao->description;
117 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $row['id'];
118 $this->addElement('checkbox',
119 $row['checkbox'],
120 NULL, NULL
121 );
122 $rows[] = $row;
123 }
124 if (empty($rows)) {
125 CRM_Core_Error::fatal(ts('There are no public mailing list groups to display.'));
126 }
127 $this->assign('rows', $rows);
128 $this->addFormRule(array('CRM_Mailing_Form_Subscribe', 'formRule'));
129 }
130
131 $addCaptcha = TRUE;
132
71fc6ea4
DG
133 // if recaptcha is not configured, then dont add it
134 // CRM-11316 Only enable ReCAPTCHA for anonymous visitors
6a488035 135 $config = CRM_Core_Config::singleton();
71fc6ea4
DG
136 $session = CRM_Core_Session::singleton();
137 $contactID = $session->get('userID');
e0ef6999 138
6a488035 139 if (empty($config->recaptchaPublicKey) ||
71fc6ea4
DG
140 empty($config->recaptchaPrivateKey) ||
141 $contactID) {
6a488035
TO
142 $addCaptcha = FALSE;
143 }
144 else {
145 // if this is POST request and came from a block,
146 // lets add recaptcha only if already present
147 // gross hack for now
148 if (!empty($_POST) &&
149 !array_key_exists('recaptcha_challenge_field', $_POST)
150 ) {
151 $addCaptcha = FALSE;
152 }
153 }
154
155 if ($addCaptcha) {
156 // add captcha
157 $captcha = CRM_Utils_ReCAPTCHA::singleton();
158 $captcha->add($this);
71fc6ea4 159 $this->assign('isCaptcha', TRUE);
6a488035
TO
160 }
161
162 $this->addButtons(array(
163 array(
164 'type' => 'next',
165 'name' => ts('Subscribe'),
166 'isDefault' => TRUE,
167 ),
168 array(
169 'type' => 'cancel',
170 'name' => ts('Cancel'),
171 ),
172 )
173 );
174 }
175
e0ef6999
EM
176 /**
177 * @param $fields
178 *
179 * @return array|bool
180 */
6a488035
TO
181 static function formRule($fields) {
182 foreach ($fields as $name => $dontCare) {
183 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
184 return TRUE;
185 }
186 }
187 return array('_qf_default' => 'Please select one or more mailing lists.');
188 }
189
190 /**
6a488035
TO
191 * @access public
192 *
355ba699 193 * @return void
6a488035
TO
194 */
195 public function postProcess() {
196 $params = $this->controller->exportValues($this->_name);
197
198 $groups = array();
199 if ($this->_groupID) {
200 $groups[] = $this->_groupID;
201 }
202 else {
203 foreach ($params as $name => $dontCare) {
204 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
205 $groups[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
206 }
207 }
208 }
209
210 CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($groups, $params);
211 }
212}
213