1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Units plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* A plugin for handling physical quantities and the units of measure in which |
6
|
|
|
* they're represented. |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\units\fields; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\base\ElementInterface; |
|
|
|
|
16
|
|
|
use craft\base\Field; |
17
|
|
|
use craft\base\PreviewableFieldInterface; |
18
|
|
|
use craft\helpers\Html; |
19
|
|
|
use craft\helpers\Json; |
20
|
|
|
use craft\i18n\Locale; |
21
|
|
|
use GraphQL\Type\Definition\Type; |
22
|
|
|
use nystudio107\units\assetbundles\unitsfield\UnitsFieldAsset; |
23
|
|
|
use nystudio107\units\gql\types\generators\UnitsDataGenerator; |
24
|
|
|
use nystudio107\units\helpers\ClassHelper; |
25
|
|
|
use nystudio107\units\models\Settings; |
26
|
|
|
use nystudio107\units\models\UnitsData; |
27
|
|
|
use nystudio107\units\Units as UnitsPlugin; |
28
|
|
|
use nystudio107\units\validators\EmbeddedUnitsDataValidator; |
29
|
|
|
use PhpUnitsOfMeasure\PhysicalQuantity\Length; |
30
|
|
|
use yii\base\InvalidConfigException; |
31
|
|
|
|
32
|
|
|
/** |
|
|
|
|
33
|
|
|
* @author nystudio107 |
|
|
|
|
34
|
|
|
* @package Units |
|
|
|
|
35
|
|
|
* @since 1.0.0 |
|
|
|
|
36
|
|
|
*/ |
|
|
|
|
37
|
|
|
class Units extends Field implements PreviewableFieldInterface |
38
|
|
|
{ |
39
|
|
|
// Static Methods |
40
|
|
|
// ========================================================================= |
41
|
|
|
|
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* @var string The default fully qualified class name of the unit of measure |
44
|
|
|
*/ |
45
|
|
|
public string $defaultUnitsClass; |
46
|
|
|
|
47
|
|
|
// Public Properties |
48
|
|
|
// ========================================================================= |
49
|
|
|
/** |
|
|
|
|
50
|
|
|
* @var ?float The default value of the unit of measure |
51
|
|
|
*/ |
52
|
|
|
public ?float $defaultValue = null; |
53
|
|
|
/** |
|
|
|
|
54
|
|
|
* @var ?string The default units that the unit of measure is in |
55
|
|
|
*/ |
56
|
|
|
public ?string $defaultUnits = null; |
57
|
|
|
/** |
|
|
|
|
58
|
|
|
* @var ?bool Whether the units the field can be changed |
59
|
|
|
*/ |
60
|
|
|
public ?bool $changeableUnits = null; |
61
|
|
|
/** |
|
|
|
|
62
|
|
|
* @var int|float|null The minimum allowed number |
63
|
|
|
*/ |
64
|
|
|
public int|float|null $min = null; |
65
|
|
|
/** |
|
|
|
|
66
|
|
|
* @var int|float|null The maximum allowed number |
67
|
|
|
*/ |
68
|
|
|
public int|float|null $max = null; |
69
|
|
|
/** |
|
|
|
|
70
|
|
|
* @var ?int The number of digits allowed after the decimal point |
71
|
|
|
*/ |
72
|
|
|
public ?int $decimals = null; |
73
|
|
|
/** |
|
|
|
|
74
|
|
|
* @var ?int The size of the field |
75
|
|
|
*/ |
76
|
|
|
public ?int $size = null; |
77
|
|
|
|
78
|
|
|
/** |
|
|
|
|
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
|
|
|
|
81
|
|
|
public static function displayName(): string |
82
|
|
|
{ |
83
|
|
|
return Craft::t('units', 'Units'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
|
|
|
|
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
|
|
|
|
89
|
|
|
public static function icon(): string |
90
|
|
|
{ |
91
|
|
|
return 'scale-balanced'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Public Methods |
95
|
|
|
// ========================================================================= |
96
|
|
|
|
97
|
|
|
/** |
|
|
|
|
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
|
|
|
|
100
|
|
|
public function init(): void |
101
|
|
|
{ |
102
|
|
|
parent::init(); |
103
|
|
|
if (UnitsPlugin::$plugin !== null) { |
104
|
|
|
/** @var Settings $settings */ |
|
|
|
|
105
|
|
|
$settings = UnitsPlugin::$plugin->getSettings(); |
106
|
|
|
if ($settings !== null) { |
107
|
|
|
$this->defaultUnitsClass = $this->defaultUnitsClass ?? $settings->defaultUnitsClass; |
108
|
|
|
$this->defaultValue = $this->defaultValue ?? $settings->defaultValue; |
109
|
|
|
$this->defaultUnits = $this->defaultUnits ?? $settings->defaultUnits; |
110
|
|
|
$this->changeableUnits = $this->changeableUnits ?? $settings->defaultChangeableUnits; |
111
|
|
|
$this->min = $this->min ?? $settings->defaultMin; |
112
|
|
|
$this->max = $this->max ?? $settings->defaultMax; |
113
|
|
|
$this->decimals = $this->decimals ?? $settings->defaultDecimals; |
114
|
|
|
$this->size = $this->size ?? $settings->defaultSize; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
|
|
|
|
120
|
|
|
* @inheritdoc |
121
|
|
|
*/ |
|
|
|
|
122
|
|
|
public function rules(): array |
123
|
|
|
{ |
124
|
|
|
$rules = parent::rules(); |
125
|
|
|
$rules = array_merge($rules, [ |
|
|
|
|
126
|
|
|
['defaultUnitsClass', 'string'], |
127
|
|
|
['defaultValue', 'number'], |
128
|
|
|
['defaultUnits', 'string'], |
129
|
|
|
['changeableUnits', 'boolean'], |
130
|
|
|
[['min', 'max'], 'number'], |
131
|
|
|
[['decimals', 'size'], 'integer'], |
132
|
|
|
[ |
133
|
|
|
['max'], |
134
|
|
|
'compare', |
135
|
|
|
'compareAttribute' => 'min', |
136
|
|
|
'operator' => '>=', |
137
|
|
|
], |
138
|
|
|
]); |
|
|
|
|
139
|
|
|
|
140
|
|
|
if (!$this->decimals) { |
|
|
|
|
141
|
|
|
$rules[] = [['min', 'max'], 'integer']; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $rules; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
|
|
|
|
148
|
|
|
* @inheritdoc |
149
|
|
|
*/ |
|
|
|
|
150
|
|
|
public function normalizeValue(mixed $value, ?ElementInterface $element = null): mixed |
151
|
|
|
{ |
152
|
|
|
if ($value instanceof UnitsData) { |
153
|
|
|
return $value; |
154
|
|
|
} |
155
|
|
|
// Default config |
156
|
|
|
$config = [ |
157
|
|
|
'unitsClass' => $this->defaultUnitsClass, |
158
|
|
|
'value' => $this->defaultValue, |
159
|
|
|
'units' => $this->defaultUnits, |
160
|
|
|
]; |
161
|
|
|
// Handle incoming values potentially being JSON or an array |
162
|
|
|
if (!empty($value)) { |
163
|
|
|
// Handle a numeric value coming in (perhaps from a Number field) |
164
|
|
|
if (is_numeric($value)) { |
165
|
|
|
$config['value'] = (float)$value; |
166
|
|
|
} elseif (is_string($value)) { |
167
|
|
|
$config = Json::decodeIfJson($value); |
168
|
|
|
} |
169
|
|
|
if (is_array($value)) { |
170
|
|
|
$config = array_merge($config, array_filter($value)); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
// Typecast it to a float |
174
|
|
|
$config['value'] = (float)$config['value']; |
175
|
|
|
// Create and validate the model |
176
|
|
|
$unitsData = new UnitsData($config); |
177
|
|
|
if (!$unitsData->validate()) { |
178
|
|
|
Craft::error( |
179
|
|
|
Craft::t('units', 'UnitsData failed validation: ') |
180
|
|
|
. print_r($unitsData->getErrors(), true), |
|
|
|
|
181
|
|
|
__METHOD__ |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $unitsData; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
|
|
|
|
189
|
|
|
* @inheritdoc |
190
|
|
|
*/ |
|
|
|
|
191
|
|
|
public function getSettingsHtml(): ?string |
192
|
|
|
{ |
193
|
|
|
$unitsClassMap = array_flip(ClassHelper::getClassesInNamespace(Length::class)); |
194
|
|
|
|
195
|
|
|
// Render the settings template |
196
|
|
|
return Craft::$app->getView()->renderTemplate( |
197
|
|
|
'units/_components/fields/Units_settings', |
198
|
|
|
[ |
199
|
|
|
'field' => $this, |
200
|
|
|
'unitsClassMap' => $unitsClassMap, |
201
|
|
|
] |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
|
|
|
|
206
|
|
|
* @inheritdoc |
207
|
|
|
*/ |
|
|
|
|
208
|
|
|
public function getInputHtml(mixed $value, ?ElementInterface $element = null): string |
209
|
|
|
{ |
210
|
|
|
if ($value instanceof UnitsData) { |
211
|
|
|
// Register our asset bundle |
212
|
|
|
try { |
213
|
|
|
Craft::$app->getView()->registerAssetBundle(UnitsFieldAsset::class); |
214
|
|
|
} catch (InvalidConfigException $e) { |
215
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
216
|
|
|
} |
217
|
|
|
$model = $value; |
218
|
|
|
$value = $model->value; |
219
|
|
|
$decimals = $this->decimals; |
220
|
|
|
// If decimals is 0 (or null, empty for whatever reason), don't run this |
221
|
|
|
if ($decimals) { |
|
|
|
|
222
|
|
|
$decimalSeparator = Craft::$app->getLocale()->getNumberSymbol(Locale::SYMBOL_DECIMAL_SEPARATOR); |
223
|
|
|
$value = number_format($value, $decimals, $decimalSeparator, ''); |
|
|
|
|
224
|
|
|
} |
225
|
|
|
// Get our id and namespace |
226
|
|
|
$id = Html::id($this->handle); |
227
|
|
|
$namespacedId = Craft::$app->getView()->namespaceInputId($id); |
228
|
|
|
|
229
|
|
|
// Variables to pass down to our field JavaScript to let it namespace properly |
230
|
|
|
$jsonVars = [ |
231
|
|
|
'id' => $id, |
232
|
|
|
'name' => $this->handle, |
233
|
|
|
'namespace' => $namespacedId, |
234
|
|
|
'prefix' => Craft::$app->getView()->namespaceInputId(''), |
235
|
|
|
]; |
236
|
|
|
$jsonVars = Json::encode($jsonVars); |
237
|
|
|
Craft::$app->getView()->registerJs("$('#{$namespacedId}-field').UnitsUnits(" . $jsonVars . ");"); |
238
|
|
|
|
239
|
|
|
// Render the input template |
240
|
|
|
return Craft::$app->getView()->renderTemplate( |
241
|
|
|
'units/_components/fields/Units_input', |
242
|
|
|
[ |
243
|
|
|
'name' => $this->handle, |
244
|
|
|
'field' => $this, |
245
|
|
|
'id' => $id, |
246
|
|
|
'namespacedId' => $namespacedId, |
247
|
|
|
'value' => $value, |
248
|
|
|
'model' => $model, |
249
|
|
|
] |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return ''; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
|
|
|
|
257
|
|
|
* @inheritdoc |
258
|
|
|
*/ |
|
|
|
|
259
|
|
|
public function getContentGqlType(): Type|array |
260
|
|
|
{ |
261
|
|
|
$typeArray = UnitsDataGenerator::generateTypes($this); |
262
|
|
|
|
263
|
|
|
return [ |
264
|
|
|
'name' => $this->handle, |
265
|
|
|
'description' => 'Units field', |
266
|
|
|
'type' => array_shift($typeArray), |
267
|
|
|
]; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
|
|
|
|
271
|
|
|
* @inheritdoc |
272
|
|
|
*/ |
|
|
|
|
273
|
|
|
public function getElementValidationRules(): array |
274
|
|
|
{ |
275
|
|
|
return [ |
276
|
|
|
[ |
277
|
|
|
EmbeddedUnitsDataValidator::class, |
278
|
|
|
'units' => $this->defaultUnits, |
279
|
|
|
'integerOnly' => !$this->decimals, |
280
|
|
|
'min' => $this->min, |
281
|
|
|
'max' => $this->max, |
282
|
|
|
], |
283
|
|
|
]; |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|