Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract function to create email activity when sending an email to contact #15133

Merged
merged 1 commit into from
Aug 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 57 additions & 41 deletions CRM/Activity/BAO/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
)
Expand All @@ -1217,7 +1233,7 @@ public static function sendEmail(
}
}

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

/**
Expand Down