Merge pull request #5123 from eileenmcnaughton/CRM-15369
[civicrm-core.git] / CRM / SMS / Provider.php
index 5fa2f04788708337437104df8b07115c5cbaa09b..eb4818ec941315985db5d0874226d376ccb77ace 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -32,7 +32,6 @@
  * $Id$
  *
  */
-
 abstract class CRM_SMS_Provider {
 
   /**
@@ -40,23 +39,21 @@ abstract class CRM_SMS_Provider {
    * pattern and cache the instance in this variable
    *
    * @var object
-   * @static
    */
   static private $_singleton = array();
-  CONST MAX_SMS_CHAR = 460;
+  const MAX_SMS_CHAR = 460;
 
   /**
-   * Singleton function used to manage this object
+   * Singleton function used to manage this object.
    *
    * @param array $providerParams
    * @param bool $force
    *
    * @return object
-   * @static
    */
-  static function &singleton($providerParams = array(), $force = FALSE) {
-    $mailingID    = CRM_Utils_Array::value('mailing_id', $providerParams);
-    $providerID   = CRM_Utils_Array::value('provider_id', $providerParams);
+  public static function &singleton($providerParams = array(), $force = FALSE) {
+    $mailingID = CRM_Utils_Array::value('mailing_id', $providerParams);
+    $providerID = CRM_Utils_Array::value('provider_id', $providerParams);
     $providerName = CRM_Utils_Array::value('provider', $providerParams);
 
     if (!$providerID && $mailingID) {
@@ -72,14 +69,15 @@ abstract class CRM_SMS_Provider {
     }
 
     $providerName = CRM_Utils_Type::escape($providerName, 'String');
-    $cacheKey     = "{$providerName}_" . (int) $providerID . "_" . (int) $mailingID;
+    $cacheKey = "{$providerName}_" . (int) $providerID . "_" . (int) $mailingID;
 
     if (!isset(self::$_singleton[$cacheKey]) || $force) {
       $ext = CRM_Extension_System::singleton()->getMapper();
       if ($ext->isExtensionKey($providerName)) {
         $paymentClass = $ext->keyToClass($providerName);
-        require_once ("{$paymentClass}.php");
-      } else {
+        require_once "{$paymentClass}.php";
+      }
+      else {
         CRM_Core_Error::fatal("Could not locate extension for {$providerName}.");
       }
 
@@ -89,18 +87,14 @@ abstract class CRM_SMS_Provider {
   }
 
   /**
-   * Send an SMS Message via the API Server
-   *
-   * @access public
+   * Send an SMS Message via the API Server.
    */
-  abstract function send($recipients, $header, $message, $dncID = NULL);
+  abstract public function send($recipients, $header, $message, $dncID = NULL);
 
   /**
    * Return message text. Child class could override this function to have better control over the message being sent.
-   *
-   * @access public
    */
-  function getMessage($message, $contactID, $contactDetails) {
+  public function getMessage($message, $contactID, $contactDetails) {
     $html = $message->getHTMLBody();
     $text = $message->getTXTBody();
 
@@ -113,7 +107,7 @@ abstract class CRM_SMS_Provider {
    *
    * @return mixed
    */
-  function getRecipientDetails($fields, $additionalDetails) {
+  public function getRecipientDetails($fields, $additionalDetails) {
     // we could do more altering here
     $fields['To'] = $fields['phone'];
     return $fields;
@@ -126,18 +120,18 @@ abstract class CRM_SMS_Provider {
    * @param int $jobID
    * @param int $userID
    *
-   * @return $this|null|object
+   * @return self|null|object
    * @throws CRM_Core_Exception
    */
-  function createActivity($apiMsgID, $message, $headers = array(), $jobID = NULL, $userID = NULL) {
+  public function createActivity($apiMsgID, $message, $headers = array(), $jobID = NULL, $userID = NULL) {
     if ($jobID) {
       $sql = "
 SELECT scheduled_id FROM civicrm_mailing m
 INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
       $sourceContactID = CRM_Core_DAO::singleValueQuery($sql, array(1 => array($jobID, 'Integer')));
     }
-    elseif($userID) {
-      $sourceContactID=$userID;
+    elseif ($userID) {
+      $sourceContactID = $userID;
     }
     else {
       $session = CRM_Core_Session::singleton();
@@ -148,7 +142,7 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
       $sourceContactID = CRM_Utils_Array::value('Contact', $headers);
     }
     if (!$sourceContactID) {
-      return false;
+      return FALSE;
     }
 
     $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'SMS delivery', 'name');
@@ -173,7 +167,7 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
    *
    * @return mixed
    */
-  function retrieve($name, $type, $abort = TRUE, $default = NULL, $location = 'REQUEST') {
+  public function retrieve($name, $type, $abort = TRUE, $default = NULL, $location = 'REQUEST') {
     static $store = NULL;
     $value = CRM_Utils_Request::retrieve($name, $type, $store,
       FALSE, $default, $location
@@ -192,15 +186,15 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
    * @param null $to
    * @param int $trackID
    *
-   * @return $this|null|object
+   * @return self|null|object
    * @throws CRM_Core_Exception
    */
-  function processInbound($from, $body, $to = NULL, $trackID = NULL) {
-    $formatFrom   = $this->formatPhone($this->stripPhone($from), $like, "like");
-    $escapedFrom  = CRM_Utils_Type::escape($formatFrom, 'String');
+  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 . '"');
 
-    if (! $fromContactID) {
+    if (!$fromContactID) {
       // unknown mobile sender -- create new contact
       // use fake @mobile.sms email address for new contact since civi
       // requires email or name for all contacts
@@ -211,15 +205,19 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
       $stripFrom = $this->stripPhone($from);
       $contactparams = array(
         'contact_type' => 'Individual',
-        'email' => array(1 => array(
-          'location_type_id' => $phoneloc,
-          'email' => $stripFrom . '@mobile.sms'
-        )),
-        'phone' => array(1 => array(
-          'phone_type_id' => $phonetype,
-          'location_type_id' => $phoneloc,
-          'phone' => $stripFrom
-        )),
+        'email' => array(
+          1 => array(
+            'location_type_id' => $phoneloc,
+            'email' => $stripFrom . '@mobile.sms',
+          ),
+        ),
+        'phone' => array(
+          1 => array(
+            'phone_type_id' => $phonetype,
+            'location_type_id' => $phoneloc,
+            'phone' => $stripFrom,
+          ),
+        ),
       );
       $fromContact = CRM_Contact_BAO_Contact::create($contactparams, FALSE, TRUE, FALSE);
       $fromContactID = $fromContact->id;
@@ -245,7 +243,7 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
         'activity_date_time' => date('YmdHis'),
         'status_id' => $actStatusIDs['Completed'],
         'details' => $body,
-        'phone_number' => $from
+        'phone_number' => $from,
       );
       if ($trackID) {
         $trackID = CRM_Utils_Type::escape($trackID, 'String');
@@ -263,7 +261,7 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
    *
    * @return mixed|string
    */
-  function stripPhone($phone) {
+  public function stripPhone($phone) {
     $newphone = preg_replace('/[^0-9x]/', '', $phone);
     while (substr($newphone, 0, 1) == "1") {
       $newphone = substr($newphone, 1);
@@ -284,7 +282,7 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
    *
    * @return mixed|string
    */
-  function formatPhone($phone, &$kind, $format = "dash") {
+  public function formatPhone($phone, &$kind, $format = "dash") {
     $phoneA = explode("x", $phone);
     switch (strlen($phoneA[0])) {
       case 0:
@@ -292,7 +290,7 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
         $area = "";
         $exch = "";
         $uniq = "";
-        $ext  = $phoneA[1];
+        $ext = $phoneA[1];
         break;
 
       case 7:
@@ -300,7 +298,7 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
         $area = "";
         $exch = substr($phone, 0, 3);
         $uniq = substr($phone, 3, 4);
-        $ext  = $phoneA[1];
+        $ext = $phoneA[1];
         break;
 
       case 10:
@@ -308,7 +306,7 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
         $area = substr($phone, 0, 3);
         $exch = substr($phone, 3, 3);
         $uniq = substr($phone, 6, 4);
-        $ext  = $phoneA[1];
+        $ext = $phoneA[1];
         break;
 
       default:
@@ -346,7 +344,7 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
    *
    * @return string
    */
-  function urlEncode($values) {
+  public function urlEncode($values) {
     $uri = '';
     foreach ($values as $key => $value) {
       $value = urlencode($value);
@@ -357,5 +355,5 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
     }
     return $uri;
   }
-}
 
+}