CRM-13883 - permanently delete contact should not remove activities if connected...
[civicrm-core.git] / CRM / SMS / Provider.php
index 3bcf023234539e5fd263d02d7ae83f7b9f76dd23..fb1a1a05310b9e6db06bea0688bb18f95387d39a 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -43,7 +43,7 @@ abstract class CRM_SMS_Provider {
    * @static
    */
   static private $_singleton = array();
-  CONST MAX_SMS_CHAR = 160;
+  CONST MAX_SMS_CHAR = 460;
 
   /**
    * singleton function used to manage this object
@@ -82,7 +82,7 @@ abstract class CRM_SMS_Provider {
         CRM_Core_Error::fatal("Could not locate extension for {$providerName}.");
       }
 
-      self::$_singleton[$cacheKey] = eval('return ' . $paymentClass . '::singleton( $providerParams, $force );');
+      self::$_singleton[$cacheKey] = $paymentClass::singleton($providerParams, $force);
     }
     return self::$_singleton[$cacheKey];
   }
@@ -113,33 +113,35 @@ abstract class CRM_SMS_Provider {
   }
 
   function createActivity($apiMsgID, $message, $headers = array(
-    ), $jobID = NULL) {
+    ), $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;
+    }
     else {
       $session = CRM_Core_Session::singleton();
       $sourceContactID = $session->get('userID');
     }
 
     if (!$sourceContactID) {
-       $sourceContactID = CRM_Utils_Array::value('Contact', $headers);
+      $sourceContactID = CRM_Utils_Array::value('Contact', $headers);
     }
     if (!$sourceContactID) {
-       return false;
+      return false;
     }
 
-    $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'SMS', 'name');
+    $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'SMS delivery', 'name');
     // note: lets not pass status here, assuming status will be updated by callback
     $activityParams = array(
       'source_contact_id' => $sourceContactID,
       'target_contact_id' => $headers['contact_id'],
       'activity_type_id' => $activityTypeID,
       'activity_date_time' => date('YmdHis'),
-      'subject' => 'SMS Sent',
       'details' => $message,
       'result' => $apiMsgID,
     );
@@ -160,36 +162,38 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
   }
 
   function processInbound($from, $body, $to = NULL, $trackID = NULL) {
-       $formatFrom   = $this->formatPhone($this->stripPhone($from), $like, "like"); 
+    $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 WHERE phone LIKE "' . $escapedFrom . '"');
-    
+    $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
-       // use fake @mobile.sms email address for new contact since civi
-       // requires email or name for all contacts
-       $locationTypes =& CRM_Core_PseudoConstant::locationType();
-       $phoneTypes    =& CRM_Core_PseudoConstant::phoneType();
-       $phoneloc  = array_search( 'Home',  $locationTypes );
-       $phonetype = array_search( 'Mobile', $phoneTypes );
-       $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 )
-                                   )
-                );
-       $fromContact = CRM_Contact_BAO_Contact::create($contactparams, FALSE, TRUE, FALSE);
+      // unknown mobile sender -- create new contact
+      // use fake @mobile.sms email address for new contact since civi
+      // requires email or name for all contacts
+      $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
+      $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
+      $phoneloc = array_search('Home', $locationTypes);
+      $phonetype = array_search('Mobile', $phoneTypes);
+      $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
+        )),
+      );
+      $fromContact = CRM_Contact_BAO_Contact::create($contactparams, FALSE, TRUE, FALSE);
       $fromContactID = $fromContact->id;
     }
 
     if ($to) {
       $to = CRM_Utils_Type::escape($to, 'String');
-      $toContactID = CRM_Core_DAO::singleValueQuery('SELECT contact_id FROM civicrm_phone WHERE phone LIKE "' . $to . '"');
+      $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;
@@ -197,7 +201,7 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
 
     if ($fromContactID) {
       $actStatusIDs = array_flip(CRM_Core_OptionGroup::values('activity_status'));
-      $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'SMS', 'name');
+      $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'Inbound SMS', 'name');
 
       // note: lets not pass status here, assuming status will be updated by callback
       $activityParams = array(
@@ -205,7 +209,6 @@ INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1";
         'target_contact_id' => $fromContactID,
         'activity_type_id' => $activityTypeID,
         'activity_date_time' => date('YmdHis'),
-        'subject' => 'SMS Received',
         'status_id' => $actStatusIDs['Completed'],
         'details' => $body,
         'phone_number' => $from