Merge pull request #22955 from colemanw/schemaTrait
[civicrm-core.git] / CRM / Event / Tokens.php
index 061e7197faf287649d40de246c337edb5609203c..5227fd62d02fe4f4d740c86ae24abeb2ca416894 100644 (file)
@@ -41,24 +41,49 @@ class CRM_Event_Tokens extends CRM_Core_EntityTokens {
    *
    * This function will be removed once the parent class can determine it.
    */
-  public function getAllTokens(): array {
-    return array_merge(
-      [
-        'event_type_id:label' => ts('Event Type'),
-        'title' => ts('Event Title'),
-        'id' => ts('Event ID'),
-        'start_date' => ts('Event Start Date'),
-        'end_date' => ts('Event End Date'),
-        'summary' => ts('Event Summary'),
-        'description' => ts('Event Description'),
-        'location' => ts('Event Location'),
-        'info_url' => ts('Event Info URL'),
-        'registration_url' => ts('Event Registration URL'),
-        'contact_email' => ts('Event Contact Email'),
-        'contact_phone' => ts('Event Contact Phone'),
+  protected function getBespokeTokens(): array {
+    return [
+      'location' => [
+        'title' => ts('Event Location'),
+        'name' => 'location',
+        'type' => 'calculated',
+        'options' => NULL,
+        'data_type' => 'String',
+        'audience' => 'user',
       ],
-      CRM_Utils_Token::getCustomFieldTokens('Event')
-    );
+      'info_url' => [
+        'title' => ts('Event Info URL'),
+        'name' => 'info_url',
+        'type' => 'calculated',
+        'options' => NULL,
+        'data_type' => 'String',
+        'audience' => 'user',
+      ],
+      'registration_url' => [
+        'title' => ts('Event Registration URL'),
+        'name' => 'registration_url',
+        'type' => 'calculated',
+        'options' => NULL,
+        'data_type' => 'String',
+        'audience' => 'user',
+      ],
+      'contact_email' => [
+        'title' => ts('Event Contact Email'),
+        'name' => 'contact_email',
+        'type' => 'calculated',
+        'options' => NULL,
+        'data_type' => 'String',
+        'audience' => 'user',
+      ],
+      'contact_phone' => [
+        'title' => ts('Event Contact Phone'),
+        'name' => 'contact_phone',
+        'type' => 'calculated',
+        'options' => NULL,
+        'data_type' => '',
+        'audience' => 'user',
+      ],
+    ];
   }
 
   /**
@@ -67,12 +92,9 @@ class CRM_Event_Tokens extends CRM_Core_EntityTokens {
    */
   public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) {
     $eventID = $this->getFieldValue($row, 'id');
-    if (!$eventID) {
-      $eventID = $row->context['actionSearchResult']->event_id;
-    }
     if (array_key_exists($field, $this->getEventTokenValues($eventID))) {
       foreach ($this->getEventTokenValues($eventID)[$field] as $format => $value) {
-        $row->format($format)->tokens($entity, $field, $value);
+        $row->format($format)->tokens($entity, $field, $value ?? '');
       }
     }
   }
@@ -92,17 +114,12 @@ class CRM_Event_Tokens extends CRM_Core_EntityTokens {
    */
   protected function getEventTokenValues(int $eventID = NULL): array {
     $cacheKey = __CLASS__ . 'event_tokens' . $eventID . '_' . CRM_Core_I18n::getLocale();
+    if ($this->checkPermissions) {
+      $cacheKey .= '__' . CRM_Core_Session::getLoggedInContactID();
+    }
     if (!Civi::cache('metadata')->has($cacheKey)) {
-      $event = Event::get(FALSE)->addWhere('id', '=', $eventID)
-        ->setSelect([
-          'event_type_id',
-          'title',
-          'id',
-          'start_date',
-          'end_date',
-          'summary',
-          'description',
-          'loc_block_id',
+      $event = Event::get($this->checkPermissions)->addWhere('id', '=', $eventID)
+        ->setSelect(array_merge([
           'loc_block_id.address_id.street_address',
           'loc_block_id.address_id.city',
           'loc_block_id.address_id.state_province_id:label',
@@ -110,7 +127,7 @@ class CRM_Event_Tokens extends CRM_Core_EntityTokens {
           'loc_block_id.email_id.email',
           'loc_block_id.phone_id.phone',
           'custom.*',
-        ])
+        ], $this->getExposedFields()))
         ->execute()->first();
       $tokens['location']['text/plain'] = \CRM_Utils_Address::format([
         'street_address' => $event['loc_block_id.address_id.street_address'],
@@ -124,17 +141,19 @@ class CRM_Event_Tokens extends CRM_Core_EntityTokens {
       $tokens['start_date']['text/html'] = !empty($event['start_date']) ? new DateTime($event['start_date']) : '';
       $tokens['end_date']['text/html'] = !empty($event['end_date']) ? new DateTime($event['end_date']) : '';
       $tokens['event_type_id:label']['text/html'] = CRM_Core_PseudoConstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']);
+      $tokens['event_type_id:name']['text/html'] = CRM_Core_PseudoConstant::getName('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']);
       $tokens['contact_phone']['text/html'] = $event['loc_block_id.phone_id.phone'];
       $tokens['contact_email']['text/html'] = $event['loc_block_id.email_id.email'];
 
-      foreach (array_keys($this->getAllTokens()) as $field) {
-        if (!isset($tokens[$field])) {
-          if ($this->isCustomField($field)) {
+      foreach ($this->getTokenMetadata() as $fieldName => $fieldSpec) {
+        if (!isset($tokens[$fieldName])) {
+          if ($fieldSpec['type'] === 'Custom') {
             $this->prefetch[$eventID] = $event;
-            $tokens[$field]['text/html'] = $this->getCustomFieldValue($eventID, $field);
+            $value = $event[$fieldSpec['name']];
+            $tokens[$fieldName]['text/html'] = CRM_Core_BAO_CustomField::displayValue($value, $fieldSpec['custom_field_id']);
           }
           else {
-            $tokens[$field]['text/html'] = $event[$field];
+            $tokens[$fieldName]['text/html'] = $event[$fieldName];
           }
         }
       }
@@ -143,4 +162,27 @@ class CRM_Event_Tokens extends CRM_Core_EntityTokens {
     return Civi::cache('metadata')->get($cacheKey);
   }
 
+  /**
+   * Get entity fields that should be exposed as tokens.
+   *
+   * Event has traditionally exposed very few fields. This is probably because
+   * a) there are a tonne of weird fields so an opt out approach doesn't work and
+   * b) so people just added what they needed at the time...
+   *
+   * @return string[]
+   *
+   */
+  protected function getExposedFields(): array {
+    return [
+      'event_type_id',
+      'title',
+      'id',
+      'pay_later_receipt',
+      'start_date',
+      'end_date',
+      'summary',
+      'description',
+    ];
+  }
+
 }