}
$success = 0;
- $escapeSmarty = $sent = FALSE;
+ $escapeSmarty = FALSE;
+ $errMsgs = array();
foreach ($contactDetails as $values) {
$contactId = $values['contact_id'];
$smsParams['To'] = '';
}
- if (self::sendSMSMessage(
- $contactId,
- $tokenText,
- $tokenHtml,
- $smsParams,
- $activityID
- )) {
- // even a single successful delivery should set this falg to true
- $sent = TRUE;
+ $sendResult = self::sendSMSMessage(
+ $contactId,
+ $tokenText,
+ $tokenHtml,
+ $smsParams,
+ $activityID
+ );
+
+ if (PEAR::isError($sendResult)) {
+ // Collect all of the PEAR_Error objects
+ $errMsgs[] = $sendResult;
+ } else {
$success++;
}
}
+ // If at least one message was sent and no errors
+ // were generated then return a boolean value of TRUE.
+ // Otherwise, return FALSE (no messages sent) or
+ // and array of 1 or more PEAR_Error objects.
+ $sent = FALSE;
+ if ($success > 0 && count($errMsgs) == 0) {
+ $sent = TRUE;
+ } elseif (count($errMsgs) > 0) {
+ $sent = $errMsgs;
+ }
+
return array($sent, $activity->id, $success);
}
* @param int $activityID the activity ID that tracks the message
* @param array $smsParams the params used for sending sms
*
- * @return boolean true if successfull else false.
+ * @return mixed true on success or PEAR_Error object
* @access public
* @static
*/
// make sure both phone are valid
// and that the recipient wants to receive sms
if (empty($toPhoneNumber) or $toDoNotSms) {
- return FALSE;
+ return PEAR::raiseError(
+ 'Recipient phone number is invalid or recipient does not want to receive SMS',
+ null,
+ PEAR_ERROR_RETURN
+ );
}
$message = $tokenHtml ? $tokenHtml : $tokenText;
$smsParams['parent_activity_id'] = $activityID;
$providerObj = CRM_SMS_Provider::singleton(array('provider_id' => $smsParams['provider_id']));
- if (!$providerObj->send($recipient, $smsParams, $message, NULL)) {
- return FALSE;
+ $sendResult = $providerObj->send($recipient, $smsParams, $message, NULL);
+ if (PEAR::isError($sendResult)) {
+ return $sendResult;
}
// add activity target record for every sms that is send
$contactIds
);
- if ($sent) {
+ if ($countSuccess > 0) {
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');
}
- //Display the name and number of contacts for those sms is not sent.
- $smsNotSent = array_diff_assoc($allContactIds, $contactIds);
-
- if (!empty($smsNotSent)) {
- $not_sent = array();
- foreach ($smsNotSent as $index => $contactId) {
- $displayName = $form->_allContactDetails[$contactId]['display_name'];
- $phone = $form->_allContactDetails[$contactId]['phone'];
- $contactViewUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$contactId");
- $not_sent[] = "<a href='$contactViewUrl' title='$phone'>$displayName</a>";
+ if (is_array($sent)) {
+ // At least one PEAR_Error object was generated.
+ // Display the error messages to the user.
+ $status = '<ul>';
+ foreach ($sent as $errMsg) {
+ $status .= '<li>' . $errMsg . '</li>';
}
- $status = '(' . ts('because no phone number on file or communication preferences specify DO NOT SMS or Contact is deceased');
- if (CRM_Utils_System::getClassName($form) == 'CRM_Activity_Form_Task_SMS') {
- $status .= ' ' . ts("or the contact is not part of the activity '%1'", array(1 => self::RECIEVED_SMS_ACTIVITY_SUBJECT));
+ $status .= '</ul>';
+ CRM_Core_Session::setStatus($status, ts('One Message Not Sent', array('count' => count($sent), 'plural' => '%count Messages Not Sent')), 'info');
+ } else {
+ //Display the name and number of contacts for those sms is not sent.
+ $smsNotSent = array_diff_assoc($allContactIds, $contactIds);
+
+ if (!empty($smsNotSent)) {
+ $not_sent = array();
+ foreach ($smsNotSent as $index => $contactId) {
+ $displayName = $form->_allContactDetails[$contactId]['display_name'];
+ $phone = $form->_allContactDetails[$contactId]['phone'];
+ $contactViewUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$contactId");
+ $not_sent[] = "<a href='$contactViewUrl' title='$phone'>$displayName</a>";
+ }
+ $status = '(' . ts('because no phone number on file or communication preferences specify DO NOT SMS or Contact is deceased');
+ if (CRM_Utils_System::getClassName($form) == 'CRM_Activity_Form_Task_SMS') {
+ $status .= ' ' . ts("or the contact is not part of the activity '%1'", array(1 => self::RECIEVED_SMS_ACTIVITY_SUBJECT));
+ }
+ $status .= ')<ul><li>' . implode('</li><li>', $not_sent) . '</li></ul>';
+ CRM_Core_Session::setStatus($status, ts('One Message Not Sent', array('count' => count($smsNotSent), 'plural' => '%count Messages Not Sent')), 'info');
}
- $status .= ')<ul><li>' . implode('</li><li>', $not_sent) . '</li></ul>';
- CRM_Core_Session::setStatus($status, ts('One Message Not Sent', array('count' => count($smsNotSent), 'plural' => '%count Messages Not Sent')), 'info');
}
-
}
}
CRM_Core_Error::setCallback();
}
- if (is_a($result, 'PEAR_Error')) {
+ // FIXME: for now we skipping bounce handling for sms
+ if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id) {
// CRM-9191
$message = $result->getMessage();
if (strpos($message,
}
else {
// TODO: Should add a failed activity instead.
-
- CRM_Core_Error::debug_log_message($response['data'] . " - for phone: {$postDataArray['to']}");
- return;
+ CRM_Core_Error::debug_log_message($response['data'] . " - for phone: {$postDataArray['to']}");
+ return PEAR::raiseError($response['data'], null, PEAR_ERROR_RETURN);
}
}
}
*
* @param array the message with a to/from/text
*
- * @return mixed true on sucess or PEAR_Error object
+ * @return mixed true on success or PEAR_Error object
* @access public
*/
function send($recipients, $header, $message, $jobID = NULL) {
return $sid;
}
else {
- return PEAR::raiseError($response['data']);
+ $errMsg = $send->RestException->Message
+ . ' For more information, see '
+ . $send->RestException->MoreInfo;
+ return PEAR::raiseError(
+ $errMsg,
+ null,
+ PEAR_ERROR_RETURN
+ );
}
}
}
$response['http_code'] = curl_getinfo($this->_ch, CURLINFO_HTTP_CODE);
if (empty($response['http_code'])) {
- return PEAR::raiseError('No HTTP Status Code was returned.');
+ return PEAR::raiseError('No HTTP Status Code was returned.', null, PEAR_ERROR_RETURN);
}
elseif ($response['http_code'] === 0) {
- return PEAR::raiseError('Cannot connect to the Twilio API Server.');
+ return PEAR::raiseError('Cannot connect to the Twilio API Server.', null, PEAR_ERROR_RETURN);
}
$response['data'] = $status;
return ($response);
}
-}
\ No newline at end of file
+}