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
|
|
|
// Public Methods |
87
|
|
|
// ========================================================================= |
88
|
|
|
|
89
|
|
|
/** |
|
|
|
|
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
|
|
|
|
92
|
|
|
public function init(): void |
93
|
|
|
{ |
94
|
|
|
parent::init(); |
95
|
|
|
if (UnitsPlugin::$plugin !== null) { |
96
|
|
|
/** @var Settings $settings */ |
|
|
|
|
97
|
|
|
$settings = UnitsPlugin::$plugin->getSettings(); |
98
|
|
|
if ($settings !== null) { |
99
|
|
|
$this->defaultUnitsClass = $this->defaultUnitsClass ?? $settings->defaultUnitsClass; |
100
|
|
|
$this->defaultValue = $this->defaultValue ?? $settings->defaultValue; |
101
|
|
|
$this->defaultUnits = $this->defaultUnits ?? $settings->defaultUnits; |
102
|
|
|
$this->changeableUnits = $this->changeableUnits ?? $settings->defaultChangeableUnits; |
103
|
|
|
$this->min = $this->min ?? $settings->defaultMin; |
104
|
|
|
$this->max = $this->max ?? $settings->defaultMax; |
105
|
|
|
$this->decimals = $this->decimals ?? $settings->defaultDecimals; |
106
|
|
|
$this->size = $this->size ?? $settings->defaultSize; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
|
|
|
|
112
|
|
|
* @inheritdoc |
113
|
|
|
*/ |
|
|
|
|
114
|
|
|
public function rules(): array |
115
|
|
|
{ |
116
|
|
|
$rules = parent::rules(); |
117
|
|
|
$rules = array_merge($rules, [ |
|
|
|
|
118
|
|
|
['defaultUnitsClass', 'string'], |
119
|
|
|
['defaultValue', 'number'], |
120
|
|
|
['defaultUnits', 'string'], |
121
|
|
|
['changeableUnits', 'boolean'], |
122
|
|
|
[['min', 'max'], 'number'], |
123
|
|
|
[['decimals', 'size'], 'integer'], |
124
|
|
|
[ |
125
|
|
|
['max'], |
126
|
|
|
'compare', |
127
|
|
|
'compareAttribute' => 'min', |
128
|
|
|
'operator' => '>=', |
129
|
|
|
], |
130
|
|
|
]); |
|
|
|
|
131
|
|
|
|
132
|
|
|
if (!$this->decimals) { |
|
|
|
|
133
|
|
|
$rules[] = [['min', 'max'], 'integer']; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $rules; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
|
|
|
|
140
|
|
|
* @inheritdoc |
141
|
|
|
*/ |
|
|
|
|
142
|
|
|
public function normalizeValue(mixed $value, ?ElementInterface $element = null): mixed |
143
|
|
|
{ |
144
|
|
|
if ($value instanceof UnitsData) { |
145
|
|
|
return $value; |
146
|
|
|
} |
147
|
|
|
// Default config |
148
|
|
|
$config = [ |
149
|
|
|
'unitsClass' => $this->defaultUnitsClass, |
150
|
|
|
'value' => $this->defaultValue, |
151
|
|
|
'units' => $this->defaultUnits, |
152
|
|
|
]; |
153
|
|
|
// Handle incoming values potentially being JSON or an array |
154
|
|
|
if (!empty($value)) { |
155
|
|
|
// Handle a numeric value coming in (perhaps from a Number field) |
156
|
|
|
if (is_numeric($value)) { |
157
|
|
|
$config['value'] = (float)$value; |
158
|
|
|
} elseif (is_string($value)) { |
159
|
|
|
$config = Json::decodeIfJson($value); |
160
|
|
|
} |
161
|
|
|
if (is_array($value)) { |
162
|
|
|
$config = array_merge($config, array_filter($value)); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
// Typecast it to a float |
166
|
|
|
$config['value'] = (float)$config['value']; |
167
|
|
|
// Create and validate the model |
168
|
|
|
$unitsData = new UnitsData($config); |
169
|
|
|
if (!$unitsData->validate()) { |
170
|
|
|
Craft::error( |
171
|
|
|
Craft::t('units', 'UnitsData failed validation: ') |
172
|
|
|
. print_r($unitsData->getErrors(), true), |
|
|
|
|
173
|
|
|
__METHOD__ |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $unitsData; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
|
|
|
|
181
|
|
|
* @inheritdoc |
182
|
|
|
*/ |
|
|
|
|
183
|
|
|
public function getSettingsHtml(): ?string |
184
|
|
|
{ |
185
|
|
|
$unitsClassMap = array_flip(ClassHelper::getClassesInNamespace(Length::class)); |
186
|
|
|
|
187
|
|
|
// Render the settings template |
188
|
|
|
return Craft::$app->getView()->renderTemplate( |
189
|
|
|
'units/_components/fields/Units_settings', |
190
|
|
|
[ |
191
|
|
|
'field' => $this, |
192
|
|
|
'unitsClassMap' => $unitsClassMap, |
193
|
|
|
] |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
|
|
|
|
198
|
|
|
* @inheritdoc |
199
|
|
|
*/ |
|
|
|
|
200
|
|
|
public function getInputHtml(mixed $value, ?ElementInterface $element = null): string |
201
|
|
|
{ |
202
|
|
|
if ($value instanceof UnitsData) { |
203
|
|
|
// Register our asset bundle |
204
|
|
|
try { |
205
|
|
|
Craft::$app->getView()->registerAssetBundle(UnitsFieldAsset::class); |
206
|
|
|
} catch (InvalidConfigException $e) { |
207
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
208
|
|
|
} |
209
|
|
|
$model = $value; |
210
|
|
|
$value = $model->value; |
211
|
|
|
$decimals = $this->decimals; |
212
|
|
|
// If decimals is 0 (or null, empty for whatever reason), don't run this |
213
|
|
|
if ($decimals) { |
|
|
|
|
214
|
|
|
$decimalSeparator = Craft::$app->getLocale()->getNumberSymbol(Locale::SYMBOL_DECIMAL_SEPARATOR); |
215
|
|
|
$value = number_format($value, $decimals, $decimalSeparator, ''); |
|
|
|
|
216
|
|
|
} |
217
|
|
|
// Get our id and namespace |
218
|
|
|
$id = Html::id($this->handle); |
|
|
|
|
219
|
|
|
$namespacedId = Craft::$app->getView()->namespaceInputId($id); |
220
|
|
|
|
221
|
|
|
// Variables to pass down to our field JavaScript to let it namespace properly |
222
|
|
|
$jsonVars = [ |
223
|
|
|
'id' => $id, |
224
|
|
|
'name' => $this->handle, |
225
|
|
|
'namespace' => $namespacedId, |
226
|
|
|
'prefix' => Craft::$app->getView()->namespaceInputId(''), |
227
|
|
|
]; |
228
|
|
|
$jsonVars = Json::encode($jsonVars); |
229
|
|
|
Craft::$app->getView()->registerJs("$('#{$namespacedId}-field').UnitsUnits(" . $jsonVars . ");"); |
230
|
|
|
|
231
|
|
|
// Render the input template |
232
|
|
|
return Craft::$app->getView()->renderTemplate( |
233
|
|
|
'units/_components/fields/Units_input', |
234
|
|
|
[ |
235
|
|
|
'name' => $this->handle, |
236
|
|
|
'field' => $this, |
237
|
|
|
'id' => $id, |
238
|
|
|
'namespacedId' => $namespacedId, |
239
|
|
|
'value' => $value, |
240
|
|
|
'model' => $model, |
241
|
|
|
] |
242
|
|
|
); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return ''; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
|
|
|
|
249
|
|
|
* @inheritdoc |
250
|
|
|
*/ |
|
|
|
|
251
|
|
|
public function getContentGqlType(): Type|array |
252
|
|
|
{ |
253
|
|
|
$typeArray = UnitsDataGenerator::generateTypes($this); |
254
|
|
|
|
255
|
|
|
return [ |
256
|
|
|
'name' => $this->handle, |
257
|
|
|
'description' => 'Units field', |
258
|
|
|
'type' => array_shift($typeArray), |
259
|
|
|
]; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
|
|
|
|
263
|
|
|
* @inheritdoc |
264
|
|
|
*/ |
|
|
|
|
265
|
|
|
public function getElementValidationRules(): array |
266
|
|
|
{ |
267
|
|
|
return [ |
268
|
|
|
[ |
269
|
|
|
EmbeddedUnitsDataValidator::class, |
270
|
|
|
'units' => $this->defaultUnits, |
271
|
|
|
'integerOnly' => !$this->decimals, |
272
|
|
|
'min' => $this->min, |
273
|
|
|
'max' => $this->max, |
274
|
|
|
], |
275
|
|
|
]; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|