-
Notifications
You must be signed in to change notification settings - Fork 642
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14148 from craftcms/feature/money-field-condition…
…-rule Update money condition rule to use money inputs
- Loading branch information
Showing
4 changed files
with
176 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
namespace craft\fields\conditions; | ||
|
||
use Craft; | ||
use craft\base\conditions\BaseNumberConditionRule; | ||
use craft\fields\Money; | ||
use craft\helpers\ArrayHelper; | ||
use craft\helpers\Cp; | ||
use craft\helpers\Html; | ||
use craft\helpers\MoneyHelper; | ||
use Money\Currency; | ||
use Money\Money as MoneyLibrary; | ||
|
||
/** | ||
* Money field condition rule. | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 5.0.0 | ||
*/ | ||
class MoneyFieldConditionRule extends BaseNumberConditionRule implements FieldConditionRuleInterface | ||
{ | ||
use FieldConditionRuleTrait; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function setAttributes($values, $safeOnly = true): void | ||
{ | ||
// Hold setting of the value attribute until we have all the info we need | ||
if (isset($values['value']) && is_array($values['value'])) { | ||
/** @var array $value */ | ||
$value = ArrayHelper::remove($values, 'value'); | ||
} | ||
|
||
if (isset($values['maxValue']) && is_array($values['maxValue'])) { | ||
/** @var array $maxValue */ | ||
$maxValue = ArrayHelper::remove($values, 'maxValue'); | ||
} | ||
|
||
parent::setAttributes($values, $safeOnly); | ||
|
||
/** @var Money $field */ | ||
$field = $this->field(); | ||
|
||
if (isset($value) && isset($this->_fieldUid)) { | ||
if (!isset($value['currency'])) { | ||
$value['currency'] = $field->currency; | ||
} | ||
$this->value = MoneyHelper::toDecimal(MoneyHelper::toMoney($value)); | ||
} | ||
|
||
if (isset($maxValue) && isset($this->_fieldUid)) { | ||
if (!isset($maxValue['currency'])) { | ||
$maxValue['currency'] = $field->currency; | ||
} | ||
$this->maxValue = MoneyHelper::toDecimal(MoneyHelper::toMoney($maxValue)); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function inputHtml(): string | ||
{ | ||
// don't show the value input if the condition checks for empty/notempty | ||
if ($this->operator === self::OPERATOR_EMPTY || $this->operator === self::OPERATOR_NOT_EMPTY) { | ||
return ''; | ||
} | ||
|
||
if ($this->operator === self::OPERATOR_BETWEEN) { | ||
return Html::tag('div', | ||
Html::hiddenLabel(Craft::t('app', 'Min Value'), 'min') . | ||
// Min value (value) input | ||
Cp::moneyInputHtml($this->inputOptions()) . | ||
Html::tag('span', Craft::t('app', 'and')) . | ||
Html::hiddenLabel(Craft::t('app', 'Max Value'), 'max') . | ||
// Max value input | ||
Cp::moneyInputHtml(array_merge( | ||
$this->inputOptions(), | ||
['id' => 'maxValue', 'name' => 'maxValue', 'value' => $this->maxValue] | ||
)) . | ||
Html::tag('span', Craft::t('app', 'The values are matched inclusively.'), ['class' => 'info']), | ||
['class' => 'flex flex-center'] | ||
); | ||
} | ||
|
||
return Cp::moneyInputHtml($this->inputOptions()); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function inputOptions(): array | ||
{ | ||
/** @var Money $field */ | ||
$field = $this->field(); | ||
$defaultValue = null; | ||
if ($field->defaultValue !== null) { | ||
$defaultValue = MoneyHelper::toNumber(new MoneyLibrary($field->defaultValue, new Currency($field->currency))); | ||
} | ||
|
||
return [ | ||
'type' => 'text', | ||
'id' => 'value', | ||
'name' => 'value', | ||
'value' => $this->value, | ||
'autocomplete' => false, | ||
'currency' => $field->currency, | ||
'currencyLabel' => $field->currencyLabel(), | ||
'showCurrency' => $field->showCurrency, | ||
'decimals' => $field->subunits(), | ||
'defaultValue' => $defaultValue, | ||
'describedBy' => $field->describedBy, | ||
'field' => $field, | ||
'showClear' => false, | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function elementQueryParam(): ?string | ||
{ | ||
return $this->paramValue(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function matchFieldValue($value): bool | ||
{ | ||
/** @var int|float|null $value */ | ||
return $this->matchValue($value); | ||
} | ||
} |