4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 use Civi\Token\AbstractTokenSubscriber
;
19 use Civi\Token\Event\TokenValueEvent
;
20 use Civi\Token\TokenRow
;
23 * Class CRM_Member_Tokens
25 * Generate "activity.*" tokens.
27 * This TokenSubscriber was originally produced by refactoring the code from the
28 * scheduled-reminder system with the goal of making that system
29 * more flexible. The current implementation is still coupled to
30 * scheduled-reminders. It would be good to figure out a more generic
31 * implementation which is not tied to scheduled reminders, although
32 * that is outside the current scope.
34 * This has been enhanced to work with PDF/letter merge
36 class CRM_Activity_Tokens
extends AbstractTokenSubscriber
{
38 use CRM_Core_TokenTrait
;
43 private function getEntityName(): string {
50 private function getEntityTableName(): string {
51 return 'civicrm_activity';
57 private function getEntityContextSchema(): string {
62 * Mapping from tokenName to api return field
63 * Using arrays allows more complex tokens to be handled that require more than one API field.
64 * For example, an address token might want ['street_address', 'city', 'postal_code']
68 private static $fieldMapping = [
69 'activity_id' => ['id'],
70 'activity_type' => ['activity_type_id'],
71 'status' => ['status_id'],
72 'campaign' => ['campaign_id'],
78 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent
$e) {
79 if ($e->mapping
->getEntity() !== $this->getEntityTableName()) {
83 // The joint expression for activities needs some extra nuance to handle.
84 // Multiple revisions of the activity.
85 // Q: Could we simplify & move the extra AND clauses into `where(...)`?
86 $e->query
->param('casEntityJoinExpr', 'e.id = reminder.entity_id AND e.is_current_revision = 1 AND e.is_deleted = 0');
92 public function prefetch(TokenValueEvent
$e) {
93 // Find all the entity IDs
95 = $e->getTokenProcessor()->getContextValues('actionSearchResult', 'entityID')
96 +
$e->getTokenProcessor()->getContextValues($this->getEntityContextSchema());
102 // Get data on all activities for basic and customfield tokens
103 $activities = civicrm_api3('Activity', 'get', [
104 'id' => ['IN' => $entityIds],
105 'options' => ['limit' => 0],
106 'return' => self
::getReturnFields($this->activeTokens
),
108 $prefetch['activity'] = $activities['values'];
110 // Store the activity types if needed
111 if (in_array('activity_type', $this->activeTokens
)) {
112 $this->activityTypes
= \CRM_Core_OptionGroup
::values('activity_type');
115 // Store the activity statuses if needed
116 if (in_array('status', $this->activeTokens
)) {
117 $this->activityStatuses
= \CRM_Core_OptionGroup
::values('activity_status');
120 // Store the campaigns if needed
121 if (in_array('campaign', $this->activeTokens
)) {
122 $this->campaigns
= \CRM_Campaign_BAO_Campaign
::getCampaigns();
131 public function evaluateToken(TokenRow
$row, $entity, $field, $prefetch = NULL) {
132 // maps token name to api field
134 'activity_id' => 'id',
137 // Get ActivityID either from actionSearchResult (for scheduled reminders) if exists
138 $activityId = $row->context
['actionSearchResult']->entityID ??
$row->context
[$this->getEntityContextSchema()];
140 $activity = $prefetch['activity'][$activityId];
142 if (in_array($field, ['activity_date_time', 'created_date', 'modified_date'])) {
143 $row->tokens($entity, $field, \CRM_Utils_Date
::customFormat($activity[$field]));
145 elseif (isset($mapping[$field]) and (isset($activity[$mapping[$field]]))) {
146 $row->tokens($entity, $field, $activity[$mapping[$field]]);
148 elseif (in_array($field, ['activity_type'])) {
149 $row->tokens($entity, $field, $this->activityTypes
[$activity['activity_type_id']]);
151 elseif (in_array($field, ['status'])) {
152 $row->tokens($entity, $field, $this->activityStatuses
[$activity['status_id']]);
154 elseif (in_array($field, ['campaign'])) {
155 $row->tokens($entity, $field, $this->campaigns
[$activity['campaign_id']]);
157 elseif (in_array($field, ['case_id'])) {
158 // An activity can be linked to multiple cases so case_id is always an array.
159 // We just return the first case ID for the token.
160 $row->tokens($entity, $field, is_array($activity['case_id']) ?
reset($activity['case_id']) : $activity['case_id']);
162 elseif (array_key_exists($field, $this->customFieldTokens
)) {
163 $row->tokens($entity, $field,
164 isset($activity[$field])
165 ? \CRM_Core_BAO_CustomField
::displayValue($activity[$field], $field)
169 elseif (isset($activity[$field])) {
170 $row->tokens($entity, $field, $activity[$field]);
175 * Get the basic tokens provided.
177 * @return array token name => token label
179 protected function getBasicTokens(): array {
180 if (!isset($this->basicTokens
)) {
181 $this->basicTokens
= [
182 'activity_id' => ts('Activity ID'),
183 'activity_type' => ts('Activity Type'),
184 'subject' => ts('Activity Subject'),
185 'details' => ts('Activity Details'),
186 'activity_date_time' => ts('Activity Date-Time'),
187 'created_date' => ts('Activity Created Date'),
188 'modified_date' => ts('Activity Modified Date'),
189 'activity_type_id' => ts('Activity Type ID'),
190 'status' => ts('Activity Status'),
191 'status_id' => ts('Activity Status ID'),
192 'location' => ts('Activity Location'),
193 'duration' => ts('Activity Duration'),
194 'campaign' => ts('Activity Campaign'),
195 'campaign_id' => ts('Activity Campaign ID'),
197 if (array_key_exists('CiviCase', CRM_Core_Component
::getEnabledComponents())) {
198 $this->basicTokens
['case_id'] = ts('Activity Case ID');
201 return $this->basicTokens
;