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\CancelableEvent; |
16
|
|
|
use craft\events\DefineBehaviorsEvent; |
17
|
|
|
use craft\events\RegisterComponentTypesEvent; |
18
|
|
|
use craft\events\RegisterElementSourcesEvent; |
19
|
|
|
use craft\events\RegisterUrlRulesEvent; |
20
|
|
|
use craft\helpers\UrlHelper; |
21
|
|
|
use craft\models\FieldLayout as FieldLayoutModel; |
22
|
|
|
use craft\services\Elements; |
23
|
|
|
use craft\services\Fields; |
24
|
|
|
use craft\web\twig\variables\CraftVariable; |
25
|
|
|
use craft\web\UrlManager; |
26
|
|
|
use flipbox\ember\modules\LoggerTrait; |
27
|
|
|
use flipbox\organizations\db\behaviors\OrganizationAttributesToUserQueryBehavior; |
28
|
|
|
use flipbox\organizations\elements\behaviors\UserOrganizationsBehavior; |
29
|
|
|
use flipbox\organizations\elements\behaviors\UserTypesBehavior; |
30
|
|
|
use flipbox\organizations\elements\Organization as OrganizationElement; |
31
|
|
|
use flipbox\organizations\fields\Organization as OrganizationField; |
32
|
|
|
use flipbox\organizations\fields\OrganizationType as OrganizationTypeField; |
33
|
|
|
use flipbox\organizations\models\Settings as OrganizationSettings; |
34
|
|
|
use flipbox\organizations\records\OrganizationType as OrganizationType; |
35
|
|
|
use flipbox\organizations\web\twig\variables\Organization as OrganizationVariable; |
36
|
|
|
use yii\base\Event; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @author Flipbox Factory <[email protected]> |
40
|
|
|
* @since 1.0.0 |
41
|
|
|
* |
42
|
|
|
* @method OrganizationSettings getSettings() |
43
|
|
|
*/ |
44
|
|
|
class Organizations extends BasePlugin |
45
|
|
|
{ |
46
|
|
|
use LoggerTrait; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
39 |
|
public function init() |
52
|
|
|
{ |
53
|
|
|
// Services |
54
|
39 |
|
$this->setComponents([ |
55
|
39 |
|
'element' => services\Element::class, |
56
|
|
|
'organizations' => services\Organizations::class, |
57
|
|
|
'organizationTypes' => services\OrganizationTypes::class, |
58
|
|
|
'organizationTypeSettings' => services\OrganizationTypeSettings::class, |
59
|
|
|
'organizationTypeAssociations' => services\OrganizationTypeAssociations::class, |
60
|
|
|
'organizationUserAssociations' => services\OrganizationUserAssociations::class, |
61
|
|
|
'records' => services\Records::class, |
62
|
|
|
'users' => services\Users::class, |
63
|
|
|
'userOrganizationAssociations' => services\UserOrganizationAssociations::class, |
64
|
|
|
'userTypes' => services\UserTypes::class, |
65
|
|
|
'userTypeAssociations' => services\UserTypeAssociations::class, |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
// Sub-Modules |
69
|
39 |
|
$this->setModules([ |
70
|
39 |
|
'cp' => cp\Cp::class |
71
|
|
|
]); |
72
|
|
|
|
73
|
39 |
|
parent::init(); |
74
|
|
|
|
75
|
|
|
// Fields |
76
|
39 |
|
Event::on( |
77
|
39 |
|
Fields::class, |
78
|
39 |
|
Fields::EVENT_REGISTER_FIELD_TYPES, |
79
|
13 |
|
function (RegisterComponentTypesEvent $event) { |
80
|
|
|
$event->types[] = OrganizationField::class; |
81
|
|
|
$event->types[] = OrganizationTypeField::class; |
82
|
39 |
|
} |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
// Elements |
86
|
39 |
|
Event::on( |
87
|
39 |
|
Elements::class, |
88
|
39 |
|
Elements::EVENT_REGISTER_ELEMENT_TYPES, |
89
|
13 |
|
function (RegisterComponentTypesEvent $event) { |
90
|
|
|
$event->types[] = OrganizationElement::class; |
91
|
39 |
|
} |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
// User Query (attach behavior) |
95
|
39 |
|
Event::on( |
96
|
39 |
|
UserQuery::class, |
97
|
39 |
|
UserQuery::EVENT_DEFINE_BEHAVIORS, |
98
|
13 |
|
function (DefineBehaviorsEvent $e) { |
99
|
|
|
$e->behaviors['organization'] = OrganizationAttributesToUserQueryBehavior::class; |
100
|
39 |
|
} |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
// User Query (prepare) |
104
|
39 |
|
Event::on( |
105
|
39 |
|
UserQuery::class, |
106
|
39 |
|
UserQuery::EVENT_AFTER_PREPARE, |
107
|
13 |
|
function (CancelableEvent $e) { |
108
|
|
|
/** @var UserQuery $query */ |
109
|
|
|
$query = $e->sender; |
110
|
|
|
|
111
|
|
|
/** @var OrganizationAttributesToUserQueryBehavior $behavior */ |
112
|
|
|
if (null !== ($behavior = $query->getBehavior('organization'))) { |
113
|
|
|
$behavior->applyOrganizationParams($query); |
114
|
|
|
} |
115
|
39 |
|
} |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
// User (attach behavior) |
119
|
39 |
|
Event::on( |
120
|
39 |
|
User::class, |
121
|
39 |
|
User::EVENT_DEFINE_BEHAVIORS, |
122
|
13 |
|
function (DefineBehaviorsEvent $e) { |
123
|
|
|
$e->behaviors['organizations'] = UserOrganizationsBehavior::class; |
124
|
|
|
$e->behaviors['types'] = UserTypesBehavior::class; |
125
|
39 |
|
} |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
// CP routes |
129
|
39 |
|
Event::on( |
130
|
39 |
|
UrlManager::class, |
131
|
39 |
|
UrlManager::EVENT_REGISTER_CP_URL_RULES, |
132
|
39 |
|
[self::class, 'onRegisterCpUrlRules'] |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
// Twig variables |
136
|
39 |
|
Event::on( |
137
|
39 |
|
CraftVariable::class, |
138
|
39 |
|
CraftVariable::EVENT_INIT, |
139
|
13 |
|
function (Event $event) { |
140
|
|
|
/** @var CraftVariable $variable */ |
141
|
|
|
$variable = $event->sender; |
142
|
|
|
$variable->set('organizations', OrganizationVariable::class); |
143
|
39 |
|
} |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
// User Type sources |
147
|
39 |
|
Event::on( |
148
|
39 |
|
User::class, |
149
|
39 |
|
User::EVENT_REGISTER_SOURCES, |
150
|
13 |
|
function (RegisterElementSourcesEvent $event) { |
151
|
|
|
if ($event->context === 'index') { |
152
|
|
|
$event->sources[] = [ |
153
|
|
|
'heading' => "Organization Groups" |
154
|
|
|
]; |
155
|
|
|
|
156
|
|
|
$types = static::getInstance()->getUserTypes()->findAll(); |
157
|
|
|
foreach ($types as $type) { |
158
|
|
|
$event->sources[] = [ |
159
|
|
|
'key' => 'type:' . $type->id, |
160
|
|
|
'label' => Craft::t('organizations', $type->name), |
161
|
|
|
'criteria' => ['organization' => ['userType' => $type->id]], |
162
|
|
|
'hasThumbs' => true |
163
|
|
|
]; |
164
|
|
|
} |
165
|
|
|
} |
166
|
39 |
|
} |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
// Show organization in the sidebar? |
170
|
39 |
|
$userSidebarTemplate = $this->getSettings()->userSidebarTemplate; |
171
|
39 |
|
if (!empty($userSidebarTemplate)) { |
172
|
13 |
|
Craft::$app->getView()->hook('cp.users.edit.details', function (&$context) use ($userSidebarTemplate) { |
173
|
|
|
return Craft::$app->getView()->renderTemplate( |
174
|
|
|
$userSidebarTemplate, |
175
|
|
|
['context' => $context] |
176
|
|
|
); |
177
|
39 |
|
}); |
178
|
|
|
} |
179
|
39 |
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
protected static function getLogFileName(): string |
185
|
|
|
{ |
186
|
|
|
return 'organizations'; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/******************************************* |
190
|
|
|
* NAV |
191
|
|
|
*******************************************/ |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @inheritdoc |
195
|
|
|
*/ |
196
|
|
|
public function getCpNavItem() |
197
|
|
|
{ |
198
|
|
|
return array_merge( |
199
|
|
|
parent::getCpNavItem(), |
200
|
|
|
[ |
201
|
|
|
'subnav' => [ |
202
|
|
|
'organizations.organizations' => [ |
203
|
|
|
'label' => Craft::t('organizations', 'Organizations'), |
204
|
|
|
'url' => 'organizations' |
205
|
|
|
], |
206
|
|
|
'organizations.general' => [ |
207
|
|
|
'label' => Craft::t('organizations', 'Settings'), |
208
|
|
|
'url' => 'organizations/settings' |
209
|
|
|
], |
210
|
|
|
'organizations.organization-types' => [ |
211
|
|
|
'label' => Craft::t('organizations', 'Organization Types'), |
212
|
|
|
'url' => 'organizations/settings/organization-types', |
213
|
|
|
], |
214
|
|
|
'organizations.user-types' => [ |
215
|
|
|
'label' => Craft::t('organizations', 'User Types'), |
216
|
|
|
'url' => 'organizations/settings/user-types', |
217
|
|
|
] |
218
|
|
|
] |
219
|
|
|
] |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/******************************************* |
224
|
|
|
* EVENTS |
225
|
|
|
*******************************************/ |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param RegisterUrlRulesEvent $event |
229
|
|
|
*/ |
230
|
|
|
public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event) |
231
|
|
|
{ |
232
|
|
|
$event->rules = array_merge( |
233
|
|
|
$event->rules, |
234
|
|
|
[ |
235
|
|
|
// SETTINGS |
236
|
|
|
'organizations/settings' => 'organizations/cp/settings/view/general/index', |
237
|
|
|
|
238
|
|
|
// SETTINGS: USER TYPES |
239
|
|
|
'organizations/settings/user-types' => 'organizations/cp/settings/view/user-types/index', |
240
|
|
|
'organizations/settings/user-types/new' => |
241
|
|
|
'organizations/cp/settings/view/user-types/upsert', |
242
|
|
|
'organizations/settings/user-types/<identifier:\d+>' => |
243
|
|
|
'organizations/cp/settings/view/user-types/upsert', |
244
|
|
|
|
245
|
|
|
// SETTINGS: ORGANIZATION TYPES |
246
|
|
|
'organizations/settings/organization-types' => |
247
|
|
|
'organizations/cp/settings/view/organization-types/index', |
248
|
|
|
'organizations/settings/organization-types/new' => |
249
|
|
|
'organizations/cp/settings/view/organization-types/upsert', |
250
|
|
|
'organizations/settings/organization-types/<identifier:\d+>' => |
251
|
|
|
'organizations/cp/settings/view/organization-types/upsert', |
252
|
|
|
|
253
|
|
|
|
254
|
|
|
// ORGANIZATION |
255
|
|
|
'organizations' => 'organizations/cp/view/organizations/index', |
256
|
|
|
'organizations/new/<typeIdentifier:{handle}>' => 'organizations/cp/view/organizations/upsert', |
257
|
|
|
'organizations/new' => 'organizations/cp/view/organizations/upsert', |
258
|
|
|
'organizations/<identifier:\d+>' => 'organizations/cp/view/organizations/upsert', |
259
|
|
|
'organizations/<identifier:\d+>/<typeIdentifier:{handle}>' => |
260
|
|
|
'organizations/cp/view/organizations/upsert' |
261
|
|
|
|
262
|
|
|
] |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
|
267
|
|
|
/******************************************* |
268
|
|
|
* SETTINGS |
269
|
|
|
*******************************************/ |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @inheritdoc |
273
|
|
|
* @return OrganizationSettings |
274
|
|
|
*/ |
275
|
39 |
|
protected function createSettingsModel() |
276
|
|
|
{ |
277
|
39 |
|
return new OrganizationSettings(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @inheritdoc |
282
|
|
|
*/ |
283
|
|
|
public function getSettingsResponse() |
284
|
|
|
{ |
285
|
|
|
|
286
|
|
|
Craft::$app->getResponse()->redirect( |
287
|
|
|
UrlHelper::cpUrl('organizations/settings') |
288
|
|
|
); |
289
|
|
|
|
290
|
|
|
Craft::$app->end(); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
|
294
|
|
|
/******************************************* |
295
|
|
|
* INSTALL / UNINSTALL |
296
|
|
|
*******************************************/ |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Delete any existing field layouts, and create default settings |
300
|
|
|
*/ |
301
|
|
|
public function afterInstall() |
302
|
|
|
{ |
303
|
|
|
// Create default field layout |
304
|
|
|
$fieldLayout = new FieldLayoutModel(); |
305
|
|
|
$fieldLayout->type = self::class; |
306
|
|
|
|
307
|
|
|
// Delete existing layouts |
308
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(self::class); |
309
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(OrganizationType::class); |
310
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(OrganizationElement::class); |
311
|
|
|
|
312
|
|
|
// Save layout |
313
|
|
|
Craft::$app->getFields()->saveLayout($fieldLayout); |
314
|
|
|
|
315
|
|
|
// Set settings array |
316
|
|
|
$settings = [ |
317
|
|
|
'fieldLayoutId' => $fieldLayout->id |
318
|
|
|
]; |
319
|
|
|
|
320
|
|
|
Craft::$app->getPlugins()->savePluginSettings( |
321
|
|
|
$this, |
322
|
|
|
$settings |
323
|
|
|
); |
324
|
|
|
|
325
|
|
|
// Do parent |
326
|
|
|
parent::afterInstall(); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Remove all field layouts |
331
|
|
|
*/ |
332
|
|
|
public function afterUninstall() |
333
|
|
|
{ |
334
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(self::class); |
335
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(OrganizationType::class); |
336
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(OrganizationElement::class); |
337
|
|
|
|
338
|
|
|
// Do parent |
339
|
|
|
parent::afterUninstall(); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/******************************************* |
343
|
|
|
* MODULES |
344
|
|
|
*******************************************/ |
345
|
|
|
/** |
346
|
|
|
* @return cp\Cp |
347
|
|
|
* @deprecated |
348
|
|
|
*/ |
349
|
|
|
public function getConfiguration() |
350
|
|
|
{ |
351
|
|
|
return $this->getCp(); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @return cp\Cp |
356
|
|
|
*/ |
357
|
|
|
public function getCp() |
358
|
|
|
{ |
359
|
|
|
return $this->getModule('cp'); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
|
363
|
|
|
/******************************************* |
364
|
|
|
* SERVICES |
365
|
|
|
*******************************************/ |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @return services\Element |
369
|
|
|
*/ |
370
|
3 |
|
public function getElement() |
371
|
|
|
{ |
372
|
3 |
|
return $this->get('element'); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @return services\Organizations |
377
|
|
|
*/ |
378
|
3 |
|
public function getOrganizations() |
379
|
|
|
{ |
380
|
3 |
|
return $this->get('organizations'); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* @return services\OrganizationTypes |
385
|
|
|
*/ |
386
|
3 |
|
public function getOrganizationTypes() |
387
|
|
|
{ |
388
|
3 |
|
return $this->get('organizationTypes'); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @return services\OrganizationTypeSettings |
393
|
|
|
*/ |
394
|
3 |
|
public function getOrganizationTypeSettings() |
395
|
|
|
{ |
396
|
3 |
|
return $this->get('organizationTypeSettings'); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* @return services\OrganizationTypeAssociations |
401
|
|
|
*/ |
402
|
3 |
|
public function getOrganizationTypeAssociations() |
403
|
|
|
{ |
404
|
3 |
|
return $this->get('organizationTypeAssociations'); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @return services\OrganizationUserAssociations |
409
|
|
|
*/ |
410
|
9 |
|
public function getOrganizationUserAssociations() |
411
|
|
|
{ |
412
|
9 |
|
return $this->get('organizationUserAssociations'); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @return services\Records |
417
|
|
|
*/ |
418
|
3 |
|
public function getRecords() |
419
|
|
|
{ |
420
|
3 |
|
return $this->get('records'); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* @return services\Users |
425
|
|
|
*/ |
426
|
3 |
|
public function getUsers() |
427
|
|
|
{ |
428
|
3 |
|
return $this->get('users'); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @return services\UserOrganizationAssociations |
433
|
|
|
*/ |
434
|
3 |
|
public function getUserOrganizationAssociations() |
435
|
|
|
{ |
436
|
3 |
|
return $this->get('userOrganizationAssociations'); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* @return services\UserTypes |
441
|
|
|
*/ |
442
|
3 |
|
public function getUserTypes() |
443
|
|
|
{ |
444
|
3 |
|
return $this->get('userTypes'); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* @return services\UserTypeAssociations |
449
|
|
|
*/ |
450
|
3 |
|
public function getUserTypeAssociations() |
451
|
|
|
{ |
452
|
3 |
|
return $this->get('userTypeAssociations'); |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
|