Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Mailing_Form_Subscribe extends CRM_Core_Form { | |
18 | protected $_groupID = NULL; | |
19 | ||
00be9182 | 20 | public function preProcess() { |
6a488035 TO |
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 | ||
6a488035 TO |
32 | if ($this->_groupID) { |
33 | $groupTypeCondition = CRM_Contact_BAO_Group::groupTypeCondition('Mailing'); | |
34 | ||
35 | // make sure requested qroup is accessible and exists | |
36 | $query = " | |
be44418d | 37 | SELECT title, frontend_title, description, frontend_description |
6a488035 | 38 | FROM civicrm_group |
03e04002 | 39 | WHERE id={$this->_groupID} |
6a488035 TO |
40 | AND visibility != 'User and User Admin Only' |
41 | AND $groupTypeCondition"; | |
42 | ||
33621c4f | 43 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 | 44 | if ($dao->fetch()) { |
be44418d | 45 | $this->assign('groupName', !empty($dao->frontend_title) ? $dao->frontend_title : $dao->title); |
94fd9d17 | 46 | $this->setTitle(ts('Subscribe to Mailing List - %1', [1 => !empty($dao->frontend_title) ? $dao->frontend_title : $dao->title])); |
6a488035 TO |
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); | |
94fd9d17 | 56 | $this->setTitle(ts('Mailing List Subscription')); |
6a488035 TO |
57 | } |
58 | } | |
59 | ||
60 | /** | |
fe482240 | 61 | * Build the form object. |
6a488035 TO |
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 | ); | |
f289559e | 73 | $this->addRule('email', ts("Please enter a valid email address."), 'email'); |
6a488035 TO |
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 = " | |
be44418d | 80 | SELECT id, title, frontend_title, description, frontend_description |
6a488035 TO |
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"; | |
33621c4f | 87 | $dao = CRM_Core_DAO::executeQuery($query); |
be2fb01f | 88 | $rows = []; |
6a488035 | 89 | while ($dao->fetch()) { |
be2fb01f | 90 | $row = []; |
353ffa53 | 91 | $row['id'] = $dao->id; |
be44418d SL |
92 | $row['title'] = $dao->frontend_title ?? $dao->title; |
93 | $row['description'] = $dao->frontend_description ?? $dao->description; | |
353ffa53 | 94 | $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $row['id']; |
6a488035 TO |
95 | $this->addElement('checkbox', |
96 | $row['checkbox'], | |
97 | NULL, NULL | |
98 | ); | |
99 | $rows[] = $row; | |
100 | } | |
101 | if (empty($rows)) { | |
2a7b8221 | 102 | throw new CRM_Core_Exception(ts('There are no public mailing list groups to display.')); |
6a488035 TO |
103 | } |
104 | $this->assign('rows', $rows); | |
be2fb01f | 105 | $this->addFormRule(['CRM_Mailing_Form_Subscribe', 'formRule']); |
6a488035 TO |
106 | } |
107 | ||
be2fb01f | 108 | $this->addButtons([ |
7e8c8317 SL |
109 | [ |
110 | 'type' => 'next', | |
111 | 'name' => ts('Subscribe'), | |
112 | 'isDefault' => TRUE, | |
113 | ], | |
7e8c8317 | 114 | ]); |
6a488035 TO |
115 | } |
116 | ||
e0ef6999 EM |
117 | /** |
118 | * @param $fields | |
119 | * | |
120 | * @return array|bool | |
121 | */ | |
00be9182 | 122 | public static function formRule($fields) { |
6a488035 TO |
123 | foreach ($fields as $name => $dontCare) { |
124 | if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { | |
125 | return TRUE; | |
126 | } | |
127 | } | |
be2fb01f | 128 | return ['_qf_default' => 'Please select one or more mailing lists.']; |
6a488035 TO |
129 | } |
130 | ||
6a488035 TO |
131 | public function postProcess() { |
132 | $params = $this->controller->exportValues($this->_name); | |
133 | ||
be2fb01f | 134 | $groups = []; |
6a488035 TO |
135 | if ($this->_groupID) { |
136 | $groups[] = $this->_groupID; | |
137 | } | |
138 | else { | |
139 | foreach ($params as $name => $dontCare) { | |
140 | if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { | |
141 | $groups[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN); | |
142 | } | |
143 | } | |
144 | } | |
145 | ||
146 | CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($groups, $params); | |
147 | } | |
96025800 | 148 | |
6a488035 | 149 | } |