diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index a42019a40510..7941aadc1043 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -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) @@ -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']); @@ -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']; } /** @@ -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 @@ -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) { @@ -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'])) { @@ -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); diff --git a/CRM/Contact/Form/Task/EmailCommon.php b/CRM/Contact/Form/Task/EmailCommon.php index 4591c8bf481a..d8e7af4c33a1 100644 --- a/CRM/Contact/Form/Task/EmailCommon.php +++ b/CRM/Contact/Form/Task/EmailCommon.php @@ -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 = ''; diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index 61bf8f4519ff..ee7179f21086 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -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()); @@ -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, @@ -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); } diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index 4f8729229981..19cc7591c43d 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -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 @@ -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'; diff --git a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php index 17f9497ba122..890a2854a8dc 100644 --- a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php @@ -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 = "
This is a test case with id: {case.id} and subject: {case.subject}
"; + $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(); + } + }