]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SettingController.php
Re-ordered and updated main settings page
[bookstack] / app / Http / Controllers / SettingController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Auth\User;
4 use BookStack\Uploads\ImageService;
5 use Illuminate\Http\Request;
6 use Illuminate\Http\Response;
7 use Setting;
8
9 class SettingController extends Controller
10 {
11     /**
12      * Display a listing of the settings.
13      * @return Response
14      */
15     public function index()
16     {
17         $this->checkPermission('settings-manage');
18         $this->setPageTitle(trans('settings.settings'));
19
20         // Get application version
21         $version = trim(file_get_contents(base_path('version')));
22
23         return view('settings.index', [
24             'version' => $version,
25             'guestUser' => User::getDefault()
26         ]);
27     }
28
29     /**
30      * Update the specified settings in storage.
31      * @param  Request $request
32      * @return Response
33      */
34     public function update(Request $request)
35     {
36         $this->preventAccessForDemoUsers();
37         $this->checkPermission('settings-manage');
38
39         // Cycles through posted settings and update them
40         foreach ($request->all() as $name => $value) {
41             if (strpos($name, 'setting-') !== 0) {
42                 continue;
43             }
44             $key = str_replace('setting-', '', trim($name));
45             Setting::put($key, $value);
46         }
47
48         session()->flash('success', trans('settings.settings_save_success'));
49         return redirect('/settings');
50     }
51
52     /**
53      * Show the page for application maintenance.
54      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
55      */
56     public function showMaintenance()
57     {
58         $this->checkPermission('settings-manage');
59         $this->setPageTitle(trans('settings.maint'));
60
61         // Get application version
62         $version = trim(file_get_contents(base_path('version')));
63
64         return view('settings.maintenance', ['version' => $version]);
65     }
66
67     /**
68      * Action to clean-up images in the system.
69      * @param Request $request
70      * @param ImageService $imageService
71      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
72      */
73     public function cleanupImages(Request $request, ImageService $imageService)
74     {
75         $this->checkPermission('settings-manage');
76
77         $checkRevisions = !($request->get('ignore_revisions', 'false') === 'true');
78         $dryRun = !($request->has('confirm'));
79
80         $imagesToDelete = $imageService->deleteUnusedImages($checkRevisions, $dryRun);
81         $deleteCount = count($imagesToDelete);
82         if ($deleteCount === 0) {
83             session()->flash('warning', trans('settings.maint_image_cleanup_nothing_found'));
84             return redirect('/settings/maintenance')->withInput();
85         }
86
87         if ($dryRun) {
88             session()->flash('cleanup-images-warning', trans('settings.maint_image_cleanup_warning', ['count' => $deleteCount]));
89         } else {
90             session()->flash('success', trans('settings.maint_image_cleanup_success', ['count' => $deleteCount]));
91         }
92
93         return redirect('/settings/maintenance#image-cleanup')->withInput();
94     }
95 }