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