| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | namespace BookStack\Users\Controllers; | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-19 03:53:39 +08:00
										 |  |  | use BookStack\Http\Controller; | 
					
						
							| 
									
										
										
										
											2023-08-14 20:11:18 +08:00
										 |  |  | use BookStack\Permissions\PermissionApplicator; | 
					
						
							| 
									
										
										
										
											2023-07-26 00:06:48 +08:00
										 |  |  | use BookStack\Settings\UserNotificationPreferences; | 
					
						
							| 
									
										
										
										
											2022-11-09 05:17:45 +08:00
										 |  |  | use BookStack\Settings\UserShortcutMap; | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Users\UserRepo; | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UserPreferencesController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-26 00:06:48 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected UserRepo $userRepo | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-15 00:29:12 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Show the overview for user preferences. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function index() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return view('users.preferences.index'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-09 05:17:45 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Show the user-specific interface shortcuts. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function showShortcuts() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $shortcuts = UserShortcutMap::fromUserPreferences(); | 
					
						
							|  |  |  |         $enabled = setting()->getForCurrentUser('ui-shortcuts-enabled', false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 03:15:49 +08:00
										 |  |  |         $this->setPageTitle(trans('preferences.shortcuts_interface')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-09 05:17:45 +08:00
										 |  |  |         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 22:40:44 +08:00
										 |  |  |         $providedShortcuts = $request->get('shortcut', []); | 
					
						
							| 
									
										
										
										
											2022-11-09 05:17:45 +08:00
										 |  |  |         $shortcuts = new UserShortcutMap($providedShortcuts); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |         setting()->putForCurrentUser('ui-shortcuts', $shortcuts->toJson()); | 
					
						
							|  |  |  |         setting()->putForCurrentUser('ui-shortcuts-enabled', $enabled); | 
					
						
							| 
									
										
										
										
											2022-11-09 05:17:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-10 18:25:28 +08:00
										 |  |  |         $this->showSuccessNotification(trans('preferences.shortcuts_update_success')); | 
					
						
							| 
									
										
										
										
											2022-11-09 05:17:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return redirect('/preferences/shortcuts'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-26 00:06:48 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Show the notification preferences for the current user. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-08-14 20:11:18 +08:00
										 |  |  |     public function showNotifications(PermissionApplicator $permissions) | 
					
						
							| 
									
										
										
										
											2023-07-26 00:06:48 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-15 00:29:12 +08:00
										 |  |  |         $this->checkPermission('receive-notifications'); | 
					
						
							| 
									
										
										
										
											2023-08-16 23:02:00 +08:00
										 |  |  |         $this->preventGuestAccess(); | 
					
						
							| 
									
										
										
										
											2023-08-15 00:29:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-26 00:06:48 +08:00
										 |  |  |         $preferences = (new UserNotificationPreferences(user())); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-03 21:19:43 +08:00
										 |  |  |         $query = user()->watches()->getQuery(); | 
					
						
							| 
									
										
										
										
											2023-08-14 20:11:18 +08:00
										 |  |  |         $query = $permissions->restrictEntityRelationQuery($query, 'watches', 'watchable_id', 'watchable_type'); | 
					
						
							| 
									
										
										
										
											2023-09-03 21:19:43 +08:00
										 |  |  |         $query = $permissions->filterDeletedFromEntityRelationQuery($query, 'watches', 'watchable_id', 'watchable_type'); | 
					
						
							| 
									
										
										
										
											2023-08-14 20:11:18 +08:00
										 |  |  |         $watches = $query->with('watchable')->paginate(20); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 03:15:49 +08:00
										 |  |  |         $this->setPageTitle(trans('preferences.notifications')); | 
					
						
							| 
									
										
										
										
											2023-07-26 00:06:48 +08:00
										 |  |  |         return view('users.preferences.notifications', [ | 
					
						
							|  |  |  |             'preferences' => $preferences, | 
					
						
							| 
									
										
										
										
											2023-08-14 20:11:18 +08:00
										 |  |  |             'watches' => $watches, | 
					
						
							| 
									
										
										
										
											2023-07-26 00:06:48 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Update the notification preferences for the current user. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function updateNotifications(Request $request) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-15 00:29:12 +08:00
										 |  |  |         $this->checkPermission('receive-notifications'); | 
					
						
							| 
									
										
										
										
											2023-08-16 23:02:00 +08:00
										 |  |  |         $this->preventGuestAccess(); | 
					
						
							| 
									
										
										
										
											2023-07-26 00:06:48 +08:00
										 |  |  |         $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'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |      * Update the preferred view format for a list view of the given type. | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |     public function changeView(Request $request, string $type) | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |         $valueViewTypes = ['books', 'bookshelves', 'bookshelf']; | 
					
						
							|  |  |  |         if (!in_array($type, $valueViewTypes)) { | 
					
						
							|  |  |  |             return redirect()->back(500); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |         $view = $request->get('view'); | 
					
						
							|  |  |  |         if (!in_array($view, ['grid', 'list'])) { | 
					
						
							|  |  |  |             $view = 'list'; | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |         $key = $type . '_view_type'; | 
					
						
							|  |  |  |         setting()->putForCurrentUser($key, $view); | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |         return redirect()->back(302, [], "/"); | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Change the stored sort type for a particular view. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |     public function changeSort(Request $request, string $type) | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-11-01 05:26:31 +08:00
										 |  |  |         $validSortTypes = ['books', 'bookshelves', 'shelf_books', 'users', 'roles', 'webhooks', 'tags', 'page_revisions']; | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08: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-10 03:30:08 +08:00
										 |  |  |         setting()->putForCurrentUser($sortKey, $sort); | 
					
						
							|  |  |  |         setting()->putForCurrentUser($orderKey, $order); | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |         return redirect()->back(302, [], "/"); | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Toggle dark mode for the current user. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function toggleDarkMode() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $enabled = setting()->getForCurrentUser('dark-mode-enabled', false); | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |         setting()->putForCurrentUser('dark-mode-enabled', $enabled ? 'false' : 'true'); | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return redirect()->back(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Update the stored section expansion preference for the given user. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |     public function changeExpansion(Request $request, string $type) | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |         $typeWhitelist = ['home-details']; | 
					
						
							|  |  |  |         if (!in_array($type, $typeWhitelist)) { | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  |             return response('Invalid key', 500); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $newState = $request->get('expand', 'false'); | 
					
						
							| 
									
										
										
										
											2022-11-10 03:30:08 +08:00
										 |  |  |         setting()->putForCurrentUser('section_expansion#' . $type, $newState); | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return response('', 204); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 04:30:14 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Update the favorite status for a code language. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  |     public function updateCodeLanguageFavourite(Request $request) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $validated = $this->validate($request, [ | 
					
						
							|  |  |  |             'language' => ['required', 'string', 'max:20'], | 
					
						
							| 
									
										
										
										
											2023-07-26 00:06:48 +08:00
										 |  |  |             'active' => ['required', 'bool'], | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08: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-10 03:30:08 +08:00
										 |  |  |         setting()->putForCurrentUser('code-language-favourites', implode(',', $currentFavorites)); | 
					
						
							| 
									
										
										
										
											2022-11-28 04:30:14 +08:00
										 |  |  |         return response('', 204); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  | } |