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