SettingsController::__construct()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 40
rs 8.8571
cc 1
eloc 30
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Http\Controllers\Admin;
13
14
use Exception;
15
use Gitamin\Models\Setting;
16
use Illuminate\Routing\Controller;
17
use Illuminate\Support\Facades\Lang;
18
use Illuminate\Support\Facades\Redirect;
19
use Illuminate\Support\Facades\Request;
20
use Illuminate\Support\Facades\Session;
21
use Illuminate\Support\Facades\View;
22
23
class SettingsController extends Controller
24
{
25
    /**
26
     * Array of sub-menu items.
27
     *
28
     * @var array
29
     */
30
    protected $subMenu = [];
31
32
    /**
33
     * Creates a new settings controller instance.
34
     */
35
    public function __construct()
36
    {
37
        $this->subMenu = [
38
            'general' => [
39
                'title' => trans('admin.settings.general.general'),
40
                'url' => route('admin.settings.general'),
41
                'icon' => 'fa fa-gear',
42
                'active' => false,
43
            ],
44
            'theme' => [
45
                'title' => trans('admin.settings.theme.theme'),
46
                'url' => route('admin.settings.theme'),
47
                'icon' => 'fa fa-list-alt',
48
                'active' => false,
49
            ],
50
            'stylesheet' => [
51
                'title' => trans('admin.settings.stylesheet.stylesheet'),
52
                'url' => route('admin.settings.stylesheet'),
53
                'icon' => 'fa fa-magic',
54
                'active' => false,
55
            ],
56
            'localization' => [
57
                'title' => trans('admin.settings.localization.localization'),
58
                'url' => route('admin.settings.localization'),
59
                'icon' => 'fa fa-language',
60
                'active' => false,
61
            ],
62
            'timezone' => [
63
                'title' => trans('admin.settings.timezone.timezone'),
64
                'url' => route('admin.settings.timezone'),
65
                'icon' => 'fa fa-calendar',
66
                'active' => false,
67
            ],
68
        ];
69
70
        View::share([
71
            'sub_title' => trans('admin.settings.settings'),
72
            'sub_menu' => $this->subMenu,
73
        ]);
74
    }
75
76
    /**
77
     * Shows the settings general view.
78
     *
79
     * @return \Illuminate\View\View
80
     */
81 View Code Duplication
    public function showGeneralView()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        $this->subMenu['general']['active'] = true;
84
85
        Session::flash('redirect_to', $this->subMenu['general']['url']);
86
87
        return View::make('admin.settings.general')
88
            ->withPageTitle(trans('admin.settings.general.general').' - '.trans('admin.admin'))
89
            ->withSubMenu($this->subMenu);
90
    }
91
92
    /**
93
     * Shows the settings localization view.
94
     *
95
     * @return \Illuminate\View\View
96
     */
97 View Code Duplication
    public function showLocalizationView()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        $this->subMenu['localization']['active'] = true;
100
101
        Session::flash('redirect_to', $this->subMenu['localization']['url']);
102
103
        return View::make('admin.settings.localization')
104
            ->withPageTitle(trans('admin.settings.localization.localization').' - '.trans('admin.admin'))
105
            ->withSubMenu($this->subMenu);
106
    }
107
108
    /**
109
     * Shows the settings theme view.
110
     *
111
     * @return \Illuminate\View\View
112
     */
113 View Code Duplication
    public function showThemeView()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        $this->subMenu['theme']['active'] = true;
116
117
        Session::flash('redirect_to', $this->subMenu['theme']['url']);
118
119
        return View::make('admin.settings.theme')
120
            ->withPageTitle(trans('admin.settings.theme.theme').' - '.trans('admin.admin'))
121
            ->withSubMenu($this->subMenu);
122
    }
123
124
    /**
125
     * Shows the settings timezone view.
126
     *
127
     * @return \Illuminate\View\View
128
     */
129 View Code Duplication
    public function showTimezoneView()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        $this->subMenu['timezone']['active'] = true;
132
133
        Session::flash('redirect_to', $this->subMenu['timezone']['url']);
134
135
        return View::make('admin.settings.timezone')
136
            ->withPageTitle(trans('admin.settings.timezone.timezone').' - '.trans('admin.admin'))
137
            ->withSubMenu($this->subMenu);
138
    }
139
140
    /**
141
     * Shows the settings stylesheet view.
142
     *
143
     * @return \Illuminate\View\View
144
     */
145 View Code Duplication
    public function showStylesheetView()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
    {
147
        $this->subMenu['stylesheet']['active'] = true;
148
149
        Session::flash('redirect_to', $this->subMenu['stylesheet']['url']);
150
151
        return View::make('admin.settings.stylesheet')
152
            ->withPageTitle(trans('admin.settings.stylesheet.stylesheet').' - '.trans('admin.admin'))
153
            ->withSubMenu($this->subMenu);
154
    }
155
156
    /**
157
     * Updates the system settings.
158
     *
159
     * @return \Illuminate\View\View
160
     */
161
    public function postSettings()
162
    {
163
        $redirectUrl = Session::get('redirect_to', route('admin.settings.general'));
164
165
        if (Request::get('remove_banner') === '1') {
166
            $setting = Setting::where('name', 'app_banner');
167
            $setting->delete();
168
        }
169
170
        if (Request::hasFile('app_banner')) {
171
            $file = Request::file('app_banner');
172
173
            // Image Validation.
174
            // Image size in bytes.
175
            $maxSize = $file->getMaxFilesize();
176
177
            if ($file->getSize() > $maxSize) {
178
                return Redirect::to($redirectUrl)->withErrors(trans('admin.settings.general.too-big', ['size' => $maxSize]));
179
            }
180
181
            if (! $file->isValid() || $file->getError()) {
182
                return Redirect::to($redirectUrl)->withErrors($file->getErrorMessage());
183
            }
184
185
            if (! starts_with($file->getMimeType(), 'image/')) {
186
                return Redirect::to($redirectUrl)->withErrors(trans('admin.settings.general.images-only'));
187
            }
188
189
            // Store the banner.
190
            Setting::firstOrCreate(['name' => 'app_banner'])->update(['value' => base64_encode(file_get_contents($file->getRealPath()))]);
191
192
            // Store the banner type
193
            Setting::firstOrCreate(['name' => 'app_banner_type'])->update(['value' => $file->getMimeType()]);
194
        }
195
196
        try {
197
            foreach (Request::except(['app_banner', 'remove_banner']) as $settingName => $settingValue) {
198
                Setting::firstOrCreate(['name' => $settingName])->update(['value' => $settingValue]);
199
            }
200
        } catch (Exception $e) {
201
            return Redirect::to($redirectUrl)->withErrors(trans('admin.settings.edit.failure'));
202
        }
203
204
        if (Request::has('app_locale')) {
205
            Lang::setLocale(Request::get('app_locale'));
206
        }
207
208
        return Redirect::to($redirectUrl)->withSuccess(trans('admin.settings.edit.success'));
209
    }
210
}
211