Merge pull request #15870 from eileenmcnaughton/init_set
[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 * $Id$
17 *
18 */
19
20/**
21 * This class generates form components for Participant
22 *
23 */
24class CRM_Event_Form_ParticipantView extends CRM_Core_Form {
25
96f50de2
CW
26 public $useLivePageJS = TRUE;
27
6a488035 28 /**
fe482240 29 * Set variables up before form is built.
6a488035
TO
30 *
31 * @return void
6a488035
TO
32 */
33 public function preProcess() {
be2fb01f 34 $values = $ids = [];
6a488035 35 $participantID = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
353ffa53 36 $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
be2fb01f 37 $params = ['id' => $participantID];
6a488035
TO
38
39 CRM_Event_BAO_Participant::getValues($params,
40 $values,
41 $ids
42 );
43
44 if (empty($values)) {
45 CRM_Core_Error::statusBounce(ts('The requested participant record does not exist (possibly the record was deleted).'));
46 }
47
48 CRM_Event_BAO_Participant::resolveDefaults($values[$participantID]);
49
a7488080 50 if (!empty($values[$participantID]['fee_level'])) {
6a488035
TO
51 CRM_Event_BAO_Participant::fixEventLevel($values[$participantID]['fee_level']);
52 }
53
fbc54416
PJ
54 $this->assign('contactId', $contactID);
55 $this->assign('participantId', $participantID);
0b2b58ea
PJ
56
57 $paymentId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment',
58 $participantID, 'id', 'participant_id'
59 );
60 $this->assign('hasPayment', $paymentId);
685dc433
PN
61 $this->assign('componentId', $participantID);
62 $this->assign('component', 'event');
0b2b58ea 63
28e4e707 64 if ($parentParticipantId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant',
353ffa53
TO
65 $participantID, 'registered_by_id'
66 )
67 ) {
28e4e707
PJ
68 $parentHasPayment = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment',
69 $parentParticipantId, 'id', 'participant_id'
70 );
71 $this->assign('parentHasPayment', $parentHasPayment);
72 }
73
fbc54416 74 $statusId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $participantID, 'status_id', 'id');
97d8187a
DG
75 $status = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantStatusType', $statusId, 'name', 'id');
76 $status = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantStatusType', $statusId, 'name', 'id');
77 if ($status == 'Transferred') {
78 $transferId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $participantID, 'transferred_to_contact_id', 'id');
79 $pid = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $transferId, 'id', 'contact_id');
80 $transferName = current(CRM_Contact_BAO_Contact::getContactDetails($transferId));
81 $this->assign('pid', $pid);
82 $this->assign('transferId', $transferId);
83 $this->assign('transferName', $transferName);
84 }
fbc54416 85 $participantStatuses = CRM_Event_PseudoConstant::participantStatus();
fbc54416 86
e219d53b 87 // CRM-20879: Show 'Transfer or Cancel' option beside 'Change fee selection'
88 // only if logged in user have 'edit event participants' permission and
89 // participant status is not Cancelled or Transferred
be2fb01f 90 if (CRM_Core_Permission::check('edit event participants') && !in_array($status, ['Cancelled', 'Transferred'])) {
e219d53b 91 $this->assign('transferOrCancelLink',
92 CRM_Utils_System::url(
93 'civicrm/event/selfsvcupdate',
be2fb01f 94 [
e219d53b 95 'reset' => 1,
96 'is_backoffice' => 1,
97 'pid' => $participantID,
98 'cs' => CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, NULL, 'inf'),
be2fb01f 99 ]
e219d53b 100 )
101 );
102 }
103
6a488035 104 if ($values[$participantID]['is_test']) {
cd355351 105 $values[$participantID]['status'] = CRM_Core_TestEntity::appendTestText($values[$participantID]['status']);
6a488035
TO
106 }
107
108 // Get Note
109 $noteValue = CRM_Core_BAO_Note::getNote($participantID, 'civicrm_participant');
110
111 $values[$participantID]['note'] = array_values($noteValue);
112
6a488035
TO
113 // Get Line Items
114 $lineItem = CRM_Price_BAO_LineItem::getLineItems($participantID);
115
116 if (!CRM_Utils_System::isNull($lineItem)) {
117 $values[$participantID]['lineItem'][] = $lineItem;
118 }
119
120 $values[$participantID]['totalAmount'] = CRM_Utils_Array::value('fee_amount', $values[$participantID]);
121
122 // Get registered_by contact ID and display_name if participant was registered by someone else (CRM-4859)
a7488080 123 if (!empty($values[$participantID]['participant_registered_by_id'])) {
6a488035
TO
124 $values[$participantID]['registered_by_contact_id'] = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Participant",
125 $values[$participantID]['participant_registered_by_id'],
126 'contact_id', 'id'
127 );
128 $values[$participantID]['registered_by_display_name'] = CRM_Contact_BAO_Contact::displayName($values[$participantID]['registered_by_contact_id']);
129 }
130
131 // Check if this is a primaryParticipant (registered for others) and retrieve additional participants if true (CRM-4859)
132 if (CRM_Event_BAO_Participant::isPrimaryParticipant($participantID)) {
133 $values[$participantID]['additionalParticipants'] = CRM_Event_BAO_Participant::getAdditionalParticipants($participantID);
134 }
135
136 // get the option value for custom data type
c460c196
PN
137 $customDataType = CRM_Core_OptionGroup::values('custom_data_type', FALSE, FALSE, FALSE, NULL, 'name');
138 $roleCustomDataTypeID = array_search('ParticipantRole', $customDataType);
139 $eventNameCustomDataTypeID = array_search('ParticipantEventName', $customDataType);
140 $eventTypeCustomDataTypeID = array_search('ParticipantEventType', $customDataType);
6a488035 141 $allRoleIDs = explode(CRM_Core_DAO::VALUE_SEPARATOR, $values[$participantID]['role_id']);
be2fb01f
CW
142 $groupTree = [];
143 $finalTree = [];
6a488035
TO
144
145 foreach ($allRoleIDs as $k => $v) {
0b330e6d
CW
146 $roleGroupTree = CRM_Core_BAO_CustomGroup::getTree('Participant', NULL, $participantID, NULL, $v, $roleCustomDataTypeID);
147 $eventGroupTree = CRM_Core_BAO_CustomGroup::getTree('Participant', NULL, $participantID, NULL,
6a488035
TO
148 $values[$participantID]['event_id'], $eventNameCustomDataTypeID
149 );
353ffa53 150 $eventTypeID = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $values[$participantID]['event_id'], 'event_type_id', 'id');
0b330e6d 151 $eventTypeGroupTree = CRM_Core_BAO_CustomGroup::getTree('Participant', NULL, $participantID, NULL, $eventTypeID, $eventTypeCustomDataTypeID);
353ffa53
TO
152 $groupTree = CRM_Utils_Array::crmArrayMerge($roleGroupTree, $eventGroupTree);
153 $groupTree = CRM_Utils_Array::crmArrayMerge($groupTree, $eventTypeGroupTree);
0b330e6d 154 $groupTree = CRM_Utils_Array::crmArrayMerge($groupTree, CRM_Core_BAO_CustomGroup::getTree('Participant', NULL, $participantID));
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 = [];
aaffa79f 195 $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
03b412ae 196 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
a32709be 197 $totalTaxAmount = 0;
6a488035
TO
198 foreach ($lineItem as $k => $v) {
199 if (CRM_Utils_Array::value('participant_count', $lineItem[$k]) > 0) {
aa4bb8d2 200 $participantCount[] = $lineItem[$k]['participant_count'];
6a488035 201 }
a32709be 202 $totalTaxAmount = $v['tax_amount'] + $totalTaxAmount;
6a488035 203 }
03b412ae
PB
204 if ($invoicing) {
205 $this->assign('totalTaxAmount', $totalTaxAmount);
206 }
6a488035
TO
207 if ($participantCount) {
208 $this->assign('pricesetFieldsCount', $participantCount);
209 }
210 $this->assign('displayName', $displayName);
211 // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container
86bfa4f6 212 CRM_Utils_System::setTitle(ts('View Event Registration for') . ' ' . $displayName);
6a488035
TO
213
214 $roleId = CRM_Utils_Array::value('role_id', $values[$participantID]);
215 $title = $displayName . ' (' . CRM_Utils_Array::value($roleId, $participantRoles) . ' - ' . $eventTitle . ')';
216
217 $sep = CRM_Core_DAO::VALUE_SEPARATOR;
be2fb01f 218 $viewRoles = [];
6a488035
TO
219 foreach (explode($sep, $values[$participantID]['role_id']) as $k => $v) {
220 $viewRoles[] = $participantRoles[$v];
221 }
222 $values[$participantID]['role_id'] = implode(', ', $viewRoles);
223 $this->assign('role', $values[$participantID]['role_id']);
224 // add Participant to Recent Items
225 CRM_Utils_Recent::add($title,
226 $url,
227 $values[$participantID]['id'],
228 'Participant',
229 $values[$participantID]['contact_id'],
230 NULL,
231 $recentOther
232 );
233 }
234
235 /**
fe482240 236 * Build the form object.
6a488035 237 *
355ba699 238 * @return void
6a488035
TO
239 */
240 public function buildQuickForm() {
be2fb01f 241 $this->addButtons([
90b461f1
SL
242 [
243 'type' => 'cancel',
244 'name' => ts('Done'),
245 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
246 'isDefault' => TRUE,
247 ],
248 ]);
6a488035 249 }
96025800 250
6a488035 251}