CRM-17335 reduce use of nullArray
[civicrm-core.git] / CRM / Mailing / Form / Subscribe.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
32 */
33 class CRM_Mailing_Form_Subscribe extends CRM_Core_Form {
34 protected $_groupID = NULL;
35
36 public function preProcess() {
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
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
55 WHERE id={$this->_groupID}
56 AND visibility != 'User and User Admin Only'
57 AND $groupTypeCondition";
58
59 $dao = CRM_Core_DAO::executeQuery($query);
60 if ($dao->fetch()) {
61 $this->assign('groupName', $dao->title);
62 CRM_Utils_System::setTitle(ts('Subscribe to Mailing List - %1', array(1 => $dao->title)));
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 /**
77 * Build the form object.
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 );
89 $this->addRule('email', ts("Please enter a valid email address."), 'email');
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";
103 $dao = CRM_Core_DAO::executeQuery($query);
104 $rows = array();
105 while ($dao->fetch()) {
106 $row = array();
107 $row['id'] = $dao->id;
108 $row['title'] = $dao->title;
109 $row['description'] = $dao->description;
110 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $row['id'];
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);
121 $this->addFormRule(array('CRM_Mailing_Form_Subscribe', 'formRule'));
122 }
123
124 $addCaptcha = TRUE;
125
126 // if recaptcha is not configured, then dont add it
127 // CRM-11316 Only enable ReCAPTCHA for anonymous visitors
128 $config = CRM_Core_Config::singleton();
129 $session = CRM_Core_Session::singleton();
130 $contactID = $session->get('userID');
131
132 if (empty($config->recaptchaPublicKey) ||
133 empty($config->recaptchaPrivateKey) ||
134 $contactID
135 ) {
136 $addCaptcha = FALSE;
137 }
138 else {
139 // If this is POST request and came from a block,
140 // lets add recaptcha only if already present.
141 // Gross hack for now.
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);
153 $this->assign('isCaptcha', TRUE);
154 }
155
156 $this->addButtons(array(
157 array(
158 'type' => 'next',
159 'name' => ts('Subscribe'),
160 'isDefault' => TRUE,
161 ),
162 array(
163 'type' => 'cancel',
164 'name' => ts('Cancel'),
165 ),
166 )
167 );
168 }
169
170 /**
171 * @param $fields
172 *
173 * @return array|bool
174 */
175 public static function formRule($fields) {
176 foreach ($fields as $name => $dontCare) {
177 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
178 return TRUE;
179 }
180 }
181 return array('_qf_default' => 'Please select one or more mailing lists.');
182 }
183
184 public function postProcess() {
185 $params = $this->controller->exportValues($this->_name);
186
187 $groups = array();
188 if ($this->_groupID) {
189 $groups[] = $this->_groupID;
190 }
191 else {
192 foreach ($params as $name => $dontCare) {
193 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
194 $groups[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
195 }
196 }
197 }
198
199 CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($groups, $params);
200 }
201
202 }