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