]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SettingController.php
Merge branch 'master' into nwalke-update_site_color
[bookstack] / app / Http / Controllers / SettingController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use Illuminate\Http\Request;
6
7 use BookStack\Http\Requests;
8 use BookStack\Http\Controllers\Controller;
9 use Setting;
10
11 class SettingController extends Controller
12 {
13     /**
14      * Display a listing of the settings.
15      *
16      * @return Response
17      */
18     public function index()
19     {
20         $this->checkPermission('settings-manage');
21         $this->setPageTitle('Settings');
22         return view('settings/index');
23     }
24
25
26     /**
27      * Update the specified settings in storage.
28      *
29      * @param  Request  $request
30      * @return Response
31      */
32     public function update(Request $request)
33     {
34         $this->preventAccessForDemoUsers();
35         $this->checkPermission('settings-manage');
36
37         // Cycles through posted settings and update them
38         foreach($request->all() as $name => $value) {
39             if(strpos($name, 'setting-') !== 0) continue;
40             $key = str_replace('setting-', '', trim($name));
41             if($key == 'app-color') {
42                 Setting::put('app-color-rgba', $this->hex2rgba($value, 0.15));
43             }
44             Setting::put($key, $value);
45         }
46
47         session()->flash('success', 'Settings Saved');
48         return redirect('/settings');
49     }
50
51     /**
52      * Adapted from http://mekshq.com/how-to-convert-hexadecimal-color-code-to-rgb-or-rgba-using-php/
53      * Converts a hex color code in to an RGBA string.
54      *
55      * @param string $color
56      * @param float|boolean $opacity
57      * @return boolean|string
58      */
59     protected function hex2rgba($color, $opacity = false)
60     {
61         // Return false if no color provided
62         if(empty($color)) {
63             return false;
64         }
65         // Trim any whitespace
66         $color = trim($color);
67
68         // Sanitize $color if "#" is provided
69         if($color[0] == '#' ) {
70             $color = substr($color, 1);
71         }
72
73         // Check if color has 6 or 3 characters and get values
74         if(strlen($color) == 6) {
75             $hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
76         } elseif( strlen( $color ) == 3 ) {
77             $hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
78         } else {
79             return false;
80         }
81
82         // Convert hexadec to rgb
83         $rgb =  array_map('hexdec', $hex);
84
85         // Check if opacity is set(rgba or rgb)
86         if($opacity) {
87             if(abs($opacity) > 1)
88                 $opacity = 1.0;
89             $output = 'rgba('.implode(",",$rgb).','.$opacity.')';
90         } else {
91             $output = 'rgb('.implode(",",$rgb).')';
92         }
93
94         // Return rgb(a) color string
95         return $output;
96     }
97
98 }