Merge pull request #17513 from JMAConsulting/core-1795
[civicrm-core.git] / CRM / Mailing / Form / Subscribe.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Mailing_Form_Subscribe extends CRM_Core_Form {
18 protected $_groupID = NULL;
19
20 public function preProcess() {
21 parent::preProcess();
22 $this->_groupID = CRM_Utils_Request::retrieve('gid', 'Integer', $this,
23 FALSE, NULL, 'REQUEST'
24 );
25
26 // ensure that there is a destination, if not set the destination to the
27 // referrer string
28 if (!$this->controller->getDestination()) {
29 $this->controller->setDestination(NULL, TRUE);
30 }
31
32 if ($this->_groupID) {
33 $groupTypeCondition = CRM_Contact_BAO_Group::groupTypeCondition('Mailing');
34
35 // make sure requested qroup is accessible and exists
36 $query = "
37 SELECT title, description
38 FROM civicrm_group
39 WHERE id={$this->_groupID}
40 AND visibility != 'User and User Admin Only'
41 AND $groupTypeCondition";
42
43 $dao = CRM_Core_DAO::executeQuery($query);
44 if ($dao->fetch()) {
45 $this->assign('groupName', $dao->title);
46 CRM_Utils_System::setTitle(ts('Subscribe to Mailing List - %1', [1 => $dao->title]));
47 }
48 else {
49 CRM_Core_Error::statusBounce("The specified group is not configured for this action OR The group doesn't exist.");
50 }
51
52 $this->assign('single', TRUE);
53 }
54 else {
55 $this->assign('single', FALSE);
56 CRM_Utils_System::setTitle(ts('Mailing List Subscription'));
57 }
58 }
59
60 /**
61 * Build the form object.
62 */
63 public function buildQuickForm() {
64 // add the email address
65 $this->add('text',
66 'email',
67 ts('Email'),
68 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email',
69 'email'
70 ),
71 TRUE
72 );
73 $this->addRule('email', ts("Please enter a valid email address."), 'email');
74
75 if (!$this->_groupID) {
76 // create a selector box of all public groups
77 $groupTypeCondition = CRM_Contact_BAO_Group::groupTypeCondition('Mailing');
78
79 $query = "
80 SELECT id, title, description
81 FROM civicrm_group
82 WHERE ( saved_search_id = 0
83 OR saved_search_id IS NULL )
84 AND visibility != 'User and User Admin Only'
85 AND $groupTypeCondition
86 ORDER BY title";
87 $dao = CRM_Core_DAO::executeQuery($query);
88 $rows = [];
89 while ($dao->fetch()) {
90 $row = [];
91 $row['id'] = $dao->id;
92 $row['title'] = $dao->title;
93 $row['description'] = $dao->description;
94 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $row['id'];
95 $this->addElement('checkbox',
96 $row['checkbox'],
97 NULL, NULL
98 );
99 $rows[] = $row;
100 }
101 if (empty($rows)) {
102 throw new CRM_Core_Exception(ts('There are no public mailing list groups to display.'));
103 }
104 $this->assign('rows', $rows);
105 $this->addFormRule(['CRM_Mailing_Form_Subscribe', 'formRule']);
106 }
107
108 $addCaptcha = TRUE;
109
110 // if recaptcha is not configured, then dont add it
111 // CRM-11316 Only enable ReCAPTCHA for anonymous visitors
112 $config = CRM_Core_Config::singleton();
113 $session = CRM_Core_Session::singleton();
114 $contactID = $session->get('userID');
115
116 if (empty($config->recaptchaPublicKey) ||
117 empty($config->recaptchaPrivateKey) ||
118 $contactID
119 ) {
120 $addCaptcha = FALSE;
121 }
122 else {
123 // If this is POST request and came from a block,
124 // lets add recaptcha only if already present.
125 // Gross hack for now.
126 if (!empty($_POST) &&
127 !array_key_exists('recaptcha_challenge_field', $_POST)
128 ) {
129 $addCaptcha = FALSE;
130 }
131 }
132
133 if ($addCaptcha) {
134 CRM_Utils_ReCAPTCHA::enableCaptchaOnForm($this);
135 }
136
137 $this->addButtons([
138 [
139 'type' => 'next',
140 'name' => ts('Subscribe'),
141 'isDefault' => TRUE,
142 ],
143 [
144 'type' => 'cancel',
145 'name' => ts('Cancel'),
146 ],
147 ]);
148 }
149
150 /**
151 * @param $fields
152 *
153 * @return array|bool
154 */
155 public static function formRule($fields) {
156 foreach ($fields as $name => $dontCare) {
157 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
158 return TRUE;
159 }
160 }
161 return ['_qf_default' => 'Please select one or more mailing lists.'];
162 }
163
164 public function postProcess() {
165 $params = $this->controller->exportValues($this->_name);
166
167 $groups = [];
168 if ($this->_groupID) {
169 $groups[] = $this->_groupID;
170 }
171 else {
172 foreach ($params as $name => $dontCare) {
173 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
174 $groups[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
175 }
176 }
177 }
178
179 CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($groups, $params);
180 }
181
182 }