3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2020
33 class CRM_Mailing_Form_Subscribe
extends CRM_Core_Form
{
34 protected $_groupID = NULL;
36 public function preProcess() {
38 $this->_groupID
= CRM_Utils_Request
::retrieve('gid', 'Integer', $this,
39 FALSE, NULL, 'REQUEST'
42 // ensure that there is a destination, if not set the destination to the
44 if (!$this->controller
->getDestination()) {
45 $this->controller
->setDestination(NULL, TRUE);
48 if ($this->_groupID
) {
49 $groupTypeCondition = CRM_Contact_BAO_Group
::groupTypeCondition('Mailing');
51 // make sure requested qroup is accessible and exists
53 SELECT title, description
55 WHERE id={$this->_groupID}
56 AND visibility != 'User and User Admin Only'
57 AND $groupTypeCondition";
59 $dao = CRM_Core_DAO
::executeQuery($query);
61 $this->assign('groupName', $dao->title
);
62 CRM_Utils_System
::setTitle(ts('Subscribe to Mailing List - %1', [1 => $dao->title
]));
65 CRM_Core_Error
::statusBounce("The specified group is not configured for this action OR The group doesn't exist.");
68 $this->assign('single', TRUE);
71 $this->assign('single', FALSE);
72 CRM_Utils_System
::setTitle(ts('Mailing List Subscription'));
77 * Build the form object.
79 public function buildQuickForm() {
80 // add the email address
84 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Email',
89 $this->addRule('email', ts("Please enter a valid email address."), 'email');
91 if (!$this->_groupID
) {
92 // create a selector box of all public groups
93 $groupTypeCondition = CRM_Contact_BAO_Group
::groupTypeCondition('Mailing');
96 SELECT id, title, description
98 WHERE ( saved_search_id = 0
99 OR saved_search_id IS NULL )
100 AND visibility != 'User and User Admin Only'
101 AND $groupTypeCondition
103 $dao = CRM_Core_DAO
::executeQuery($query);
105 while ($dao->fetch()) {
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',
118 throw new CRM_Core_Exception(ts('There are no public mailing list groups to display.'));
120 $this->assign('rows', $rows);
121 $this->addFormRule(['CRM_Mailing_Form_Subscribe', 'formRule']);
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');
132 if (empty($config->recaptchaPublicKey
) ||
133 empty($config->recaptchaPrivateKey
) ||
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)
150 CRM_Utils_ReCAPTCHA
::enableCaptchaOnForm($this);
156 'name' => ts('Subscribe'),
161 'name' => ts('Cancel'),
171 public 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
) {
177 return ['_qf_default' => 'Please select one or more mailing lists.'];
180 public function postProcess() {
181 $params = $this->controller
->exportValues($this->_name
);
184 if ($this->_groupID
) {
185 $groups[] = $this->_groupID
;
188 foreach ($params as $name => $dontCare) {
189 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
190 $groups[] = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
195 CRM_Mailing_Event_BAO_Subscribe
::commonSubscribe($groups, $params);