| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\FormMigration\Steps; |
| 4 |
|
| 5 |
use Give\FormMigration\Contracts\FormMigrationStep; |
| 6 |
use Give\Framework\Blocks\BlockModel; |
| 7 |
|
| 8 |
class DonationOptions extends FormMigrationStep |
| 9 |
{ |
| 10 |
public function process() |
| 11 |
{ |
| 12 |
/** @var BlockModel $amountField */ |
| 13 |
$amountField = $this->fieldBlocks->findByName('givewp/donation-amount'); |
| 14 |
|
| 15 |
$priceOption = $this->getMetaV2('_give_price_option'); |
| 16 |
$amountField->setAttribute('priceOption', $priceOption); |
| 17 |
|
| 18 |
if('set' === $priceOption) { |
| 19 |
$amountField->setAttribute('setPrice', $this->getMetaV2('_give_set_price')); |
| 20 |
} |
| 21 |
|
| 22 |
if('multi' === $priceOption) { |
| 23 |
// @note $formV2->levels only returns a single level |
| 24 |
$donationLevels = $this->getMetaV2('_give_donation_levels'); |
| 25 |
|
| 26 |
$isDescriptionEnabled = false; |
| 27 |
$amountField->setAttribute('levels', |
| 28 |
array_map(function ($donationLevel) use (&$isDescriptionEnabled) { |
| 29 |
$isDescriptionEnabled = $isDescriptionEnabled || ! empty($donationLevel['_give_text']); |
| 30 |
|
| 31 |
return [ |
| 32 |
'value' => $this->roundAmount($donationLevel['_give_amount']), |
| 33 |
'label' => $donationLevel['_give_text'], |
| 34 |
'checked' => isset($donationLevel['_give_default']) && $donationLevel['_give_default'] === 'default', |
| 35 |
]; |
| 36 |
}, $donationLevels) |
| 37 |
); |
| 38 |
$amountField->setAttribute('descriptionsEnabled', $isDescriptionEnabled); |
| 39 |
} |
| 40 |
|
| 41 |
$isCustomAmountEnabled = $this->formV2->isCustomAmountOptionEnabled(); |
| 42 |
$amountField->setAttribute('customAmount', $isCustomAmountEnabled); |
| 43 |
|
| 44 |
if ($isCustomAmountEnabled) { |
| 45 |
$customAmountMin = $this->getMetaV2('_give_custom_amount_range_minimum'); |
| 46 |
$customAmountMax = $this->getMetaV2('_give_custom_amount_range_maximum'); |
| 47 |
|
| 48 |
if ($customAmountMin) { |
| 49 |
$amountField->setAttribute('customAmountMin', $this->roundAmount($customAmountMin)); |
| 50 |
} |
| 51 |
|
| 52 |
if ($customAmountMax) { |
| 53 |
$amountField->setAttribute('customAmountMax', $this->roundAmount($customAmountMax)); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// @note No corresponding setting in v3 for "Custom Amount Text" |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @since 3.0.0 |
| 62 |
*/ |
| 63 |
private function roundAmount($amount): float |
| 64 |
{ |
| 65 |
return round((float)$amount, 2); |
| 66 |
} |
| 67 |
} |
| 68 |
|