Merge pull request #23075 from civicrm/5.48
[civicrm-core.git] / CRM / Event / Form / ParticipantView.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * This class generates form components for Participant
20 *
21 */
22class CRM_Event_Form_ParticipantView extends CRM_Core_Form {
23
96f50de2
CW
24 public $useLivePageJS = TRUE;
25
6a488035 26 /**
fe482240 27 * Set variables up before form is built.
6a488035
TO
28 *
29 * @return void
6a488035
TO
30 */
31 public function preProcess() {
be2fb01f 32 $values = $ids = [];
6a488035 33 $participantID = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
353ffa53 34 $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
be2fb01f 35 $params = ['id' => $participantID];
6a488035
TO
36
37 CRM_Event_BAO_Participant::getValues($params,
38 $values,
39 $ids
40 );
41
42 if (empty($values)) {
43 CRM_Core_Error::statusBounce(ts('The requested participant record does not exist (possibly the record was deleted).'));
44 }
45
46 CRM_Event_BAO_Participant::resolveDefaults($values[$participantID]);
47
a7488080 48 if (!empty($values[$participantID]['fee_level'])) {
6a488035
TO
49 CRM_Event_BAO_Participant::fixEventLevel($values[$participantID]['fee_level']);
50 }
51
fbc54416
PJ
52 $this->assign('contactId', $contactID);
53 $this->assign('participantId', $participantID);
0b2b58ea
PJ
54
55 $paymentId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment',
56 $participantID, 'id', 'participant_id'
57 );
58 $this->assign('hasPayment', $paymentId);
685dc433
PN
59 $this->assign('componentId', $participantID);
60 $this->assign('component', 'event');
0b2b58ea 61
28e4e707 62 if ($parentParticipantId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant',
353ffa53
TO
63 $participantID, 'registered_by_id'
64 )
65 ) {
28e4e707
PJ
66 $parentHasPayment = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment',
67 $parentParticipantId, 'id', 'participant_id'
68 );
69 $this->assign('parentHasPayment', $parentHasPayment);
70 }
71
fbc54416 72 $statusId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $participantID, 'status_id', 'id');
97d8187a 73 $status = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantStatusType', $statusId, 'name', 'id');
97d8187a
DG
74 if ($status == 'Transferred') {
75 $transferId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $participantID, 'transferred_to_contact_id', 'id');
76 $pid = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $transferId, 'id', 'contact_id');
77 $transferName = current(CRM_Contact_BAO_Contact::getContactDetails($transferId));
78 $this->assign('pid', $pid);
79 $this->assign('transferId', $transferId);
80 $this->assign('transferName', $transferName);
81 }
fbc54416 82
e219d53b 83 // CRM-20879: Show 'Transfer or Cancel' option beside 'Change fee selection'
84 // only if logged in user have 'edit event participants' permission and
85 // participant status is not Cancelled or Transferred
be2fb01f 86 if (CRM_Core_Permission::check('edit event participants') && !in_array($status, ['Cancelled', 'Transferred'])) {
e219d53b 87 $this->assign('transferOrCancelLink',
88 CRM_Utils_System::url(
89 'civicrm/event/selfsvcupdate',
be2fb01f 90 [
e219d53b 91 'reset' => 1,
92 'is_backoffice' => 1,
93 'pid' => $participantID,
94 'cs' => CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, NULL, 'inf'),
be2fb01f 95 ]
e219d53b 96 )
97 );
98 }
99
6a488035 100 if ($values[$participantID]['is_test']) {
cd355351 101 $values[$participantID]['status'] = CRM_Core_TestEntity::appendTestText($values[$participantID]['status']);
6a488035
TO
102 }
103
104 // Get Note
105 $noteValue = CRM_Core_BAO_Note::getNote($participantID, 'civicrm_participant');
106
107 $values[$participantID]['note'] = array_values($noteValue);
108
6a488035
TO
109 // Get Line Items
110 $lineItem = CRM_Price_BAO_LineItem::getLineItems($participantID);
111
112 if (!CRM_Utils_System::isNull($lineItem)) {
113 $values[$participantID]['lineItem'][] = $lineItem;
114 }
115
9c1bc317 116 $values[$participantID]['totalAmount'] = $values[$participantID]['fee_amount'] ?? NULL;
6a488035
TO
117
118 // Get registered_by contact ID and display_name if participant was registered by someone else (CRM-4859)
a7488080 119 if (!empty($values[$participantID]['participant_registered_by_id'])) {
6a488035
TO
120 $values[$participantID]['registered_by_contact_id'] = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Participant",
121 $values[$participantID]['participant_registered_by_id'],
122 'contact_id', 'id'
123 );
124 $values[$participantID]['registered_by_display_name'] = CRM_Contact_BAO_Contact::displayName($values[$participantID]['registered_by_contact_id']);
125 }
126
127 // Check if this is a primaryParticipant (registered for others) and retrieve additional participants if true (CRM-4859)
128 if (CRM_Event_BAO_Participant::isPrimaryParticipant($participantID)) {
129 $values[$participantID]['additionalParticipants'] = CRM_Event_BAO_Participant::getAdditionalParticipants($participantID);
130 }
131
132 // get the option value for custom data type
c460c196
PN
133 $customDataType = CRM_Core_OptionGroup::values('custom_data_type', FALSE, FALSE, FALSE, NULL, 'name');
134 $roleCustomDataTypeID = array_search('ParticipantRole', $customDataType);
135 $eventNameCustomDataTypeID = array_search('ParticipantEventName', $customDataType);
136 $eventTypeCustomDataTypeID = array_search('ParticipantEventType', $customDataType);
6a488035 137 $allRoleIDs = explode(CRM_Core_DAO::VALUE_SEPARATOR, $values[$participantID]['role_id']);
be2fb01f 138 $finalTree = [];
6a488035
TO
139
140 foreach ($allRoleIDs as $k => $v) {
317103ab
CW
141 $roleGroupTree = CRM_Core_BAO_CustomGroup::getTree('Participant', NULL, $participantID, NULL, $v, $roleCustomDataTypeID,
142 TRUE, NULL, FALSE, CRM_Core_Permission::VIEW);
0b330e6d 143 $eventGroupTree = CRM_Core_BAO_CustomGroup::getTree('Participant', NULL, $participantID, NULL,
317103ab
CW
144 $values[$participantID]['event_id'], $eventNameCustomDataTypeID,
145 TRUE, NULL, FALSE, CRM_Core_Permission::VIEW
6a488035 146 );
353ffa53 147 $eventTypeID = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $values[$participantID]['event_id'], 'event_type_id', 'id');
317103ab
CW
148 $eventTypeGroupTree = CRM_Core_BAO_CustomGroup::getTree('Participant', NULL, $participantID, NULL, $eventTypeID, $eventTypeCustomDataTypeID,
149 TRUE, NULL, FALSE, CRM_Core_Permission::VIEW);
150 $participantGroupTree = CRM_Core_BAO_CustomGroup::getTree('Participant', NULL, $participantID, NULL, [], NULL,
151 TRUE, NULL, FALSE, CRM_Core_Permission::VIEW);
353ffa53
TO
152 $groupTree = CRM_Utils_Array::crmArrayMerge($roleGroupTree, $eventGroupTree);
153 $groupTree = CRM_Utils_Array::crmArrayMerge($groupTree, $eventTypeGroupTree);
317103ab 154 $groupTree = CRM_Utils_Array::crmArrayMerge($groupTree, $participantGroupTree);
6a488035
TO
155 foreach ($groupTree as $treeId => $trees) {
156 $finalTree[$treeId] = $trees;
157 }
158 }
e34e6979 159 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $finalTree, FALSE, NULL, NULL, NULL, $participantID);
6a488035
TO
160 $eventTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $values[$participantID]['event_id'], 'title');
161 //CRM-7150, show event name on participant view even if the event is disabled
a7488080 162 if (empty($values[$participantID]['event'])) {
6a488035
TO
163 $values[$participantID]['event'] = $eventTitle;
164 }
165
166 //do check for campaigns
167 if ($campaignId = CRM_Utils_Array::value('campaign_id', $values[$participantID])) {
168 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId);
169 $values[$participantID]['campaign'] = $campaigns[$campaignId];
170 }
171
172 $this->assign($values[$participantID]);
173
174 // add viewed participant to recent items list
175 $url = CRM_Utils_System::url('civicrm/contact/view/participant',
176 "action=view&reset=1&id={$values[$participantID]['id']}&cid={$values[$participantID]['contact_id']}&context=home"
177 );
178
be2fb01f 179 $recentOther = [];
6a488035
TO
180 if (CRM_Core_Permission::check('edit event participants')) {
181 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/participant',
182 "action=update&reset=1&id={$values[$participantID]['id']}&cid={$values[$participantID]['contact_id']}&context=home"
183 );
184 }
185 if (CRM_Core_Permission::check('delete in CiviEvent')) {
186 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/participant',
187 "action=delete&reset=1&id={$values[$participantID]['id']}&cid={$values[$participantID]['contact_id']}&context=home"
188 );
189 }
190
191 $participantRoles = CRM_Event_PseudoConstant::participantRole();
192 $displayName = CRM_Contact_BAO_Contact::displayName($values[$participantID]['contact_id']);
193
be2fb01f 194 $participantCount = [];
a32709be 195 $totalTaxAmount = 0;
6a488035
TO
196 foreach ($lineItem as $k => $v) {
197 if (CRM_Utils_Array::value('participant_count', $lineItem[$k]) > 0) {
aa4bb8d2 198 $participantCount[] = $lineItem[$k]['participant_count'];
6a488035 199 }
a32709be 200 $totalTaxAmount = $v['tax_amount'] + $totalTaxAmount;
6a488035 201 }
e9b4319f 202 if (Civi::settings()->get('invoicing')) {
03b412ae
PB
203 $this->assign('totalTaxAmount', $totalTaxAmount);
204 }
6a488035
TO
205 if ($participantCount) {
206 $this->assign('pricesetFieldsCount', $participantCount);
207 }
208 $this->assign('displayName', $displayName);
209 // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container
94fd9d17 210 $this->setTitle(ts('View Event Registration for') . ' ' . $displayName);
6a488035 211
9c1bc317 212 $roleId = $values[$participantID]['role_id'] ?? NULL;
6a488035
TO
213 $title = $displayName . ' (' . CRM_Utils_Array::value($roleId, $participantRoles) . ' - ' . $eventTitle . ')';
214
215 $sep = CRM_Core_DAO::VALUE_SEPARATOR;
be2fb01f 216 $viewRoles = [];
6a488035
TO
217 foreach (explode($sep, $values[$participantID]['role_id']) as $k => $v) {
218 $viewRoles[] = $participantRoles[$v];
219 }
220 $values[$participantID]['role_id'] = implode(', ', $viewRoles);
221 $this->assign('role', $values[$participantID]['role_id']);
222 // add Participant to Recent Items
223 CRM_Utils_Recent::add($title,
224 $url,
225 $values[$participantID]['id'],
226 'Participant',
227 $values[$participantID]['contact_id'],
228 NULL,
229 $recentOther
230 );
231 }
232
233 /**
fe482240 234 * Build the form object.
6a488035 235 *
355ba699 236 * @return void
6a488035
TO
237 */
238 public function buildQuickForm() {
be2fb01f 239 $this->addButtons([
90b461f1
SL
240 [
241 'type' => 'cancel',
242 'name' => ts('Done'),
243 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
244 'isDefault' => TRUE,
245 ],
246 ]);
6a488035 247 }
96025800 248
6a488035 249}