3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This form used for changing / updating fee selections for the events
20 * event/contribution is partially paid
23 class CRM_Event_Form_ParticipantFeeSelection
extends CRM_Core_Form
{
25 public $useLivePageJS = TRUE;
27 protected $_contactId = NULL;
29 protected $_contributorDisplayName = NULL;
31 protected $_contributorEmail = NULL;
33 protected $_toDoNotEmail = NULL;
35 protected $_contributionId = NULL;
37 protected $fromEmailId = NULL;
39 public $_eventId = NULL;
41 public $_action = NULL;
43 public $_values = NULL;
45 public $_participantId = NULL;
47 protected $_participantStatus = NULL;
49 protected $_paidAmount = NULL;
51 public $_isPaidEvent = NULL;
53 protected $contributionAmt = NULL;
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
);
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');
68 if ($this->_contributionId
) {
69 $this->_isPaidEvent
= TRUE;
71 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, TRUE);
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
);
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);
81 $this->assign('contactId', $this->_contactId
);
82 $this->assign('id', $this->_participantId
);
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
);
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);
96 $title = ts("Change selections for %1", [1 => $this->_contributorDisplayName
]);
98 CRM_Utils_System
::setTitle($title);
103 * Set default values for the form.
107 public function setDefaultValues() {
108 $params = ['id' => $this->_participantId
];
110 CRM_Event_BAO_Participant
::getValues($params, $defaults, $ids);
111 $priceSetId = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $this->_eventId
);
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);
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;
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
]));
133 $defaults = $defaults[$this->_participantId
];
140 * @throws \CRM_Core_Exception
142 public function buildQuickForm() {
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
);
149 $this->assign('currencySymbol', CRM_Core_BAO_Country
::defaultCurrencySymbol());
152 $lineItem = $event = [];
153 $params = ['id' => $this->_eventId
];
154 CRM_Event_BAO_Event
::retrieve($params, $event);
156 //retrieve custom information
159 CRM_Event_Form_Registration
::initEventFee($this, $event['id'], $this->_action
!== CRM_Core_Action
::UPDATE
);
160 CRM_Event_Form_Registration_Register
::buildAmount($this, TRUE);
162 if (!CRM_Utils_System
::isNull(CRM_Utils_Array
::value('line_items', $this->_values
))) {
163 $lineItem[] = $this->_values
['line_items'];
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
]);
169 $statusOptions = CRM_Event_PseudoConstant
::participantStatus(NULL, NULL, 'label');
170 $this->add('select', 'status_id', ts('Participant Status'),
172 '' => ts('- select -'),
177 $this->addElement('checkbox',
179 ts('Send Confirmation?'), NULL,
180 ['onclick' => "showHideByValue('send_receipt','','notice','table-row','radio',false); showHideByValue('send_receipt','','from-email','table-row','radio',false);"]
183 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails
['from_email_id']);
185 $this->add('textarea', 'receipt_text', ts('Confirmation Message'));
187 $noteAttributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Note');
188 $this->add('textarea', 'note', ts('Notes'), $noteAttributes['note']);
192 'name' => ts('Save'),
196 if (CRM_Event_BAO_Participant
::isPrimaryParticipant($this->_participantId
)) {
199 'name' => ts('Save and Record Payment'),
205 'name' => ts('Cancel'),
208 $this->addButtons($buttons);
209 $this->addFormRule(['CRM_Event_Form_ParticipantFeeSelection', 'formRule'], $this);
219 public static function formRule($fields, $files, $self) {
227 * @throws \CRM_Core_Exception
228 * @throws \CiviCRM_API3_Exception
230 public function postProcess() {
231 $params = $this->controller
->exportValues($this->_name
);
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');
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);
245 // update participant
246 CRM_Core_DAO
::setFieldValue('CRM_Event_DAO_Participant', $this->_participantId
, 'status_id', $params['status_id']);
247 if (!empty($params['note'])) {
249 'entity_table' => 'civicrm_participant',
250 'note' => $params['note'],
251 'entity_id' => $this->_participantId
,
252 'contact_id' => $this->_contactId
,
254 CRM_Core_BAO_Note
::add($noteParams);
256 CRM_Core_Session
::setStatus(ts("The fee selection has been changed for this participant"), ts('Saved'), 'success');
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}"
268 * @param array $params
272 public function emailReceipt(&$params) {
273 $updatedLineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_participantId
, 'participant', FALSE, FALSE);
275 if ($updatedLineItem) {
276 $lineItem[] = $updatedLineItem;
278 $this->assign('lineItem', empty($lineItem) ?
FALSE : $lineItem);
280 // offline receipt sending
281 if (array_key_exists($params['from_email_address'], $this->_fromEmails
['from_email_id'])) {
282 $receiptFrom = $params['from_email_address'];
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'];
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']);
296 $role = CRM_Event_PseudoConstant
::participantRole();
297 $participantRoles = $params['role_id'] ??
NULL;
298 if (is_array($participantRoles)) {
300 foreach (array_keys($participantRoles) as $roleId) {
301 $selectedRoles[] = $role[$roleId];
303 $event['participant_role'] = implode(', ', $selectedRoles);
306 $event['participant_role'] = $role[$participantRoles] ??
NULL;
308 $event['is_monetary'] = $this->_isPaidEvent
;
310 if ($params['receipt_text']) {
311 $event['confirm_email_text'] = $params['receipt_text'];
314 $this->assign('isAmountzero', 1);
315 $this->assign('event', $event);
317 $this->assign('isShowLocation', $event['is_show_location']);
318 if (CRM_Utils_Array
::value('is_show_location', $event) == 1) {
320 'entity_id' => $params['event_id'],
321 'entity_table' => 'civicrm_event',
323 $location = CRM_Core_BAO_Location
::getValues($locationParams, TRUE);
324 $this->assign('location', $location);
327 $status = CRM_Event_PseudoConstant
::participantStatus();
328 if ($this->_isPaidEvent
) {
329 $paymentInstrument = CRM_Contribute_PseudoConstant
::paymentInstrument();
331 if (isset($params['payment_instrument_id'])) {
332 $this->assign('paidBy',
333 CRM_Utils_Array
::value($params['payment_instrument_id'],
340 $this->assign('totalAmount', $this->contributionAmt
);
342 $this->assign('isPrimary', 1);
343 $this->assign('checkNumber', CRM_Utils_Array
::value('check_number', $params));
346 $this->assign('register_date', $params['register_date']);
347 $template = CRM_Core_Smarty
::singleton();
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
);
352 $this->_contributorDisplayName
= ($this->_contributorDisplayName
== ' ') ?
$this->_contributorEmail
: $this->_contributorDisplayName
;
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);
358 $this->assign('contactID', $this->_contactId
);
359 $this->assign('participantID', $this->_participantId
);
361 $sendTemplateParams = [
362 'groupName' => 'msg_tpl_workflow_event',
363 'valueName' => 'event_offline_receipt',
364 'contactId' => $this->_contactId
,
366 'PDFFilename' => ts('confirmation') . '.pdf',
369 // try to send emails only if email id is present
370 // and the do-not-email option is not checked for that contact
371 if ($this->_contributorEmail
&& !$this->_toDoNotEmail
) {
372 $sendTemplateParams['from'] = $receiptFrom;
373 $sendTemplateParams['toName'] = $this->_contributorDisplayName
;
374 $sendTemplateParams['toEmail'] = $this->_contributorEmail
;
375 $sendTemplateParams['cc'] = $this->_fromEmails
['cc'] ??
NULL;
376 $sendTemplateParams['bcc'] = $this->_fromEmails
['bcc'] ??
NULL;
379 list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);