]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SettingController.php
Merge branch 'v0.30.x'
[bookstack] / app / Http / Controllers / SettingController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Actions\ActivityType;
4 use BookStack\Auth\User;
5 use BookStack\Uploads\ImageRepo;
6 use Illuminate\Http\Request;
7
8 class SettingController extends Controller
9 {
10     protected $imageRepo;
11
12     /**
13      * SettingController constructor.
14      */
15     public function __construct(ImageRepo $imageRepo)
16     {
17         $this->imageRepo = $imageRepo;
18     }
19
20     /**
21      * Display a listing of the settings.
22      */
23     public function index()
24     {
25         $this->checkPermission('settings-manage');
26         $this->setPageTitle(trans('settings.settings'));
27
28         // Get application version
29         $version = trim(file_get_contents(base_path('version')));
30
31         return view('settings.index', [
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)
41     {
42         $this->preventAccessInDemoMode();
43         $this->checkPermission('settings-manage');
44         $this->validate($request, [
45             'app_logo' => '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 ($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 ($request->get('app_logo_reset', null)) {
67             $this->imageRepo->destroyByType('system');
68             setting()->remove('app-logo');
69         }
70
71         $section = $request->get('section', '');
72         $this->logActivity(ActivityType::SETTINGS_UPDATE, $section);
73         $this->showSuccessNotification(trans('settings.settings_save_success'));
74         $redirectLocation = '/settings#' . $section;
75         return redirect(rtrim($redirectLocation, '#'));
76     }
77 }