3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Mailing_Form_Subscribe
extends CRM_Core_Form
{
18 protected $_groupID = NULL;
20 public function preProcess() {
22 $this->_groupID
= CRM_Utils_Request
::retrieve('gid', 'Integer', $this,
23 FALSE, NULL, 'REQUEST'
26 // ensure that there is a destination, if not set the destination to the
28 if (!$this->controller
->getDestination()) {
29 $this->controller
->setDestination(NULL, TRUE);
32 if ($this->_groupID
) {
33 $groupTypeCondition = CRM_Contact_BAO_Group
::groupTypeCondition('Mailing');
35 // make sure requested qroup is accessible and exists
37 SELECT title, description
39 WHERE id={$this->_groupID}
40 AND visibility != 'User and User Admin Only'
41 AND $groupTypeCondition";
43 $dao = CRM_Core_DAO
::executeQuery($query);
45 $this->assign('groupName', $dao->title
);
46 CRM_Utils_System
::setTitle(ts('Subscribe to Mailing List - %1', [1 => $dao->title
]));
49 CRM_Core_Error
::statusBounce("The specified group is not configured for this action OR The group doesn't exist.");
52 $this->assign('single', TRUE);
55 $this->assign('single', FALSE);
56 CRM_Utils_System
::setTitle(ts('Mailing List Subscription'));
61 * Build the form object.
63 public function buildQuickForm() {
64 // add the email address
68 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Email',
73 $this->addRule('email', ts("Please enter a valid email address."), 'email');
75 if (!$this->_groupID
) {
76 // create a selector box of all public groups
77 $groupTypeCondition = CRM_Contact_BAO_Group
::groupTypeCondition('Mailing');
80 SELECT id, title, description
82 WHERE ( saved_search_id = 0
83 OR saved_search_id IS NULL )
84 AND visibility != 'User and User Admin Only'
85 AND $groupTypeCondition
87 $dao = CRM_Core_DAO
::executeQuery($query);
89 while ($dao->fetch()) {
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',
102 throw new CRM_Core_Exception(ts('There are no public mailing list groups to display.'));
104 $this->assign('rows', $rows);
105 $this->addFormRule(['CRM_Mailing_Form_Subscribe', 'formRule']);
108 // CRM-11316 Enable ReCAPTCHA for anonymous visitors
109 $session = CRM_Core_Session
::singleton();
110 $contactID = $session->get('userID');
113 CRM_Utils_ReCAPTCHA
::enableCaptchaOnForm($this);
119 'name' => ts('Subscribe'),
124 'name' => ts('Cancel'),
134 public static function formRule($fields) {
135 foreach ($fields as $name => $dontCare) {
136 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
140 return ['_qf_default' => 'Please select one or more mailing lists.'];
143 public function postProcess() {
144 $params = $this->controller
->exportValues($this->_name
);
147 if ($this->_groupID
) {
148 $groups[] = $this->_groupID
;
151 foreach ($params as $name => $dontCare) {
152 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
153 $groups[] = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
158 CRM_Mailing_Event_BAO_Subscribe
::commonSubscribe($groups, $params);