From 4047ad527a81cf09e841c5f2c6bfd70752bf2027 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 24 Nov 2023 11:00:43 +1300 Subject: [PATCH] Consolidate getCurrency() --- CRM/Contribute/Form/AbstractEditPayment.php | 13 ------------- CRM/Contribute/Form/Contribution.php | 9 --------- CRM/Contribute/Form/ContributionBase.php | 21 +++++++++++++++++++++ CRM/Core/Form.php | 19 ++++++------------- CRM/Event/Form/Registration.php | 21 +++++++++++++++++++++ CRM/Financial/Form/Payment.php | 7 ++----- 6 files changed, 50 insertions(+), 40 deletions(-) diff --git a/CRM/Contribute/Form/AbstractEditPayment.php b/CRM/Contribute/Form/AbstractEditPayment.php index 408a4b7a56..aff8f72a66 100644 --- a/CRM/Contribute/Form/AbstractEditPayment.php +++ b/CRM/Contribute/Form/AbstractEditPayment.php @@ -404,19 +404,6 @@ class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task { $this->assign_by_ref('paymentProcessor', $this->_paymentProcessor); } - - /** - * Get the currency in use. - * - * This just defaults to getting the default currency - * where the form does not have better currency support. - * - * @return string - */ - public function getCurrency(): string { - return (string) \Civi::settings()->get('defaultCurrency'); - } - /** * @param array $submittedValues * diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index 8db797ae7b..f37dc232d2 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -555,15 +555,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP return $defaults; } - /** - * Get submitted currency, or use default. - * - * @return string - */ - public function getCurrency(): string { - return (string) $this->getSubmittedValue('currency') ?: CRM_Core_Config::singleton()->defaultCurrency; - } - /** * Build the form object. * diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index ac4e5d27d7..c40d24d7a3 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -1502,4 +1502,25 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { } } + /** + * Get the currency for the form. + * + * Rather historic - might have unneeded stuff + * + * @return string + */ + public function getCurrency() { + $currency = $this->_values['currency'] ?? NULL; + // For event forms, currency is in a different spot + if (empty($currency)) { + $currency = CRM_Utils_Array::value('currency', CRM_Utils_Array::value('event', $this->_values)); + } + if (empty($currency)) { + $currency = CRM_Utils_Request::retrieveValue('currency', 'String'); + } + // @todo If empty there is a problem - we should probably put in a deprecation notice + // to warn if that seems to be happening. + return (string) $currency; + } + } diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 539f85b6e0..461a0c3275 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -2933,24 +2933,17 @@ class CRM_Core_Form extends HTML_QuickForm_Page { /** * Get the currency for the form. * - * @todo this should be overriden on the forms rather than having this - * historic, possible handling in here. As we clean that up we should - * add deprecation notices into here. - * * @return string */ public function getCurrency() { - $currency = $this->_values['currency'] ?? NULL; - // For event forms, currency is in a different spot - if (empty($currency)) { - $currency = CRM_Utils_Array::value('currency', CRM_Utils_Array::value('event', $this->_values)); + if ($this->getSubmittedValue('currency')) { + return $this->getSubmittedValue('currency'); } - if (empty($currency)) { - $currency = CRM_Utils_Request::retrieveValue('currency', 'String'); + $currency = CRM_Utils_Request::retrieveValue('currency', 'String'); + if ($currency) { + return $currency; } - // @todo If empty there is a problem - we should probably put in a deprecation notice - // to warn if that seems to be happening. - return $currency; + return \Civi::settings()->get('defaultCurrency'); } /** diff --git a/CRM/Event/Form/Registration.php b/CRM/Event/Form/Registration.php index 1c7a8fd96c..de3baa8de7 100644 --- a/CRM/Event/Form/Registration.php +++ b/CRM/Event/Form/Registration.php @@ -1727,4 +1727,25 @@ class CRM_Event_Form_Registration extends CRM_Core_Form { return $this->getPriceSetID() && CRM_Price_BAO_PriceSet::isQuickConfig($this->getPriceSetID()); } + /** + * Get the currency for the form. + * + * Rather historic - might have unneeded stuff + * + * @return string + */ + public function getCurrency() { + $currency = $this->_values['currency'] ?? NULL; + // For event forms, currency is in a different spot + if (empty($currency)) { + $currency = CRM_Utils_Array::value('currency', CRM_Utils_Array::value('event', $this->_values)); + } + if (empty($currency)) { + $currency = CRM_Utils_Request::retrieveValue('currency', 'String'); + } + // @todo If empty there is a problem - we should probably put in a deprecation notice + // to warn if that seems to be happening. + return $currency; + } + } diff --git a/CRM/Financial/Form/Payment.php b/CRM/Financial/Form/Payment.php index e1937e149b..f52d2cad70 100644 --- a/CRM/Financial/Form/Payment.php +++ b/CRM/Financial/Form/Payment.php @@ -69,13 +69,10 @@ class CRM_Financial_Form_Payment extends CRM_Core_Form { /** * Get currency * - * @param array $submittedValues - * Required for consistency with other form methods. - * * @return string */ - public function getCurrency($submittedValues = []) { - return $this->currency; + public function getCurrency(): string { + return (string) $this->currency; } /** -- 2.25.1