Skip to content

Commit

Permalink
Extract function to create email activity when sending an email to co…
Browse files Browse the repository at this point in the history
…ntact
  • Loading branch information
mattwire committed Aug 25, 2019
1 parent ac179e8 commit c464890
Showing 1 changed file with 66 additions and 41 deletions.
107 changes: 66 additions & 41 deletions CRM/Activity/BAO/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,65 @@ public static function getActivitiesCount($input) {
return civicrm_api3('Activity', 'getcount', $activityParams);
}

/**
* Create an email activity when we send an email
*
* @param int $activityId
* activity id.
* @param array $params
*
* @return CRM_Activity_BAO_Activity|null|object
*/
/**
* @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.
*
Expand Down Expand Up @@ -1018,6 +1077,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,
Expand Down Expand Up @@ -1061,45 +1122,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) {
Expand Down Expand Up @@ -1207,8 +1231,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
)
Expand All @@ -1217,7 +1242,7 @@ public static function sendEmail(
}
}

return [$sent, $activity->id];
return [$sent, $activityID];
}

/**
Expand Down

0 comments on commit c464890

Please sign in to comment.