|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Units plugin for Craft CMS |
|
4
|
|
|
* |
|
5
|
|
|
* A plugin for handling physical quantities and the units of measure in which they're represented. |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
|
|
8
|
|
|
* @copyright Copyright (c) nystudio107 |
|
|
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace nystudio107\units\models; |
|
12
|
|
|
|
|
13
|
|
|
use craft\base\Model; |
|
14
|
|
|
use PhpUnitsOfMeasure\PhysicalQuantity\Length; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
|
|
|
|
|
17
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
18
|
|
|
* @package Units |
|
|
|
|
|
|
19
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
20
|
|
|
*/ |
|
|
|
|
|
|
21
|
|
|
class Settings extends Model |
|
22
|
|
|
{ |
|
23
|
|
|
// Public Properties |
|
24
|
|
|
// ========================================================================= |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
|
|
|
|
|
27
|
|
|
* @var string The default fully qualified class name of the unit of measure |
|
28
|
|
|
*/ |
|
29
|
|
|
public string $defaultUnitsClass = Length::class; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
|
|
|
|
|
32
|
|
|
* @var float The default value of the unit of measure |
|
33
|
|
|
*/ |
|
34
|
|
|
public float $defaultValue = 0.0; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
|
|
|
|
|
37
|
|
|
* @var string The default units that the unit of measure is in |
|
38
|
|
|
*/ |
|
39
|
|
|
public string $defaultUnits = 'ft'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
|
|
|
|
|
42
|
|
|
* @var bool Whether the units the field can be changed |
|
43
|
|
|
*/ |
|
44
|
|
|
public bool $defaultChangeableUnits = true; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
|
|
|
|
|
47
|
|
|
* @var int|float The default minimum allowed number |
|
48
|
|
|
*/ |
|
49
|
|
|
public int|float $defaultMin = 0; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
|
|
|
|
|
52
|
|
|
* @var int|float|null The default maximum allowed number |
|
53
|
|
|
*/ |
|
54
|
|
|
public int|null|float $defaultMax = null; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
|
|
|
|
|
57
|
|
|
* @var int The default number of digits allowed after the decimal point |
|
58
|
|
|
*/ |
|
59
|
|
|
public int $defaultDecimals = 3; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
|
|
|
|
|
62
|
|
|
* @var int|null The default size of the field |
|
63
|
|
|
*/ |
|
64
|
|
|
public ?int $defaultSize = 6; |
|
65
|
|
|
|
|
66
|
|
|
// Public Methods |
|
67
|
|
|
// ========================================================================= |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
|
|
|
|
|
70
|
|
|
* @inheritdoc |
|
71
|
|
|
*/ |
|
|
|
|
|
|
72
|
|
|
public function rules(): array |
|
73
|
|
|
{ |
|
74
|
|
|
$rules = parent::rules(); |
|
75
|
|
|
$rules = array_merge($rules, [ |
|
|
|
|
|
|
76
|
|
|
['defaultUnitsClass', 'string'], |
|
77
|
|
|
['defaultUnitsClass', 'default', 'value' => Length::class], |
|
78
|
|
|
['defaultValue', 'number'], |
|
79
|
|
|
['defaultValue', 'default', 'value' => 0.0], |
|
80
|
|
|
['defaultUnits', 'string'], |
|
81
|
|
|
['defaultUnits', 'default', 'value' => 'ft'], |
|
82
|
|
|
['defaultChangeableUnits', 'boolean'], |
|
83
|
|
|
['defaultChangeableUnits', 'default', 'value' => true], |
|
84
|
|
|
[['defaultMin', 'defaultMax'], 'number'], |
|
85
|
|
|
[ |
|
86
|
|
|
['defaultMax'], |
|
87
|
|
|
'compare', |
|
88
|
|
|
'compareAttribute' => 'defaultMin', |
|
89
|
|
|
'operator' => '>=', |
|
90
|
|
|
], |
|
91
|
|
|
['defaultMin', 'default', 'value' => 0], |
|
92
|
|
|
['defaultMax', 'default', 'value' => null], |
|
93
|
|
|
[['defaultDecimals', 'defaultSize'], 'integer'], |
|
94
|
|
|
['defaultDecimals', 'default', 'value' => 3], |
|
95
|
|
|
['defaultSize', 'default', 'value' => 6], |
|
96
|
|
|
]); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
if (!$this->defaultDecimals) { |
|
99
|
|
|
$rules[] = [['defaultMin', 'defaultMax'], 'integer']; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $rules; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|