Merge pull request #23974 from colemanw/filterFieldsAfform
[civicrm-core.git] / CRM / Event / Form / ParticipantFeeSelection.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 form used for changing / updating fee selections for the events
20 * event/contribution is partially paid
21 *
22 */
23 class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
24
25 public $useLivePageJS = TRUE;
26
27 protected $_contactId = NULL;
28
29 protected $_contributorDisplayName = NULL;
30
31 protected $_contributorEmail = NULL;
32
33 protected $_toDoNotEmail = NULL;
34
35 protected $_contributionId = NULL;
36
37 protected $fromEmailId = NULL;
38
39 public $_eventId = NULL;
40
41 public $_action = NULL;
42
43 public $_values = NULL;
44
45 public $_participantId = NULL;
46
47 protected $_participantStatus = NULL;
48
49 protected $_paidAmount = NULL;
50
51 public $_isPaidEvent = NULL;
52
53 protected $contributionAmt = NULL;
54
55 public function preProcess() {
56 $this->_participantId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
57 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
58 $this->_eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_participantId, 'event_id');
59 $this->_fromEmails = CRM_Event_BAO_Event::getFromEmailIds($this->_eventId);
60
61 $this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_participantId, 'contribution_id', 'participant_id');
62 if (!$this->_contributionId) {
63 if ($primaryParticipantId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $this->_participantId, 'registered_by_id')) {
64 $this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantPayment', $primaryParticipantId, 'contribution_id', 'participant_id');
65 }
66 }
67
68 if ($this->_contributionId) {
69 $this->_isPaidEvent = TRUE;
70 }
71 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, TRUE);
72
73 list($this->_contributorDisplayName, $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactId);
74 $this->assign('displayName', $this->_contributorDisplayName);
75 $this->assign('email', $this->_contributorEmail);
76
77 $this->_participantStatus = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $this->_participantId, 'status_id');
78 //set the payment mode - _mode property is defined in parent class
79 $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
80
81 $this->assign('contactId', $this->_contactId);
82 $this->assign('id', $this->_participantId);
83
84 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_participantId, 'event');
85 $this->_paidAmount = $paymentInfo['paid'];
86 $this->assign('paymentInfo', $paymentInfo);
87 $this->assign('feePaid', $this->_paidAmount);
88
89 $ids = CRM_Event_BAO_Participant::getParticipantIds($this->_contributionId);
90 if (count($ids) > 1) {
91 $total = CRM_Price_BAO_LineItem::getLineTotal($this->_contributionId);
92 $this->assign('totalLineTotal', $total);
93 $this->assign('lineItemTotal', $total);
94 }
95
96 $title = ts("Change selections for %1", [1 => $this->_contributorDisplayName]);
97 if ($title) {
98 $this->setTitle($title);
99 }
100 }
101
102 /**
103 * Set default values for the form.
104 *
105 * @return array
106 */
107 public function setDefaultValues() {
108 $params = ['id' => $this->_participantId];
109
110 CRM_Event_BAO_Participant::getValues($params, $defaults, $ids);
111 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $this->_eventId);
112
113 $priceSetValues = CRM_Event_Form_EventFees::setDefaultPriceSet($this->_participantId, $this->_eventId, FALSE);
114 $priceFieldId = (array_keys($this->_values['fee']));
115 if (!empty($priceSetValues)) {
116 $defaults[$this->_participantId] = array_merge($defaults[$this->_participantId], $priceSetValues);
117 }
118 else {
119 foreach ($priceFieldId as $key => $value) {
120 if (!empty($value) && ($this->_values['fee'][$value]['html_type'] == 'Radio' || $this->_values['fee'][$value]['html_type'] == 'Select') && !$this->_values['fee'][$value]['is_required']) {
121 $fee_keys = array_keys($this->_values['fee']);
122 $defaults[$this->_participantId]['price_' . $fee_keys[$key]] = 0;
123 }
124 }
125 }
126 $this->assign('totalAmount', CRM_Utils_Array::value('fee_amount', $defaults[$this->_participantId]));
127 if ($this->_action == CRM_Core_Action::UPDATE) {
128 $fee_level = $defaults[$this->_participantId]['fee_level'];
129 CRM_Event_BAO_Participant::fixEventLevel($fee_level);
130 $this->assign('fee_level', $fee_level);
131 $this->assign('fee_amount', CRM_Utils_Array::value('fee_amount', $defaults[$this->_participantId]));
132 }
133 $defaults = $defaults[$this->_participantId];
134 return $defaults;
135 }
136
137 /**
138 * Build form.
139 *
140 * @throws \CRM_Core_Exception
141 */
142 public function buildQuickForm() {
143
144 $statuses = CRM_Event_PseudoConstant::participantStatus();
145 $this->assign('partiallyPaid', array_search('Partially paid', $statuses));
146 $this->assign('pendingRefund', array_search('Pending refund', $statuses));
147 $this->assign('participantStatus', $this->_participantStatus);
148
149 $this->assign('currencySymbol', CRM_Core_BAO_Country::defaultCurrencySymbol());
150
151 // line items block
152 $lineItem = $event = [];
153 $params = ['id' => $this->_eventId];
154 CRM_Event_BAO_Event::retrieve($params, $event);
155
156 //retrieve custom information
157 $this->_values = [];
158
159 CRM_Event_Form_Registration::initEventFee($this, $event['id'], $this->_action !== CRM_Core_Action::UPDATE);
160 CRM_Event_Form_Registration_Register::buildAmount($this, TRUE);
161
162 if (!CRM_Utils_System::isNull(CRM_Utils_Array::value('line_items', $this->_values))) {
163 $lineItem[] = $this->_values['line_items'];
164 }
165 $this->assign('lineItem', empty($lineItem) ? FALSE : $lineItem);
166 $event = CRM_Event_BAO_Event::getEvents(0, $this->_eventId);
167 $this->assign('eventName', $event[$this->_eventId]);
168
169 $statusOptions = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label');
170 $this->add('select', 'status_id', ts('Participant Status'),
171 [
172 '' => ts('- select -'),
173 ] + $statusOptions,
174 TRUE
175 );
176
177 $this->addElement('checkbox',
178 'send_receipt',
179 ts('Send Confirmation?'), NULL,
180 ['onclick' => "showHideByValue('send_receipt','','notice','table-row','radio',false); showHideByValue('send_receipt','','from-email','table-row','radio',false);"]
181 );
182
183 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails['from_email_id']);
184
185 $this->add('textarea', 'receipt_text', ts('Confirmation Message'));
186
187 $noteAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note');
188 $this->add('textarea', 'note', ts('Notes'), $noteAttributes['note']);
189
190 $buttons[] = [
191 'type' => 'upload',
192 'name' => ts('Save'),
193 'isDefault' => TRUE,
194 ];
195
196 if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_participantId)) {
197 $buttons[] = [
198 'type' => 'upload',
199 'name' => ts('Save and Record Payment'),
200 'subName' => 'new',
201 ];
202 }
203 $buttons[] = [
204 'type' => 'cancel',
205 'name' => ts('Cancel'),
206 ];
207
208 $this->addButtons($buttons);
209 $this->addFormRule(['CRM_Event_Form_ParticipantFeeSelection', 'formRule'], $this);
210 }
211
212 /**
213 * @param $fields
214 * @param $files
215 * @param self $self
216 *
217 * @return array
218 */
219 public static function formRule($fields, $files, $self) {
220 $errors = [];
221 return $errors;
222 }
223
224 /**
225 * Post process form.
226 *
227 * @throws \CRM_Core_Exception
228 * @throws \CiviCRM_API3_Exception
229 */
230 public function postProcess() {
231 $params = $this->controller->exportValues($this->_name);
232
233 $feeBlock = $this->_values['fee'];
234 $lineItems = $this->_values['line_items'];
235 CRM_Price_BAO_LineItem::changeFeeSelections($params, $this->_participantId, 'participant', $this->_contributionId, $feeBlock, $lineItems);
236 $this->contributionAmt = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'total_amount');
237 // email sending
238 if (!empty($params['send_receipt'])) {
239 $fetchParticipantVals = ['id' => $this->_participantId];
240 CRM_Event_BAO_Participant::getValues($fetchParticipantVals, $participantDetails);
241 $participantParams = array_merge($params, $participantDetails[$this->_participantId]);
242 $mailSent = $this->emailReceipt($participantParams);
243 }
244
245 // update participant
246 CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Participant', $this->_participantId, 'status_id', $params['status_id']);
247 if (!empty($params['note'])) {
248 $noteParams = [
249 'entity_table' => 'civicrm_participant',
250 'note' => $params['note'],
251 'entity_id' => $this->_participantId,
252 'contact_id' => $this->_contactId,
253 ];
254 CRM_Core_BAO_Note::add($noteParams);
255 }
256 CRM_Core_Session::setStatus(ts("The fee selection has been changed for this participant"), ts('Saved'), 'success');
257
258 $buttonName = $this->controller->getButtonName();
259 if ($buttonName == $this->getButtonName('upload', 'new')) {
260 $session = CRM_Core_Session::singleton();
261 $session->pushUserContext(CRM_Utils_System::url('civicrm/payment/add',
262 "reset=1&action=add&component=event&id={$this->_participantId}&cid={$this->_contactId}"
263 ));
264 }
265 }
266
267 /**
268 * @param array $params
269 *
270 * @return mixed
271 */
272 public function emailReceipt(&$params) {
273 $updatedLineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant', FALSE, FALSE);
274 $lineItem = [];
275 if ($updatedLineItem) {
276 $lineItem[] = $updatedLineItem;
277 }
278 $this->assign('lineItem', empty($lineItem) ? FALSE : $lineItem);
279
280 // offline receipt sending
281 if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) {
282 $receiptFrom = $params['from_email_address'];
283 }
284
285 $this->assign('module', 'Event Registration');
286 //use of the message template below requires variables in different format
287 $event = $events = [];
288 $returnProperties = ['fee_label', 'start_date', 'end_date', 'is_show_location', 'title'];
289
290 //get all event details.
291 CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $params['event_id'], $events, $returnProperties);
292 $event = $events[$params['event_id']];
293 unset($event['start_date']);
294 unset($event['end_date']);
295
296 $role = CRM_Event_PseudoConstant::participantRole();
297 $participantRoles = $params['role_id'] ?? NULL;
298 if (is_array($participantRoles)) {
299 $selectedRoles = [];
300 foreach (array_keys($participantRoles) as $roleId) {
301 $selectedRoles[] = $role[$roleId];
302 }
303 $event['participant_role'] = implode(', ', $selectedRoles);
304 }
305 else {
306 $event['participant_role'] = $role[$participantRoles] ?? NULL;
307 }
308 $event['is_monetary'] = $this->_isPaidEvent;
309
310 if ($params['receipt_text']) {
311 $event['confirm_email_text'] = $params['receipt_text'];
312 }
313
314 $this->assign('isAmountzero', 1);
315 $this->assign('event', $event);
316
317 $this->assign('isShowLocation', $event['is_show_location']);
318 if (CRM_Utils_Array::value('is_show_location', $event) == 1) {
319 $locationParams = [
320 'entity_id' => $params['event_id'],
321 'entity_table' => 'civicrm_event',
322 ];
323 $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE);
324 $this->assign('location', $location);
325 }
326
327 $status = CRM_Event_PseudoConstant::participantStatus();
328 if ($this->_isPaidEvent) {
329 $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
330 if (!$this->_mode) {
331 if (isset($params['payment_instrument_id'])) {
332 $this->assign('paidBy',
333 CRM_Utils_Array::value($params['payment_instrument_id'],
334 $paymentInstrument
335 )
336 );
337 }
338 }
339
340 $this->assign('totalAmount', $this->contributionAmt);
341
342 $this->assign('isPrimary', 1);
343 $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params));
344 }
345
346 $this->assign('register_date', $params['register_date']);
347 $template = CRM_Core_Smarty::singleton();
348
349 // Retrieve the name and email of the contact - this will be the TO for receipt email
350 list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId);
351
352 $this->_contributorDisplayName = ($this->_contributorDisplayName == ' ') ? $this->_contributorEmail : $this->_contributorDisplayName;
353
354 $waitStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
355 if ($waitingStatus = CRM_Utils_Array::value($params['status_id'], $waitStatus)) {
356 $this->assign('isOnWaitlist', TRUE);
357 }
358 $this->assign('contactID', $this->_contactId);
359
360 $sendTemplateParams = [
361 'workflow' => 'event_offline_receipt',
362 'contactId' => $this->_contactId,
363 'isTest' => FALSE,
364 'PDFFilename' => ts('confirmation') . '.pdf',
365 'modelProps' => [
366 'participantID' => $this->_participantId,
367 'eventID' => $params['event_id'],
368 ],
369 ];
370
371 // try to send emails only if email id is present
372 // and the do-not-email option is not checked for that contact
373 if ($this->_contributorEmail && !$this->_toDoNotEmail) {
374 $sendTemplateParams['from'] = $receiptFrom;
375 $sendTemplateParams['toName'] = $this->_contributorDisplayName;
376 $sendTemplateParams['toEmail'] = $this->_contributorEmail;
377 $sendTemplateParams['cc'] = $this->_fromEmails['cc'] ?? NULL;
378 $sendTemplateParams['bcc'] = $this->_fromEmails['bcc'] ?? NULL;
379 }
380
381 list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
382 return $mailSent;
383 }
384
385 }