From 1a1375248ca085bcabd1b23766200ece3fafb96f Mon Sep 17 00:00:00 2001
From: Eileen McNaughton <emcnaughton@wikimedia.org>
Date: Mon, 25 Sep 2023 11:17:44 +1300
Subject: [PATCH] Fix event.location token to include supplemental address
 details, use in event receipts

---
 CRM/Event/Tokens.php                                      | 8 ++++++++
 tests/phpunit/CRM/Event/Form/ParticipantTest.php          | 5 +----
 tests/phpunit/CRM/Utils/TokenConsistencyTest.php          | 1 +
 .../message_templates/event_offline_receipt_html.tpl      | 1 -
 .../message_templates/event_offline_receipt_html.tpl      | 2 +-
 .../message_templates/event_offline_receipt_text.tpl      | 2 +-
 .../message_templates/event_online_receipt_html.tpl       | 2 +-
 .../message_templates/event_online_receipt_text.tpl       | 2 +-
 8 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/CRM/Event/Tokens.php b/CRM/Event/Tokens.php
index e7b9436f1c..6bc9db1aae 100644
--- a/CRM/Event/Tokens.php
+++ b/CRM/Event/Tokens.php
@@ -177,7 +177,11 @@ class CRM_Event_Tokens extends CRM_Core_EntityTokens {
     if (!Civi::cache('metadata')->has($cacheKey)) {
       $event = Event::get($this->checkPermissions)->addWhere('id', '=', $eventID)
         ->setSelect(array_merge([
+          'loc_block_id.address_id.name',
           'loc_block_id.address_id.street_address',
+          'loc_block_id.address_id.supplemental_address_1',
+          'loc_block_id.address_id.supplemental_address_2',
+          'loc_block_id.address_id.supplemental_address_3',
           'loc_block_id.address_id.city',
           'loc_block_id.address_id.state_province_id:label',
           'loc_block_id.address_id.postal_code',
@@ -206,7 +210,11 @@ class CRM_Event_Tokens extends CRM_Core_EntityTokens {
         ], $this->getExposedFields()))
         ->execute()->first();
       $tokens['location']['text/plain'] = \CRM_Utils_Address::format([
+        'address_name' => $event['loc_block_id.address_id.name'],
         'street_address' => $event['loc_block_id.address_id.street_address'],
+        'supplemental_address_1' => $event['loc_block_id.address_id.supplemental_address_1'],
+        'supplemental_address_2' => $event['loc_block_id.address_id.supplemental_address_2'],
+        'supplemental_address_3' => $event['loc_block_id.address_id.supplemental_address_3'],
         'city' => $event['loc_block_id.address_id.city'],
         'state_province' => $event['loc_block_id.address_id.state_province_id:label'],
         'postal_code' => $event['loc_block_id.address_id.postal_code'],
diff --git a/tests/phpunit/CRM/Event/Form/ParticipantTest.php b/tests/phpunit/CRM/Event/Form/ParticipantTest.php
index 44f17d43b0..12b13ea5cd 100644
--- a/tests/phpunit/CRM/Event/Form/ParticipantTest.php
+++ b/tests/phpunit/CRM/Event/Form/ParticipantTest.php
@@ -340,11 +340,8 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {
       'contribution.balance_amount|boolean:::No',
       'contribution.paid_amount|boolean:::Yes',
       '<p>Test event type - 1</p>event.location:8 Baker Street<br />
+Upstairs<br />
 London,',
-      '$location.address.1.display:<div class="location vcard"><span class="adr"><span class="street-address">8 Baker Street</span><br />
-<span class="extended-address">Upstairs</span><br />
-<span class="locality">London</span>,<br />
-</span></div>',
     ]);
 
     $this->callAPISuccess('Email', 'delete', ['id' => $email['id']]);
diff --git a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php
index 8a69080a6f..6d6bbeddec 100644
--- a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php
+++ b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php
@@ -715,6 +715,7 @@ event.loc_block_id.email_id.email :event@example.com
 event.loc_block_id.phone_id.phone :456 789
 event.description :event description
 event.location :15 Walton St<br />
+up the road<br />
 Emerald City, Maine 90210<br />
 event.info_url :' . CRM_Utils_System::url('civicrm/event/info', NULL, TRUE) . '&reset=1&id=1
 event.registration_url :' . CRM_Utils_System::url('civicrm/event/register', NULL, TRUE) . '&reset=1&id=1
diff --git a/tests/templates/message_templates/event_offline_receipt_html.tpl b/tests/templates/message_templates/event_offline_receipt_html.tpl
index 28d58687d4..927a948159 100644
--- a/tests/templates/message_templates/event_offline_receipt_html.tpl
+++ b/tests/templates/message_templates/event_offline_receipt_html.tpl
@@ -1,2 +1 @@
 event.location:{event.location}
-$location.address.1.display:{$location.address.1.display|smarty:nodefaults|nl2br}
diff --git a/xml/templates/message_templates/event_offline_receipt_html.tpl b/xml/templates/message_templates/event_offline_receipt_html.tpl
index 79f1b5293b..d1f58cc5be 100644
--- a/xml/templates/message_templates/event_offline_receipt_html.tpl
+++ b/xml/templates/message_templates/event_offline_receipt_html.tpl
@@ -67,7 +67,7 @@
      {if {event.is_show_location|boolean}}
       <tr>
        <td colspan="2" {$valueStyle}>
-        {$location.address.1.display|nl2br}
+         {event.location}
        </td>
       </tr>
      {/if}
diff --git a/xml/templates/message_templates/event_offline_receipt_text.tpl b/xml/templates/message_templates/event_offline_receipt_text.tpl
index 03de2f48bd..62bb785446 100644
--- a/xml/templates/message_templates/event_offline_receipt_text.tpl
+++ b/xml/templates/message_templates/event_offline_receipt_text.tpl
@@ -45,7 +45,7 @@
 {/if}
 
 {if !empty($isShowLocation)}
-{$location.address.1.display|strip_tags:false}
+{event.location}
 {/if}{*End of isShowLocation condition*}
 
 {if {event.loc_block_id.phone_id.phone|boolean} || {event.loc_block_id.email_id.email|boolean}}
diff --git a/xml/templates/message_templates/event_online_receipt_html.tpl b/xml/templates/message_templates/event_online_receipt_html.tpl
index fa44cb0588..efb34424cb 100644
--- a/xml/templates/message_templates/event_online_receipt_html.tpl
+++ b/xml/templates/message_templates/event_online_receipt_html.tpl
@@ -80,7 +80,7 @@
         {if !empty($isShowLocation)}
           <tr>
             <td colspan="2" {$valueStyle}>
-              {$location.address.1.display|nl2br}
+              {event.location}
             </td>
           </tr>
         {/if}
diff --git a/xml/templates/message_templates/event_online_receipt_text.tpl b/xml/templates/message_templates/event_online_receipt_text.tpl
index c17e7f971d..e2efd08b9e 100644
--- a/xml/templates/message_templates/event_online_receipt_text.tpl
+++ b/xml/templates/message_templates/event_online_receipt_text.tpl
@@ -55,7 +55,7 @@
 {/if}
 
 {if !empty($isShowLocation)}
-{$location.address.1.display|strip_tags:false}
+{event.location}
 {/if}{*End of isShowLocation condition*}
 
 {if {event.loc_block_id.phone_id.phone|boolean} || {event.loc_block_id.email_id.email|boolean}}
-- 
2.25.1