Skip to content

Commit

Permalink
Extract getDiscountID()
Browse files Browse the repository at this point in the history
This is part of a general cleanup to stop passing the form into functions & then checking the form
for a possible property. Instead each form that calls the function has a consistent
(publicly supported) getDiscountID() function that it calls & discountID is passed in.

Note I was actually trying to give getPriceSetID this treatment but discountID was in the
way. In general I'm trying to make some consisent public functions available on forms
for retrieving relevant entity IDs rather than the current property / form
variable patchwork
  • Loading branch information
eileenmcnaughton committed Aug 11, 2023
1 parent bd65d4e commit afb392b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 14 deletions.
32 changes: 28 additions & 4 deletions CRM/Event/Form/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,8 @@ public function setDefaultValues(): array {

$this->_eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventID, 'event_type_id', 'id');

$this->_discountId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'discount_id');
if ($this->_discountId) {
if ($this->getDiscountID()) {
// This doesn't seem used....
$this->set('discountId', $this->_discountId);
}
}
Expand Down Expand Up @@ -1436,8 +1436,8 @@ public function buildEventFeeForm($form) {

//retrieve custom information
$form->_values = [];
CRM_Event_Form_Registration::initEventFee($form, $event['id'], FALSE);
CRM_Event_Form_Registration_Register::buildAmount($form, TRUE, $form->_discountId);
CRM_Event_Form_Registration::initEventFee($form, $event['id'], FALSE, $form->getDiscountID());
CRM_Event_Form_Registration_Register::buildAmount($form, TRUE, $form->getDiscountID());
$lineItem = [];
$totalTaxAmount = 0;
if (!CRM_Utils_System::isNull($form->_values['line_items'] ?? NULL)) {
Expand Down Expand Up @@ -2266,4 +2266,28 @@ protected function sendReceipts($params, $total_amount, array $customFields, arr
return ['sent' => count($sent), 'not_sent' => count($notSent)];
}

/**
* Get the discount ID.
*
* @return int|null
*
* @api This function will not change in a minor release and is supported for
* use outside of core. This annotation / external support for properties
* is only given where there is specific test cover.
*
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/
public function getDiscountID(): ?int {
if ($this->_discountId === NULL) {
if ($this->getParticipantID()) {
$this->_discountId = (int) CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->getParticipantID(), 'discount_id');
}
else {
CRM_Core_BAO_Discount::findSet($this->getEventID(), 'civicrm_event');
}
}
return $this->_discountId ?: NULL;
}

}
38 changes: 37 additions & 1 deletion CRM/Event/Form/ParticipantFeeSelection.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function buildQuickForm() {
//retrieve custom information
$this->_values = [];

CRM_Event_Form_Registration::initEventFee($this, $event['id'], $this->_action !== CRM_Core_Action::UPDATE);
CRM_Event_Form_Registration::initEventFee($this, $event['id'], $this->_action !== CRM_Core_Action::UPDATE, $this->getDiscountID());
CRM_Event_Form_Registration_Register::buildAmount($this, TRUE);

if (!CRM_Utils_System::isNull($this->_values['line_items'] ?? NULL)) {
Expand Down Expand Up @@ -225,6 +225,23 @@ public function buildQuickForm() {
$this->addFormRule(['CRM_Event_Form_ParticipantFeeSelection', 'formRule'], $this);
}

/**
* Get the discount ID.
*
* @return int|null
*
* @api This function will not change in a minor release and is supported for
* use outside of core. This annotation / external support for properties
* is only given where there is specific test cover.
*
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/
public function getDiscountID(): ?int {
$discountID = (int) CRM_Core_BAO_Discount::findSet($this->getEventID(), 'civicrm_event');
return $discountID ?: NULL;
}

/**
* @param $fields
* @param $files
Expand Down Expand Up @@ -390,4 +407,23 @@ private function emailReceipt(array $params): void {
CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
}

/**
* Get the event ID.
*
* This function is supported for use outside of core.
*
* @api This function will not change in a minor release and is supported for
* use outside of core. This annotation / external support for properties
* is only given where there is specific test cover.
*
* @return int
* @throws \CRM_Core_Exception
*/
public function getEventID(): int {
if (!$this->_eventId) {
$this->_eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_participantId, 'event_id');
}
return $this->_eventId;
}

}
33 changes: 24 additions & 9 deletions CRM/Event/Form/Registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,9 @@ public function preProcess() {
));
$this->assignPaymentProcessor($isPayLater);
}
//init event fee.
self::initEventFee($this, $this->_eventId);

$discountId = $this->getDiscountID();
self::initEventFee($this, $this->_eventId, TRUE, $discountId);

// get the profile ids
$ufJoinParams = [
Expand Down Expand Up @@ -570,18 +571,15 @@ public function buildCustom($id, $name) {
* @param int $eventID
* @param bool $doNotIncludeExpiredFields
* See CRM-16456.
* @param int|null $discountId
* ID of any discount in use.
*
* @throws Exception
*/
public static function initEventFee(&$form, $eventID, $doNotIncludeExpiredFields = TRUE) {
public static function initEventFee(&$form, $eventID, $doNotIncludeExpiredFields = TRUE, $discountId = NULL) {
// get price info

// retrive all active price set fields.
$discountId = CRM_Core_BAO_Discount::findSet($eventID, 'civicrm_event');
if (property_exists($form, '_discountId') && $form->_discountId) {
$discountId = $form->_discountId;
}

if ($discountId) {
$priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
}
Expand Down Expand Up @@ -892,7 +890,7 @@ protected function addParticipant(&$form, $contactID) {
$participantParams['custom'] = [];
foreach ($form->_params as $paramName => $paramValue) {
if (strpos($paramName, 'custom_') === 0) {
list($customFieldID, $customValueID) = CRM_Core_BAO_CustomField::getKeyID($paramName, TRUE);
[$customFieldID, $customValueID] = CRM_Core_BAO_CustomField::getKeyID($paramName, TRUE);
CRM_Core_BAO_CustomField::formatCustomField($customFieldID, $participantParams['custom'], $paramValue, 'Participant', $customValueID);

}
Expand Down Expand Up @@ -1717,4 +1715,21 @@ private function getInfoPageUrl(): string {
);
}

/**
* Get the discount ID.
*
* @return int|null
*
* @api This function will not change in a minor release and is supported for
* use outside of core. This annotation / external support for properties
* is only given where there is specific test cover.
*
* @noinspection PhpUnhandledExceptionInspection
* @noinspection PhpDocMissingThrowsInspection
*/
protected function getDiscountID(): ?int {
$id = CRM_Core_BAO_Discount::findSet($this->getEventID(), 'civicrm_event');
return $id ?: NULL;
}

}

0 comments on commit afb392b

Please sign in to comment.