Skip to content

Commit

Permalink
Merge pull request #16105 from seamuslee001/dev_core_522_204
Browse files Browse the repository at this point in the history
dev/core#522 CRM-19767 dev/core#204 Add Case Tokens to emails
  • Loading branch information
seamuslee001 authored Jan 8, 2020
2 parents 81d5ba4 + f7f1cc3 commit edc10fa
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 16 deletions.
23 changes: 17 additions & 6 deletions CRM/Activity/BAO/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -979,12 +979,13 @@ public static function getActivitiesCount($input) {
* @param string $additionalDetails
* @param int $campaignID
* @param array $attachments
* @param int $caseID
*
* @return int
* The created activity ID
* @throws \CRM_Core_Exception
*/
public static function createEmailActivity($userID, $subject, $html, $text, $additionalDetails, $campaignID, $attachments) {
public static function createEmailActivity($userID, $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)
Expand All @@ -1006,6 +1007,9 @@ public static function createEmailActivity($userID, $subject, $html, $text, $add
'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']);
Expand All @@ -1016,9 +1020,8 @@ public static function createEmailActivity($userID, $subject, $html, $text, $add
$activityParams = array_merge($activityParams, $attachments);
}

$activity = self::create($activityParams);

return $activity->id;
$activity = civicrm_api3('Activity', 'create', $activityParams);
return $activity['id'];
}

