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; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\base\Model; |
15
|
|
|
use craft\base\Plugin; |
16
|
|
|
use craft\events\RegisterComponentTypesEvent; |
17
|
|
|
use craft\services\Fields; |
18
|
|
|
use craft\web\twig\variables\CraftVariable; |
19
|
|
|
use nystudio107\units\fields\Units as UnitsField; |
20
|
|
|
use nystudio107\units\helpers\ClassHelper; |
21
|
|
|
use nystudio107\units\models\Settings; |
22
|
|
|
use nystudio107\units\variables\UnitsVariable; |
23
|
|
|
use PhpUnitsOfMeasure\PhysicalQuantity\Length; |
24
|
|
|
use yii\base\Event; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class Units |
28
|
|
|
* |
29
|
|
|
* @author nystudio107 |
|
|
|
|
30
|
|
|
* @package Units |
|
|
|
|
31
|
|
|
* @since 1.0.0 |
|
|
|
|
32
|
|
|
*/ |
|
|
|
|
33
|
|
|
class Units extends Plugin |
34
|
|
|
{ |
35
|
|
|
// Static Properties |
36
|
|
|
// ========================================================================= |
37
|
|
|
|
38
|
|
|
/** |
|
|
|
|
39
|
|
|
* @var Units |
40
|
|
|
*/ |
41
|
|
|
public static $plugin; |
42
|
|
|
|
43
|
|
|
/** |
|
|
|
|
44
|
|
|
* @var UnitsVariable |
45
|
|
|
*/ |
46
|
|
|
public static $variable; |
47
|
|
|
|
48
|
|
|
// Public Properties |
49
|
|
|
// ========================================================================= |
50
|
|
|
|
51
|
|
|
/** |
|
|
|
|
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
public string $schemaVersion = '1.0.0'; |
55
|
|
|
|
56
|
|
|
/** |
|
|
|
|
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
public bool $hasCpSettings = true; |
60
|
|
|
/** |
|
|
|
|
61
|
|
|
* @var bool |
62
|
|
|
*/ |
63
|
|
|
public bool $hasCpSection = false; |
64
|
|
|
|
65
|
|
|
// Public Methods |
66
|
|
|
// ========================================================================= |
67
|
|
|
|
68
|
|
|
/** |
|
|
|
|
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
|
|
|
|
71
|
|
|
public function init(): void |
72
|
|
|
{ |
73
|
|
|
parent::init(); |
74
|
|
|
self::$plugin = $this; |
75
|
|
|
|
76
|
|
|
Event::on( |
77
|
|
|
Fields::class, |
78
|
|
|
Fields::EVENT_REGISTER_FIELD_TYPES, |
79
|
|
|
static function(RegisterComponentTypesEvent $event) { |
|
|
|
|
80
|
|
|
$event->types[] = UnitsField::class; |
81
|
|
|
} |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
self::$variable = new UnitsVariable(); |
85
|
|
|
Event::on( |
86
|
|
|
CraftVariable::class, |
87
|
|
|
CraftVariable::EVENT_INIT, |
88
|
|
|
static function(Event $event) { |
|
|
|
|
89
|
|
|
/** @var CraftVariable $variable */ |
|
|
|
|
90
|
|
|
$variable = $event->sender; |
91
|
|
|
$variable->set('units', self::$variable); |
92
|
|
|
} |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
Event::on( |
96
|
|
|
UnitsField::class, |
97
|
|
|
'craftQlGetFieldSchema', |
98
|
|
|
static function($event) { |
|
|
|
|
99
|
|
|
$field = $event->sender; |
100
|
|
|
|
101
|
|
|
if (!$field instanceof UnitsField) { |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$object = $event->schema->createObjectType(ucfirst($field->handle) . 'Units'); |
106
|
|
|
$object->addFloatField('value'); |
107
|
|
|
$object->addStringField('units'); |
108
|
|
|
|
109
|
|
|
$event->schema->addField($field)->type($object); |
110
|
|
|
$event->handled = true; |
111
|
|
|
} |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
Craft::info( |
115
|
|
|
Craft::t( |
116
|
|
|
'units', |
117
|
|
|
'{name} plugin loaded', |
118
|
|
|
['name' => $this->name] |
119
|
|
|
), |
120
|
|
|
__METHOD__ |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Protected Methods |
125
|
|
|
// ========================================================================= |
126
|
|
|
|
127
|
|
|
/** |
|
|
|
|
128
|
|
|
* @inheritdoc |
129
|
|
|
*/ |
|
|
|
|
130
|
|
|
protected function createSettingsModel(): ?Model |
131
|
|
|
{ |
132
|
|
|
return new Settings(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
|
|
|
|
136
|
|
|
* @inheritdoc |
137
|
|
|
*/ |
|
|
|
|
138
|
|
|
protected function settingsHtml(): ?string |
139
|
|
|
{ |
140
|
|
|
$unitsClassMap = array_flip(ClassHelper::getClassesInNamespace(Length::class)); |
141
|
|
|
return Craft::$app->view->renderTemplate( |
142
|
|
|
'units/settings', |
143
|
|
|
[ |
144
|
|
|
'settings' => $this->getSettings(), |
145
|
|
|
'unitsClassMap' => $unitsClassMap, |
146
|
|
|
] |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|