Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class provides the common functionality for sending sms to | |
38 | * one or a group of contact ids. | |
39 | */ | |
40 | class CRM_Contact_Form_Task_SMSCommon { | |
7da04cde | 41 | const RECIEVED_SMS_ACTIVITY_SUBJECT = "SMS Received"; |
6a488035 TO |
42 | |
43 | public $_contactDetails = array(); | |
44 | ||
45 | public $_allContactDetails = array(); | |
46 | ||
47 | public $_toContactPhone = array(); | |
48 | ||
49 | ||
86538308 | 50 | /** |
c490a46a | 51 | * @param CRM_Core_Form $form |
86538308 | 52 | */ |
00be9182 | 53 | public static function preProcessProvider(&$form) { |
6a488035 TO |
54 | $form->_single = FALSE; |
55 | $className = CRM_Utils_System::getClassName($form); | |
56 | ||
57 | if (property_exists($form, '_context') && | |
58 | $form->_context != 'search' && | |
59 | $className == 'CRM_Contact_Form_Task_SMS' | |
60 | ) { | |
61 | $form->_single = TRUE; | |
62 | } | |
63 | ||
64 | $providersCount = CRM_SMS_BAO_Provider::activeProviderCount(); | |
65 | ||
66 | if (!$providersCount) { | |
67 | CRM_Core_Error::statusBounce(ts('There are no SMS providers configured, or no SMS providers are set active')); | |
68 | } | |
69 | ||
70 | if ($className == 'CRM_Activity_Form_Task_SMS') { | |
71 | $activityCheck = 0; | |
72 | foreach ($form->_activityHolderIds as $value) { | |
73 | if (CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $value, 'subject', 'id') != self::RECIEVED_SMS_ACTIVITY_SUBJECT) { | |
74 | $activityCheck++; | |
75 | } | |
76 | } | |
77 | if ($activityCheck == count($form->_activityHolderIds)) { | |
78 | CRM_Core_Error::statusBounce(ts("The Reply SMS Could only be sent for activities with '%1' subject.", | |
79 | array(1 => self::RECIEVED_SMS_ACTIVITY_SUBJECT) | |
80 | )); | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
85 | /** | |
c490a46a | 86 | * Build the form object |
6a488035 | 87 | * |
6a488035 | 88 | * |
c490a46a | 89 | * @param CRM_Core_Form $form |
dd244018 | 90 | * |
6a488035 TO |
91 | * @return void |
92 | */ | |
00be9182 | 93 | public static function buildQuickForm(&$form) { |
6a488035 TO |
94 | |
95 | $toArray = array(); | |
96 | ||
6a488035 TO |
97 | $providers = CRM_SMS_BAO_Provider::getProviders(NULL, NULL, TRUE, 'is_default desc'); |
98 | ||
99 | $providerSelect = array(); | |
100 | foreach ($providers as $provider) { | |
101 | $providerSelect[$provider['id']] = $provider['title']; | |
102 | } | |
103 | $suppressedSms = 0; | |
104 | //here we are getting logged in user id as array but we need target contact id. CRM-5988 | |
105 | $cid = $form->get('cid'); | |
106 | ||
107 | if ($cid) { | |
108 | $form->_contactIds = array($cid); | |
109 | } | |
110 | ||
b792e485 M |
111 | $to = $form->add('text', 'to', ts('To'), array('class' => 'huge'), TRUE); |
112 | $form->add('text', 'activity_subject', ts('Name The SMS'), array('class' => 'huge'), TRUE); | |
6a488035 TO |
113 | |
114 | $toSetDefault = TRUE; | |
115 | if (property_exists($form, '_context') && $form->_context == 'standalone') { | |
116 | $toSetDefault = FALSE; | |
117 | } | |
118 | ||
119 | // when form is submitted recompute contactIds | |
120 | $allToSMS = array(); | |
121 | if ($to->getValue()) { | |
122 | $allToPhone = explode(',', $to->getValue()); | |
123 | ||
124 | $form->_contactIds = array(); | |
125 | foreach ($allToPhone as $value) { | |
126 | list($contactId, $phone) = explode('::', $value); | |
127 | if ($contactId) { | |
128 | $form->_contactIds[] = $contactId; | |
129 | $form->_toContactPhone[] = $phone; | |
130 | } | |
131 | } | |
132 | $toSetDefault = TRUE; | |
133 | } | |
134 | ||
135 | //get the group of contacts as per selected by user in case of Find Activities | |
136 | if (!empty($form->_activityHolderIds)) { | |
137 | $extendTargetContacts = 0; | |
138 | $invalidActivity = 0; | |
139 | $validActivities = 0; | |
140 | foreach ($form->_activityHolderIds as $key => $id) { | |
141 | //valid activity check | |
142 | if (CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $id, 'subject', 'id') != self::RECIEVED_SMS_ACTIVITY_SUBJECT) { | |
143 | $invalidActivity++; | |
144 | continue; | |
145 | } | |
146 | ||
e7e657f0 | 147 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
034500d4 | 148 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
6a488035 | 149 | //target contacts limit check |
034500d4 | 150 | $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $targetID)); |
6a488035 TO |
151 | |
152 | if (count($ids) > 1) { | |
153 | $extendTargetContacts++; | |
154 | continue; | |
155 | } | |
156 | $validActivities++; | |
157 | $form->_contactIds = empty($form->_contactIds) ? $ids : array_unique(array_merge($form->_contactIds, $ids)); | |
158 | } | |
159 | ||
160 | if (!$validActivities) { | |
161 | $errorMess = ""; | |
162 | if ($extendTargetContacts) { | |
163 | $errorMess = ts('One selected activity consists of more than one target contact.', array( | |
164 | 'count' => $extendTargetContacts, | |
165 | 'plural' => '%count selected activities consist of more than one target contact.')); | |
166 | } | |
167 | if ($invalidActivity) { | |
168 | $errorMess = ($errorMess ? ' ' : ''); | |
169 | $errorMess .= ts('The selected activity is invalid.', array( | |
170 | 'count' => $invalidActivity, | |
171 | 'plural' => '%count selected activities are invalid.')); | |
172 | } | |
173 | CRM_Core_Error::statusBounce(ts("%1: SMS Reply will not be sent.", array(1 => $errorMess))); | |
174 | } | |
175 | } | |
176 | ||
f53ea1ce | 177 | if (is_array($form->_contactIds) && !empty($form->_contactIds) && $toSetDefault) { |
6a488035 TO |
178 | $returnProperties = array( |
179 | 'sort_name' => 1, | |
180 | 'phone' => 1, | |
181 | 'do_not_sms' => 1, | |
182 | 'is_deceased' => 1, | |
183 | 'display_name' => 1, | |
184 | ); | |
185 | ||
186 | list($form->_contactDetails) = CRM_Utils_Token::getTokenDetails($form->_contactIds, | |
187 | $returnProperties, | |
188 | FALSE, | |
189 | FALSE | |
190 | ); | |
191 | ||
192 | // make a copy of all contact details | |
193 | $form->_allContactDetails = $form->_contactDetails; | |
194 | ||
195 | foreach ($form->_contactIds as $key => $contactId) { | |
196 | $value = $form->_contactDetails[$contactId]; | |
197 | ||
198 | //to check if the phone type is "Mobile" | |
199 | $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name'); | |
200 | ||
201 | if (CRM_Utils_System::getClassName($form) == 'CRM_Activity_Form_Task_SMS') { | |
202 | //to check for "if the contact id belongs to a specified activity type" | |
203 | $actDetails = CRM_Activity_BAO_Activity::getContactActivity($contactId); | |
204 | if (self::RECIEVED_SMS_ACTIVITY_SUBJECT != | |
205 | CRM_Utils_Array::retrieveValueRecursive($actDetails, 'subject') | |
206 | ) { | |
207 | $suppressedSms++; | |
208 | unset($form->_contactDetails[$contactId]); | |
209 | continue; | |
210 | } | |
211 | } | |
212 | ||
8cc574cf | 213 | if ((isset($value['phone_type_id']) && $value['phone_type_id'] != CRM_Utils_Array::value('Mobile', $phoneTypes)) || $value['do_not_sms'] || empty($value['phone']) || !empty($value['is_deceased'])) { |
6a488035 TO |
214 | |
215 | //if phone is not primary check if non-primary phone is "Mobile" | |
216 | if (!empty($value['phone']) | |
8cc574cf | 217 | && $value['phone_type_id'] != CRM_Utils_Array::value('Mobile', $phoneTypes) && empty($value['is_deceased'])) { |
6a488035 TO |
218 | $filter = array('do_not_sms' => 0); |
219 | $contactPhones = CRM_Core_BAO_Phone::allPhones($contactId, FALSE, 'Mobile', $filter); | |
220 | if (count($contactPhones) > 0) { | |
221 | $mobilePhone = CRM_Utils_Array::retrieveValueRecursive($contactPhones, 'phone'); | |
222 | $form->_contactDetails[$contactId]['phone_id'] = CRM_Utils_Array::retrieveValueRecursive($contactPhones, 'id'); | |
223 | $form->_contactDetails[$contactId]['phone'] = $mobilePhone; | |
224 | $form->_contactDetails[$contactId]['phone_type_id'] = CRM_Utils_Array::value('Mobile', $phoneTypes); | |
225 | } | |
226 | else { | |
227 | $suppressedSms++; | |
228 | unset($form->_contactDetails[$contactId]); | |
229 | continue; | |
230 | } | |
231 | } | |
232 | else { | |
233 | $suppressedSms++; | |
234 | unset($form->_contactDetails[$contactId]); | |
235 | continue; | |
236 | } | |
237 | } | |
238 | ||
239 | if (isset($mobilePhone)) { | |
240 | $phone = $mobilePhone; | |
241 | } | |
242 | elseif (empty($form->_toContactPhone)) { | |
243 | $phone = $value['phone']; | |
244 | } | |
245 | else { | |
246 | $phone = CRM_Utils_Array::value($key, $form->_toContactPhone); | |
247 | } | |
248 | ||
249 | if ($phone) { | |
250 | $toArray[] = array( | |
b792e485 | 251 | 'text' => '"' . $value['sort_name'] . '" (' . $phone . ')', |
6a488035 TO |
252 | 'id' => "$contactId::{$phone}", |
253 | ); | |
254 | } | |
255 | } | |
256 | ||
257 | if (empty($toArray)) { | |
258 | CRM_Core_Error::statusBounce(ts('Selected contact(s) do not have a valid Phone, or communication preferences specify DO NOT SMS, or they are deceased')); | |
259 | } | |
260 | } | |
261 | ||
262 | //activity related variables | |
263 | if (isset($invalidActivity)) { | |
264 | $form->assign('invalidActivity', $invalidActivity); | |
265 | } | |
266 | if (isset($extendTargetContacts)) { | |
267 | $form->assign('extendTargetContacts', $extendTargetContacts); | |
268 | } | |
269 | ||
270 | ||
271 | $form->assign('toContact', json_encode($toArray)); | |
272 | $form->assign('suppressedSms', $suppressedSms); | |
273 | $form->assign('totalSelectedContacts', count($form->_contactIds)); | |
274 | ||
275 | $form->add('select', 'sms_provider_id', ts('From'), $providerSelect, TRUE); | |
276 | ||
277 | CRM_Mailing_BAO_Mailing::commonCompose($form); | |
278 | ||
279 | if ($form->_single) { | |
280 | // also fix the user context stack | |
281 | if ($form->_context) { | |
282 | $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1'); | |
283 | } | |
284 | else { | |
285 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
286 | "&show=1&action=browse&cid={$form->_contactIds[0]}&selectedChild=activity" | |
287 | ); | |
288 | } | |
289 | ||
290 | $session = CRM_Core_Session::singleton(); | |
291 | $session->replaceUserContext($url); | |
292 | $form->addDefaultButtons(ts('Send SMS'), 'upload', 'cancel'); | |
293 | } | |
294 | else { | |
295 | $form->addDefaultButtons(ts('Send SMS'), 'upload'); | |
296 | } | |
297 | ||
298 | $form->addFormRule(array('CRM_Contact_Form_Task_SMSCommon', 'formRule'), $form); | |
299 | } | |
300 | ||
301 | /** | |
100fef9d | 302 | * Form rule |
6a488035 | 303 | * |
77c5b619 TO |
304 | * @param array $fields |
305 | * The input form values. | |
6a488035 | 306 | * @param array $dontCare |
77c5b619 TO |
307 | * @param array $self |
308 | * Additional values form 'this'. | |
6a488035 TO |
309 | * |
310 | * @return true if no errors, else array of errors | |
6a488035 TO |
311 | * |
312 | */ | |
00be9182 | 313 | public static function formRule($fields, $dontCare, $self) { |
6a488035 TO |
314 | $errors = array(); |
315 | ||
316 | $template = CRM_Core_Smarty::singleton(); | |
317 | ||
1e035d58 | 318 | if (empty($fields['sms_text_message'])) { |
319 | $errors['sms_text_message'] = ts('Please provide Text message.'); | |
6a488035 TO |
320 | } |
321 | else { | |
1e035d58 | 322 | if (!empty($fields['sms_text_message'])) { |
323 | $messageCheck = CRM_Utils_Array::value('sms_text_message', $fields); | |
6a488035 TO |
324 | $messageCheck = str_replace("\r\n", "\n", $messageCheck); |
325 | if ($messageCheck && (strlen($messageCheck) > CRM_SMS_Provider::MAX_SMS_CHAR)) { | |
1e035d58 | 326 | $errors['sms_text_message'] = ts("You can configure the SMS message body up to %1 characters", array(1 => CRM_SMS_Provider::MAX_SMS_CHAR)); |
6a488035 TO |
327 | } |
328 | } | |
329 | } | |
330 | ||
331 | //Added for CRM-1393 | |
a7488080 | 332 | if (!empty($fields['saveTemplate']) && empty($fields['saveTemplateName'])) { |
6a488035 TO |
333 | $errors['saveTemplateName'] = ts("Enter name to save message template"); |
334 | } | |
335 | ||
336 | return empty($errors) ? TRUE : $errors; | |
337 | } | |
338 | ||
339 | /** | |
100fef9d | 340 | * Process the form after the input has been submitted and validated |
6a488035 | 341 | * |
6a488035 | 342 | * |
c490a46a | 343 | * @param CRM_Core_Form $form |
2a6da8d7 | 344 | * |
355ba699 | 345 | * @return void |
6a488035 | 346 | */ |
00be9182 | 347 | public static function postProcess(&$form) { |
6a488035 TO |
348 | |
349 | // check and ensure that | |
350 | $thisValues = $form->controller->exportValues($form->getName()); | |
351 | ||
352 | $fromSmsProviderId = $thisValues['sms_provider_id']; | |
353 | ||
354 | // process message template | |
8cc574cf | 355 | if (!empty($thisValues['saveTemplate']) || !empty($thisValues['updateTemplate'])) { |
6a488035 | 356 | $messageTemplate = array( |
1e035d58 | 357 | 'msg_text' => $thisValues['sms_text_message'], |
6a488035 TO |
358 | 'is_active' => TRUE, |
359 | ); | |
360 | ||
a7488080 | 361 | if (!empty($thisValues['saveTemplate'])) { |
6a488035 | 362 | $messageTemplate['msg_title'] = $thisValues['saveTemplateName']; |
c6327d7d | 363 | CRM_Core_BAO_MessageTemplate::add($messageTemplate); |
6a488035 TO |
364 | } |
365 | ||
8cc574cf | 366 | if (!empty($thisValues['template']) && !empty($thisValues['updateTemplate'])) { |
6a488035 TO |
367 | $messageTemplate['id'] = $thisValues['template']; |
368 | unset($messageTemplate['msg_title']); | |
c6327d7d | 369 | CRM_Core_BAO_MessageTemplate::add($messageTemplate); |
6a488035 TO |
370 | } |
371 | } | |
372 | ||
373 | // format contact details array to handle multiple sms from same contact | |
374 | $formattedContactDetails = array(); | |
375 | $tempPhones = array(); | |
376 | ||
377 | foreach ($form->_contactIds as $key => $contactId) { | |
378 | $phone = $form->_toContactPhone[$key]; | |
379 | ||
380 | if ($phone) { | |
381 | $phoneKey = "{$contactId}::{$phone}"; | |
382 | if (!in_array($phoneKey, $tempPhones)) { | |
383 | $tempPhones[] = $phoneKey; | |
a7488080 | 384 | if (!empty($form->_contactDetails[$contactId])) { |
6a488035 TO |
385 | $formattedContactDetails[] = $form->_contactDetails[$contactId]; |
386 | } | |
387 | } | |
388 | } | |
389 | } | |
390 | ||
391 | // $smsParams carries all the arguments provided on form (or via hooks), to the provider->send() method | |
392 | // this gives flexibity to the users / implementors to add their own args via hooks specific to their sms providers | |
393 | $smsParams = $thisValues; | |
1e035d58 | 394 | unset($smsParams['sms_text_message']); |
6a488035 | 395 | $smsParams['provider_id'] = $fromSmsProviderId; |
f53ea1ce DS |
396 | $contactIds = array_keys($form->_contactDetails); |
397 | $allContactIds = array_keys($form->_allContactDetails); | |
6a488035 | 398 | |
f53ea1ce | 399 | list($sent, $activityId, $countSuccess) = CRM_Activity_BAO_Activity::sendSMS($formattedContactDetails, |
6a488035 TO |
400 | $thisValues, |
401 | $smsParams, | |
f53ea1ce | 402 | $contactIds |
6a488035 TO |
403 | ); |
404 | ||
c5a6413b | 405 | if ($countSuccess > 0) { |
f53ea1ce | 406 | CRM_Core_Session::setStatus(ts('One message was sent successfully.', array('plural' => '%count messages were sent successfully.', 'count' => $countSuccess)), ts('Message Sent', array('plural' => 'Messages Sent', 'count' => $countSuccess)), 'success'); |
6a488035 TO |
407 | } |
408 | ||
c5a6413b DS |
409 | if (is_array($sent)) { |
410 | // At least one PEAR_Error object was generated. | |
411 | // Display the error messages to the user. | |
412 | $status = '<ul>'; | |
413 | foreach ($sent as $errMsg) { | |
414 | $status .= '<li>' . $errMsg . '</li>'; | |
6a488035 | 415 | } |
c5a6413b DS |
416 | $status .= '</ul>'; |
417 | CRM_Core_Session::setStatus($status, ts('One Message Not Sent', array('count' => count($sent), 'plural' => '%count Messages Not Sent')), 'info'); | |
418 | } else { | |
419 | //Display the name and number of contacts for those sms is not sent. | |
420 | $smsNotSent = array_diff_assoc($allContactIds, $contactIds); | |
421 | ||
422 | if (!empty($smsNotSent)) { | |
423 | $not_sent = array(); | |
424 | foreach ($smsNotSent as $index => $contactId) { | |
425 | $displayName = $form->_allContactDetails[$contactId]['display_name']; | |
426 | $phone = $form->_allContactDetails[$contactId]['phone']; | |
427 | $contactViewUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$contactId"); | |
428 | $not_sent[] = "<a href='$contactViewUrl' title='$phone'>$displayName</a>"; | |
429 | } | |
430 | $status = '(' . ts('because no phone number on file or communication preferences specify DO NOT SMS or Contact is deceased'); | |
431 | if (CRM_Utils_System::getClassName($form) == 'CRM_Activity_Form_Task_SMS') { | |
432 | $status .= ' ' . ts("or the contact is not part of the activity '%1'", array(1 => self::RECIEVED_SMS_ACTIVITY_SUBJECT)); | |
433 | } | |
434 | $status .= ')<ul><li>' . implode('</li><li>', $not_sent) . '</li></ul>'; | |
435 | CRM_Core_Session::setStatus($status, ts('One Message Not Sent', array('count' => count($smsNotSent), 'plural' => '%count Messages Not Sent')), 'info'); | |
6a488035 | 436 | } |
6a488035 | 437 | } |
6a488035 TO |
438 | } |
439 | } |