Import from SVN (r45945, r596)
[civicrm-core.git] / CRM / Mailing / Form / Subscribe.php
CommitLineData
6a488035
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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
32 * @copyright CiviCRM LLC (c) 2004-2013
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
59 WHERE id={$this->_groupID}
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 /**
81 * Function to actually build the form
82 *
83 * @return None
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 );
96 $this->addRule('email', ts("Please enter a valid email address (e.g. 'yourname@example.com')."), 'email');
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
133 // if recaptcha is not set, then dont add it
134 $config = CRM_Core_Config::singleton();
135 if (empty($config->recaptchaPublicKey) ||
136 empty($config->recaptchaPrivateKey)
137 ) {
138 $addCaptcha = FALSE;
139 }
140 else {
141 // if this is POST request and came from a block,
142 // lets add recaptcha only if already present
143 // gross hack for now
144 if (!empty($_POST) &&
145 !array_key_exists('recaptcha_challenge_field', $_POST)
146 ) {
147 $addCaptcha = FALSE;
148 }
149 }
150
151 if ($addCaptcha) {
152 // add captcha
153 $captcha = CRM_Utils_ReCAPTCHA::singleton();
154 $captcha->add($this);
155 }
156
157 $this->addButtons(array(
158 array(
159 'type' => 'next',
160 'name' => ts('Subscribe'),
161 'isDefault' => TRUE,
162 ),
163 array(
164 'type' => 'cancel',
165 'name' => ts('Cancel'),
166 ),
167 )
168 );
169 }
170
171 static function formRule($fields) {
172 foreach ($fields as $name => $dontCare) {
173 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
174 return TRUE;
175 }
176 }
177 return array('_qf_default' => 'Please select one or more mailing lists.');
178 }
179
180 /**
181 *
182 * @access public
183 *
184 * @return None
185 */
186 public function postProcess() {
187 $params = $this->controller->exportValues($this->_name);
188
189 $groups = array();
190 if ($this->_groupID) {
191 $groups[] = $this->_groupID;
192 }
193 else {
194 foreach ($params as $name => $dontCare) {
195 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
196 $groups[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
197 }
198 }
199 }
200
201 CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($groups, $params);
202 }
203}
204