]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SettingController.php
Split out settings view and made functional
[bookstack] / app / Http / Controllers / SettingController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\User;
7 use BookStack\Uploads\ImageRepo;
8 use Illuminate\Http\Request;
9
10 class SettingController extends Controller
11 {
12     protected ImageRepo $imageRepo;
13
14     public function __construct(ImageRepo $imageRepo)
15     {
16         $this->imageRepo = $imageRepo;
17     }
18
19     /**
20      * Display a listing of the settings.
21      */
22     public function index(string $category)
23     {
24         $this->checkPermission('settings-manage');
25         $this->setPageTitle(trans('settings.settings'));
26
27         // Get application version
28         $version = trim(file_get_contents(base_path('version')));
29
30         return view('settings.' . $category, [
31             'category'  => $category,
32             'version'   => $version,
33             'guestUser' => User::getDefault(),
34         ]);
35     }
36
37     /**
38      * Update the specified settings in storage.
39      */
40     public function update(Request $request, string $category)
41     {
42         $this->preventAccessInDemoMode();
43         $this->checkPermission('settings-manage');
44         $this->validate($request, [
45             'app_logo' => array_merge(['nullable'], $this->getImageValidationRules()),
46         ]);
47
48         // Cycles through posted settings and update them
49         foreach ($request->all() as $name => $value) {
50             $key = str_replace('setting-', '', trim($name));
51             if (strpos($name, 'setting-') !== 0) {
52                 continue;
53             }
54             setting()->put($key, $value);
55         }
56
57         // Update logo image if set
58         if ($category === 'customization' && $request->hasFile('app_logo')) {
59             $logoFile = $request->file('app_logo');
60             $this->imageRepo->destroyByType('system');
61             $image = $this->imageRepo->saveNew($logoFile, 'system', 0, null, 86);
62             setting()->put('app-logo', $image->url);
63         }
64
65         // Clear logo image if requested
66         if ($category === 'customization' &&  $request->get('app_logo_reset', null)) {
67             $this->imageRepo->destroyByType('system');
68             setting()->remove('app-logo');
69         }
70
71         $this->logActivity(ActivityType::SETTINGS_UPDATE, $category);
72         $this->showSuccessNotification(trans('settings.settings_save_success'));
73
74         return redirect("/settings/${category}");
75     }
76 }