|
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\organizations; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\base\Plugin as BasePlugin; |
|
13
|
|
|
use craft\elements\db\UserQuery; |
|
14
|
|
|
use craft\elements\User; |
|
15
|
|
|
use craft\events\RegisterUrlRulesEvent; |
|
16
|
|
|
use craft\helpers\UrlHelper; |
|
17
|
|
|
use craft\models\FieldLayout as FieldLayoutModel; |
|
18
|
|
|
use craft\services\Elements; |
|
19
|
|
|
use craft\services\Fields; |
|
20
|
|
|
use craft\services\ProjectConfig; |
|
21
|
|
|
use craft\web\twig\variables\CraftVariable; |
|
22
|
|
|
use craft\web\UrlManager; |
|
23
|
|
|
use flipbox\craft\ember\modules\LoggerTrait; |
|
24
|
|
|
use flipbox\organizations\elements\Organization as OrganizationElement; |
|
25
|
|
|
use flipbox\organizations\events\handlers\ProjectConfigHandler; |
|
26
|
|
|
use flipbox\organizations\models\Settings as OrganizationSettings; |
|
27
|
|
|
use flipbox\organizations\records\OrganizationType as OrganizationType; |
|
28
|
|
|
use flipbox\organizations\records\UserType; |
|
29
|
|
|
use flipbox\organizations\web\twig\variables\Organization as OrganizationVariable; |
|
30
|
|
|
use yii\base\Event; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @author Flipbox Factory <[email protected]> |
|
34
|
|
|
* @since 1.0.0 |
|
35
|
|
|
* |
|
36
|
|
|
* @method OrganizationSettings getSettings() |
|
37
|
|
|
*/ |
|
38
|
|
|
class Organizations extends BasePlugin |
|
39
|
|
|
{ |
|
40
|
|
|
use LoggerTrait; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The plugin category (used for logging) |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
public static $category = 'organizations'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritdoc |
|
51
|
|
|
*/ |
|
52
|
3 |
|
public function init() |
|
53
|
|
|
{ |
|
54
|
|
|
// Sub-Modules |
|
55
|
3 |
|
$this->setModules([ |
|
56
|
3 |
|
'cp' => cp\Cp::class |
|
57
|
|
|
]); |
|
58
|
|
|
|
|
59
|
3 |
|
parent::init(); |
|
60
|
|
|
|
|
61
|
|
|
// Project config |
|
62
|
3 |
|
$this->registerProjectConfigEvents(); |
|
63
|
|
|
|
|
64
|
|
|
// Fields |
|
65
|
3 |
|
Event::on( |
|
66
|
3 |
|
Fields::class, |
|
67
|
3 |
|
Fields::EVENT_REGISTER_FIELD_TYPES, |
|
68
|
|
|
[ |
|
69
|
3 |
|
events\handlers\RegisterFieldTypes::class, |
|
70
|
|
|
'handle' |
|
71
|
|
|
] |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
// Elements |
|
75
|
3 |
|
Event::on( |
|
76
|
3 |
|
Elements::class, |
|
77
|
3 |
|
Elements::EVENT_REGISTER_ELEMENT_TYPES, |
|
78
|
|
|
[ |
|
79
|
3 |
|
events\handlers\RegisterElements::class, |
|
80
|
|
|
'handle' |
|
81
|
|
|
] |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
// User Query Behavior(s) |
|
85
|
3 |
|
Event::on( |
|
86
|
3 |
|
UserQuery::class, |
|
87
|
3 |
|
UserQuery::EVENT_DEFINE_BEHAVIORS, |
|
88
|
|
|
[ |
|
89
|
3 |
|
events\handlers\AttachUserQueryBehaviors::class, |
|
90
|
|
|
'handle' |
|
91
|
|
|
] |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
// User Query (prepare) |
|
95
|
3 |
|
Event::on( |
|
96
|
3 |
|
UserQuery::class, |
|
97
|
3 |
|
UserQuery::EVENT_BEFORE_PREPARE, |
|
98
|
|
|
[ |
|
99
|
3 |
|
events\handlers\PrepareUserQuery::class, |
|
100
|
|
|
'handle' |
|
101
|
|
|
] |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
// User Behavior(s) |
|
105
|
3 |
|
Event::on( |
|
106
|
3 |
|
User::class, |
|
107
|
3 |
|
User::EVENT_DEFINE_BEHAVIORS, |
|
108
|
|
|
[ |
|
109
|
3 |
|
events\handlers\AttachUserBehaviors::class, |
|
110
|
|
|
'handle' |
|
111
|
|
|
] |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
// User Type sources |
|
115
|
3 |
|
Event::on( |
|
116
|
3 |
|
User::class, |
|
117
|
3 |
|
User::EVENT_REGISTER_SOURCES, |
|
118
|
|
|
[ |
|
119
|
3 |
|
events\handlers\RegisterUserElementSources::class, |
|
120
|
|
|
'handle' |
|
121
|
|
|
] |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
// Register attributes available on User index view |
|
125
|
3 |
|
Event::on( |
|
126
|
3 |
|
User::class, |
|
127
|
3 |
|
User::EVENT_REGISTER_TABLE_ATTRIBUTES, |
|
128
|
|
|
[ |
|
129
|
3 |
|
events\handlers\RegisterUserTableAttributes::class, |
|
130
|
|
|
'handle' |
|
131
|
|
|
] |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
|
|
// Set attributes on User index |
|
135
|
3 |
|
Event::on( |
|
136
|
3 |
|
User::class, |
|
137
|
3 |
|
User::EVENT_SET_TABLE_ATTRIBUTE_HTML, |
|
138
|
|
|
[ |
|
139
|
3 |
|
events\handlers\SetUserTableAttributeHtml::class, |
|
140
|
|
|
'handle' |
|
141
|
|
|
] |
|
142
|
|
|
); |
|
143
|
|
|
|
|
144
|
|
|
// CP routes |
|
145
|
3 |
|
Event::on( |
|
146
|
3 |
|
UrlManager::class, |
|
147
|
3 |
|
UrlManager::EVENT_REGISTER_CP_URL_RULES, |
|
148
|
3 |
|
[self::class, 'onRegisterCpUrlRules'] |
|
149
|
|
|
); |
|
150
|
|
|
|
|
151
|
|
|
// Twig variables |
|
152
|
3 |
|
Event::on( |
|
153
|
3 |
|
CraftVariable::class, |
|
154
|
3 |
|
CraftVariable::EVENT_INIT, |
|
155
|
1 |
|
function (Event $event) { |
|
156
|
|
|
/** @var CraftVariable $variable */ |
|
157
|
|
|
$variable = $event->sender; |
|
158
|
|
|
$variable->set('organizations', OrganizationVariable::class); |
|
159
|
3 |
|
} |
|
160
|
|
|
); |
|
161
|
|
|
|
|
162
|
|
|
// Show organization in the sidebar? |
|
163
|
3 |
|
$userSidebarTemplate = $this->getSettings()->userSidebarTemplate; |
|
164
|
3 |
|
if (!empty($userSidebarTemplate)) { |
|
165
|
1 |
|
Craft::$app->getView()->hook('cp.users.edit.details', function (&$context) use ($userSidebarTemplate) { |
|
166
|
|
|
return Craft::$app->getView()->renderTemplate( |
|
167
|
|
|
$userSidebarTemplate, |
|
168
|
|
|
['context' => $context] |
|
169
|
|
|
); |
|
170
|
3 |
|
}); |
|
171
|
|
|
} |
|
172
|
3 |
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Register project config events, if we're able to |
|
176
|
|
|
*/ |
|
177
|
3 |
|
protected function registerProjectConfigEvents() |
|
178
|
|
|
{ |
|
179
|
3 |
|
if (!version_compare(Craft::$app->getVersion(), '3.1', '>=')) { |
|
180
|
|
|
return; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// Project Config |
|
184
|
3 |
|
Craft::$app->projectConfig |
|
185
|
3 |
|
->onAdd( |
|
186
|
3 |
|
'plugins.organizations.organizationTypes.{uid}', |
|
187
|
|
|
[ |
|
188
|
3 |
|
ProjectConfigHandler::class, |
|
189
|
|
|
'handleChangedOrganizationType' |
|
190
|
|
|
] |
|
191
|
|
|
) |
|
192
|
3 |
|
->onUpdate( |
|
193
|
3 |
|
'plugins.organizations.organizationTypes.{uid}', |
|
194
|
|
|
[ |
|
195
|
3 |
|
ProjectConfigHandler::class, |
|
196
|
|
|
'handleChangedOrganizationType' |
|
197
|
|
|
] |
|
198
|
|
|
) |
|
199
|
3 |
|
->onRemove( |
|
200
|
3 |
|
'plugins.organizations.organizationTypes.{uid}', |
|
201
|
|
|
[ |
|
202
|
3 |
|
ProjectConfigHandler::class, |
|
203
|
|
|
'handleDeletedOrganizationType' |
|
204
|
|
|
] |
|
205
|
|
|
) |
|
206
|
3 |
|
->onAdd( |
|
207
|
3 |
|
'plugins.organizations.userTypes.{uid}', |
|
208
|
|
|
[ |
|
209
|
3 |
|
ProjectConfigHandler::class, |
|
210
|
|
|
'handleChangedUserType' |
|
211
|
|
|
] |
|
212
|
|
|
) |
|
213
|
3 |
|
->onUpdate( |
|
214
|
3 |
|
'plugins.organizations.userTypes.{uid}', |
|
215
|
|
|
[ |
|
216
|
3 |
|
ProjectConfigHandler::class, |
|
217
|
|
|
'handleChangedUserType' |
|
218
|
|
|
] |
|
219
|
|
|
) |
|
220
|
3 |
|
->onRemove( |
|
221
|
3 |
|
'plugins.organizations.userTypes.{uid}', |
|
222
|
|
|
[ |
|
223
|
3 |
|
ProjectConfigHandler::class, |
|
224
|
|
|
'handleDeletedUserType' |
|
225
|
|
|
] |
|
226
|
|
|
); |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Rebuilding project configs was introduce in 3.1.20 |
|
230
|
|
|
*/ |
|
231
|
3 |
|
if (version_compare(Craft::$app->getVersion(), '3.1.20', '>=')) { |
|
232
|
3 |
|
Event::on( |
|
233
|
3 |
|
ProjectConfig::class, |
|
234
|
3 |
|
ProjectConfig::EVENT_REBUILD, |
|
235
|
|
|
[ |
|
236
|
3 |
|
events\handlers\ProjectConfigHandler::class, |
|
237
|
|
|
'rebuild' |
|
238
|
|
|
] |
|
239
|
|
|
); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
3 |
|
Event::on( |
|
243
|
3 |
|
OrganizationType::class, |
|
244
|
3 |
|
OrganizationType::EVENT_AFTER_INSERT, |
|
245
|
|
|
[ |
|
246
|
3 |
|
events\ManageOrganizationTypeProjectConfig::class, |
|
247
|
|
|
'save' |
|
248
|
|
|
] |
|
249
|
|
|
); |
|
250
|
|
|
|
|
251
|
3 |
|
Event::on( |
|
252
|
3 |
|
OrganizationType::class, |
|
253
|
3 |
|
OrganizationType::EVENT_AFTER_UPDATE, |
|
254
|
|
|
[ |
|
255
|
3 |
|
events\ManageOrganizationTypeProjectConfig::class, |
|
256
|
|
|
'save' |
|
257
|
|
|
] |
|
258
|
|
|
); |
|
259
|
|
|
|
|
260
|
3 |
|
Event::on( |
|
261
|
3 |
|
OrganizationType::class, |
|
262
|
3 |
|
OrganizationType::EVENT_AFTER_DELETE, |
|
263
|
|
|
[ |
|
264
|
3 |
|
events\ManageOrganizationTypeProjectConfig::class, |
|
265
|
|
|
'delete' |
|
266
|
|
|
] |
|
267
|
|
|
); |
|
268
|
|
|
|
|
269
|
3 |
|
Event::on( |
|
270
|
3 |
|
UserType::class, |
|
271
|
3 |
|
UserType::EVENT_AFTER_INSERT, |
|
272
|
|
|
[ |
|
273
|
3 |
|
events\ManageUserTypeProjectConfig::class, |
|
274
|
|
|
'save' |
|
275
|
|
|
] |
|
276
|
|
|
); |
|
277
|
|
|
|
|
278
|
3 |
|
Event::on( |
|
279
|
3 |
|
UserType::class, |
|
280
|
3 |
|
UserType::EVENT_AFTER_UPDATE, |
|
281
|
|
|
[ |
|
282
|
3 |
|
events\ManageUserTypeProjectConfig::class, |
|
283
|
|
|
'save' |
|
284
|
|
|
] |
|
285
|
|
|
); |
|
286
|
|
|
|
|
287
|
3 |
|
Event::on( |
|
288
|
3 |
|
UserType::class, |
|
289
|
3 |
|
UserType::EVENT_AFTER_DELETE, |
|
290
|
|
|
[ |
|
291
|
3 |
|
events\ManageUserTypeProjectConfig::class, |
|
292
|
|
|
'delete' |
|
293
|
|
|
] |
|
294
|
|
|
); |
|
295
|
3 |
|
} |
|
296
|
|
|
|
|
297
|
|
|
/******************************************* |
|
298
|
|
|
* NAV |
|
299
|
|
|
*******************************************/ |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* @inheritdoc |
|
303
|
|
|
*/ |
|
304
|
|
|
public function getCpNavItem() |
|
305
|
|
|
{ |
|
306
|
|
|
return array_merge( |
|
307
|
|
|
parent::getCpNavItem(), |
|
308
|
|
|
[ |
|
309
|
|
|
'subnav' => [ |
|
310
|
|
|
'organizations.organizations' => [ |
|
311
|
|
|
'label' => static::t('Organizations'), |
|
312
|
|
|
'url' => 'organizations' |
|
313
|
|
|
], |
|
314
|
|
|
'organizations.general' => [ |
|
315
|
|
|
'label' => static::t('Settings'), |
|
316
|
|
|
'url' => 'organizations/settings' |
|
317
|
|
|
], |
|
318
|
|
|
'organizations.organization-types' => [ |
|
319
|
|
|
'label' => static::t('Organization Types'), |
|
320
|
|
|
'url' => 'organizations/settings/organization-types', |
|
321
|
|
|
], |
|
322
|
|
|
'organizations.user-types' => [ |
|
323
|
|
|
'label' => static::t('User Types'), |
|
324
|
|
|
'url' => 'organizations/settings/user-types', |
|
325
|
|
|
] |
|
326
|
|
|
] |
|
327
|
|
|
] |
|
328
|
|
|
); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/******************************************* |
|
332
|
|
|
* EVENTS |
|
333
|
|
|
*******************************************/ |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* @param RegisterUrlRulesEvent $event |
|
337
|
|
|
*/ |
|
338
|
|
|
public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event) |
|
339
|
|
|
{ |
|
340
|
|
|
$event->rules = array_merge( |
|
341
|
|
|
$event->rules, |
|
342
|
|
|
[ |
|
343
|
|
|
// SETTINGS |
|
344
|
|
|
'organizations/settings' => 'organizations/cp/settings/view/general/index', |
|
345
|
|
|
|
|
346
|
|
|
// SETTINGS: USER TYPES |
|
347
|
|
|
'organizations/settings/user-types' => 'organizations/cp/settings/view/user-types/index', |
|
348
|
|
|
'organizations/settings/user-types/new' => |
|
349
|
|
|
'organizations/cp/settings/view/user-types/upsert', |
|
350
|
|
|
'organizations/settings/user-types/<identifier:\d+>' => |
|
351
|
|
|
'organizations/cp/settings/view/user-types/upsert', |
|
352
|
|
|
|
|
353
|
|
|
// SETTINGS: ORGANIZATION TYPES |
|
354
|
|
|
'organizations/settings/organization-types' => |
|
355
|
|
|
'organizations/cp/settings/view/organization-types/index', |
|
356
|
|
|
'organizations/settings/organization-types/new' => |
|
357
|
|
|
'organizations/cp/settings/view/organization-types/upsert', |
|
358
|
|
|
'organizations/settings/organization-types/<identifier:\d+>' => |
|
359
|
|
|
'organizations/cp/settings/view/organization-types/upsert', |
|
360
|
|
|
|
|
361
|
|
|
|
|
362
|
|
|
// ORGANIZATION |
|
363
|
|
|
'organizations' => 'organizations/cp/view/organizations/index', |
|
364
|
|
|
'organizations/new/<typeIdentifier:{handle}>' => 'organizations/cp/view/organizations/upsert', |
|
365
|
|
|
'organizations/new' => 'organizations/cp/view/organizations/upsert', |
|
366
|
|
|
'organizations/<identifier:\d+>' => 'organizations/cp/view/organizations/upsert', |
|
367
|
|
|
'organizations/<identifier:\d+>/<typeIdentifier:{handle}>' => |
|
368
|
|
|
'organizations/cp/view/organizations/upsert' |
|
369
|
|
|
|
|
370
|
|
|
] |
|
371
|
|
|
); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
|
|
375
|
|
|
/******************************************* |
|
376
|
|
|
* SETTINGS |
|
377
|
|
|
*******************************************/ |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* @inheritdoc |
|
381
|
|
|
* @return OrganizationSettings |
|
382
|
|
|
*/ |
|
383
|
3 |
|
protected function createSettingsModel() |
|
384
|
|
|
{ |
|
385
|
3 |
|
return new OrganizationSettings(); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
/** |
|
389
|
|
|
* @inheritdoc |
|
390
|
|
|
* @return mixed|void|\yii\web\Response |
|
391
|
|
|
* @throws \yii\base\ExitException |
|
392
|
|
|
*/ |
|
393
|
|
|
public function getSettingsResponse() |
|
394
|
|
|
{ |
|
395
|
|
|
|
|
396
|
|
|
Craft::$app->getResponse()->redirect( |
|
397
|
|
|
UrlHelper::cpUrl('organizations/settings') |
|
398
|
|
|
); |
|
399
|
|
|
|
|
400
|
|
|
Craft::$app->end(); |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
|
|
404
|
|
|
/******************************************* |
|
405
|
|
|
* INSTALL / UNINSTALL |
|
406
|
|
|
*******************************************/ |
|
407
|
|
|
|
|
408
|
|
|
/** |
|
409
|
|
|
* @throws \yii\base\Exception |
|
410
|
|
|
*/ |
|
411
|
|
|
public function afterInstall() |
|
412
|
|
|
{ |
|
413
|
|
|
// CreateOrganization default field layout |
|
414
|
|
|
$fieldLayout = new FieldLayoutModel(); |
|
415
|
|
|
$fieldLayout->type = self::class; |
|
416
|
|
|
|
|
417
|
|
|
// Delete existing layouts |
|
418
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(self::class); |
|
419
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(OrganizationType::class); |
|
420
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(OrganizationElement::class); |
|
421
|
|
|
|
|
422
|
|
|
// Save layout |
|
423
|
|
|
Craft::$app->getFields()->saveLayout($fieldLayout); |
|
424
|
|
|
|
|
425
|
|
|
// Set settings array |
|
426
|
|
|
$settings = [ |
|
427
|
|
|
'fieldLayoutId' => $fieldLayout->id |
|
428
|
|
|
]; |
|
429
|
|
|
|
|
430
|
|
|
Craft::$app->getPlugins()->savePluginSettings( |
|
431
|
|
|
$this, |
|
432
|
|
|
$settings |
|
433
|
|
|
); |
|
434
|
|
|
|
|
435
|
|
|
// Do parent |
|
436
|
|
|
parent::afterInstall(); |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
/** |
|
440
|
|
|
* Remove all field layouts |
|
441
|
|
|
*/ |
|
442
|
|
|
public function afterUninstall() |
|
443
|
|
|
{ |
|
444
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(self::class); |
|
445
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(OrganizationType::class); |
|
446
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(OrganizationElement::class); |
|
447
|
|
|
|
|
448
|
|
|
// Do parent |
|
449
|
|
|
parent::afterUninstall(); |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
/******************************************* |
|
453
|
|
|
* MODULES |
|
454
|
|
|
*******************************************/ |
|
455
|
|
|
|
|
456
|
|
|
/** |
|
457
|
|
|
* @return cp\Cp |
|
458
|
|
|
*/ |
|
459
|
3 |
|
public function getCp() |
|
460
|
|
|
{ |
|
461
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
462
|
3 |
|
return $this->getModule('cp'); |
|
463
|
|
|
} |
|
464
|
|
|
|
|
465
|
|
|
/******************************************* |
|
466
|
|
|
* TRANSLATE |
|
467
|
|
|
*******************************************/ |
|
468
|
|
|
|
|
469
|
|
|
/** |
|
470
|
|
|
* Translates a message to the specified language. |
|
471
|
|
|
* |
|
472
|
|
|
* This is a shortcut method of [[\Craft::t()]]. |
|
473
|
|
|
* * |
|
474
|
|
|
* @param string $message the message to be translated. |
|
475
|
|
|
* @param array $params the parameters that will be used to replace the corresponding placeholders in the message. |
|
476
|
|
|
* @param string $language the language code (e.g. `en-US`, `en`). If this is null, the current |
|
477
|
|
|
* [[\yii\base\Application::language|application language]] will be used. |
|
478
|
|
|
* @return string the translated message. |
|
479
|
|
|
*/ |
|
480
|
|
|
public static function t($message, $params = [], $language = null) |
|
481
|
|
|
{ |
|
482
|
|
|
return Craft::t(self::$category, $message, $params, $language); |
|
483
|
|
|
} |
|
484
|
|
|
} |
|
485
|
|
|
|