Skip to content

Commit

Permalink
CIVIMM-261: Support automatic email filing for case type categories o…
Browse files Browse the repository at this point in the history
…ther than cases
  • Loading branch information
Muhammad Shahrukh committed Jan 16, 2025
1 parent 9230a07 commit 71a22c3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 85 deletions.

This file was deleted.

58 changes: 58 additions & 0 deletions CRM/Civicase/Hook/alterMailParams/SubjectProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* Remove word 'case' in email subject.
*/
class CRM_Civicase_Hook_alterMailParams_SubjectProcessor {

/**
* A substring of email subject that should be removed.
*
* @var string
* This substring will be removed.
*/
private $toRemove = '[case ';

/**
* Email subject processor.
*
* Remove word 'case' in email subject.
*
* @param array $params
* Mail parameters.
* @param string $context
* Mail context.
*/
public function run(array &$params, $context) {
$caseId = CRM_Utils_Request::retrieve('caseid', 'Integer');
if (!$this->shouldRun($params, $context, $caseId)) {
return;
}

// Make sure we make just 1 replacement.
$subject = explode($this->toRemove, $params['subject'], 2);
$params['subject'] = '[' . $subject[1];
}

/**
* Determines if the hook will run.
*
* @param array $params
* Mail parameters.
* @param string $context
* Mail context.
* @param int $caseId
* Case id.
*
* @return bool
* returns TRUE if hook should run, FALSE otherwise.
*/
private function shouldRun(array $params, $context, $caseId) {
if (empty($params['subject'])) {
return FALSE;
}
// If case id is set and email subject starts with '[case '.
return $caseId && strpos($params['subject'], $this->toRemove) === 0;
}

}
10 changes: 9 additions & 1 deletion civicase.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ function _civicase_add_case_category_case_type_entity(array &$entityTypes) {
*/
function civicase_civicrm_alterMailParams(&$params, $context) {
$hooks = [
new CRM_Civicase_Hook_alterMailParams_SubjectCaseTypeCategoryProcessor(),
new CRM_Civicase_Hook_alterMailParams_SubjectProcessor(),
new CRM_Civicase_Hook_alterMailParams_AttachQuotation(),
];

Expand Down Expand Up @@ -660,3 +660,11 @@ function civicase_civicrm_alterContent(&$content, $context, $tplName, &$object)
$hook->run();
}
}

/**
* Implements hook_civicrm_caseEmailSubjectPatterns().
*/
function civicase_civicrm_caseEmailSubjectPatterns(&$subjectPatterns) {
$subjectPatterns[] = '/\[[a-z0-9\s]*#([0-9a-f]{7})\]/i';
$subjectPatterns[] = '/\[[a-z0-9\s]*#(\d+)\]/i';
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use CRM_Civicase_Hook_alterMailParams_SubjectCaseTypeCategoryProcessor as SubjectCaseTypeCategoryProcessor;
use CRM_Civicase_Hook_alterMailParams_SubjectProcessor as SubjectProcessor;
use CRM_Civicase_Test_Fabricator_CaseCategoryInstance as CaseCategoryInstanceFabricator;
use CRM_Civicase_Test_Fabricator_CaseCategory as CaseCategoryFabricator;
use CRM_Civicase_Test_Fabricator_CaseCategoryInstanceType as CaseCategoryInstanceTypeFabricator;
Expand All @@ -19,7 +19,7 @@ class CRM_Civicase_Hook_alterMailParams_SubjectCaseTypeCategoryProcessorTest ext
/**
* Test first instance of case is replaced.
*/
public function testRunReplacesTheFirstInstanceOfCaseInMailSubjectCorrectly() {
public function testRunRemovesTheFirstInstanceOfCaseInMailSubjectCorrectly() {
$categoryInstanceTypeOne = CaseCategoryInstanceTypeFabricator::fabricate();
$categoryParams = ['label' => 'Awards', 'singular_label' => 'Award'];
$caseCategory = $this->createCaseTypeCategory($categoryParams);
Expand All @@ -30,12 +30,11 @@ public function testRunReplacesTheFirstInstanceOfCaseInMailSubjectCorrectly() {
]
);

$emailSubjectProcessor = new SubjectCaseTypeCategoryProcessor();
$emailSubjectProcessor = new SubjectProcessor();
$_REQUEST['caseid'] = $this->getCase($caseCategory['value'])['id'];
$params['subject'] = "[case ] This is a test email subject case";
$emailSubjectProcessor->run($params, $context = '');
$replacedValue = strtolower($categoryParams['singular_label']);
$expectedSubject = "[{$replacedValue} ] This is a test email subject case";
$expectedSubject = "[] This is a test email subject case";
$this->assertEquals($expectedSubject, $params['subject']);
}

Expand Down

0 comments on commit 71a22c3

Please sign in to comment.