From: Effy Elden Date: Fri, 10 Mar 2017 09:53:00 +0000 (+1100) Subject: CRM-20238 - Create hook for inbound SMS X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=0613768a63cffe1d99f5fd57c2d9b9461fb4de3f;p=civicrm-core.git CRM-20238 - Create hook for inbound SMS --- diff --git a/CRM/SMS/Provider.php b/CRM/SMS/Provider.php index 9d914dce0d..68c6e25080 100644 --- a/CRM/SMS/Provider.php +++ b/CRM/SMS/Provider.php @@ -203,9 +203,17 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1"; * @throws CRM_Core_Exception */ public function processInbound($from, $body, $to = NULL, $trackID = NULL) { - $formatFrom = $this->formatPhone($this->stripPhone($from), $like, "like"); - $escapedFrom = CRM_Utils_Type::escape($formatFrom, 'String'); - $fromContactID = CRM_Core_DAO::singleValueQuery('SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE "%' . $escapedFrom . '"'); + $fromContactID = NULL; + $toContactID = NULL; + // call hook_civicrm_inboundSMS + CRM_Utils_Hook::inboundSMS($from, $fromContactID, $to, $toContactID, $body, $trackID); + + if (!$fromContactID) { + // find sender by phone number if $fromContactID not set by hook + $formatFrom = $this->formatPhone($this->stripPhone($from), $like, "like"); + $escapedFrom = CRM_Utils_Type::escape($formatFrom, 'String'); + $fromContactID = CRM_Core_DAO::singleValueQuery('SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE "%' . $escapedFrom . '"'); + } if (!$fromContactID) { // unknown mobile sender -- create new contact @@ -236,12 +244,15 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1"; $fromContactID = $fromContact->id; } - if ($to) { - $to = CRM_Utils_Type::escape($to, 'String'); - $toContactID = CRM_Core_DAO::singleValueQuery('SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE "%' . $to . '"'); - } - else { - $toContactID = $fromContactID; + if (!($toContactID)) { + // find recipient if $toContactID not set by hook + if ($to) { + $to = CRM_Utils_Type::escape($to, 'String'); + $toContactID = CRM_Core_DAO::singleValueQuery('SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE "%' . $to . '"'); + } + else { + $toContactID = $fromContactID; + } } if ($fromContactID) { diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index 602028a378..e6e130f6a8 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -2299,4 +2299,27 @@ abstract class CRM_Utils_Hook { ); } + /** + * This hook is called before an inbound SMS is processed. + * + * @param string $from + * The phone number the message is from, as set by SMS provider + * @param int $fromContactID + * Set to override default matching + * @param string $to + * The optional phone number the message is to, as set by SMS provider + * @param int $toContactID + * Set to override default matching + * @param string $body + * The body text of the message + * @param string $trackID + * The tracking ID of the message + * + * @return mixed + */ + public static function inboundSMS(&$from, &$fromContactID = NULL, &$to, &$toContactID = NULL, &$body, &$trackID) { + return self::singleton() + ->invoke(6, $from, $fromContactID, $to, $toContactID, $body, $trackID, 'civicrm_inboundSMS'); + } + }