Commit | Line | Data |
---|---|---|
46f5566c TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
46f5566c | 6 | | | |
bc77d7c0 TO |
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 | | |
46f5566c TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
7808aae6 SB |
13 | /** |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
7808aae6 SB |
16 | */ |
17 | ||
46f5566c TO |
18 | /** |
19 | * Class CRM_Member_Tokens | |
20 | * | |
21 | * Generate "activity.*" tokens. | |
22 | * | |
da9977bd | 23 | * This TokenSubscriber was originally produced by refactoring the code from the |
46f5566c TO |
24 | * scheduled-reminder system with the goal of making that system |
25 | * more flexible. The current implementation is still coupled to | |
26 | * scheduled-reminders. It would be good to figure out a more generic | |
27 | * implementation which is not tied to scheduled reminders, although | |
28 | * that is outside the current scope. | |
da9977bd AS |
29 | * |
30 | * This has been enhanced to work with PDF/letter merge | |
46f5566c TO |
31 | */ |
32 | class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber { | |
33 | ||
88d88c53 | 34 | use CRM_Core_TokenTrait; |
da9977bd AS |
35 | |
36 | /** | |
88d88c53 | 37 | * @return string |
da9977bd | 38 | */ |
88d88c53 MW |
39 | private function getEntityName(): string { |
40 | return 'activity'; | |
41 | } | |
da9977bd | 42 | |
8246bca4 | 43 | /** |
88d88c53 | 44 | * @return string |
8246bca4 | 45 | */ |
88d88c53 MW |
46 | private function getEntityTableName(): string { |
47 | return 'civicrm_activity'; | |
46f5566c TO |
48 | } |
49 | ||
70599df6 | 50 | /** |
88d88c53 | 51 | * @return string |
70599df6 | 52 | */ |
51e25a23 | 53 | private function getEntityContextSchema(): string { |
54 | return 'activityId'; | |
da9977bd AS |
55 | } |
56 | ||
57 | /** | |
88d88c53 | 58 | * Mapping from tokenName to api return field |
f10c962e MW |
59 | * Using arrays allows more complex tokens to be handled that require more than one API field. |
60 | * For example, an address token might want ['street_address', 'city', 'postal_code'] | |
88d88c53 MW |
61 | * |
62 | * @var array | |
da9977bd | 63 | */ |
88d88c53 MW |
64 | private static $fieldMapping = [ |
65 | 'activity_id' => ['id'], | |
66 | 'activity_type' => ['activity_type_id'], | |
67 | 'status' => ['status_id'], | |
68 | 'campaign' => ['campaign_id'], | |
69 | ]; | |
46f5566c | 70 | |
298795cd TO |
71 | /** |
72 | * @inheritDoc | |
73 | */ | |
f9ec2da6 | 74 | public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) { |
88d88c53 | 75 | if ($e->mapping->getEntity() !== $this->getEntityTableName()) { |
f9ec2da6 TO |
76 | return; |
77 | } | |
78 | ||
7808aae6 SB |
79 | // The joint expression for activities needs some extra nuance to handle. |
80 | // Multiple revisions of the activity. | |
81 | // Q: Could we simplify & move the extra AND clauses into `where(...)`? | |
f9ec2da6 | 82 | $e->query->param('casEntityJoinExpr', 'e.id = reminder.entity_id AND e.is_current_revision = 1 AND e.is_deleted = 0'); |
da9977bd | 83 | } |
f9ec2da6 | 84 | |
da9977bd AS |
85 | /** |
86 | * @inheritDoc | |
87 | */ | |
88 | public function prefetch(\Civi\Token\Event\TokenValueEvent $e) { | |
88d88c53 MW |
89 | // Find all the entity IDs |
90 | $entityIds | |
da9977bd | 91 | = $e->getTokenProcessor()->getContextValues('actionSearchResult', 'entityID') |
51e25a23 | 92 | + $e->getTokenProcessor()->getContextValues($this->getEntityContextSchema()); |
da9977bd | 93 | |
88d88c53 MW |
94 | if (!$entityIds) { |
95 | return NULL; | |
da9977bd AS |
96 | } |
97 | ||
98 | // Get data on all activities for basic and customfield tokens | |
99 | $activities = civicrm_api3('Activity', 'get', [ | |
88d88c53 | 100 | 'id' => ['IN' => $entityIds], |
da9977bd AS |
101 | 'options' => ['limit' => 0], |
102 | 'return' => self::getReturnFields($this->activeTokens), | |
103 | ]); | |
104 | $prefetch['activity'] = $activities['values']; | |
105 | ||
106 | // Store the activity types if needed | |
107 | if (in_array('activity_type', $this->activeTokens)) { | |
108 | $this->activityTypes = \CRM_Core_OptionGroup::values('activity_type'); | |
109 | } | |
110 | ||
111 | // Store the activity statuses if needed | |
112 | if (in_array('status', $this->activeTokens)) { | |
113 | $this->activityStatuses = \CRM_Core_OptionGroup::values('activity_status'); | |
114 | } | |
115 | ||
116 | // Store the campaigns if needed | |
117 | if (in_array('campaign', $this->activeTokens)) { | |
118 | $this->campaigns = \CRM_Campaign_BAO_Campaign::getCampaigns(); | |
f9ec2da6 | 119 | } |
da9977bd AS |
120 | |
121 | return $prefetch; | |
f9ec2da6 TO |
122 | } |
123 | ||
46f5566c | 124 | /** |
298795cd | 125 | * @inheritDoc |
46f5566c TO |
126 | */ |
127 | public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) { | |
da9977bd AS |
128 | // maps token name to api field |
129 | $mapping = [ | |
130 | 'activity_id' => 'id', | |
131 | ]; | |
132 | ||
133 | // Get ActivityID either from actionSearchResult (for scheduled reminders) if exists | |
51e25a23 | 134 | $activityId = $row->context['actionSearchResult']->entityID ?? $row->context[$this->getEntityContextSchema()]; |
46f5566c | 135 | |
f10c962e | 136 | $activity = $prefetch['activity'][$activityId]; |
da9977bd | 137 | |
c3317f50 | 138 | if (in_array($field, ['activity_date_time', 'created_date', 'modified_date'])) { |
f10c962e | 139 | $row->tokens($entity, $field, \CRM_Utils_Date::customFormat($activity[$field])); |
da9977bd | 140 | } |
f10c962e MW |
141 | elseif (isset($mapping[$field]) and (isset($activity[$mapping[$field]]))) { |
142 | $row->tokens($entity, $field, $activity[$mapping[$field]]); | |
46f5566c | 143 | } |
da9977bd | 144 | elseif (in_array($field, ['activity_type'])) { |
f10c962e | 145 | $row->tokens($entity, $field, $this->activityTypes[$activity['activity_type_id']]); |
46f5566c | 146 | } |
da9977bd | 147 | elseif (in_array($field, ['status'])) { |
f10c962e | 148 | $row->tokens($entity, $field, $this->activityStatuses[$activity['status_id']]); |
4e9b6a62 | 149 | } |
da9977bd | 150 | elseif (in_array($field, ['campaign'])) { |
f10c962e | 151 | $row->tokens($entity, $field, $this->campaigns[$activity['campaign_id']]); |
da9977bd | 152 | } |
28f7a9b1 MW |
153 | elseif (in_array($field, ['case_id'])) { |
154 | // An activity can be linked to multiple cases so case_id is always an array. | |
155 | // We just return the first case ID for the token. | |
f10c962e | 156 | $row->tokens($entity, $field, is_array($activity['case_id']) ? reset($activity['case_id']) : $activity['case_id']); |
28f7a9b1 | 157 | } |
da9977bd AS |
158 | elseif (array_key_exists($field, $this->customFieldTokens)) { |
159 | $row->tokens($entity, $field, | |
f10c962e MW |
160 | isset($activity[$field]) |
161 | ? \CRM_Core_BAO_CustomField::displayValue($activity[$field], $field) | |
da9977bd AS |
162 | : '' |
163 | ); | |
164 | } | |
f10c962e MW |
165 | elseif (isset($activity[$field])) { |
166 | $row->tokens($entity, $field, $activity[$field]); | |
46f5566c TO |
167 | } |
168 | } | |
169 | ||
86420016 | 170 | /** |
171 | * Get the basic tokens provided. | |
172 | * | |
173 | * @return array token name => token label | |
174 | */ | |
175 | protected function getBasicTokens() { | |
da9977bd AS |
176 | if (!isset($this->basicTokens)) { |
177 | $this->basicTokens = [ | |
178 | 'activity_id' => ts('Activity ID'), | |
179 | 'activity_type' => ts('Activity Type'), | |
180 | 'subject' => ts('Activity Subject'), | |
181 | 'details' => ts('Activity Details'), | |
182 | 'activity_date_time' => ts('Activity Date-Time'), | |
c3317f50 MW |
183 | 'created_date' => ts('Activity Created Date'), |
184 | 'modified_date' => ts('Activity Modified Date'), | |
da9977bd AS |
185 | 'activity_type_id' => ts('Activity Type ID'), |
186 | 'status' => ts('Activity Status'), | |
187 | 'status_id' => ts('Activity Status ID'), | |
188 | 'location' => ts('Activity Location'), | |
da9977bd AS |
189 | 'duration' => ts('Activity Duration'), |
190 | 'campaign' => ts('Activity Campaign'), | |
191 | 'campaign_id' => ts('Activity Campaign ID'), | |
192 | ]; | |
193 | if (array_key_exists('CiviCase', CRM_Core_Component::getEnabledComponents())) { | |
194 | $this->basicTokens['case_id'] = ts('Activity Case ID'); | |
195 | } | |
196 | } | |
197 | return $this->basicTokens; | |
86420016 | 198 | } |
199 | ||
46f5566c | 200 | } |