Skip to content

Commit

Permalink
Merge pull request civicrm#28800 from eileenmcnaughton/act_example
Browse files Browse the repository at this point in the history
Add preliminary previewability and token support to case-activity
  • Loading branch information
demeritcowboy authored Jan 5, 2024
2 parents c94ea2f + 8f6e8d4 commit ee0be2a
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 46 deletions.
7 changes: 6 additions & 1 deletion CRM/Case/BAO/Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -1405,14 +1405,19 @@ public static function sendActivityCopy($clientId, $activityId, $contacts, $atta

[$result[$info['contact_id'] ?? NULL], $subject, $message, $html] = CRM_Core_BAO_MessageTemplate::sendTemplate(
[
'groupName' => 'msg_tpl_workflow_case',
'workflow' => 'case_activity',
'contactId' => $info['contact_id'] ?? NULL,
'tplParams' => $tplParams,
'from' => $receiptFrom,
'toName' => $displayName,
'toEmail' => $mail,
'attachments' => $attachments,
'modelProps' => $caseId ? [
'activityID' => $activityId,
'caseID' => $caseId,
] : [
'activityID' => $activityId,
],
]
);

Expand Down
69 changes: 69 additions & 0 deletions CRM/Case/WorkflowMessage/ActivityExamples.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

use Civi\Api4\WorkflowMessage;
use Civi\WorkflowMessage\WorkflowMessageExample;

/**
* Basic contribution example for contribution templates.
*
* @noinspection PhpUnused
*/
class CRM_Case_WorkflowMessage_ActivityExamples extends WorkflowMessageExample {

/**
* Get the examples this class is able to deliver.
*/
public function getExamples(): iterable {
$workflows = ['case_activity'];
foreach ($workflows as $workflow) {
foreach ([TRUE, FALSE] as $caseID) {
yield [
'name' => 'workflow/' . $workflow . '/activity' . (bool) $caseID,
'title' => $caseID ? ts('Case Activity') : ts('Activity'),
'tags' => ['preview'],
'workflow' => $workflow,
'case_id' => $caseID,
];
}
}
}

/**
* Build an example to use when rendering the workflow.
*
* @param array $example
*
* @throws \API_Exception
*/
public function build(array &$example): void {
$workFlow = WorkflowMessage::get(TRUE)->addWhere('name', '=', $example['workflow'])->execute()->first();
$this->setWorkflowName($workFlow['name']);
$messageTemplate = new $workFlow['class']();
$this->addExampleData($messageTemplate, $example);
$example['data'] = $this->toArray($messageTemplate);
}

/**
* Add relevant example data.
*
* @param \CRM_Case_WorkflowMessage_CaseActivity $messageTemplate
* @param array $example
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
private function addExampleData(CRM_Case_WorkflowMessage_CaseActivity $messageTemplate, array $example): void {
$messageTemplate->setContact(\Civi\Test::example('entity/Contact/Barb'));
$messageTemplate->setActivity([
'activity_type_id:label' => ts('Follow up'),
// Ideally something better but let's not add more strings until we hae a good one.
'subject' => ('Follow up'),
]);
if (!empty($example['case_id'])) {
$messageTemplate->setCase(['subject' => ts('Sample case')]);
}
}

}
90 changes: 90 additions & 0 deletions CRM/Case/WorkflowMessage/CaseActivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

use Civi\WorkflowMessage\GenericWorkflowMessage;

/**
* When an activity is created in a case, the "case_activity" email is sent.
* Generally, the email is sent to the assignee, although (depending on
* the configuration/add-ons) additional copies may be sent.
*
* @method $this setActivityID(int $activityID)
* @method $this setCaseID(int $caseID)
* @method int getActivityID()
* @method int getCaseID()
*
* @see CRM_Case_BAO_Case::sendActivityCopy
*
* @support template-only
*/
class CRM_Case_WorkflowMessage_CaseActivity extends GenericWorkflowMessage {

public const WORKFLOW = 'case_activity';

/**
* The activity.
*
* @var array|null
*
* @scope tokenContext as activity
*/
public $activity;

/**
* @var int
*
* @scope tokenContext as activityId
*/
public $activityID;

/**
* The activity.
*
* @var array|null
*
* @scope tokenContext as case
*/
public $case;

/**
* @var int
*
* @scope tokenContext as caseId
*/
public $caseID;

/**
* @param array $activity
*
* @return CRM_Case_WorkflowMessage_CaseActivity
*/
public function setActivity(array $activity): CRM_Case_WorkflowMessage_CaseActivity {
$this->activity = $activity;
if (!empty($activity['id'])) {
$this->activityID = $activity['id'];
}
return $this;
}

/**
* @param array $case
*
* @return CRM_Case_WorkflowMessage_CaseActivity
*/
public function setCase(array $case): CRM_Case_WorkflowMessage_CaseActivity {
$this->case = $case;
if (!empty($case['id'])) {
$this->caseID = $case['id'];
}
return $this;
}

}
5 changes: 1 addition & 4 deletions tests/phpunit/api/v4/Entity/WorkflowMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ public function testGet(): void {
->addWhere('name', 'LIKE', 'case%')
->execute()
->indexBy('name');
// Temporarily make this false - we are going to put the real slim shady
// in place soon - at which point we can re-enable but turn off for
// the bait & switch.
$this->assertFalse(isset($result['case_activity']));
$this->assertTrue(isset($result['case_activity']));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion xml/templates/message_templates/case_activity_html.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<table style="border: 1px solid #999; margin: 1em 0em 1em; border-collapse: collapse; width:100%;">
<tr>
<th {$headerStyle}>
{ts}Activity Summary{/ts} - {$activityTypeName}
{ts}Activity Summary{/ts} - {activity.activity_type_id:label}
</th>
</tr>
{if !empty($isCaseActivity)}
Expand Down
40 changes: 0 additions & 40 deletions xml/templates/message_templates/case_activity_text.tpl
Original file line number Diff line number Diff line change
@@ -1,40 +0,0 @@
===========================================================
{ts}Activity Summary{/ts} - {$activityTypeName}
===========================================================
{if !empty($isCaseActivity)}
{ts}Your Case Role(s){/ts} : {$contact.role|default:''}
{if !empty($manageCaseURL)}
{ts}Manage Case{/ts} : {$manageCaseURL}
{/if}
{/if}

{if !empty($editActURL)}
{ts}Edit activity{/ts} : {$editActURL}
{/if}
{if !empty($viewActURL)}
{ts}View activity{/ts} : {$viewActURL}
{/if}

{foreach from=$activity.fields item=field}
{if $field.type eq 'Date'}
{$field.label} : {$field.value|crmDate:$config->dateformatDatetime}
{else}
{$field.label} : {$field.value}
{/if}
{/foreach}

{if !empty($activity.customGroups)}
{foreach from=$activity.customGroups key=customGroupName item=customGroup}
==========================================================
{$customGroupName}
==========================================================
{foreach from=$customGroup item=field}
{if $field.type eq 'Date'}
{$field.label} : {$field.value|crmDate:$config->dateformatDatetime}
{else}
{$field.label} : {$field.value}
{/if}
{/foreach}

{/foreach}
{/if}

0 comments on commit ee0be2a

Please sign in to comment.