]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SettingController.php
Apply fixes from StyleCI
[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;
13
14     /**
15      * SettingController constructor.
16      */
17     public function __construct(ImageRepo $imageRepo)
18     {
19         $this->imageRepo = $imageRepo;
20     }
21
22     /**
23      * Display a listing of the settings.
24      */
25     public function index()
26     {
27         $this->checkPermission('settings-manage');
28         $this->setPageTitle(trans('settings.settings'));
29
30         // Get application version
31         $version = trim(file_get_contents(base_path('version')));
32
33         return view('settings.index', [
34             'version'   => $version,
35             'guestUser' => User::getDefault(),
36         ]);
37     }
38
39     /**
40      * Update the specified settings in storage.
41      */
42     public function update(Request $request)
43     {
44         $this->preventAccessInDemoMode();
45         $this->checkPermission('settings-manage');
46         $this->validate($request, [
47             'app_logo' => 'nullable|' . $this->getImageValidationRules(),
48         ]);
49
50         // Cycles through posted settings and update them
51         foreach ($request->all() as $name => $value) {
52             $key = str_replace('setting-', '', trim($name));
53             if (strpos($name, 'setting-') !== 0) {
54                 continue;
55             }
56             setting()->put($key, $value);
57         }
58
59         // Update logo image if set
60         if ($request->hasFile('app_logo')) {
61             $logoFile = $request->file('app_logo');
62             $this->imageRepo->destroyByType('system');
63             $image = $this->imageRepo->saveNew($logoFile, 'system', 0, null, 86);
64             setting()->put('app-logo', $image->url);
65         }
66
67         // Clear logo image if requested
68         if ($request->get('app_logo_reset', null)) {
69             $this->imageRepo->destroyByType('system');
70             setting()->remove('app-logo');
71         }
72
73         $section = $request->get('section', '');
74         $this->logActivity(ActivityType::SETTINGS_UPDATE, $section);
75         $this->showSuccessNotification(trans('settings.settings_save_success'));
76         $redirectLocation = '/settings#' . $section;
77
78         return redirect(rtrim($redirectLocation, '#'));
79     }
80 }