From cd6e40b4364f5453dd7921e0c593cdcac7fe08ba Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 11 Aug 2023 16:29:08 +1200 Subject: [PATCH] Extract getDiscountID() 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 --- CRM/Event/Form/Participant.php | 32 +++++++++++++++--- CRM/Event/Form/ParticipantFeeSelection.php | 38 +++++++++++++++++++++- CRM/Event/Form/Registration.php | 33 ++++++++++++++----- 3 files changed, 89 insertions(+), 14 deletions(-) diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index fee7ae3faa..ab1052f215 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -524,8 +524,8 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment $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); } } @@ -1436,8 +1436,8 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment //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)) { @@ -2266,4 +2266,28 @@ INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_ 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 { + $this->_discountId = (int) CRM_Core_BAO_Discount::findSet($this->getEventID(), 'civicrm_event'); + } + } + return $this->_discountId ?: NULL; + } + } diff --git a/CRM/Event/Form/ParticipantFeeSelection.php b/CRM/Event/Form/ParticipantFeeSelection.php index c42cdc721f..e27662096a 100644 --- a/CRM/Event/Form/ParticipantFeeSelection.php +++ b/CRM/Event/Form/ParticipantFeeSelection.php @@ -172,7 +172,7 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form { //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)) { @@ -225,6 +225,23 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form { $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 @@ -390,4 +407,23 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form { 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; + } + } diff --git a/CRM/Event/Form/Registration.php b/CRM/Event/Form/Registration.php index ebde060eb3..5685a1d682 100644 --- a/CRM/Event/Form/Registration.php +++ b/CRM/Event/Form/Registration.php @@ -297,8 +297,9 @@ class CRM_Event_Form_Registration extends CRM_Core_Form { )); $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 = [ @@ -570,18 +571,15 @@ class CRM_Event_Form_Registration extends CRM_Core_Form { * @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'); } @@ -892,7 +890,7 @@ class CRM_Event_Form_Registration extends CRM_Core_Form { $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); } @@ -1717,4 +1715,21 @@ class CRM_Event_Form_Registration extends CRM_Core_Form { ); } + /** + * 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; + } + } -- 2.25.1