2022-10-30 16:25:02 +01:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 18:56:55 +02:00
|
|
|
namespace BookStack\Users\Controllers;
|
2022-10-30 16:25:02 +01:00
|
|
|
|
2023-05-18 21:53:39 +02:00
|
|
|
use BookStack\Http\Controller;
|
2023-08-14 14:11:18 +02:00
|
|
|
use BookStack\Permissions\PermissionApplicator;
|
2023-07-25 18:06:48 +02:00
|
|
|
use BookStack\Settings\UserNotificationPreferences;
|
2022-11-08 22:17:45 +01:00
|
|
|
use BookStack\Settings\UserShortcutMap;
|
2023-05-17 18:56:55 +02:00
|
|
|
use BookStack\Users\UserRepo;
|
2022-10-30 16:25:02 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class UserPreferencesController extends Controller
|
|
|
|
{
|
2023-07-25 18:06:48 +02:00
|
|
|
public function __construct(
|
|
|
|
protected UserRepo $userRepo
|
|
|
|
) {
|
2022-10-30 16:25:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-09 20:30:08 +01:00
|
|
|
* Update the preferred view format for a list view of the given type.
|
2022-10-30 16:25:02 +01:00
|
|
|
*/
|
2022-11-09 20:30:08 +01:00
|
|
|
public function changeView(Request $request, string $type)
|
2022-10-30 16:25:02 +01:00
|
|
|
{
|
2022-11-09 20:30:08 +01:00
|
|
|
$valueViewTypes = ['books', 'bookshelves', 'bookshelf'];
|
|
|
|
if (!in_array($type, $valueViewTypes)) {
|
|
|
|
return redirect()->back(500);
|
|
|
|
}
|
2022-10-30 16:25:02 +01:00
|
|
|
|
2022-11-09 20:30:08 +01:00
|
|
|
$view = $request->get('view');
|
|
|
|
if (!in_array($view, ['grid', 'list'])) {
|
|
|
|
$view = 'list';
|
2022-10-30 16:25:02 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 20:30:08 +01:00
|
|
|
$key = $type . '_view_type';
|
|
|
|
setting()->putForCurrentUser($key, $view);
|
2022-10-30 16:25:02 +01:00
|
|
|
|
2022-11-09 20:30:08 +01:00
|
|
|
return redirect()->back(302, [], "/");
|
2022-10-30 16:25:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the stored sort type for a particular view.
|
|
|
|
*/
|
2022-11-09 20:30:08 +01:00
|
|
|
public function changeSort(Request $request, string $type)
|
2022-10-30 16:25:02 +01:00
|
|
|
{
|
2022-10-31 22:26:31 +01:00
|
|
|
$validSortTypes = ['books', 'bookshelves', 'shelf_books', 'users', 'roles', 'webhooks', 'tags', 'page_revisions'];
|
2022-10-30 16:25:02 +01:00
|
|
|
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';
|
2022-11-09 20:30:08 +01:00
|
|
|
setting()->putForCurrentUser($sortKey, $sort);
|
|
|
|
setting()->putForCurrentUser($orderKey, $order);
|
2022-10-30 16:25:02 +01:00
|
|
|
|
2022-11-09 20:30:08 +01:00
|
|
|
return redirect()->back(302, [], "/");
|
2022-10-30 16:25:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle dark mode for the current user.
|
|
|
|
*/
|
|
|
|
public function toggleDarkMode()
|
|
|
|
{
|
2023-09-14 14:53:24 +02:00
|
|
|
$enabled = setting()->getForCurrentUser('dark-mode-enabled');
|
2022-11-09 20:30:08 +01:00
|
|
|
setting()->putForCurrentUser('dark-mode-enabled', $enabled ? 'false' : 'true');
|
2022-10-30 16:25:02 +01:00
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the stored section expansion preference for the given user.
|
|
|
|
*/
|
2022-11-09 20:30:08 +01:00
|
|
|
public function changeExpansion(Request $request, string $type)
|
2022-10-30 16:25:02 +01:00
|
|
|
{
|
2022-11-09 20:30:08 +01:00
|
|
|
$typeWhitelist = ['home-details'];
|
|
|
|
if (!in_array($type, $typeWhitelist)) {
|
2022-10-30 16:25:02 +01:00
|
|
|
return response('Invalid key', 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
$newState = $request->get('expand', 'false');
|
2022-11-09 20:30:08 +01:00
|
|
|
setting()->putForCurrentUser('section_expansion#' . $type, $newState);
|
2022-10-30 16:25:02 +01:00
|
|
|
|
|
|
|
return response('', 204);
|
|
|
|
}
|
|
|
|
|
2022-11-27 21:30:14 +01:00
|
|
|
/**
|
|
|
|
* Update the favorite status for a code language.
|
|
|
|
*/
|
2022-10-30 16:25:02 +01:00
|
|
|
public function updateCodeLanguageFavourite(Request $request)
|
|
|
|
{
|
|
|
|
$validated = $this->validate($request, [
|
|
|
|
'language' => ['required', 'string', 'max:20'],
|
2023-07-25 18:06:48 +02:00
|
|
|
'active' => ['required', 'bool'],
|
2022-10-30 16:25:02 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2022-11-09 20:30:08 +01:00
|
|
|
setting()->putForCurrentUser('code-language-favourites', implode(',', $currentFavorites));
|
2022-11-27 21:30:14 +01:00
|
|
|
return response('', 204);
|
|
|
|
}
|
2022-10-30 16:25:02 +01:00
|
|
|
}
|