From 045915ef4ed163c08fb1691aafa66f6d95380a15 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 16 Dec 2022 12:30:35 +1300 Subject: [PATCH] Divide & conquer, initSet no longer shared as much code is not --- CRM/Contribute/Form/ContributionBase.php | 102 ++++++++++++++++++++++- CRM/Event/Form/Registration.php | 102 ++++++++++++++++++++++- CRM/Price/BAO/PriceSet.php | 2 +- 3 files changed, 203 insertions(+), 3 deletions(-) diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index d75498099a..c917eb7daa 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -361,7 +361,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { // get price info // CRM-5095 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id); - CRM_Price_BAO_PriceSet::initSet($this, 'civicrm_contribution_page', FALSE, $priceSetId); + $this->initSet($this, 'civicrm_contribution_page', FALSE, $priceSetId); // this avoids getting E_NOTICE errors in php $setNullFields = [ @@ -502,6 +502,106 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { } } + /** + * Initiate price set such that various non-BAO things are set on the form. + * + * This function is not really a BAO function so the location is misleading. + * + * @param CRM_Core_Form $form + * Form entity id. + * @param string $entityTable + * @param bool $doNotIncludeExpiredFields + * @param int $priceSetId + * Price Set ID + * + * @todo - removed unneeded code from previously-shared function + */ + private function initSet(&$form, $entityTable = 'civicrm_event', $doNotIncludeExpiredFields = FALSE, $priceSetId = NULL) { + + //check if price set is is_config + if (is_numeric($priceSetId)) { + if (CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config') && $form->getVar('_name') != 'Participant') { + $form->assign('quickConfig', 1); + } + } + // get price info + if ($priceSetId) { + if ($form->_action & CRM_Core_Action::UPDATE) { + $entityId = $entity = NULL; + + switch ($entityTable) { + case 'civicrm_event': + $entity = 'participant'; + if (in_array(CRM_Utils_System::getClassName($form), ['CRM_Event_Form_Participant', 'CRM_Event_Form_Task_Register'])) { + $entityId = $form->_id; + } + else { + $entityId = $form->_participantId; + } + break; + + case 'civicrm_contribution_page': + case 'civicrm_contribution': + $entity = 'contribution'; + $entityId = $form->_id; + break; + } + + if ($entityId && $entity) { + $form->_values['line_items'] = CRM_Price_BAO_LineItem::getLineItems($entityId, $entity); + } + $required = FALSE; + } + else { + $required = TRUE; + } + + $form->_priceSetId = $priceSetId; + $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, $required, $doNotIncludeExpiredFields); + $form->_priceSet = $priceSet[$priceSetId] ?? NULL; + $form->_values['fee'] = $form->_priceSet['fields'] ?? NULL; + + //get the price set fields participant count. + if ($entityTable == 'civicrm_event') { + //get option count info. + $form->_priceSet['optionsCountTotal'] = CRM_Price_BAO_PriceSet::getPricesetCount($priceSetId); + if ($form->_priceSet['optionsCountTotal']) { + $optionsCountDetails = []; + if (!empty($form->_priceSet['fields'])) { + foreach ($form->_priceSet['fields'] as $field) { + foreach ($field['options'] as $option) { + $count = CRM_Utils_Array::value('count', $option, 0); + $optionsCountDetails['fields'][$field['id']]['options'][$option['id']] = $count; + } + } + } + $form->_priceSet['optionsCountDetails'] = $optionsCountDetails; + } + + //get option max value info. + $optionsMaxValueTotal = 0; + $optionsMaxValueDetails = []; + + if (!empty($form->_priceSet['fields'])) { + foreach ($form->_priceSet['fields'] as $field) { + foreach ($field['options'] as $option) { + $maxVal = CRM_Utils_Array::value('max_value', $option, 0); + $optionsMaxValueDetails['fields'][$field['id']]['options'][$option['id']] = $maxVal; + $optionsMaxValueTotal += $maxVal; + } + } + } + + $form->_priceSet['optionsMaxValueTotal'] = $optionsMaxValueTotal; + if ($optionsMaxValueTotal) { + $form->_priceSet['optionsMaxValueDetails'] = $optionsMaxValueDetails; + } + } + $form->set('priceSetId', $form->_priceSetId); + $form->set('priceSet', $form->_priceSet); + } + } + /** * Set the default values. */ diff --git a/CRM/Event/Form/Registration.php b/CRM/Event/Form/Registration.php index 6da4f9dfa6..b22ac78543 100644 --- a/CRM/Event/Form/Registration.php +++ b/CRM/Event/Form/Registration.php @@ -586,7 +586,7 @@ class CRM_Event_Form_Registration extends CRM_Core_Form { else { $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventID); } - CRM_Price_BAO_PriceSet::initSet($form, 'civicrm_event', $doNotIncludeExpiredFields, $priceSetId); + self::initSet($form, 'civicrm_event', $doNotIncludeExpiredFields, $priceSetId); if (property_exists($form, '_context') && ($form->_context == 'standalone' || $form->_context == 'participant') @@ -636,6 +636,106 @@ class CRM_Event_Form_Registration extends CRM_Core_Form { } } + /** + * Initiate price set such that various non-BAO things are set on the form. + * + * This function is not really a BAO function so the location is misleading. + * + * @param CRM_Core_Form $form + * Form entity id. + * @param string $entityTable + * @param bool $doNotIncludeExpiredFields + * @param int $priceSetId + * Price Set ID + * + * @todo - removed unneeded code from previously-shared function + */ + private static function initSet(&$form, $entityTable = 'civicrm_event', $doNotIncludeExpiredFields = FALSE, $priceSetId = NULL) { + + //check if price set is is_config + if (is_numeric($priceSetId)) { + if (CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config') && $form->getVar('_name') != 'Participant') { + $form->assign('quickConfig', 1); + } + } + // get price info + if ($priceSetId) { + if ($form->_action & CRM_Core_Action::UPDATE) { + $entityId = $entity = NULL; + + switch ($entityTable) { + case 'civicrm_event': + $entity = 'participant'; + if (in_array(CRM_Utils_System::getClassName($form), ['CRM_Event_Form_Participant', 'CRM_Event_Form_Task_Register'])) { + $entityId = $form->_id; + } + else { + $entityId = $form->_participantId; + } + break; + + case 'civicrm_contribution_page': + case 'civicrm_contribution': + $entity = 'contribution'; + $entityId = $form->_id; + break; + } + + if ($entityId && $entity) { + $form->_values['line_items'] = CRM_Price_BAO_LineItem::getLineItems($entityId, $entity); + } + $required = FALSE; + } + else { + $required = TRUE; + } + + $form->_priceSetId = $priceSetId; + $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, $required, $doNotIncludeExpiredFields); + $form->_priceSet = $priceSet[$priceSetId] ?? NULL; + $form->_values['fee'] = $form->_priceSet['fields'] ?? NULL; + + //get the price set fields participant count. + if ($entityTable == 'civicrm_event') { + //get option count info. + $form->_priceSet['optionsCountTotal'] = CRM_Price_BAO_PriceSet::getPricesetCount($priceSetId); + if ($form->_priceSet['optionsCountTotal']) { + $optionsCountDetails = []; + if (!empty($form->_priceSet['fields'])) { + foreach ($form->_priceSet['fields'] as $field) { + foreach ($field['options'] as $option) { + $count = CRM_Utils_Array::value('count', $option, 0); + $optionsCountDetails['fields'][$field['id']]['options'][$option['id']] = $count; + } + } + } + $form->_priceSet['optionsCountDetails'] = $optionsCountDetails; + } + + //get option max value info. + $optionsMaxValueTotal = 0; + $optionsMaxValueDetails = []; + + if (!empty($form->_priceSet['fields'])) { + foreach ($form->_priceSet['fields'] as $field) { + foreach ($field['options'] as $option) { + $maxVal = CRM_Utils_Array::value('max_value', $option, 0); + $optionsMaxValueDetails['fields'][$field['id']]['options'][$option['id']] = $maxVal; + $optionsMaxValueTotal += $maxVal; + } + } + } + + $form->_priceSet['optionsMaxValueTotal'] = $optionsMaxValueTotal; + if ($optionsMaxValueTotal) { + $form->_priceSet['optionsMaxValueDetails'] = $optionsMaxValueDetails; + } + } + $form->set('priceSetId', $form->_priceSetId); + $form->set('priceSet', $form->_priceSet); + } + } + /** * Handle process after the confirmation of payment by User. * diff --git a/CRM/Price/BAO/PriceSet.php b/CRM/Price/BAO/PriceSet.php index a0aeb3bc09..bfa8565048 100644 --- a/CRM/Price/BAO/PriceSet.php +++ b/CRM/Price/BAO/PriceSet.php @@ -555,7 +555,7 @@ WHERE id = %1"; * Price Set ID */ public static function initSet(&$form, $entityTable = 'civicrm_event', $doNotIncludeExpiredFields = FALSE, $priceSetId = NULL) { - + CRM_Core_Error::deprecatedFunctionWarning('no alternative'); //check if price set is is_config if (is_numeric($priceSetId)) { if (CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config') && $form->getVar('_name') != 'Participant') { -- 2.25.1