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