Merge pull request #19125 from eileenmcnaughton/ev533
[civicrm-core.git] / CRM / Event / Form / ParticipantView.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for Participant
20 *
21 */
22 class CRM_Event_Form_ParticipantView extends CRM_Core_Form {
23
24 public $useLivePageJS = TRUE;
25
26 /**
27 * Set variables up before form is built.
28 *
29 * @return void
30 */
31 public function preProcess() {
32 $values = $ids = [];
33 $participantID = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
34 $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
35 $params = ['id' => $participantID];
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
48 if (!empty($values[$participantID]['fee_level'])) {
49 CRM_Event_BAO_Participant::fixEventLevel($values[$participantID]['fee_level']);
50 }
51
52 $this->assign('contactId', $contactID);
53 $this->assign('participantId', $participantID);
54
55 $paymentId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment',
56 $participantID, 'id', 'participant_id'
57 );
58 $this->assign('hasPayment', $paymentId);
59 $this->assign('componentId', $participantID);
60 $this->assign('component', 'event');
61
62 if ($parentParticipantId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant',
63 $participantID, 'registered_by_id'
64 )
65 ) {
66 $parentHasPayment = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment',
67 $parentParticipantId, 'id', 'participant_id'
68 );
69 $this->assign('parentHasPayment', $parentHasPayment);
70 }
71
72 $statusId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $participantID, 'status_id', 'id');
73 $status = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantStatusType', $statusId, 'name', 'id');
74 $status = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantStatusType', $statusId, 'name', 'id');
75 if ($status == 'Transferred') {
76 $transferId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $participantID, 'transferred_to_contact_id', 'id');
77 $pid = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $transferId, 'id', 'contact_id');
78 $transferName = current(CRM_Contact_BAO_Contact::getContactDetails($transferId));
79 $this->assign('pid', $pid);
80 $this->assign('transferId', $transferId);
81 $this->assign('transferName', $transferName);
82 }
83 $participantStatuses = CRM_Event_PseudoConstant::participantStatus();
84
85 // CRM-20879: Show 'Transfer or Cancel' option beside 'Change fee selection'
86 // only if logged in user have 'edit event participants' permission and
87 // participant status is not Cancelled or Transferred
88 if (CRM_Core_Permission::check('edit event participants') && !in_array($status, ['Cancelled', 'Transferred'])) {
89 $this->assign('transferOrCancelLink',
90 CRM_Utils_System::url(
91 'civicrm/event/selfsvcupdate',
92 [
93 'reset' => 1,
94 'is_backoffice' => 1,
95 'pid' => $participantID,
96 'cs' => CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, NULL, 'inf'),
97 ]
98 )
99 );
100 }
101
102 if ($values[$participantID]['is_test']) {
103 $values[$participantID]['status'] = CRM_Core_TestEntity::appendTestText($values[$participantID]['status']);
104 }
105
106 // Get Note
107 $noteValue = CRM_Core_BAO_Note::getNote($participantID, 'civicrm_participant');
108
109 $values[$participantID]['note'] = array_values($noteValue);
110
111 // Get Line Items
112 $lineItem = CRM_Price_BAO_LineItem::getLineItems($participantID);
113
114 if (!CRM_Utils_System::isNull($lineItem)) {
115 $values[$participantID]['lineItem'][] = $lineItem;
116 }
117
118 $values[$participantID]['totalAmount'] = $values[$participantID]['fee_amount'] ?? NULL;
119
120 // Get registered_by contact ID and display_name if participant was registered by someone else (CRM-4859)
121 if (!empty($values[$participantID]['participant_registered_by_id'])) {
122 $values[$participantID]['registered_by_contact_id'] = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Participant",
123 $values[$participantID]['participant_registered_by_id'],
124 'contact_id', 'id'
125 );
126 $values[$participantID]['registered_by_display_name'] = CRM_Contact_BAO_Contact::displayName($values[$participantID]['registered_by_contact_id']);
127 }
128
129 // Check if this is a primaryParticipant (registered for others) and retrieve additional participants if true (CRM-4859)
130 if (CRM_Event_BAO_Participant::isPrimaryParticipant($participantID)) {
131 $values[$participantID]['additionalParticipants'] = CRM_Event_BAO_Participant::getAdditionalParticipants($participantID);
132 }
133
134 // get the option value for custom data type
135 $customDataType = CRM_Core_OptionGroup::values('custom_data_type', FALSE, FALSE, FALSE, NULL, 'name');
136 $roleCustomDataTypeID = array_search('ParticipantRole', $customDataType);
137 $eventNameCustomDataTypeID = array_search('ParticipantEventName', $customDataType);
138 $eventTypeCustomDataTypeID = array_search('ParticipantEventType', $customDataType);
139 $allRoleIDs = explode(CRM_Core_DAO::VALUE_SEPARATOR, $values[$participantID]['role_id']);
140 $groupTree = [];
141 $finalTree = [];
142
143 foreach ($allRoleIDs as $k => $v) {
144 $roleGroupTree = CRM_Core_BAO_CustomGroup::getTree('Participant', NULL, $participantID, NULL, $v, $roleCustomDataTypeID);
145 $eventGroupTree = CRM_Core_BAO_CustomGroup::getTree('Participant', NULL, $participantID, NULL,
146 $values[$participantID]['event_id'], $eventNameCustomDataTypeID
147 );
148 $eventTypeID = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $values[$participantID]['event_id'], 'event_type_id', 'id');
149 $eventTypeGroupTree = CRM_Core_BAO_CustomGroup::getTree('Participant', NULL, $participantID, NULL, $eventTypeID, $eventTypeCustomDataTypeID);
150 $groupTree = CRM_Utils_Array::crmArrayMerge($roleGroupTree, $eventGroupTree);
151 $groupTree = CRM_Utils_Array::crmArrayMerge($groupTree, $eventTypeGroupTree);
152 $groupTree = CRM_Utils_Array::crmArrayMerge($groupTree, CRM_Core_BAO_CustomGroup::getTree('Participant', NULL, $participantID));
153 foreach ($groupTree as $treeId => $trees) {
154 $finalTree[$treeId] = $trees;
155 }
156 }
157 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $finalTree, FALSE, NULL, NULL, NULL, $participantID);
158 $eventTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $values[$participantID]['event_id'], 'title');
159 //CRM-7150, show event name on participant view even if the event is disabled
160 if (empty($values[$participantID]['event'])) {
161 $values[$participantID]['event'] = $eventTitle;
162 }
163
164 //do check for campaigns
165 if ($campaignId = CRM_Utils_Array::value('campaign_id', $values[$participantID])) {
166 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId);
167 $values[$participantID]['campaign'] = $campaigns[$campaignId];
168 }
169
170 $this->assign($values[$participantID]);
171
172 // add viewed participant to recent items list
173 $url = CRM_Utils_System::url('civicrm/contact/view/participant',
174 "action=view&reset=1&id={$values[$participantID]['id']}&cid={$values[$participantID]['contact_id']}&context=home"
175 );
176
177 $recentOther = [];
178 if (CRM_Core_Permission::check('edit event participants')) {
179 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/participant',
180 "action=update&reset=1&id={$values[$participantID]['id']}&cid={$values[$participantID]['contact_id']}&context=home"
181 );
182 }
183 if (CRM_Core_Permission::check('delete in CiviEvent')) {
184 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/participant',
185 "action=delete&reset=1&id={$values[$participantID]['id']}&cid={$values[$participantID]['contact_id']}&context=home"
186 );
187 }
188
189 $participantRoles = CRM_Event_PseudoConstant::participantRole();
190 $displayName = CRM_Contact_BAO_Contact::displayName($values[$participantID]['contact_id']);
191
192 $participantCount = [];
193 $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
194 $invoicing = $invoiceSettings['invoicing'] ?? NULL;
195 $totalTaxAmount = 0;
196 foreach ($lineItem as $k => $v) {
197 if (CRM_Utils_Array::value('participant_count', $lineItem[$k]) > 0) {
198 $participantCount[] = $lineItem[$k]['participant_count'];
199 }
200 $totalTaxAmount = $v['tax_amount'] + $totalTaxAmount;
201 }
202 if ($invoicing) {
203 $this->assign('totalTaxAmount', $totalTaxAmount);
204 }
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
210 CRM_Utils_System::setTitle(ts('View Event Registration for') . ' ' . $displayName);
211
212 $roleId = $values[$participantID]['role_id'] ?? NULL;
213 $title = $displayName . ' (' . CRM_Utils_Array::value($roleId, $participantRoles) . ' - ' . $eventTitle . ')';
214
215 $sep = CRM_Core_DAO::VALUE_SEPARATOR;
216 $viewRoles = [];
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 /**
234 * Build the form object.
235 *
236 * @return void
237 */
238 public function buildQuickForm() {
239 $this->addButtons([
240 [
241 'type' => 'cancel',
242 'name' => ts('Done'),
243 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
244 'isDefault' => TRUE,
245 ],
246 ]);
247 }
248
249 }