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