Skip to content

Commit

Permalink
Merge pull request #26375 from larssandergreen/Refactor-price-sets
Browse files Browse the repository at this point in the history
[REF] Move repeated code for Price Field labels into separate function
  • Loading branch information
eileenmcnaughton authored Jun 5, 2023
2 parents 9cf73ea + 3785632 commit 03ab15f
Showing 1 changed file with 53 additions and 88 deletions.
141 changes: 53 additions & 88 deletions CRM/Price/BAO/PriceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,38 +370,15 @@ public static function addQuickFormElement(
}

foreach ($customOption as $opId => $opt) {
$preHelpText = $postHelpText = '';
$opt['label'] = !empty($opt['label']) ? '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' : '';
if (!empty($opt['help_pre'])) {
$preHelpText = '<span class="crm-price-amount-help-pre description">' . $opt['help_pre'] . '</span><span class="crm-price-amount-help-pre-separator">:&nbsp;</span>';
}
if (!empty($opt['help_post'])) {
$postHelpText = '<span class="crm-price-amount-help-post-separator">:&nbsp;</span><span class="crm-price-amount-help-post description">' . $opt['help_post'] . '</span>';
}

$taxAmount = $opt['tax_amount'] ?? NULL;
if ($field->is_display_amounts) {
$opt['label'] = !empty($opt['label']) ? $opt['label'] . '<span class="crm-price-amount-label-separator">&nbsp;-&nbsp;</span>' : '';
if (isset($taxAmount) && $invoicing) {
$opt['label'] = $opt['label'] . self::getTaxLabel($opt, $valueFieldName);
}
else {
$opt['label'] = $opt['label'] . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span>';
}
}

$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'];
}
else {
$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),
Expand All @@ -418,7 +395,7 @@ public static function addQuickFormElement(
$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']);
Expand All @@ -445,7 +422,7 @@ public static function addQuickFormElement(
$none = ts('- none -');
}

$choice['0'] = $none;
$choice['0'] = '<span class="crm-price-amount-label">' . $none . '</span>';
$choiceAttrs['0'] = ['price' => json_encode([$elementName, '0'])];
}

Expand All @@ -472,46 +449,22 @@ public static function addQuickFormElement(
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'] . ':&nbsp;';
}
if (!empty($opt['help_post'])) {
$postHelpText = ':&nbsp;' . $opt['help_post'];
}

$taxAmount = $opt['tax_amount'] ?? NULL;
if ($field->is_display_amounts) {
$opt['label'] = !empty($opt['label']) ? $opt['label'] . '&nbsp;-&nbsp;' : '';
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'];
}
// 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']);
Expand All @@ -535,7 +488,7 @@ public static function addQuickFormElement(

$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),
]);
Expand All @@ -548,46 +501,19 @@ public static function addQuickFormElement(
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']) ? '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' : '';
if (!empty($opt['help_pre'])) {
$preHelpText = '<span class="crm-price-amount-help-pre description">' . $opt['help_pre'] . '</span><span class="crm-price-amount-help-pre-separator">:&nbsp;</span>';
}
if (!empty($opt['help_post'])) {
$postHelpText = '<span class="crm-price-amount-help-post-separator">:&nbsp;</span><span class="crm-price-amount-help-post description">' . $opt['help_post'] . '</span>';
}

$taxAmount = $opt['tax_amount'] ?? NULL;
if ($field->is_display_amounts) {
$opt['label'] = !empty($opt['label']) ? $opt['label'] . '<span class="crm-price-amount-label-separator">&nbsp;-&nbsp;</span>' : '';
if (isset($taxAmount) && $invoicing) {
$opt['label'] = $opt['label'] . self::getTaxLabel($opt, $valueFieldName);
}
else {
$opt['label'] = $opt['label'] . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span>';
}
}

$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
Expand Down Expand Up @@ -840,6 +766,45 @@ public static function priceSetValidation($priceSetId, $fields, &$error, $allowN
}
}

/**
* 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']) ? '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' : '';
if (!empty($opt['help_pre'])) {
$preHelpText = '<span class="crm-price-amount-help-pre description">' . $opt['help_pre'] . '</span><span class="crm-price-amount-help-pre-separator">:&nbsp;</span>';
}
if (!empty($opt['help_post'])) {
$postHelpText = '<span class="crm-price-amount-help-post-separator">:&nbsp;</span><span class="crm-price-amount-help-post description">' . $opt['help_post'] . '</span>';
}

$invoicing = Civi::settings()->get('invoicing');
$taxAmount = $opt['tax_amount'] ?? NULL;
if ($isDisplayAmounts) {
$optionLabel = !empty($optionLabel) ? $optionLabel . '<span class="crm-price-amount-label-separator">&nbsp;-&nbsp;</span>' : '';
if (isset($taxAmount) && $invoicing) {
$optionLabel = $optionLabel . self::getTaxLabel($opt, $valueFieldName);
}
else {
$optionLabel = $optionLabel . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span>';
}
}

$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.
*
Expand Down

0 comments on commit 03ab15f

Please sign in to comment.