diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index d0ac8f4ada30..2a999323cd61 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -987,6 +987,56 @@ public static function getActivitiesCount($input) { return civicrm_api3('Activity', 'getcount', $activityParams); } + /** + * @param int $userID + * @param string $subject + * @param string $html + * @param string $text + * @param string $additionalDetails + * @param int $campaignID + * @param array $attachments + * + * @return int + * The created activity ID + * @throws \CRM_Core_Exception + */ + public static function createEmailActivity($userID, $subject, $html, $text, $additionalDetails, $campaignID, $attachments) { + $activityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Email'); + + // CRM-6265: save both text and HTML parts in details (if present) + if ($html and $text) { + $details = "-ALTERNATIVE ITEM 0-\n$html$additionalDetails\n-ALTERNATIVE ITEM 1-\n$text$additionalDetails\n-ALTERNATIVE END-\n"; + } + else { + $details = $html ? $html : $text; + $details .= $additionalDetails; + } + + $activityParams = [ + 'source_contact_id' => $userID, + 'activity_type_id' => $activityTypeID, + 'activity_date_time' => date('YmdHis'), + 'subject' => $subject, + 'details' => $details, + // FIXME: check for name Completed and get ID from that lookup + 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Completed'), + 'campaign_id' => $campaignID, + ]; + + // CRM-5916: strip [case #…] before saving the activity (if present in subject) + $activityParams['subject'] = preg_replace('/\[case #([0-9a-h]{7})\] /', '', $activityParams['subject']); + + // add the attachments to activity params here + if ($attachments) { + // first process them + $activityParams = array_merge($activityParams, $attachments); + } + + $activity = self::create($activityParams); + + return $activity->id; + } + /** * Send the message to all the contacts. * @@ -1018,6 +1068,8 @@ public static function getActivitiesCount($input) { * * @return array * ( sent, activityId) if any email is sent and activityId + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ public static function sendEmail( &$contactDetails, @@ -1061,45 +1113,8 @@ public static function sendEmail( } //create the meta level record first ( email activity ) - $activityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Email'); + $activityID = self::createEmailActivity($userID, $subject, $html, $text, $additionalDetails, $campaignId, $attachments); - // CRM-6265: save both text and HTML parts in details (if present) - if ($html and $text) { - $details = "-ALTERNATIVE ITEM 0-\n$html$additionalDetails\n-ALTERNATIVE ITEM 1-\n$text$additionalDetails\n-ALTERNATIVE END-\n"; - } - else { - $details = $html ? $html : $text; - $details .= $additionalDetails; - } - - $activityParams = [ - 'source_contact_id' => $userID, - 'activity_type_id' => $activityTypeID, - 'activity_date_time' => date('YmdHis'), - 'subject' => $subject, - 'details' => $details, - // FIXME: check for name Completed and get ID from that lookup - 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Completed'), - 'campaign_id' => $campaignId, - ]; - - // CRM-5916: strip [case #…] before saving the activity (if present in subject) - $activityParams['subject'] = preg_replace('/\[case #([0-9a-h]{7})\] /', '', $activityParams['subject']); - - // add the attachments to activity params here - if ($attachments) { - // first process them - $activityParams = array_merge($activityParams, - $attachments - ); - } - - $activity = self::create($activityParams); - - // get the set of attachments from where they are stored - $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_activity', - $activity->id - ); $returnProperties = []; if (isset($messageToken['contact'])) { foreach ($messageToken['contact'] as $key => $value) { @@ -1207,8 +1222,9 @@ public static function sendEmail( $tokenText, $tokenHtml, $emailAddress, - $activity->id, - $attachments, + $activityID, + // get the set of attachments from where they are stored + CRM_Core_BAO_File::getEntityFile('civicrm_activity', $activityID), $cc, $bcc ) @@ -1217,7 +1233,7 @@ public static function sendEmail( } } - return [$sent, $activity->id]; + return [$sent, $activityID]; } /**