/**
Expand Down Expand Up @@ -1049,6 +1052,7 @@ public static function createEmailActivity($userID, $subject, $html, $text, $add
* The additional information of CC and BCC appended to the activity Details.
* @param array $contributionIds
* @param int $campaignId
* @param int $caseId
*
* @return array
* ( sent, activityId) if any email is sent and activityId
Expand All @@ -1069,7 +1073,8 @@ public static function sendEmail(
$contactIds = NULL,
$additionalDetails = NULL,
$contributionIds = NULL,
$campaignId = NULL
$campaignId = NULL,
$caseId = NULL
) {
// get the contact details of logged in contact, which we set as from email
if ($userID == NULL) {
Expand Down Expand Up @@ -1097,7 +1102,7 @@ public static function sendEmail(
}

//create the meta level record first ( email activity )
$activityID = self::createEmailActivity($userID, $subject, $html, $text, $additionalDetails, $campaignId, $attachments);
$activityID = self::createEmailActivity($userID, $subject, $html, $text, $additionalDetails, $campaignId, $attachments, $caseId);

$returnProperties = [];
if (isset($messageToken['contact'])) {
Expand Down Expand Up @@ -1188,6 +1193,12 @@ public static function sendEmail(
$tokenHtml = NULL;
}

if ($caseId) {
$tokenSubject = CRM_Utils_Token::replaceCaseTokens($caseId, $tokenSubject, $subjectToken, $escapeSmarty);
$tokenText = CRM_Utils_Token::replaceCaseTokens($caseId, $tokenText, $messageToken, $escapeSmarty);
$tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseId, $tokenHtml, $messageToken, $escapeSmarty);
}

if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
// also add the contact tokens to the template
$smarty->assign_by_ref('contact', $values);
Expand Down
3 changes: 2 additions & 1 deletion CRM/Contact/Form/Task/EmailCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,8 @@ public static function submit(&$form, $formValues) {
array_keys($form->_toContactDetails),
$additionalDetails,
$contributionIds,
CRM_Utils_Array::value('campaign_id', $formValues)
CRM_Utils_Array::value('campaign_id', $formValues),
$form->getVar('_caseId')
);

$followupStatus = '';
Expand Down
17 changes: 10 additions & 7 deletions CRM/Contact/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public static function processMessageTemplate($formValues) {
* Process the form after the input has been submitted and validated.
*
* @param CRM_Core_Form $form
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public static function postProcess(&$form) {
$formValues = $form->controller->exportValues($form->getName());
Expand All @@ -127,6 +127,14 @@ public static function postProcess(&$form) {
$caseId = NULL;
$params = ['contact_id' => $contactId];

$caseId = $form->getVar('_caseId');
if (empty($caseId) && !empty($form->_caseIds[$item])) {
$caseId = $form->_caseIds[$item];
}
if ($caseId) {
$params['case_id'] = $caseId;
}

list($contact) = CRM_Utils_Token::getTokenDetails($params,
$returnProperties,
$skipOnHold,
Expand All @@ -142,12 +150,7 @@ public static function postProcess(&$form) {
}

$tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], TRUE, $messageToken);
if (!empty($form->_caseId)) {
$caseId = $form->_caseId;
}
if (empty($caseId) && !empty($form->_caseIds[$item])) {
$caseId = $form->_caseIds[$item];
}

if ($caseId) {
$tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseId, $tokenHtml, $messageToken);
}
Expand Down
4 changes: 2 additions & 2 deletions CRM/Utils/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@ public static function replaceEntityTokens($entity, $entityArray, $str, $knownTo

/**
* @param int $caseId
* @param int $str
* @param string $str
* @param array $knownTokens
* @param bool $escapeSmarty
* @return string
Expand Down Expand Up @@ -1875,7 +1875,7 @@ public static function formatTokensForDisplay($tokens) {
else {
$split = explode('.', trim($k, '{}'));
if (isset($split[1])) {
$entity = array_key_exists($split[1], CRM_Core_DAO_Address::export()) ? 'Address' : ucfirst($split[0]);
$entity = array_key_exists($split[1], CRM_Core_DAO_Address::export()) ? 'Address' : ucwords(str_replace('_', ' ', $split[0]));
}
else {
$entity = 'Contact';
Expand Down
74 changes: 74 additions & 0 deletions tests/phpunit/CRM/Activity/BAO/ActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1343,4 +1343,78 @@ public function hook_civicrm_aclWhereClause($type, &$tables, &$whereTables, &$co
}
}

public function testCaseTokens() {
$caseTest = new CiviCaseTestCase();
$caseTest->setUp();
// Create a contact and contactDetails array.
$contactId = $this->individualCreate();

// create a case for this user
$result = $this->callAPISuccess('Case', 'create', [
'contact_id' => $contactId,
'case_type_id' => '1',
'subject' => "my case",
'status_id' => "Open",
]);

$caseId = $result['id'];
$html_message = "<p>This is a test case with id: {case.id} and subject: {case.subject}</p>";
$html_message = CRM_Utils_Token::replaceCaseTokens($caseId, $html_message);

$this->assertTrue(strpos($html_message, 'id: ' . $caseId) !== 0);
$this->assertTrue(strpos($html_message, 'subject: my case') !== 0);
$caseTest->tearDown();
}

public function testSendEmailWithCaseId() {
$caseTest = new CiviCaseTestCase();
$caseTest->setUp();
// Create a contact and contactDetails array.
$contactId = $this->individualCreate();
$contact = $this->callAPISuccess('Contact', 'get', ['id' => $contactId]);

// create a logged in USER since the code references it for sendEmail user.
$this->createLoggedInUser();
CRM_Core_Config::singleton()->userPermissionClass->permissions = ['view all contacts', 'access CiviCRM', 'access all cases and activities', 'administer CiviCase'];
$session = CRM_Core_Session::singleton();
$loggedInUser = $session->get('userID');

// create a case for this user
$result = $this->callAPISuccess('Case', 'create', [
'contact_id' => $contactId,
'case_type_id' => 1,
'subject' => "my case",
'status_id' => "Open",
]);

$caseId = $result['id'];

$subject = __FUNCTION__ . ' subject {case.subject}';
$html = __FUNCTION__ . ' html {case.subject}';
$text = __FUNCTION__ . ' text';

$mut = new CiviMailUtils($this, TRUE);
list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail(
$contact['values'],
$subject,
$text,
$html,
$contact['values'][$contactId]['email'],
$loggedInUser,
$from = __FUNCTION__ . '@example.com',
NULL,
NULL,
NULL,
[$contactId],
NULL,
NULL,
NULL,
$caseId
);
$activity = $this->callAPISuccess('Activity', 'getsingle', ['id' => $activity_id, 'return' => ['case_id']]);
$this->assertEquals($caseId, $activity['case_id'][0], 'Activity case_id does not match.');
$mut->checkMailLog(['subject my case']);
$mut->stop();
}

}

0 comments on commit edc10fa

Please sign in to comment.