Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
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 | ||
18 | /** | |
39404680 | 19 | * Choose include / exclude groups and mass sms. |
6a488035 TO |
20 | */ |
21 | class CRM_SMS_Form_Group extends CRM_Contact_Form_Task { | |
22 | ||
23 | /** | |
fe482240 | 24 | * Set variables up before form is built. |
6a488035 TO |
25 | */ |
26 | public function preProcess() { | |
27 | if (!CRM_SMS_BAO_Provider::activeProviderCount()) { | |
4a0e3fe7 | 28 | CRM_Core_Error::statusBounce(ts('The <a href="%1">SMS Provider</a> has not been configured or is not active.', [1 => CRM_Utils_System::url('civicrm/admin/sms/provider', 'reset=1')])); |
6a488035 TO |
29 | } |
30 | ||
31 | $session = CRM_Core_Session::singleton(); | |
32 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/mailing/browse', 'reset=1&sms=1')); | |
fed95fdf | 33 | $this->assign('isAdmin', CRM_Core_Permission::check('administer CiviCRM')); |
6a488035 TO |
34 | } |
35 | ||
36 | /** | |
c490a46a | 37 | * Set default values for the form. |
39404680 | 38 | * The default values are retrieved from the database. |
6a488035 | 39 | */ |
00be9182 | 40 | public function setDefaultValues() { |
6a488035 TO |
41 | $mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE, NULL); |
42 | $continue = CRM_Utils_Request::retrieve('continue', 'String', $this, FALSE, NULL); | |
43 | ||
be2fb01f | 44 | $defaults = []; |
6a488035 TO |
45 | |
46 | if ($mailingID) { | |
47 | $mailing = new CRM_Mailing_DAO_Mailing(); | |
48 | $mailing->id = $mailingID; | |
49 | $mailing->addSelect('name'); | |
50 | $mailing->find(TRUE); | |
51 | ||
52 | $defaults['name'] = $mailing->name; | |
53 | if (!$continue) { | |
be2fb01f | 54 | $defaults['name'] = ts('Copy of %1', [1 => $mailing->name]); |
6a488035 TO |
55 | } |
56 | else { | |
57 | // CRM-7590, reuse same mailing ID if we are continuing | |
58 | $this->set('mailing_id', $mailingID); | |
59 | } | |
60 | ||
04124b30 | 61 | $dao = new CRM_Mailing_DAO_MailingGroup(); |
6a488035 | 62 | |
be2fb01f | 63 | $mailingGroups = []; |
6a488035 TO |
64 | $dao->mailing_id = $mailingID; |
65 | $dao->find(); | |
66 | while ($dao->fetch()) { | |
67 | $mailingGroups[$dao->entity_table][$dao->group_type][] = $dao->entity_id; | |
68 | } | |
69 | ||
70 | $defaults['includeGroups'] = $mailingGroups['civicrm_group']['Include']; | |
9c1bc317 | 71 | $defaults['excludeGroups'] = $mailingGroups['civicrm_group']['Exclude'] ?? NULL; |
6a488035 TO |
72 | |
73 | $defaults['includeMailings'] = CRM_Utils_Array::value('Include', CRM_Utils_Array::value('civicrm_mailing', $mailingGroups)); | |
74 | $defaults['excludeMailings'] = CRM_Utils_Array::value('Exclude', CRM_Utils_Array::value('civicrm_mailing', $mailingGroups)); | |
75 | } | |
76 | ||
77 | return $defaults; | |
78 | } | |
79 | ||
80 | /** | |
fe482240 | 81 | * Build the form object. |
6a488035 TO |
82 | */ |
83 | public function buildQuickForm() { | |
84 | ||
39404680 | 85 | // Get the context. |
6a488035 TO |
86 | $context = $this->get('context'); |
87 | ||
88 | $this->assign('context', $context); | |
89 | ||
90 | $this->add('text', 'name', ts('Name Your SMS'), | |
91 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Mailing', 'name'), | |
92 | TRUE | |
93 | ); | |
94 | ||
6eee0915 | 95 | $this->add('select', 'sms_provider_id', |
96 | ts('Select SMS Provider'), | |
7efcdebd | 97 | CRM_Utils_Array::collect('title', CRM_SMS_BAO_Provider::getProviders(NULL, ['is_active' => 1])), |
6eee0915 | 98 | TRUE |
99 | ); | |
100 | ||
39404680 | 101 | // Get the mailing groups. |
ca6b3004 | 102 | $groups = CRM_Core_PseudoConstant::nestedGroup(TRUE, 'Mailing'); |
6a488035 | 103 | |
39404680 | 104 | // Get the sms mailing list. |
6a488035 TO |
105 | $mailings = CRM_Mailing_PseudoConstant::completed('sms'); |
106 | if (!$mailings) { | |
be2fb01f | 107 | $mailings = []; |
6a488035 TO |
108 | } |
109 | ||
110 | // run the groups through a hook so users can trim it if needed | |
111 | CRM_Utils_Hook::mailingGroups($this, $groups, $mailings); | |
112 | ||
be2fb01f | 113 | $select2style = [ |
b14215fa CW |
114 | 'multiple' => TRUE, |
115 | 'style' => 'width: 100%; max-width: 60em;', | |
116 | 'class' => 'crm-select2', | |
117 | 'placeholder' => ts('- select -'), | |
be2fb01f | 118 | ]; |
6a488035 | 119 | |
b14215fa CW |
120 | $this->add('select', 'includeGroups', |
121 | ts('Include Group(s)'), | |
6a488035 | 122 | $groups, |
b14215fa CW |
123 | TRUE, |
124 | $select2style | |
6a488035 TO |
125 | ); |
126 | ||
b14215fa CW |
127 | $this->add('select', 'excludeGroups', |
128 | ts('Exclude Group(s)'), | |
129 | $groups, | |
130 | FALSE, | |
131 | $select2style | |
132 | ); | |
6a488035 | 133 | |
b14215fa CW |
134 | $this->add('select', 'includeMailings', |
135 | ts('INCLUDE Recipients of These Message(s)'), | |
6a488035 | 136 | $mailings, |
b14215fa CW |
137 | FALSE, |
138 | $select2style | |
6a488035 | 139 | ); |
b14215fa CW |
140 | $this->add('select', 'excludeMailings', |
141 | ts('EXCLUDE Recipients of These Message(s)'), | |
6a488035 | 142 | $mailings, |
b14215fa CW |
143 | FALSE, |
144 | $select2style | |
6a488035 TO |
145 | ); |
146 | ||
be2fb01f | 147 | $this->addFormRule(['CRM_SMS_Form_Group', 'formRule']); |
6a488035 | 148 | |
be2fb01f CW |
149 | $buttons = [ |
150 | [ | |
6ea503d4 | 151 | 'type' => 'next', |
f212d37d | 152 | 'name' => ts('Next'), |
6a488035 TO |
153 | 'spacing' => ' ', |
154 | 'isDefault' => TRUE, | |
be2fb01f CW |
155 | ], |
156 | [ | |
6a488035 TO |
157 | 'type' => 'cancel', |
158 | 'name' => ts('Cancel'), | |
be2fb01f CW |
159 | ], |
160 | ]; | |
6a488035 TO |
161 | |
162 | $this->addButtons($buttons); | |
163 | ||
164 | $this->assign('groupCount', count($groups)); | |
165 | $this->assign('mailingCount', count($mailings)); | |
166 | } | |
167 | ||
168 | public function postProcess() { | |
169 | $values = $this->controller->exportValues($this->_name); | |
170 | ||
be2fb01f | 171 | $groups = []; |
6a488035 | 172 | |
be2fb01f | 173 | foreach ([ |
c5c263ca AH |
174 | 'name', |
175 | 'group_id', | |
176 | 'is_sms', | |
6eee0915 | 177 | 'sms_provider_id', |
be2fb01f | 178 | ] as $n) { |
a7488080 | 179 | if (!empty($values[$n])) { |
6a488035 | 180 | $params[$n] = $values[$n]; |
6eee0915 | 181 | if ($n == 'sms_provider_id') { |
182 | // Get the from Name. | |
183 | $params['from_name'] = CRM_Core_DAO::getFieldValue('CRM_SMS_DAO_Provider', $params['sms_provider_id'], 'username'); | |
184 | } | |
6a488035 TO |
185 | } |
186 | } | |
187 | ||
188 | $qf_Group_submit = $this->controller->exportValue($this->_name, '_qf_Group_submit'); | |
189 | $this->set('name', $params['name']); | |
190 | ||
353ffa53 TO |
191 | $inGroups = $values['includeGroups']; |
192 | $outGroups = $values['excludeGroups']; | |
193 | $inMailings = $values['includeMailings']; | |
6a488035 TO |
194 | $outMailings = $values['excludeMailings']; |
195 | ||
196 | if (is_array($inGroups)) { | |
197 | foreach ($inGroups as $key => $id) { | |
198 | if ($id) { | |
199 | $groups['include'][] = $id; | |
200 | } | |
201 | } | |
202 | } | |
203 | if (is_array($outGroups)) { | |
204 | foreach ($outGroups as $key => $id) { | |
205 | if ($id) { | |
206 | $groups['exclude'][] = $id; | |
207 | } | |
208 | } | |
209 | } | |
210 | ||
be2fb01f | 211 | $mailings = []; |
6a488035 TO |
212 | if (is_array($inMailings)) { |
213 | foreach ($inMailings as $key => $id) { | |
214 | if ($id) { | |
215 | $mailings['include'][] = $id; | |
216 | } | |
217 | } | |
218 | } | |
219 | if (is_array($outMailings)) { | |
220 | foreach ($outMailings as $key => $id) { | |
221 | if ($id) { | |
222 | $mailings['exclude'][] = $id; | |
223 | } | |
224 | } | |
225 | } | |
226 | ||
353ffa53 TO |
227 | $session = CRM_Core_Session::singleton(); |
228 | $params['groups'] = $groups; | |
6a488035 | 229 | $params['mailings'] = $mailings; |
be2fb01f | 230 | $ids = []; |
6a488035 TO |
231 | if ($this->get('mailing_id')) { |
232 | ||
233 | // don't create a new mass sms if already exists | |
234 | $ids['mailing_id'] = $this->get('mailing_id'); | |
235 | ||
236 | $groupTableName = CRM_Contact_BAO_Group::getTableName(); | |
237 | $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName(); | |
238 | ||
239 | // delete previous includes/excludes, if mailing already existed | |
be2fb01f | 240 | foreach ([ |
c5c263ca AH |
241 | 'groups', |
242 | 'mailings', | |
be2fb01f | 243 | ] as $entity) { |
353ffa53 TO |
244 | $mg = new CRM_Mailing_DAO_MailingGroup(); |
245 | $mg->mailing_id = $ids['mailing_id']; | |
6a488035 TO |
246 | $mg->entity_table = ($entity == 'groups') ? $groupTableName : $mailingTableName; |
247 | $mg->find(); | |
248 | while ($mg->fetch()) { | |
249 | $mg->delete(); | |
250 | } | |
251 | } | |
252 | } | |
253 | else { | |
254 | // new mailing, so lets set the created_id | |
255 | $session = CRM_Core_Session::singleton(); | |
256 | $params['created_id'] = $session->get('userID'); | |
257 | $params['created_date'] = date('YmdHis'); | |
258 | } | |
259 | ||
260 | $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids); | |
261 | ||
262 | $this->set('mailing_id', $mailing->id); | |
263 | ||
264 | // also compute the recipients and store them in the mailing recipients table | |
6dd717a6 | 265 | CRM_Mailing_BAO_Mailing::getRecipients($mailing->id); |
6a488035 TO |
266 | |
267 | $count = CRM_Mailing_BAO_Recipients::mailingSize($mailing->id); | |
268 | $this->set('count', $count); | |
269 | $this->assign('count', $count); | |
270 | $this->set('groups', $groups); | |
271 | $this->set('mailings', $mailings); | |
272 | ||
273 | if ($qf_Group_submit) { | |
274 | $status = ts("Your Mass SMS has been saved."); | |
275 | CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); | |
276 | $url = CRM_Utils_System::url('civicrm/mailing', 'reset=1&sms=1'); | |
277 | return $this->controller->setDestination($url); | |
278 | } | |
279 | } | |
280 | ||
281 | /** | |
fe482240 | 282 | * Display Name of the form. |
6a488035 | 283 | * |
6a488035 TO |
284 | * |
285 | * @return string | |
286 | */ | |
287 | public function getTitle() { | |
288 | return ts('Select Recipients'); | |
289 | } | |
290 | ||
291 | /** | |
fe482240 | 292 | * Global validation rules for the form. |
6a488035 | 293 | * |
901b501a TO |
294 | * @param array $fields |
295 | * Posted values of the form. | |
6a488035 | 296 | * |
a6c01b45 CW |
297 | * @return array |
298 | * list of errors to be posted back to the form | |
6a488035 | 299 | */ |
00be9182 | 300 | public static function formRule($fields) { |
be2fb01f | 301 | $errors = []; |
6a488035 TO |
302 | if (isset($fields['includeGroups']) && |
303 | is_array($fields['includeGroups']) && | |
304 | isset($fields['excludeGroups']) && | |
305 | is_array($fields['excludeGroups']) | |
306 | ) { | |
be2fb01f | 307 | $checkGroups = []; |
6a488035 TO |
308 | $checkGroups = array_intersect($fields['includeGroups'], $fields['excludeGroups']); |
309 | if (!empty($checkGroups)) { | |
310 | $errors['excludeGroups'] = ts('Cannot have same groups in Include Group(s) and Exclude Group(s).'); | |
311 | } | |
312 | } | |
313 | ||
314 | if (isset($fields['includeMailings']) && | |
315 | is_array($fields['includeMailings']) && | |
316 | isset($fields['excludeMailings']) && | |
317 | is_array($fields['excludeMailings']) | |
318 | ) { | |
be2fb01f | 319 | $checkMailings = []; |
6a488035 TO |
320 | $checkMailings = array_intersect($fields['includeMailings'], $fields['excludeMailings']); |
321 | if (!empty($checkMailings)) { | |
322 | $errors['excludeMailings'] = ts('Cannot have same sms in Include mailing(s) and Exclude mailing(s).'); | |
323 | } | |
324 | } | |
325 | ||
326 | return empty($errors) ? TRUE : $errors; | |
327 | } | |
96025800 | 328 | |
6a488035 | 329 | } |