Completed
Push — develop ( d69ef5...5f4c50 )
by Nate
02:28
created

Organization::defineTemplateComponent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organization;
10
11
use Craft;
12
use craft\base\Plugin as BasePlugin;
13
use craft\events\RegisterComponentTypesEvent;
14
use craft\events\RegisterUrlRulesEvent;
15
use craft\helpers\UrlHelper;
16
use craft\models\FieldLayout as FieldLayoutModel;
17
use craft\services\Elements;
18
use craft\services\Fields;
19
use craft\web\twig\variables\CraftVariable;
20
use craft\web\UrlManager;
21
use flipbox\organization\controllers\OrganizationController;
22
use flipbox\organization\elements\Organization as OrganizationElement;
23
use flipbox\organization\fields\Organization as OrganizationField;
24
use flipbox\organization\fields\User as UserField;
25
use flipbox\organization\models\Settings as OrganizationSettings;
26
use flipbox\organization\models\Type as OrganizationType;
27
use flipbox\organization\web\twig\variables\Organization as OrganizationVariable;
28
use yii\base\Event;
29
30
/**
31
 * @author Flipbox Factory <[email protected]>
32
 * @since 1.0.0
33
 */
34
class Organization extends BasePlugin
35
{
36
37
    /**
38
     * @var array
39
     */
40
    public $controllerMap = [
41
        'save' => [
42
            'class' => OrganizationController::class,
43
            'defaultAction' => 'save'
44
        ],
45
        'delete' => [
46
            'class' => OrganizationController::class,
47
            'defaultAction' => 'delete'
48
        ]
49
    ];
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function init()
55
    {
56
57
        // Do parent
58
        parent::init();
59
60
        // Register our fields
61
        Event::on(
62
            Fields::class,
63
            Fields::EVENT_REGISTER_FIELD_TYPES,
64
            function (RegisterComponentTypesEvent $event) {
65
                $event->types[] = OrganizationField::class;
66
                $event->types[] = UserField::class;
67
            }
68
        );
69
70
        // Register our elements
71
        Event::on(
72
            Elements::class,
73
            Elements::EVENT_REGISTER_ELEMENT_TYPES,
74
            function (RegisterComponentTypesEvent $event) {
75
                $event->types[] = OrganizationElement::class;
76
            }
77
        );
78
79
        // Register our CP routes
80
        Event::on(
81
            UrlManager::class,
82
            UrlManager::EVENT_REGISTER_CP_URL_RULES,
83
            [self::class, 'onRegisterCpUrlRules']
84
        );
85
86
        // Twig variables
87
        Event::on(
88
            CraftVariable::class,
89
            CraftVariable::EVENT_INIT,
90
            function (Event $event) {
91
                /** @var CraftVariable $variable */
92
                $variable = $event->sender;
93
                $variable->set('organization', OrganizationVariable::class);
94
            }
95
        );
96
    }
97
98
99
    /*******************************************
100
     * EVENTS
101
     *******************************************/
102
103
    /**
104
     * @param RegisterUrlRulesEvent $event
105
     */
106
    public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event)
107
    {
108
109
        $event->rules = array_merge(
110
            $event->rules,
111
            [
112
                // SETTINGS
113
                'organization/configuration' => 'organization/configuration/view/general/index',
114
                'organization/configuration/layout' => 'organization/configuration/view/layout/index',
115
                'organization/configuration/type' => 'organization/configuration/view/type/index',
116
                'organization/configuration/type/new' => 'organization/configuration/view/type/upsert',
117
                'organization/configuration/type/<typeIdentifier:\d+>' => 'organization/configuration/view/type/upsert',
118
119
                // ORGANIZATION
120
                'organization' => 'organization/view/organization/index',
121
                'organization/new/<typeIdentifier:{handle}>' => 'organization/view/organization/upsert',
122
                'organization/new' => 'organization/view/organization/upsert',
123
                'organization/<identifier:\d+>' => 'organization/view/organization/upsert',
124
                'organization/<identifier:\d+>/<typeIdentifier:{handle}>' => 'organization/view/organization/upsert'
125
126
            ]
127
        );
128
    }
129
130
    /**
131
     * @inheritdoc
132
     *
133
     * @return OrganizationSettings
134
     */
135
    public function getSettings()
136
    {
137
        return parent::getSettings();
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::getSettings(); of type craft\base\Model|boolean|null adds the type boolean to the return on line 137 which is incompatible with the return type declared by the interface craft\base\PluginInterface::getSettings of type craft\base\Model|null.
Loading history...
138
    }
139
140
    /**
141
     * @return OrganizationSettings
142
     */
143
    protected function createSettingsModel()
144
    {
145
        return new OrganizationSettings();
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function getSettingsResponse()
152
    {
153
154
        Craft::$app->getResponse()->redirect(
155
            UrlHelper::cpUrl('organization/configuration')
156
        );
157
158
        Craft::$app->end();
159
    }
160
161
    /**
162
     * Delete any existing field layouts, and create default settings
163
     */
164
    public function afterInstall()
165
    {
166
167
        // Create default field layout
168
        $fieldLayout = new FieldLayoutModel();
169
        $fieldLayout->type = self::class;
170
171
        // Delete existing layouts
172
        Craft::$app->getFields()->deleteLayoutsByType($fieldLayout->type);
173
174
        // Delete existing layouts
175
        Craft::$app->getFields()->deleteLayoutsByType(OrganizationType::class);
176
177
        // Save layout
178
        Craft::$app->getFields()->saveLayout($fieldLayout);
179
180
        // Set settings array
181
        $settings = [
182
            'fieldLayoutId' => $fieldLayout->id
183
        ];
184
185
        Craft::$app->getPlugins()->savePluginSettings(
186
            $this,
187
            $settings
188
        );
189
190
        // Do parent
191
        parent::afterInstall();
192
    }
193
194
    /**
195
     * Remove all field layouts
196
     */
197
    public function afterUninstall()
198
    {
199
200
        // Delete existing layouts
201
        Craft::$app->getFields()->deleteLayoutsByType(self::class);
202
203
        // Delete existing layouts
204
        Craft::$app->getFields()->deleteLayoutsByType(OrganizationType::class);
205
206
        // Do parent
207
        parent::afterUninstall();
208
    }
209
210
    /*******************************************
211
     * MODULES
212
     *******************************************/
213
    /**
214
     * @return modules\configuration\Module
215
     */
216
    public function getConfiguration()
217
    {
218
        return $this->getModule('configuration');
219
    }
220
221
222
    /*******************************************
223
     * SERVICES
224
     *******************************************/
225
226
    /**
227
     * @return services\Field
228
     */
229
    public function getField()
230
    {
231
        return $this->get('field');
232
    }
233
234
    /**
235
     * @return services\Organization
236
     */
237
    public function getOrganization()
238
    {
239
        return $this->get('organization');
240
    }
241
242
    /**
243
     * @return services\Permission
244
     */
245
    public function getPermission()
246
    {
247
        return $this->get('permission');
248
    }
249
250
    /**
251
     * @return services\Type
252
     */
253
    public function getType()
254
    {
255
        return $this->get('type');
256
    }
257
258
    /**
259
     * @return services\User
260
     */
261
    public function getUser()
262
    {
263
        return $this->get('user');
264
    }
265
}
266