3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the common functionality for sending sms to one or a group of contact ids.
21 class CRM_Contact_Form_Task_SMSCommon
{
22 const RECIEVED_SMS_ACTIVITY_SUBJECT
= "SMS Received";
24 public $_contactDetails = [];
26 public $_allContactDetails = [];
28 public $_toContactPhone = [];
31 * Pre process the provider.
33 * @param CRM_Core_Form $form
35 public static function preProcessProvider(&$form) {
36 $form->_single
= FALSE;
37 $className = CRM_Utils_System
::getClassName($form);
39 if (property_exists($form, '_context') &&
40 $form->_context
!= 'search' &&
41 $className == 'CRM_Contact_Form_Task_SMS'
43 $form->_single
= TRUE;
46 $providersCount = CRM_SMS_BAO_Provider
::activeProviderCount();
48 if (!$providersCount) {
49 CRM_Core_Error
::statusBounce(ts('There are no SMS providers configured, or no SMS providers are set active'));
52 if ($className == 'CRM_Activity_Form_Task_SMS') {
54 foreach ($form->_activityHolderIds
as $value) {
55 if (CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity', $value, 'subject', 'id') != self
::RECIEVED_SMS_ACTIVITY_SUBJECT
) {
59 if ($activityCheck == count($form->_activityHolderIds
)) {
60 CRM_Core_Error
::statusBounce(ts("The Reply SMS Could only be sent for activities with '%1' subject.",
61 [1 => self
::RECIEVED_SMS_ACTIVITY_SUBJECT
]
68 * Build the form object.
70 * @param CRM_Core_Form $form
72 public static function buildQuickForm(&$form) {
76 $providers = CRM_SMS_BAO_Provider
::getProviders(NULL, NULL, TRUE, 'is_default desc');
79 foreach ($providers as $provider) {
80 $providerSelect[$provider['id']] = $provider['title'];
83 //here we are getting logged in user id as array but we need target contact id. CRM-5988
84 $cid = $form->get('cid');
87 $form->_contactIds
= [$cid];
90 $to = $form->add('text', 'to', ts('To'), ['class' => 'huge'], TRUE);
91 $form->add('text', 'activity_subject', ts('Name The SMS'), ['class' => 'huge'], TRUE);
94 if (property_exists($form, '_context') && $form->_context
== 'standalone') {
95 $toSetDefault = FALSE;
98 // when form is submitted recompute contactIds
100 if ($to->getValue()) {
101 $allToPhone = explode(',', $to->getValue());
103 $form->_contactIds
= [];
104 foreach ($allToPhone as $value) {
105 list($contactId, $phone) = explode('::', $value);
107 $form->_contactIds
[] = $contactId;
108 $form->_toContactPhone
[] = $phone;
111 $toSetDefault = TRUE;
114 //get the group of contacts as per selected by user in case of Find Activities
115 if (!empty($form->_activityHolderIds
)) {
116 $extendTargetContacts = 0;
117 $invalidActivity = 0;
118 $validActivities = 0;
119 foreach ($form->_activityHolderIds
as $key => $id) {
120 //valid activity check
121 if (CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity', $id, 'subject', 'id') != self
::RECIEVED_SMS_ACTIVITY_SUBJECT
) {
126 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
127 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
128 //target contacts limit check
129 $ids = array_keys(CRM_Activity_BAO_ActivityContact
::getNames($id, $targetID));
131 if (count($ids) > 1) {
132 $extendTargetContacts++
;
136 $form->_contactIds
= empty($form->_contactIds
) ?
$ids : array_unique(array_merge($form->_contactIds
, $ids));
139 if (!$validActivities) {
141 if ($extendTargetContacts) {
142 $errorMess = ts('One selected activity consists of more than one target contact.', [
143 'count' => $extendTargetContacts,
144 'plural' => '%count selected activities consist of more than one target contact.',
147 if ($invalidActivity) {
148 $errorMess = ($errorMess ?
' ' : '');
149 $errorMess .= ts('The selected activity is invalid.', [
150 'count' => $invalidActivity,
151 'plural' => '%count selected activities are invalid.',
154 CRM_Core_Error
::statusBounce(ts("%1: SMS Reply will not be sent.", [1 => $errorMess]));
158 if (is_array($form->_contactIds
) && !empty($form->_contactIds
) && $toSetDefault) {
159 $returnProperties = [
167 list($form->_contactDetails
) = CRM_Utils_Token
::getTokenDetails($form->_contactIds
,
173 // make a copy of all contact details
174 $form->_allContactDetails
= $form->_contactDetails
;
176 foreach ($form->_contactIds
as $key => $contactId) {
177 $value = $form->_contactDetails
[$contactId];
179 //to check if the phone type is "Mobile"
180 $phoneTypes = CRM_Core_OptionGroup
::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
182 if (CRM_Utils_System
::getClassName($form) == 'CRM_Activity_Form_Task_SMS') {
183 //to check for "if the contact id belongs to a specified activity type"
184 // @todo use the api instead - function is deprecated.
185 $actDetails = CRM_Activity_BAO_Activity
::getContactActivity($contactId);
186 if (self
::RECIEVED_SMS_ACTIVITY_SUBJECT
!=
187 CRM_Utils_Array
::retrieveValueRecursive($actDetails, 'subject')
190 unset($form->_contactDetails
[$contactId]);
195 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'])) {
197 //if phone is not primary check if non-primary phone is "Mobile"
198 if (!empty($value['phone'])
199 && $value['phone_type_id'] != CRM_Utils_Array
::value('Mobile', $phoneTypes) && empty($value['is_deceased'])
201 $filter = ['do_not_sms' => 0];
202 $contactPhones = CRM_Core_BAO_Phone
::allPhones($contactId, FALSE, 'Mobile', $filter);
203 if (count($contactPhones) > 0) {
204 $mobilePhone = CRM_Utils_Array
::retrieveValueRecursive($contactPhones, 'phone');
205 $form->_contactDetails
[$contactId]['phone_id'] = CRM_Utils_Array
::retrieveValueRecursive($contactPhones, 'id');
206 $form->_contactDetails
[$contactId]['phone'] = $mobilePhone;
207 $form->_contactDetails
[$contactId]['phone_type_id'] = $phoneTypes['Mobile'] ??
NULL;
211 unset($form->_contactDetails
[$contactId]);
217 unset($form->_contactDetails
[$contactId]);
222 if (isset($mobilePhone)) {
223 $phone = $mobilePhone;
225 elseif (empty($form->_toContactPhone
)) {
226 $phone = $value['phone'];
229 $phone = $form->_toContactPhone
[$key] ??
NULL;
234 'text' => '"' . $value['sort_name'] . '" (' . $phone . ')',
235 'id' => "$contactId::{$phone}",
240 if (empty($toArray)) {
241 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'));
245 //activity related variables
246 if (isset($invalidActivity)) {
247 $form->assign('invalidActivity', $invalidActivity);
249 if (isset($extendTargetContacts)) {
250 $form->assign('extendTargetContacts', $extendTargetContacts);
253 $form->assign('toContact', json_encode($toArray));
254 $form->assign('suppressedSms', $suppressedSms);
255 $form->assign('totalSelectedContacts', count($form->_contactIds
));
257 $form->add('select', 'sms_provider_id', ts('From'), $providerSelect, TRUE);
259 CRM_Mailing_BAO_Mailing
::commonCompose($form);
261 if ($form->_single
) {
262 // also fix the user context stack
263 if ($form->_context
) {
264 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
267 $url = CRM_Utils_System
::url('civicrm/contact/view',
268 "&show=1&action=browse&cid={$form->_contactIds[0]}&selectedChild=activity"
272 $session = CRM_Core_Session
::singleton();
273 $session->replaceUserContext($url);
274 $form->addDefaultButtons(ts('Send SMS'), 'upload', 'cancel');
277 $form->addDefaultButtons(ts('Send SMS'), 'upload');
280 $form->addFormRule(['CRM_Contact_Form_Task_SMSCommon', 'formRule'], $form);
286 * @param array $fields
287 * The input form values.
288 * @param array $dontCare
290 * Additional values form 'this'.
293 * true if no errors, else array of errors
295 public static function formRule($fields, $dontCare, $self) {
298 $template = CRM_Core_Smarty
::singleton();
300 if (empty($fields['sms_text_message'])) {
301 $errors['sms_text_message'] = ts('Please provide Text message.');
304 if (!empty($fields['sms_text_message'])) {
305 $messageCheck = $fields['sms_text_message'] ??
NULL;
306 $messageCheck = str_replace("\r\n", "\n", $messageCheck);
307 if ($messageCheck && (strlen($messageCheck) > CRM_SMS_Provider
::MAX_SMS_CHAR
)) {
308 $errors['sms_text_message'] = ts("You can configure the SMS message body up to %1 characters", [1 => CRM_SMS_Provider
::MAX_SMS_CHAR
]);
314 if (!empty($fields['SMSsaveTemplate']) && empty($fields['SMSsaveTemplateName'])) {
315 $errors['SMSsaveTemplateName'] = ts("Enter name to save message template");
318 return empty($errors) ?
TRUE : $errors;
322 * Process the form after the input has been submitted and validated.
324 * @param CRM_Core_Form $form
326 public static function postProcess(&$form) {
328 // check and ensure that
329 $thisValues = $form->controller
->exportValues($form->getName());
331 $fromSmsProviderId = $thisValues['sms_provider_id'];
333 // process message template
334 if (!empty($thisValues['SMSsaveTemplate']) ||
!empty($thisValues['SMSupdateTemplate'])) {
336 'msg_text' => $thisValues['sms_text_message'],
341 if (!empty($thisValues['SMSsaveTemplate'])) {
342 $messageTemplate['msg_title'] = $thisValues['SMSsaveTemplateName'];
343 CRM_Core_BAO_MessageTemplate
::add($messageTemplate);
346 if (!empty($thisValues['SMStemplate']) && !empty($thisValues['SMSupdateTemplate'])) {
347 $messageTemplate['id'] = $thisValues['SMStemplate'];
348 unset($messageTemplate['msg_title']);
349 CRM_Core_BAO_MessageTemplate
::add($messageTemplate);
353 // format contact details array to handle multiple sms from same contact
354 $formattedContactDetails = [];
357 foreach ($form->_contactIds
as $key => $contactId) {
358 $phone = $form->_toContactPhone
[$key];
361 $phoneKey = "{$contactId}::{$phone}";
362 if (!in_array($phoneKey, $tempPhones)) {
363 $tempPhones[] = $phoneKey;
364 if (!empty($form->_contactDetails
[$contactId])) {
365 $formattedContactDetails[] = $form->_contactDetails
[$contactId];
371 // $smsParams carries all the arguments provided on form (or via hooks), to the provider->send() method
372 // this gives flexibity to the users / implementors to add their own args via hooks specific to their sms providers
373 $smsParams = $thisValues;
374 unset($smsParams['sms_text_message']);
375 $smsParams['provider_id'] = $fromSmsProviderId;
376 $contactIds = array_keys($form->_contactDetails
);
377 $allContactIds = array_keys($form->_allContactDetails
);
379 list($sent, $activityId, $countSuccess) = CRM_Activity_BAO_Activity
::sendSMS($formattedContactDetails,
385 if ($countSuccess > 0) {
386 CRM_Core_Session
::setStatus(ts('One message was sent successfully.', [
387 'plural' => '%count messages were sent successfully.',
388 'count' => $countSuccess,
389 ]), ts('Message Sent', ['plural' => 'Messages Sent', 'count' => $countSuccess]), 'success');
392 if (is_array($sent)) {
393 // At least one PEAR_Error object was generated.
394 // Display the error messages to the user.
396 foreach ($sent as $errMsg) {
397 $status .= '<li>' . $errMsg . '</li>';
400 CRM_Core_Session
::setStatus($status, ts('One Message Not Sent', [
401 'count' => count($sent),
402 'plural' => '%count Messages Not Sent',
406 //Display the name and number of contacts for those sms is not sent.
407 $smsNotSent = array_diff_assoc($allContactIds, $contactIds);
409 if (!empty($smsNotSent)) {
411 foreach ($smsNotSent as $index => $contactId) {
412 $displayName = $form->_allContactDetails
[$contactId]['display_name'];
413 $phone = $form->_allContactDetails
[$contactId]['phone'];
414 $contactViewUrl = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid=$contactId");
415 $not_sent[] = "<a href='$contactViewUrl' title='$phone'>$displayName</a>";
417 $status = '(' . ts('because no phone number on file or communication preferences specify DO NOT SMS or Contact is deceased');
418 if (CRM_Utils_System
::getClassName($form) == 'CRM_Activity_Form_Task_SMS') {
419 $status .= ' ' . ts("or the contact is not part of the activity '%1'", [1 => self
::RECIEVED_SMS_ACTIVITY_SUBJECT
]);
421 $status .= ')<ul><li>' . implode('</li><li>', $not_sent) . '</li></ul>';
422 CRM_Core_Session
::setStatus($status, ts('One Message Not Sent', [
423 'count' => count($smsNotSent),
424 'plural' => '%count Messages Not Sent',