nystudio107 /
craft-units
| 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/ |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 8 | * @copyright Copyright (c) nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 9 | */ |
||
|
0 ignored issues
–
show
|
|||
| 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 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 30 | * @package Units |
||
|
0 ignored issues
–
show
|
|||
| 31 | * @since 1.0.0 |
||
|
0 ignored issues
–
show
|
|||
| 32 | */ |
||
|
0 ignored issues
–
show
|
|||
| 33 | class Units extends Plugin |
||
| 34 | { |
||
| 35 | // Static Properties |
||
| 36 | // ========================================================================= |
||
| 37 | |||
| 38 | /** |
||
|
0 ignored issues
–
show
|
|||
| 39 | * @var Units |
||
| 40 | */ |
||
| 41 | public static $plugin; |
||
| 42 | |||
| 43 | /** |
||
|
0 ignored issues
–
show
|
|||
| 44 | * @var UnitsVariable |
||
| 45 | */ |
||
| 46 | public static $variable; |
||
| 47 | |||
| 48 | // Public Properties |
||
| 49 | // ========================================================================= |
||
| 50 | |||
| 51 | /** |
||
|
0 ignored issues
–
show
|
|||
| 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 | /** |
||
|
0 ignored issues
–
show
|
|||
| 69 | * @inheritdoc |
||
| 70 | */ |
||
|
0 ignored issues
–
show
|
|||
| 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) { |
||
|
0 ignored issues
–
show
|
|||
| 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) { |
||
|
0 ignored issues
–
show
|
|||
| 89 | /** @var CraftVariable $variable */ |
||
|
0 ignored issues
–
show
|
|||
| 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) { |
||
|
0 ignored issues
–
show
|
|||
| 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 | /** |
||
|
0 ignored issues
–
show
|
|||
| 128 | * @inheritdoc |
||
| 129 | */ |
||
|
0 ignored issues
–
show
|
|||
| 130 | protected function createSettingsModel(): ?Model |
||
| 131 | { |
||
| 132 | return new Settings(); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
|
0 ignored issues
–
show
|
|||
| 136 | * @inheritdoc |
||
| 137 | */ |
||
|
0 ignored issues
–
show
|
|||
| 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 |