CRM-20308 added method getReceiptFrom to set FROM in sendActivityCopy - send email...
authorErik Hommel <hommel@ee-atwork.nl>
Tue, 4 Apr 2017 07:17:43 +0000 (09:17 +0200)
committerdeb.monish <monish.deb@jmaconsulting.biz>
Sun, 16 Apr 2017 09:13:02 +0000 (14:43 +0530)
CRM/Case/BAO/Case.php

index 78943a5254a191cb4ef6059d6b5191bb3a4829c9..7290a3320758e04f195b85913c4fdfcc8086d8e2 100644 (file)
@@ -1347,9 +1347,8 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
     }
 
     $result = array();
-    list($name, $address) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID);
-
-    $receiptFrom = "$name <$address>";
+    // CRM-20308 get receiptFrom defaults see https://issues.civicrm.org/jira/browse/CRM-20308
+    $receiptFrom = self::getReceiptFrom($activityId);
 
     $recordedActivityParams = array();
 
@@ -3170,4 +3169,59 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
     return $clauses;
   }
 
+  /**
+   * CRM-20308
+   * Method to get the contact id to use as from contact for email copy
+   * 1. Activity Added by Contact's email address
+   * 2. System Default From Address
+   * 3. Default Organization Contact email address
+   * 4. Logged in user
+   *
+   * @param int $activityId
+   * @return mixed $emailFromContactId
+   * @see https://issues.civicrm.org/jira/browse/CRM-20308
+   */
+  private static function getReceiptFrom($activityId) {
+    $emailFromContactId = NULL;
+    if (!empty($activityId)) {
+      try {
+        $emailFromContactId = civicrm_api3('ActivityContact', 'getvalue', array(
+          'activity_id' => $activityId,
+          'record_type_id' => 'Activity Source',
+          'return' => 'contact_id',
+        ));
+      }
+      catch (CiviCRM_API3_Exception $ex) {
+        // get default from address from domain
+        try {
+          $domain = civicrm_api3('Domain', 'getsingle', array('id' => CRM_Core_Config::domainID()));
+          if (isset($domain['from_email'])) {
+            $emailFromContactId = $domain['from_email'];
+          }
+        }
+        catch (CiviCRM_API3_Exception $ex) {
+        }
+        // if not found get email for contact_id 1
+        if (!$emailFromContactId) {
+          try {
+            $emailFromContactId = civicrm_api3('Email', 'getvalue', array(
+              'contact_id' => 1,
+              'return' => 'email',
+            ));
+          }
+          catch (CiviCRM_API3_Exception $ex) {
+          }
+        }
+      }
+
+    }
+    // if not found, use logged in user
+    if (!$emailFromContactId) {
+      $session = CRM_Core_Session::singleton();
+      $emailFromContactId = $session->get('userID');
+    }
+    list($name, $address) = CRM_Contact_BAO_Contact_Location::getEmailDetails($emailFromContactId);
+    return "$name <$address>";
+  }
+
 }