| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | Copyright CiviCRM LLC. All rights reserved. | |
| 6 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 11 | */ |
| 12 | |
| 13 | use Civi\Api4\Event; |
| 14 | use Civi\Token\TokenRow; |
| 15 | |
| 16 | /** |
| 17 | * Class CRM_Event_Tokens |
| 18 | * |
| 19 | * Generate "event.*" tokens. |
| 20 | * |
| 21 | * This TokenSubscriber was produced by refactoring the code from the |
| 22 | * scheduled-reminder system with the goal of making that system |
| 23 | * more flexible. The current implementation is still coupled to |
| 24 | * scheduled-reminders. It would be good to figure out a more generic |
| 25 | * implementation which is not tied to scheduled reminders, although |
| 26 | * that is outside the current scope. |
| 27 | */ |
| 28 | class CRM_Event_Tokens extends CRM_Core_EntityTokens { |
| 29 | |
| 30 | /** |
| 31 | * Get the entity name for api v4 calls. |
| 32 | * |
| 33 | * @return string |
| 34 | */ |
| 35 | protected function getApiEntityName(): string { |
| 36 | return 'Event'; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get all tokens. |
| 41 | * |
| 42 | * This function will be removed once the parent class can determine it. |
| 43 | */ |
| 44 | protected function getBespokeTokens(): array { |
| 45 | return [ |
| 46 | 'location' => [ |
| 47 | 'title' => ts('Event Location'), |
| 48 | 'name' => 'location', |
| 49 | 'type' => 'calculated', |
| 50 | 'options' => NULL, |
| 51 | 'data_type' => 'String', |
| 52 | 'audience' => 'user', |
| 53 | ], |
| 54 | 'info_url' => [ |
| 55 | 'title' => ts('Event Info URL'), |
| 56 | 'name' => 'info_url', |
| 57 | 'type' => 'calculated', |
| 58 | 'options' => NULL, |
| 59 | 'data_type' => 'String', |
| 60 | 'audience' => 'user', |
| 61 | ], |
| 62 | 'registration_url' => [ |
| 63 | 'title' => ts('Event Registration URL'), |
| 64 | 'name' => 'registration_url', |
| 65 | 'type' => 'calculated', |
| 66 | 'options' => NULL, |
| 67 | 'data_type' => 'String', |
| 68 | 'audience' => 'user', |
| 69 | ], |
| 70 | 'contact_email' => [ |
| 71 | 'title' => ts('Event Contact Email'), |
| 72 | 'name' => 'contact_email', |
| 73 | 'type' => 'calculated', |
| 74 | 'options' => NULL, |
| 75 | 'data_type' => 'String', |
| 76 | 'audience' => 'user', |
| 77 | ], |
| 78 | 'contact_phone' => [ |
| 79 | 'title' => ts('Event Contact Phone'), |
| 80 | 'name' => 'contact_phone', |
| 81 | 'type' => 'calculated', |
| 82 | 'options' => NULL, |
| 83 | 'data_type' => '', |
| 84 | 'audience' => 'user', |
| 85 | ], |
| 86 | ]; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @inheritDoc |
| 91 | * @throws \API_Exception |
| 92 | */ |
| 93 | public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) { |
| 94 | $eventID = $this->getFieldValue($row, 'id'); |
| 95 | if (array_key_exists($field, $this->getEventTokenValues($eventID))) { |
| 96 | foreach ($this->getEventTokenValues($eventID)[$field] as $format => $value) { |
| 97 | $row->format($format)->tokens($entity, $field, $value ?? ''); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the tokens available for the event. |
| 104 | * |
| 105 | * Cache by event as it's l |
| 106 | * |
| 107 | * @param int|null $eventID |
| 108 | * |
| 109 | * @return array |
| 110 | * |
| 111 | * @throws \API_Exception|\CRM_Core_Exception |
| 112 | * |
| 113 | * @internal |
| 114 | */ |
| 115 | protected function getEventTokenValues(int $eventID = NULL): array { |
| 116 | $cacheKey = __CLASS__ . 'event_tokens' . $eventID . '_' . CRM_Core_I18n::getLocale(); |
| 117 | if ($this->checkPermissions) { |
| 118 | $cacheKey .= '__' . CRM_Core_Session::getLoggedInContactID(); |
| 119 | } |
| 120 | if (!Civi::cache('metadata')->has($cacheKey)) { |
| 121 | $event = Event::get($this->checkPermissions)->addWhere('id', '=', $eventID) |
| 122 | ->setSelect(array_merge([ |
| 123 | 'loc_block_id.address_id.street_address', |
| 124 | 'loc_block_id.address_id.city', |
| 125 | 'loc_block_id.address_id.state_province_id:label', |
| 126 | 'loc_block_id.address_id.postal_code', |
| 127 | 'loc_block_id.email_id.email', |
| 128 | 'loc_block_id.phone_id.phone', |
| 129 | 'custom.*', |
| 130 | ], $this->getExposedFields())) |
| 131 | ->execute()->first(); |
| 132 | $tokens['location']['text/plain'] = \CRM_Utils_Address::format([ |
| 133 | 'street_address' => $event['loc_block_id.address_id.street_address'], |
| 134 | 'city' => $event['loc_block_id.address_id.city'], |
| 135 | 'state_province' => $event['loc_block_id.address_id.state_province_id:label'], |
| 136 | 'postal_code' => $event['loc_block_id.address_id.postal_code'], |
| 137 | |
| 138 | ]); |
| 139 | $tokens['info_url']['text/html'] = \CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE, TRUE); |
| 140 | $tokens['registration_url']['text/html'] = \CRM_Utils_System::url('civicrm/event/register', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE, TRUE); |
| 141 | $tokens['start_date']['text/html'] = !empty($event['start_date']) ? new DateTime($event['start_date']) : ''; |
| 142 | $tokens['end_date']['text/html'] = !empty($event['end_date']) ? new DateTime($event['end_date']) : ''; |
| 143 | $tokens['event_type_id:label']['text/html'] = CRM_Core_PseudoConstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']); |
| 144 | $tokens['event_type_id:name']['text/html'] = CRM_Core_PseudoConstant::getName('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']); |
| 145 | $tokens['contact_phone']['text/html'] = $event['loc_block_id.phone_id.phone']; |
| 146 | $tokens['contact_email']['text/html'] = $event['loc_block_id.email_id.email']; |
| 147 | |
| 148 | foreach ($this->getTokenMetadata() as $fieldName => $fieldSpec) { |
| 149 | if (!isset($tokens[$fieldName])) { |
| 150 | if ($fieldSpec['type'] === 'Custom') { |
| 151 | $this->prefetch[$eventID] = $event; |
| 152 | $value = $event[$fieldSpec['name']]; |
| 153 | $tokens[$fieldName]['text/html'] = CRM_Core_BAO_CustomField::displayValue($value, $fieldSpec['custom_field_id']); |
| 154 | } |
| 155 | else { |
| 156 | $tokens[$fieldName]['text/html'] = $event[$fieldName]; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | Civi::cache('metadata')->set($cacheKey, $tokens); |
| 161 | } |
| 162 | return Civi::cache('metadata')->get($cacheKey); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Get entity fields that should be exposed as tokens. |
| 167 | * |
| 168 | * Event has traditionally exposed very few fields. This is probably because |
| 169 | * a) there are a tonne of weird fields so an opt out approach doesn't work and |
| 170 | * b) so people just added what they needed at the time... |
| 171 | * |
| 172 | * @return string[] |
| 173 | * |
| 174 | */ |
| 175 | protected function getExposedFields(): array { |
| 176 | return [ |
| 177 | 'event_type_id', |
| 178 | 'title', |
| 179 | 'id', |
| 180 | 'pay_later_receipt', |
| 181 | 'start_date', |
| 182 | 'end_date', |
| 183 | 'summary', |
| 184 | 'description', |
| 185 | ]; |
| 186 | } |
| 187 | |
| 188 | } |