Issues (462)

src/Units.php (33 issues)

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
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
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
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
30
 * @package   Units
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
31
 * @since     1.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
32
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
33
class Units extends Plugin
34
{
35
    // Static Properties
36
    // =========================================================================
37
38
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
39
     * @var Units
40
     */
41
    public static $plugin;
42
43
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
44
     * @var UnitsVariable
45
     */
46
    public static $variable;
47
48
    // Public Properties
49
    // =========================================================================
50
51
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
52
     * @var string
53
     */
54
    public string $schemaVersion = '1.0.0';
55
56
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
57
     * @var bool
58
     */
59
    public bool $hasCpSettings = true;
60
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
61
     * @var bool
62
     */
63
    public bool $hasCpSection = false;
64
65
    // Public Methods
66
    // =========================================================================
67
68
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
69
     * @inheritdoc
70
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
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
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
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
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
89
                /** @var CraftVariable $variable */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
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
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
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
Missing short description in doc comment
Loading history...
128
     * @inheritdoc
129
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
130
    protected function createSettingsModel(): ?Model
131
    {
132
        return new Settings();
133
    }
134
135
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
136
     * @inheritdoc
137
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
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