From: larssandergreen Date: Mon, 29 May 2023 22:48:34 +0000 (-0600) Subject: Move repeated code into separate function X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=3785632ad5f868862e1d168edd7492bab189ad89;p=civicrm-core.git Move repeated code into separate function --- diff --git a/CRM/Price/BAO/PriceField.php b/CRM/Price/BAO/PriceField.php index 9ff1d0cc93..9813cbdbc5 100644 --- a/CRM/Price/BAO/PriceField.php +++ b/CRM/Price/BAO/PriceField.php @@ -370,30 +370,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { } foreach ($customOption as $opId => $opt) { - $preHelpText = $postHelpText = ''; - $opt['label'] = !empty($opt['label']) ? '' . $opt['label'] . '' : ''; - if (!empty($opt['help_pre'])) { - $preHelpText = '' . $opt['help_pre'] . ''; - } - if (!empty($opt['help_post'])) { - $postHelpText = '' . $opt['help_post'] . ''; - } - - $taxAmount = $opt['tax_amount'] ?? NULL; - if ($field->is_display_amounts) { - $opt['label'] = !empty($opt['label']) ? $opt['label'] . ' - ' : ''; - if (isset($taxAmount) && $invoicing) { - $opt['label'] = $opt['label'] . self::getTaxLabel($opt, $valueFieldName); - } - else { - $opt['label'] = $opt['label'] . '' . CRM_Utils_Money::format($opt[$valueFieldName]) . ''; - } - } - - $opt['label'] = $preHelpText . $opt['label'] . $postHelpText; - $count = CRM_Utils_Array::value('count', $opt, ''); - $max_value = CRM_Utils_Array::value('max_value', $opt, ''); - $priceVal = implode($separator, [$opt[$valueFieldName] + $taxAmount, $count, $max_value]); + $priceOptionText = self::buildPriceOptionText($opt, $field->is_display_amounts, $valueFieldName); if (isset($opt['visibility_id'])) { $visibility_id = $opt['visibility_id']; } @@ -401,7 +378,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { $visibility_id = self::getVisibilityOptionID('public'); } $extra = [ - 'price' => json_encode([$elementName, $priceVal]), + 'price' => json_encode([$elementName, $priceOptionText['priceVal']]), 'data-amount' => $opt[$valueFieldName], 'data-currency' => $currencyName, 'data-price-field-values' => json_encode($customOption), @@ -418,7 +395,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { $qf->assign('membershipFieldID', $field->id); } - $choice[$opt['id']] = $opt['label']; + $choice[$opt['id']] = $priceOptionText['label']; $choiceAttrs[$opt['id']] = $extra; if ($is_pay_later) { $qf->add('text', 'txt-' . $elementName, $label, ['size' => '4']); @@ -445,7 +422,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { $none = ts('- none -'); } - $choice['0'] = $none; + $choice['0'] = '' . $none . ''; $choiceAttrs['0'] = ['price' => json_encode([$elementName, '0'])]; } @@ -472,35 +449,11 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { break; case 'Select': - $selectOption = $allowedOptions = $priceVal = []; + $selectOption = $allowedOptions = []; foreach ($customOption as $opt) { - $taxAmount = $opt['tax_amount'] ?? NULL; - $count = CRM_Utils_Array::value('count', $opt, ''); - $max_value = CRM_Utils_Array::value('max_value', $opt, ''); - - $preHelpText = $postHelpText = ''; - if (!empty($opt['help_pre'])) { - $preHelpText = $opt['help_pre'] . ': '; - } - if (!empty($opt['help_post'])) { - $postHelpText = ': ' . $opt['help_post']; - } - - $taxAmount = $opt['tax_amount'] ?? NULL; - if ($field->is_display_amounts) { - $opt['label'] = !empty($opt['label']) ? $opt['label'] . ' - ' : ''; - if (isset($taxAmount) && $invoicing) { - $opt['label'] = $opt['label'] . self::getTaxLabel($opt, $valueFieldName); - } - else { - $opt['label'] = $opt['label'] . CRM_Utils_Money::format($opt[$valueFieldName]); - } - } - - $opt['label'] = $preHelpText . $opt['label'] . $postHelpText; - - $priceVal[$opt['id']] = implode($separator, [$opt[$valueFieldName] + $taxAmount, $count, $max_value]); + $priceOptionText = self::buildPriceOptionText($opt, $field->is_display_amounts, $valueFieldName); + $priceOptionText['label'] = strip_tags($priceOptionText['label']); if (!in_array($opt['id'], $freezeOptions)) { $allowedOptions[] = $opt['id']; @@ -508,10 +461,10 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { // CRM-14696 - Improve display for sold out price set options else { $opt['id'] = 'crm_disabled_opt-' . $opt['id']; - $opt['label'] = $opt['label'] . ' (' . ts('Sold out') . ')'; + $priceOptionText['label'] = $priceOptionText['label'] . ' (' . ts('Sold out') . ')'; } - $selectOption[$opt['id']] = $opt['label']; + $selectOption[$opt['id']] = $priceOptionText['label']; if ($is_pay_later) { $qf->add('text', 'txt-' . $elementName, $label, ['size' => '4']); @@ -535,7 +488,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { $element = &$qf->add('select', $elementName, $label, $selectOption, $useRequired && $field->is_required, [ 'placeholder' => ts('- select %1 -', [1 => $label]), - 'price' => json_encode($priceVal), + 'price' => json_encode($priceOptionText['priceVal']), 'class' => 'crm-select2' . $class, 'data-price-field-values' => json_encode($customOption), ]); @@ -548,46 +501,19 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { break; case 'CheckBox': - $check = []; foreach ($customOption as $opId => $opt) { - $taxAmount = $opt['tax_amount'] ?? NULL; - $count = CRM_Utils_Array::value('count', $opt, ''); - $max_value = CRM_Utils_Array::value('max_value', $opt, ''); - - $preHelpText = $postHelpText = ''; - $opt['label'] = !empty($opt['label']) ? '' . $opt['label'] . '' : ''; - if (!empty($opt['help_pre'])) { - $preHelpText = '' . $opt['help_pre'] . ''; - } - if (!empty($opt['help_post'])) { - $postHelpText = '' . $opt['help_post'] . ''; - } - - $taxAmount = $opt['tax_amount'] ?? NULL; - if ($field->is_display_amounts) { - $opt['label'] = !empty($opt['label']) ? $opt['label'] . ' - ' : ''; - if (isset($taxAmount) && $invoicing) { - $opt['label'] = $opt['label'] . self::getTaxLabel($opt, $valueFieldName); - } - else { - $opt['label'] = $opt['label'] . '' . CRM_Utils_Money::format($opt[$valueFieldName]) . ''; - } - } - - $opt['label'] = $preHelpText . $opt['label'] . $postHelpText; - - $priceVal = implode($separator, [$opt[$valueFieldName] + $taxAmount, $count, $max_value]); - $check[$opId] = &$qf->createElement('checkbox', $opt['id'], NULL, $opt['label'], + $priceOptionText = self::buildPriceOptionText($opt, $field->is_display_amounts, $valueFieldName); + $check[$opId] = &$qf->createElement('checkbox', $opt['id'], NULL, $priceOptionText['label'], [ - 'price' => json_encode([$opt['id'], $priceVal]), + 'price' => json_encode([$opt['id'], $priceOptionText['priceVal']]), 'data-amount' => $opt[$valueFieldName], 'data-currency' => $currencyName, 'visibility' => $opt['visibility_id'], ] ); if ($is_pay_later) { - $txtcheck[$opId] =& $qf->createElement('text', $opId, $opt['label'], ['size' => '4']); + $txtcheck[$opId] =& $qf->createElement('text', $opId, $priceOptionText['label'], ['size' => '4']); $qf->addGroup($txtcheck, 'txt-' . $elementName, $label); } // CRM-6902 - Add "max" option for a price set field @@ -840,6 +766,45 @@ WHERE id IN (" . implode(',', array_keys($priceFields)) . ')'; } } + /** + * Build the label and priceVal string for a price option. + * + * @param array $opt + * Price field option. + * @param bool $isDisplayAmounts + * @param string $valueFieldName + * + * @return array + * Price field option label, price value + */ + public static function buildPriceOptionText($opt, $isDisplayAmounts, $valueFieldName) { + $preHelpText = $postHelpText = ''; + $optionLabel = !empty($opt['label']) ? '' . $opt['label'] . '' : ''; + if (!empty($opt['help_pre'])) { + $preHelpText = '' . $opt['help_pre'] . ''; + } + if (!empty($opt['help_post'])) { + $postHelpText = '' . $opt['help_post'] . ''; + } + + $invoicing = Civi::settings()->get('invoicing'); + $taxAmount = $opt['tax_amount'] ?? NULL; + if ($isDisplayAmounts) { + $optionLabel = !empty($optionLabel) ? $optionLabel . ' - ' : ''; + if (isset($taxAmount) && $invoicing) { + $optionLabel = $optionLabel . self::getTaxLabel($opt, $valueFieldName); + } + else { + $optionLabel = $optionLabel . '' . CRM_Utils_Money::format($opt[$valueFieldName]) . ''; + } + } + + $optionLabel = $preHelpText . $optionLabel . $postHelpText; + $priceVal = implode('|', [$opt[$valueFieldName] + $taxAmount, $opt['count'] ?? '', $opt['max_value'] ?? '']); + + return ['label' => $optionLabel, 'priceVal' => $priceVal]; + } + /** * Generate the label for price fields based on tax display setting option on CiviContribute Component Settings page. *