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\web\twig\variables\CraftVariable; |
21
|
|
|
use craft\web\UrlManager; |
22
|
|
|
use flipbox\craft\ember\modules\LoggerTrait; |
23
|
|
|
use flipbox\organizations\elements\Organization as OrganizationElement; |
24
|
|
|
use flipbox\organizations\models\Settings as OrganizationSettings; |
25
|
|
|
use flipbox\organizations\records\OrganizationType as OrganizationType; |
26
|
|
|
use flipbox\organizations\web\twig\variables\Organization as OrganizationVariable; |
27
|
|
|
use yii\base\Event; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @author Flipbox Factory <[email protected]> |
31
|
|
|
* @since 1.0.0 |
32
|
|
|
* |
33
|
|
|
* @method OrganizationSettings getSettings() |
34
|
|
|
*/ |
35
|
|
|
class Organizations extends BasePlugin |
36
|
|
|
{ |
37
|
|
|
use LoggerTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public function init() |
43
|
|
|
{ |
44
|
|
|
// Sub-Modules |
45
|
|
|
$this->setModules([ |
46
|
|
|
'cp' => cp\Cp::class |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
parent::init(); |
50
|
|
|
|
51
|
|
|
// Fields |
52
|
|
|
Event::on( |
53
|
|
|
Fields::class, |
54
|
|
|
Fields::EVENT_REGISTER_FIELD_TYPES, |
55
|
|
|
[ |
56
|
|
|
events\handlers\RegisterFieldTypes::class, |
57
|
|
|
'handle' |
58
|
|
|
] |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
// Elements |
62
|
|
|
Event::on( |
63
|
|
|
Elements::class, |
64
|
|
|
Elements::EVENT_REGISTER_ELEMENT_TYPES, |
65
|
|
|
[ |
66
|
|
|
events\handlers\RegisterElements::class, |
67
|
|
|
'handle' |
68
|
|
|
] |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
// User Query Behavior(s) |
72
|
|
|
Event::on( |
73
|
|
|
UserQuery::class, |
74
|
|
|
UserQuery::EVENT_DEFINE_BEHAVIORS, |
75
|
|
|
[ |
76
|
|
|
events\handlers\AttachUserQueryBehaviors::class, |
77
|
|
|
'handle' |
78
|
|
|
] |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
// User Query (prepare) |
82
|
|
|
Event::on( |
83
|
|
|
UserQuery::class, |
84
|
|
|
UserQuery::EVENT_BEFORE_PREPARE, |
85
|
|
|
[ |
86
|
|
|
events\handlers\PrepareUserQuery::class, |
87
|
|
|
'handle' |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
// User Behavior(s) |
92
|
|
|
Event::on( |
93
|
|
|
User::class, |
94
|
|
|
User::EVENT_DEFINE_BEHAVIORS, |
95
|
|
|
[ |
96
|
|
|
events\handlers\AttachUserBehaviors::class, |
97
|
|
|
'handle' |
98
|
|
|
] |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
// User Type sources |
102
|
|
|
Event::on( |
103
|
|
|
User::class, |
104
|
|
|
User::EVENT_REGISTER_SOURCES, |
105
|
|
|
[ |
106
|
|
|
events\handlers\RegisterUserElementSources::class, |
107
|
|
|
'handle' |
108
|
|
|
] |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
// Register attributes available on User index view |
112
|
|
|
Event::on( |
113
|
|
|
User::class, |
114
|
|
|
User::EVENT_REGISTER_TABLE_ATTRIBUTES, |
115
|
|
|
[ |
116
|
|
|
events\handlers\RegisterUserTableAttributes::class, |
117
|
|
|
'handle' |
118
|
|
|
] |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
// Set attributes on User index |
122
|
|
|
Event::on( |
123
|
|
|
User::class, |
124
|
|
|
User::EVENT_SET_TABLE_ATTRIBUTE_HTML, |
125
|
|
|
[ |
126
|
|
|
events\handlers\SetUserTableAttributeHtml::class, |
127
|
|
|
'handle' |
128
|
|
|
] |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
// CP routes |
132
|
|
|
Event::on( |
133
|
|
|
UrlManager::class, |
134
|
|
|
UrlManager::EVENT_REGISTER_CP_URL_RULES, |
135
|
|
|
[self::class, 'onRegisterCpUrlRules'] |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
// Twig variables |
139
|
|
|
Event::on( |
140
|
|
|
CraftVariable::class, |
141
|
|
|
CraftVariable::EVENT_INIT, |
142
|
|
|
function (Event $event) { |
143
|
|
|
/** @var CraftVariable $variable */ |
144
|
|
|
$variable = $event->sender; |
145
|
|
|
$variable->set('organizations', OrganizationVariable::class); |
146
|
|
|
} |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
// Show organization in the sidebar? |
150
|
|
|
$userSidebarTemplate = $this->getSettings()->userSidebarTemplate; |
151
|
|
|
if (!empty($userSidebarTemplate)) { |
152
|
|
|
Craft::$app->getView()->hook('cp.users.edit.details', function (&$context) use ($userSidebarTemplate) { |
153
|
|
|
return Craft::$app->getView()->renderTemplate( |
154
|
|
|
$userSidebarTemplate, |
155
|
|
|
['context' => $context] |
156
|
|
|
); |
157
|
|
|
}); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
protected static function getLogFileName(): string |
165
|
|
|
{ |
166
|
|
|
return 'organizations'; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/******************************************* |
170
|
|
|
* NAV |
171
|
|
|
*******************************************/ |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @inheritdoc |
175
|
|
|
*/ |
176
|
|
|
public function getCpNavItem() |
177
|
|
|
{ |
178
|
|
|
return array_merge( |
179
|
|
|
parent::getCpNavItem(), |
180
|
|
|
[ |
181
|
|
|
'subnav' => [ |
182
|
|
|
'organizations.organizations' => [ |
183
|
|
|
'label' => Craft::t('organizations', 'Organizations'), |
184
|
|
|
'url' => 'organizations' |
185
|
|
|
], |
186
|
|
|
'organizations.general' => [ |
187
|
|
|
'label' => Craft::t('organizations', 'Settings'), |
188
|
|
|
'url' => 'organizations/settings' |
189
|
|
|
], |
190
|
|
|
'organizations.organization-types' => [ |
191
|
|
|
'label' => Craft::t('organizations', 'Organization Types'), |
192
|
|
|
'url' => 'organizations/settings/organization-types', |
193
|
|
|
], |
194
|
|
|
'organizations.user-types' => [ |
195
|
|
|
'label' => Craft::t('organizations', 'User Types'), |
196
|
|
|
'url' => 'organizations/settings/user-types', |
197
|
|
|
] |
198
|
|
|
] |
199
|
|
|
] |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/******************************************* |
204
|
|
|
* EVENTS |
205
|
|
|
*******************************************/ |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param RegisterUrlRulesEvent $event |
209
|
|
|
*/ |
210
|
|
|
public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event) |
211
|
|
|
{ |
212
|
|
|
$event->rules = array_merge( |
213
|
|
|
$event->rules, |
214
|
|
|
[ |
215
|
|
|
// SETTINGS |
216
|
|
|
'organizations/settings' => 'organizations/cp/settings/view/general/index', |
217
|
|
|
|
218
|
|
|
// SETTINGS: USER TYPES |
219
|
|
|
'organizations/settings/user-types' => 'organizations/cp/settings/view/user-types/index', |
220
|
|
|
'organizations/settings/user-types/new' => |
221
|
|
|
'organizations/cp/settings/view/user-types/upsert', |
222
|
|
|
'organizations/settings/user-types/<identifier:\d+>' => |
223
|
|
|
'organizations/cp/settings/view/user-types/upsert', |
224
|
|
|
|
225
|
|
|
// SETTINGS: ORGANIZATION TYPES |
226
|
|
|
'organizations/settings/organization-types' => |
227
|
|
|
'organizations/cp/settings/view/organization-types/index', |
228
|
|
|
'organizations/settings/organization-types/new' => |
229
|
|
|
'organizations/cp/settings/view/organization-types/upsert', |
230
|
|
|
'organizations/settings/organization-types/<identifier:\d+>' => |
231
|
|
|
'organizations/cp/settings/view/organization-types/upsert', |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
// ORGANIZATION |
235
|
|
|
'organizations' => 'organizations/cp/view/organizations/index', |
236
|
|
|
'organizations/new/<typeIdentifier:{handle}>' => 'organizations/cp/view/organizations/upsert', |
237
|
|
|
'organizations/new' => 'organizations/cp/view/organizations/upsert', |
238
|
|
|
'organizations/<identifier:\d+>' => 'organizations/cp/view/organizations/upsert', |
239
|
|
|
'organizations/<identifier:\d+>/<typeIdentifier:{handle}>' => |
240
|
|
|
'organizations/cp/view/organizations/upsert' |
241
|
|
|
|
242
|
|
|
] |
243
|
|
|
); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
/******************************************* |
248
|
|
|
* SETTINGS |
249
|
|
|
*******************************************/ |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @inheritdoc |
253
|
|
|
* @return OrganizationSettings |
254
|
|
|
*/ |
255
|
|
|
protected function createSettingsModel() |
256
|
|
|
{ |
257
|
|
|
return new OrganizationSettings(); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @inheritdoc |
262
|
|
|
* @return mixed|void|\yii\web\Response |
263
|
|
|
* @throws \yii\base\ExitException |
264
|
|
|
*/ |
265
|
|
|
public function getSettingsResponse() |
266
|
|
|
{ |
267
|
|
|
|
268
|
|
|
Craft::$app->getResponse()->redirect( |
269
|
|
|
UrlHelper::cpUrl('organizations/settings') |
270
|
|
|
); |
271
|
|
|
|
272
|
|
|
Craft::$app->end(); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
|
276
|
|
|
/******************************************* |
277
|
|
|
* INSTALL / UNINSTALL |
278
|
|
|
*******************************************/ |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @throws \yii\base\Exception |
282
|
|
|
*/ |
283
|
|
|
public function afterInstall() |
284
|
|
|
{ |
285
|
|
|
// CreateOrganization default field layout |
286
|
|
|
$fieldLayout = new FieldLayoutModel(); |
287
|
|
|
$fieldLayout->type = self::class; |
288
|
|
|
|
289
|
|
|
// Delete existing layouts |
290
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(self::class); |
291
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(OrganizationType::class); |
292
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(OrganizationElement::class); |
293
|
|
|
|
294
|
|
|
// Save layout |
295
|
|
|
Craft::$app->getFields()->saveLayout($fieldLayout); |
296
|
|
|
|
297
|
|
|
// Set settings array |
298
|
|
|
$settings = [ |
299
|
|
|
'fieldLayoutId' => $fieldLayout->id |
300
|
|
|
]; |
301
|
|
|
|
302
|
|
|
Craft::$app->getPlugins()->savePluginSettings( |
303
|
|
|
$this, |
304
|
|
|
$settings |
305
|
|
|
); |
306
|
|
|
|
307
|
|
|
// Do parent |
308
|
|
|
parent::afterInstall(); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Remove all field layouts |
313
|
|
|
*/ |
314
|
|
|
public function afterUninstall() |
315
|
|
|
{ |
316
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(self::class); |
317
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(OrganizationType::class); |
318
|
|
|
Craft::$app->getFields()->deleteLayoutsByType(OrganizationElement::class); |
319
|
|
|
|
320
|
|
|
// Do parent |
321
|
|
|
parent::afterUninstall(); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/******************************************* |
325
|
|
|
* MODULES |
326
|
|
|
*******************************************/ |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @return cp\Cp |
330
|
|
|
*/ |
331
|
|
|
public function getCp() |
332
|
|
|
{ |
333
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
334
|
|
|
return $this->getModule('cp'); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|