-
Notifications
You must be signed in to change notification settings - Fork 611
/
MoneyFieldConditionRule.php
136 lines (120 loc) · 4.11 KB
/
MoneyFieldConditionRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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. <support@pixelandtonic.com>
* @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);
}
}