From 1daa1b50a236f8c7dcf10e35cad66b0f96a7812b Mon Sep 17 00:00:00 2001
From: Bradley Taylor <hello@brad-taylor.co.uk>
Date: Tue, 8 Feb 2022 21:36:01 +0000
Subject: [PATCH] dev/user-interface#45 Show associated participants on
 contributionView form

---
 CRM/Contribute/Form/ContributionView.php      | 23 +++++++++++++++++++
 .../CRM/Contribute/Form/ContributionView.tpl  |  8 +++++++
 ...ContributionViewAssociatedParticipants.tpl | 16 +++++++++++++
 3 files changed, 47 insertions(+)
 create mode 100644 templates/CRM/Contribute/Form/ContributionViewAssociatedParticipants.tpl

diff --git a/CRM/Contribute/Form/ContributionView.php b/CRM/Contribute/Form/ContributionView.php
index 64f4c1473c8..b711e8bd02d 100644
--- a/CRM/Contribute/Form/ContributionView.php
+++ b/CRM/Contribute/Form/ContributionView.php
@@ -94,6 +94,29 @@ class CRM_Contribute_Form_ContributionView extends CRM_Core_Form {
       }
     }
 
+    $participantLineItems = \Civi\Api4\LineItem::get()
+      ->addSelect('entity_id', 'participant.role_id:label', 'participant.fee_level', 'participant.contact_id', 'contact.display_name')
+      ->addJoin('Participant AS participant', 'LEFT', ['participant.id', '=', 'entity_id'])
+      ->addJoin('Contact AS contact', 'LEFT', ['contact.id', '=', 'participant.contact_id'])
+      ->addWhere('entity_table', '=', 'civicrm_participant')
+      ->addWhere('contribution_id', '=', $id)
+      ->execute();
+
+    $associatedParticipants = FALSE;
+    if ($participantLineItems->count()) {
+      foreach ($participantLineItems as $participant) {
+        $associatedParticipants[] = [
+          'participantLink' => CRM_Utils_System::url('civicrm/contact/view/participant',
+            "action=view&reset=1&id={$participant['entity_id']}&cid={$participant['participant.contact_id']}&context=home"
+          ),
+          'participantName' => $participant['contact.display_name'],
+          'fee' => implode(', ', $participant['participant.fee_level']),
+          'role' => implode(', ', $participant['participant.role_id:label']),
+        ];
+      }
+    }
+    $this->assign('associatedParticipants', $associatedParticipants);
+
     $groupTree = CRM_Core_BAO_CustomGroup::getTree('Contribution', NULL, $id, 0, $values['financial_type_id'] ?? NULL,
       NULL, TRUE, NULL, FALSE, CRM_Core_Permission::VIEW);
     CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $id);
diff --git a/templates/CRM/Contribute/Form/ContributionView.tpl b/templates/CRM/Contribute/Form/ContributionView.tpl
index 751cb143869..c3b0f1e2272 100644
--- a/templates/CRM/Contribute/Form/ContributionView.tpl
+++ b/templates/CRM/Contribute/Form/ContributionView.tpl
@@ -87,6 +87,14 @@
         {/if}
     </td>
   </tr>
+  {if $associatedParticipants}
+    <tr>
+      <td class="label">{ts}Associated participants{/ts}</td>
+      <td>
+        {include file="CRM/Contribute/Form/ContributionViewAssociatedParticipants.tpl" associatedParticipants=$associatedParticipants}
+      </td>
+    </tr>
+  {/if}
   {if $invoicing && $tax_amount}
     <tr>
       <td class="label">{ts 1=$taxTerm}Total %1 Amount{/ts}</td>
diff --git a/templates/CRM/Contribute/Form/ContributionViewAssociatedParticipants.tpl b/templates/CRM/Contribute/Form/ContributionViewAssociatedParticipants.tpl
new file mode 100644
index 00000000000..77044748358
--- /dev/null
+++ b/templates/CRM/Contribute/Form/ContributionViewAssociatedParticipants.tpl
@@ -0,0 +1,16 @@
+<table>
+  <tbody>
+    <tr class="columnheader">
+      <th>{ts}Participant{/ts}</th>
+      <th>{ts}Role{/ts}</th>
+      <th>{ts}Fee{/ts}</th>
+    </tr>
+    {foreach from=$associatedParticipants item="participant"}
+      <tr>
+        <td><a href='{$participant.participantLink}'>{$participant.participantName|escape}</a></td></td>
+        <td>{$participant.role|escape}</td>
+        <td>{$participant.fee|escape}</td>
+      </tr>
+    {/foreach}
+  </tbody>
+</table>
-- 
GitLab