mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-01 01:36:00 -04:00
100b28707c
Includes testing to cover. Also added file missing from previous commit.
172 lines
5.3 KiB
PHP
172 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace BookStack\Users\Controllers;
|
|
|
|
use BookStack\Http\Controller;
|
|
use BookStack\Settings\UserNotificationPreferences;
|
|
use BookStack\Settings\UserShortcutMap;
|
|
use BookStack\Users\UserRepo;
|
|
use Illuminate\Http\Request;
|
|
|
|
class UserPreferencesController extends Controller
|
|
{
|
|
public function __construct(
|
|
protected UserRepo $userRepo
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Show the user-specific interface shortcuts.
|
|
*/
|
|
public function showShortcuts()
|
|
{
|
|
$shortcuts = UserShortcutMap::fromUserPreferences();
|
|
$enabled = setting()->getForCurrentUser('ui-shortcuts-enabled', false);
|
|
|
|
return view('users.preferences.shortcuts', [
|
|
'shortcuts' => $shortcuts,
|
|
'enabled' => $enabled,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the user-specific interface shortcuts.
|
|
*/
|
|
public function updateShortcuts(Request $request)
|
|
{
|
|
$enabled = $request->get('enabled') === 'true';
|
|
$providedShortcuts = $request->get('shortcut', []);
|
|
$shortcuts = new UserShortcutMap($providedShortcuts);
|
|
|
|
setting()->putForCurrentUser('ui-shortcuts', $shortcuts->toJson());
|
|
setting()->putForCurrentUser('ui-shortcuts-enabled', $enabled);
|
|
|
|
$this->showSuccessNotification(trans('preferences.shortcuts_update_success'));
|
|
|
|
return redirect('/preferences/shortcuts');
|
|
}
|
|
|
|
/**
|
|
* Show the notification preferences for the current user.
|
|
*/
|
|
public function showNotifications()
|
|
{
|
|
$preferences = (new UserNotificationPreferences(user()));
|
|
|
|
return view('users.preferences.notifications', [
|
|
'preferences' => $preferences,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the notification preferences for the current user.
|
|
*/
|
|
public function updateNotifications(Request $request)
|
|
{
|
|
$data = $this->validate($request, [
|
|
'preferences' => ['required', 'array'],
|
|
'preferences.*' => ['required', 'string'],
|
|
]);
|
|
|
|
$preferences = (new UserNotificationPreferences(user()));
|
|
$preferences->updateFromSettingsArray($data['preferences']);
|
|
$this->showSuccessNotification(trans('preferences.notifications_update_success'));
|
|
|
|
return redirect('/preferences/notifications');
|
|
}
|
|
|
|
/**
|
|
* Update the preferred view format for a list view of the given type.
|
|
*/
|
|
public function changeView(Request $request, string $type)
|
|
{
|
|
$valueViewTypes = ['books', 'bookshelves', 'bookshelf'];
|
|
if (!in_array($type, $valueViewTypes)) {
|
|
return redirect()->back(500);
|
|
}
|
|
|
|
$view = $request->get('view');
|
|
if (!in_array($view, ['grid', 'list'])) {
|
|
$view = 'list';
|
|
}
|
|
|
|
$key = $type . '_view_type';
|
|
setting()->putForCurrentUser($key, $view);
|
|
|
|
return redirect()->back(302, [], "/");
|
|
}
|
|
|
|
/**
|
|
* Change the stored sort type for a particular view.
|
|
*/
|
|
public function changeSort(Request $request, string $type)
|
|
{
|
|
$validSortTypes = ['books', 'bookshelves', 'shelf_books', 'users', 'roles', 'webhooks', 'tags', 'page_revisions'];
|
|
if (!in_array($type, $validSortTypes)) {
|
|
return redirect()->back(500);
|
|
}
|
|
|
|
$sort = substr($request->get('sort') ?: 'name', 0, 50);
|
|
$order = $request->get('order') === 'desc' ? 'desc' : 'asc';
|
|
|
|
$sortKey = $type . '_sort';
|
|
$orderKey = $type . '_sort_order';
|
|
setting()->putForCurrentUser($sortKey, $sort);
|
|
setting()->putForCurrentUser($orderKey, $order);
|
|
|
|
return redirect()->back(302, [], "/");
|
|
}
|
|
|
|
/**
|
|
* Toggle dark mode for the current user.
|
|
*/
|
|
public function toggleDarkMode()
|
|
{
|
|
$enabled = setting()->getForCurrentUser('dark-mode-enabled', false);
|
|
setting()->putForCurrentUser('dark-mode-enabled', $enabled ? 'false' : 'true');
|
|
|
|
return redirect()->back();
|
|
}
|
|
|
|
/**
|
|
* Update the stored section expansion preference for the given user.
|
|
*/
|
|
public function changeExpansion(Request $request, string $type)
|
|
{
|
|
$typeWhitelist = ['home-details'];
|
|
if (!in_array($type, $typeWhitelist)) {
|
|
return response('Invalid key', 500);
|
|
}
|
|
|
|
$newState = $request->get('expand', 'false');
|
|
setting()->putForCurrentUser('section_expansion#' . $type, $newState);
|
|
|
|
return response('', 204);
|
|
}
|
|
|
|
/**
|
|
* Update the favorite status for a code language.
|
|
*/
|
|
public function updateCodeLanguageFavourite(Request $request)
|
|
{
|
|
$validated = $this->validate($request, [
|
|
'language' => ['required', 'string', 'max:20'],
|
|
'active' => ['required', 'bool'],
|
|
]);
|
|
|
|
$currentFavoritesStr = setting()->getForCurrentUser('code-language-favourites', '');
|
|
$currentFavorites = array_filter(explode(',', $currentFavoritesStr));
|
|
|
|
$isFav = in_array($validated['language'], $currentFavorites);
|
|
if (!$isFav && $validated['active']) {
|
|
$currentFavorites[] = $validated['language'];
|
|
} elseif ($isFav && !$validated['active']) {
|
|
$index = array_search($validated['language'], $currentFavorites);
|
|
array_splice($currentFavorites, $index, 1);
|
|
}
|
|
|
|
setting()->putForCurrentUser('code-language-favourites', implode(',', $currentFavorites));
|
|
return response('', 204);
|
|
}
|
|
}
|