Merge pull request #15785 from eileenmcnaughton/contribution_url_params
[civicrm-core.git] / CRM / SMS / Form / Group.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Choose include / exclude groups and mass sms.
20 */
21 class CRM_SMS_Form_Group extends CRM_Contact_Form_Task {
22
23 /**
24 * Set variables up before form is built.
25 */
26 public function preProcess() {
27 if (!CRM_SMS_BAO_Provider::activeProviderCount()) {
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')]));
29 }
30
31 $session = CRM_Core_Session::singleton();
32 $session->replaceUserContext(CRM_Utils_System::url('civicrm/mailing/browse', 'reset=1&sms=1'));
33
34 if (CRM_Core_Permission::check('administer CiviCRM')) {
35 $this->assign('isAdmin', 1);
36 }
37 }
38
39 /**
40 * Set default values for the form.
41 * The default values are retrieved from the database.
42 */
43 public function setDefaultValues() {
44 $mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE, NULL);
45 $continue = CRM_Utils_Request::retrieve('continue', 'String', $this, FALSE, NULL);
46
47 $defaults = [];
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) {
57 $defaults['name'] = ts('Copy of %1', [1 => $mailing->name]);
58 }
59 else {
60 // CRM-7590, reuse same mailing ID if we are continuing
61 $this->set('mailing_id', $mailingID);
62 }
63
64 $dao = new CRM_Mailing_DAO_MailingGroup();
65
66 $mailingGroups = [];
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 /**
84 * Build the form object.
85 */
86 public function buildQuickForm() {
87
88 // Get the context.
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
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
104 // Get the mailing groups.
105 $groups = CRM_Core_PseudoConstant::nestedGroup('Mailing');
106
107 // Get the sms mailing list.
108 $mailings = CRM_Mailing_PseudoConstant::completed('sms');
109 if (!$mailings) {
110 $mailings = [];
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
116 $select2style = [
117 'multiple' => TRUE,
118 'style' => 'width: 100%; max-width: 60em;',
119 'class' => 'crm-select2',
120 'placeholder' => ts('- select -'),
121 ];
122
123 $this->add('select', 'includeGroups',
124 ts('Include Group(s)'),
125 $groups,
126 TRUE,
127 $select2style
128 );
129
130 $this->add('select', 'excludeGroups',
131 ts('Exclude Group(s)'),
132 $groups,
133 FALSE,
134 $select2style
135 );
136
137 $this->add('select', 'includeMailings',
138 ts('INCLUDE Recipients of These Message(s)'),
139 $mailings,
140 FALSE,
141 $select2style
142 );
143 $this->add('select', 'excludeMailings',
144 ts('EXCLUDE Recipients of These Message(s)'),
145 $mailings,
146 FALSE,
147 $select2style
148 );
149
150 $this->addFormRule(['CRM_SMS_Form_Group', 'formRule']);
151
152 $buttons = [
153 [
154 'type' => 'next',
155 'name' => ts('Next'),
156 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
157 'isDefault' => TRUE,
158 ],
159 [
160 'type' => 'cancel',
161 'name' => ts('Cancel'),
162 ],
163 ];
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
174 $groups = [];
175
176 foreach ([
177 'name',
178 'group_id',
179 'is_sms',
180 'sms_provider_id',
181 ] as $n) {
182 if (!empty($values[$n])) {
183 $params[$n] = $values[$n];
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 }
188 }
189 }
190
191 $qf_Group_submit = $this->controller->exportValue($this->_name, '_qf_Group_submit');
192 $this->set('name', $params['name']);
193
194 $inGroups = $values['includeGroups'];
195 $outGroups = $values['excludeGroups'];
196 $inMailings = $values['includeMailings'];
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
214 $mailings = [];
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
230 $session = CRM_Core_Session::singleton();
231 $params['groups'] = $groups;
232 $params['mailings'] = $mailings;
233 $ids = [];
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
243 foreach ([
244 'groups',
245 'mailings',
246 ] as $entity) {
247 $mg = new CRM_Mailing_DAO_MailingGroup();
248 $mg->mailing_id = $ids['mailing_id'];
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
268 CRM_Mailing_BAO_Mailing::getRecipients($mailing->id);
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 /**
285 * Display Name of the form.
286 *
287 *
288 * @return string
289 */
290 public function getTitle() {
291 return ts('Select Recipients');
292 }
293
294 /**
295 * Global validation rules for the form.
296 *
297 * @param array $fields
298 * Posted values of the form.
299 *
300 * @return array
301 * list of errors to be posted back to the form
302 */
303 public static function formRule($fields) {
304 $errors = [];
305 if (isset($fields['includeGroups']) &&
306 is_array($fields['includeGroups']) &&
307 isset($fields['excludeGroups']) &&
308 is_array($fields['excludeGroups'])
309 ) {
310 $checkGroups = [];
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 ) {
322 $checkMailings = [];
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 }
331
332 }