2022-10-30 11:25:02 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Http\Controllers;
|
|
|
|
|
|
|
|
use BookStack\Auth\UserRepo;
|
2022-11-08 16:17:45 -05:00
|
|
|
use BookStack\Settings\UserShortcutMap;
|
2022-10-30 11:25:02 -04:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class UserPreferencesController extends Controller
|
|
|
|
{
|
|
|
|
protected UserRepo $userRepo;
|
|
|
|
|
|
|
|
public function __construct(UserRepo $userRepo)
|
|
|
|
{
|
|
|
|
$this->userRepo = $userRepo;
|
|
|
|
}
|
|
|
|
|
2022-11-08 16:17:45 -05:00
|
|
|
/**
|
|
|
|
* 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';
|
2022-11-09 09:40:44 -05:00
|
|
|
$providedShortcuts = $request->get('shortcut', []);
|
2022-11-08 16:17:45 -05:00
|
|
|
$shortcuts = new UserShortcutMap($providedShortcuts);
|
|
|
|
|
|
|
|
setting()->putUser(user(), 'ui-shortcuts', $shortcuts->toJson());
|
|
|
|
setting()->putUser(user(), 'ui-shortcuts-enabled', $enabled);
|
|
|
|
|
2022-11-09 13:42:54 -05:00
|
|
|
$this->showSuccessNotification('Shortcut preferences have been updated!');
|
2022-11-08 16:17:45 -05:00
|
|
|
|
|
|
|
return redirect('/preferences/shortcuts');
|
|
|
|
}
|
|
|
|
|
2022-10-30 11:25:02 -04:00
|
|
|
/**
|
|
|
|
* Update the user's preferred book-list display setting.
|
|
|
|
*/
|
|
|
|
public function switchBooksView(Request $request, int $id)
|
|
|
|
{
|
|
|
|
return $this->switchViewType($id, $request, 'books');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the user's preferred shelf-list display setting.
|
|
|
|
*/
|
|
|
|
public function switchShelvesView(Request $request, int $id)
|
|
|
|
{
|
|
|
|
return $this->switchViewType($id, $request, 'bookshelves');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the user's preferred shelf-view book list display setting.
|
|
|
|
*/
|
|
|
|
public function switchShelfView(Request $request, int $id)
|
|
|
|
{
|
|
|
|
return $this->switchViewType($id, $request, 'bookshelf');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For a type of list, switch with stored view type for a user.
|
|
|
|
*/
|
|
|
|
protected function switchViewType(int $userId, Request $request, string $listName)
|
|
|
|
{
|
|
|
|
$this->checkPermissionOrCurrentUser('users-manage', $userId);
|
|
|
|
|
|
|
|
$viewType = $request->get('view_type');
|
|
|
|
if (!in_array($viewType, ['grid', 'list'])) {
|
|
|
|
$viewType = 'list';
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $this->userRepo->getById($userId);
|
|
|
|
$key = $listName . '_view_type';
|
|
|
|
setting()->putUser($user, $key, $viewType);
|
|
|
|
|
|
|
|
return redirect()->back(302, [], "/settings/users/$userId");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the stored sort type for a particular view.
|
|
|
|
*/
|
|
|
|
public function changeSort(Request $request, string $id, string $type)
|
|
|
|
{
|
2022-10-31 17:26:31 -04:00
|
|
|
$validSortTypes = ['books', 'bookshelves', 'shelf_books', 'users', 'roles', 'webhooks', 'tags', 'page_revisions'];
|
2022-10-30 11:25:02 -04:00
|
|
|
if (!in_array($type, $validSortTypes)) {
|
|
|
|
return redirect()->back(500);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->checkPermissionOrCurrentUser('users-manage', $id);
|
|
|
|
|
|
|
|
$sort = substr($request->get('sort') ?: 'name', 0, 50);
|
|
|
|
$order = $request->get('order') === 'desc' ? 'desc' : 'asc';
|
|
|
|
|
|
|
|
$user = $this->userRepo->getById($id);
|
|
|
|
$sortKey = $type . '_sort';
|
|
|
|
$orderKey = $type . '_sort_order';
|
|
|
|
setting()->putUser($user, $sortKey, $sort);
|
|
|
|
setting()->putUser($user, $orderKey, $order);
|
|
|
|
|
|
|
|
return redirect()->back(302, [], "/settings/users/{$id}");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle dark mode for the current user.
|
|
|
|
*/
|
|
|
|
public function toggleDarkMode()
|
|
|
|
{
|
|
|
|
$enabled = setting()->getForCurrentUser('dark-mode-enabled', false);
|
|
|
|
setting()->putUser(user(), 'dark-mode-enabled', $enabled ? 'false' : 'true');
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the stored section expansion preference for the given user.
|
|
|
|
*/
|
|
|
|
public function updateExpansionPreference(Request $request, string $id, string $key)
|
|
|
|
{
|
|
|
|
$this->checkPermissionOrCurrentUser('users-manage', $id);
|
|
|
|
$keyWhitelist = ['home-details'];
|
|
|
|
if (!in_array($key, $keyWhitelist)) {
|
|
|
|
return response('Invalid key', 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
$newState = $request->get('expand', 'false');
|
|
|
|
|
|
|
|
$user = $this->userRepo->getById($id);
|
|
|
|
setting()->putUser($user, 'section_expansion#' . $key, $newState);
|
|
|
|
|
|
|
|
return response('', 204);
|
|
|
|
}
|
|
|
|
|
|
|
|
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()->putUser(user(), 'code-language-favourites', implode(',', $currentFavorites));
|
|
|
|
}
|
|
|
|
}
|