-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Plugin.php
292 lines (262 loc) · 11.3 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php namespace Winter\TailwindUI;
use Backend\Classes\BackendController as CoreBackendController;
use Backend\Classes\Controller as BaseBackendController;
use Backend\Classes\WidgetBase;
use Backend\Controllers\Auth as AuthController;
use Backend\Controllers\Preferences as PreferencesController;
use Backend\Models\BrandSetting;
use Backend\Models\Preference as PreferenceModel;
use Backend\Models\UserRole;
use BackendAuth;
use Config;
use Event;
use Request;
use System\Classes\PluginBase;
use System\Controllers\Settings as SettingsController;
use Url;
use Winter\Storm\Support\Str;
use Yaml;
/**
* TailwindUI Plugin Information File
*/
class Plugin extends PluginBase
{
public $elevated = true;
/**
* Returns information about this plugin.
*/
public function pluginDetails(): array
{
return [
'name' => 'winter.tailwindui::lang.plugin.name',
'description' => 'winter.tailwindui::lang.plugin.description',
'author' => 'Winter CMS',
'icon' => 'icon-leaf',
];
}
/**
* Returns the permissions provided by this plugin
*/
public function registerPermissions(): array
{
return [
'winter.tailwindui.manage_own_appearance.dark_mode' => [
'label' => 'winter.tailwindui::lang.permissions.manage_appearance.dark_mode',
'tab' => 'winter.tailwindui::lang.plugin.name',
'roles' => [UserRole::CODE_PUBLISHER, UserRole::CODE_DEVELOPER],
],
'winter.tailwindui.manage_own_appearance.menu_location' => [
'label' => 'winter.tailwindui::lang.permissions.manage_appearance.menu_location',
'tab' => 'winter.tailwindui::lang.plugin.name',
'roles' => [UserRole::CODE_PUBLISHER, UserRole::CODE_DEVELOPER],
],
'winter.tailwindui.manage_own_appearance.item_location' => [
'label' => 'winter.tailwindui::lang.permissions.manage_appearance.item_location',
'tab' => 'winter.tailwindui::lang.plugin.name',
'roles' => [UserRole::CODE_PUBLISHER, UserRole::CODE_DEVELOPER],
],
];
}
/**
* Boot method, called right before the request route.
*/
public function boot()
{
// Only apply skin modifications to the backend context
if ($this->app->runningInBackend()) {
$this->applyBackendSkin();
$this->extendBackendControllers();
$this->extendBackendWidgets();
$this->extendBrandSettingsForm();
$this->extendBackendAuthController();
}
}
/**
* Guess the override view path for the provided object
*/
protected function guessOverrideViewPath(object $object): string
{
$path = strtolower(get_class($object));
$cleanPath = str_replace("\\", "/", $path);
if (!in_array(Str::before($cleanPath, '/'), ['backend', 'cms', 'system'])) {
$cleanPath = 'plugins/' . $cleanPath;
}
return '$/winter/tailwindui/skins/tailwindui/views/' . $cleanPath;
}
/**
* Apply the TailwindUI backend skin as the selected backend skin
*/
protected function applyBackendSkin()
{
Config::set('cms.backendSkin', \Winter\TailwindUI\Skins\TailwindUI::class);
Config::set('brand.backgroundImage', '/plugins/winter/tailwindui/assets/images/background.jpg');
// Set a default logo that will work with the default dark sidebar as a fallback
// @TODO: add support for light / dark modes / variations of all primary branding (logo, favicon, colour scheme) and apply as necessary
if (empty(Config::get('brand.logoPath'))) {
Config::set('brand.logoPath', '~/modules/backend/assets/images/winter-logo-white.svg');
// Config::set('brand.logoPath', '~/modules/backend/assets/images/winter-logo.svg');
}
}
/**
* Extend all backend controllers to inject view overrides and apply custom
* brand settings data
*/
protected function extendBackendControllers(): void
{
// Add our view override paths
BaseBackendController::extend(function (\Backend\Classes\Controller $controller) {
$controller->addViewPath($this->guessOverrideViewPath($controller));
$controller->addVite('assets/src/css/app.css', 'Winter.TailwindUI');
$this->extendBrandSettingsData();
$controller->addDynamicMethod('onTailwindUISetTheme', function () {
$user = BackendAuth::user();
if (!$user || !$user->hasAccess('winter.tailwindui.manage_own_appearance.dark_mode')) {
abort(403, "You do not have permission to do that.");
}
$darkMode = post('dark_mode');
$prefs = PreferenceModel::instance();
$prefs->set('dark_mode', $darkMode);
return [
'dark_mode' => $darkMode,
];
});
});
// Extend the Settings controller to force the page to reload after updating branding
SettingsController::extend(function ($controller) {
$controller->bindEvent('ajax.beforeRunHandler', function ($handler) use ($controller) {
if ($handler === 'onSave' && !empty(Request::post('BrandSetting'))) {
$controller->update_onSave(...CoreBackendController::$params);
return redirect()->refresh();
}
});
});
// Extend the Preferences controller to force the page to reload after updating preferences
PreferencesController::extend(function ($controller) {
$controller->bindEvent('ajax.beforeRunHandler', function ($handler) use ($controller) {
if ($handler === 'onSave' && !empty(Request::post('Preference'))) {
$controller->update_onSave(...CoreBackendController::$params);
return redirect()->refresh();
}
});
});
}
/**
* Extends backend widgets to use the custom views provided by this plugin
*/
protected function extendBackendWidgets(): void
{
WidgetBase::extend(function ($widget) {
// @TODO: BlogMarkdown and all ML overrides break this
$widget->addViewPath($this->guessOverrideViewPath($widget) . '/partials');
});
}
/**
* Populate brand settings data with initial values if not set
*/
protected function extendBrandSettingsData(): void
{
$settings = BrandSetting::instance();
$userSettings = (!is_null(BackendAuth::user()))
? PreferenceModel::instance()
: null;
// Initialize the backend branding data from the config if it's not set already
$fieldDefaults = [];
$fields = Yaml::parseFile(plugins_path('winter/tailwindui/models/brandsetting/fields.yaml'));
if (!empty($fields['tabs'])) {
foreach ($fields['tabs']['fields'] as $name => $config) {
if (isset($config['default'])) {
$fieldDefaults[$name] = $config['default'];
}
}
}
if (!empty($fieldDefaults)) {
foreach ($fieldDefaults as $name => $default) {
// Check the current user for an overridden preference
$userValue = (!is_null($userSettings)) ? $userSettings->get($name) : null;
if (!empty($userValue)) {
$settings->setSettingsValue($name, $userValue);
$settings->attributes[$name] = $userValue;
// Apply defaults from brand config if no value is set in the DB
} elseif (empty($settings->getSettingsValue($name))) {
$settings->setSettingsValue($name, $this->app->config->get("brand.$name", $default));
$settings->attributes[$name] = $this->app->config->get("brand.$name", $default);
}
}
}
// Set the default values for the User Preferences
if ($userSettings) {
$applyDefaults = [
'dark_mode',
];
$preferenceDefaults = [];
$preferenceFields = Yaml::parseFile(plugins_path('winter/tailwindui/models/preference/fields.yaml'));
if (!empty($preferenceFields['tabs'])) {
foreach ($preferenceFields['tabs']['fields'] as $name => $config) {
if (isset($config['default']) && in_array($name, $applyDefaults)) {
$preferenceDefaults[$name] = $config['default'];
}
}
}
if (!empty($preferenceDefaults)) {
foreach ($preferenceDefaults as $name => $default) {
// Check the current user for an overridden preference
$userValue = $userSettings->get($name);
if (empty($userSettings->get($name))) {
$userSettings->setSettingsValue($name, $this->app->config->get("brand.$name", $default));
$userSettings->attributes[$name] = $this->app->config->get("brand.$name", $default);
}
}
}
}
}
/**
* Extend the brand settings form to include the settings provided by this plugin
*/
protected function extendBrandSettingsForm(): void
{
BrandSetting::extend(function($model) {
$model->addAttachOneRelation('background_image', [\System\Models\File::class]);
});
Event::listen('backend.form.extendFields', function ($form) {
// Only extend the desired form
if (!(
$form instanceof \Backend\Widgets\Form
&& !$form->isNested
&& (
(
$form->getController() instanceof SettingsController
&& $form->model instanceof BrandSetting
)
|| (
$form->getController() instanceof PreferencesController
&& $form->model instanceof PreferenceModel
)
)
)) {
return;
}
$fields = [
BrandSetting::class => Yaml::parseFile(plugins_path('winter/tailwindui/models/brandsetting/fields.yaml')),
PreferenceModel::class => Yaml::parseFile(plugins_path('winter/tailwindui/models/preference/fields.yaml')),
];
if (!empty($fields[get_class($form->model)]['tabs'])) {
$form->addTabFields($fields[get_class($form->model)]['tabs']['fields']);
}
// Remove fields that are no longer relevant
$form->removeField('menu_mode');
});
}
/**
* Extend the backend auth controller to change the layout based on the
* brand settings provided by this plugin
*/
protected function extendBackendAuthController(): void
{
AuthController::extend(function ($controller) {
$controller->bindEvent('page.beforeDisplay', function () use ($controller) {
$authLayout = BrandSetting::get('auth_layout');
$controller->layout = "auth-$authLayout";
});
});
}
}