From 95435ba00ff52c96d520e8ced8ce0c869451e7ff Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 1 Oct 2021 08:54:58 +1300 Subject: [PATCH] Return another function to the email trait Another function that is only used from one place & is unnecessarily complicated because of the input & output paramters --- CRM/Activity/BAO/Activity.php | 6 +++ CRM/Contact/Form/Task/EmailTrait.php | 57 +++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index 6ea357f9fb..68ab519949 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -924,6 +924,10 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { } /** + * DO NOT USE. + * + * Deprecated from core - will be removed. + * * @param int $sourceContactID * The contact ID of the email "from". * @param string $subject @@ -935,6 +939,8 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { * @param array $attachments * @param int $caseID * + * @deprecated + * * @return int * The created activity ID * @throws \CRM_Core_Exception diff --git a/CRM/Contact/Form/Task/EmailTrait.php b/CRM/Contact/Form/Task/EmailTrait.php index da674268c3..89f16a2eb0 100644 --- a/CRM/Contact/Form/Task/EmailTrait.php +++ b/CRM/Contact/Form/Task/EmailTrait.php @@ -881,7 +881,7 @@ trait CRM_Contact_Form_Task_EmailTrait { } // Create email activity. - $activityID = CRM_Activity_BAO_Activity::createEmailActivity($userID, $renderedTemplate['subject'], $renderedTemplate['html'], $renderedTemplate['text'], $additionalDetails, $campaignId, $attachments, $caseId); + $activityID = $this->createEmailActivity($userID, $renderedTemplate['subject'], $renderedTemplate['html'], $renderedTemplate['text'], $additionalDetails, $campaignId, $attachments, $caseId); $activityIds[] = $activityID; if ($firstActivityCreated == FALSE && !empty($attachments)) { @@ -911,6 +911,61 @@ trait CRM_Contact_Form_Task_EmailTrait { return [$sent, $activityIds]; } + /** + * @param int $sourceContactID + * The contact ID of the email "from". + * @param string $subject + * @param string $html + * @param string $text + * @param string $additionalDetails + * The additional information of CC and BCC appended to the activity details. + * @param int $campaignID + * @param array $attachments + * @param int $caseID + * + * @return int + * The created activity ID + * @throws \CRM_Core_Exception + */ + protected function createEmailActivity($sourceContactID, $subject, $html, $text, $additionalDetails, $campaignID, $attachments, $caseID) { + $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' => $sourceContactID, + 'activity_type_id' => $activityTypeID, + 'activity_date_time' => date('YmdHis'), + 'subject' => $subject, + 'details' => $details, + 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Completed'), + 'campaign_id' => $campaignID, + ]; + if (!empty($caseID)) { + $activityParams['case_id'] = $caseID; + } + + // 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 = civicrm_api3('Activity', 'create', $activityParams); + + return $activity['id']; + } + /** * Send message - under refactor. * -- 2.25.1