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