Completed
Push — master ( c5b829...d3ec7e )
by Phecho
03:25
created

app/Http/Controllers/Admin/SettingsController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 GrahamCampbell\Binput\Facades\Binput;
17
use Illuminate\Routing\Controller;
18
use Illuminate\Support\Facades\Lang;
19
use Illuminate\Support\Facades\Redirect;
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
     * @return void
36
     */
37 View Code Duplication
    public function __construct()
0 ignored issues
show
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...
38
    {
39
        $this->subMenu = [
40
            'overview' => [
41
                'title'  => 'Overview',
42
                'url'    => route('admin.index'),
43
                'icon'   => 'fa fa-wrench',
44
                'active' => false,
45
            ],
46
            'general' => [
47
                'title'  => trans('admin.settings.general.general'),
48
                'url'    => route('admin.settings.general'),
49
                'icon'   => 'fa fa-gear',
50
                'active' => false,
51
            ],
52
            'theme' => [
53
                'title'  => trans('admin.settings.theme.theme'),
54
                'url'    => route('admin.settings.theme'),
55
                'icon'   => 'fa fa-list-alt',
56
                'active' => false,
57
            ],
58
            'stylesheet' => [
59
                'title'  => trans('admin.settings.stylesheet.stylesheet'),
60
                'url'    => route('admin.settings.stylesheet'),
61
                'icon'   => 'fa fa-magic',
62
                'active' => false,
63
            ],
64
            'localization' => [
65
                'title'  => trans('admin.settings.localization.localization'),
66
                'url'    => route('admin.settings.localization'),
67
                'icon'   => 'fa fa-language',
68
                'active' => false,
69
            ],
70
            'timezone' => [
71
                'title'  => trans('admin.settings.timezone.timezone'),
72
                'url'    => route('admin.settings.timezone'),
73
                'icon'   => 'fa fa-calendar',
74
                'active' => false,
75
            ],
76
        ];
77
78
        View::share([
79
            'sub_title' => trans('admin.settings.settings'),
80
            'sub_menu'  => $this->subMenu,
81
        ]);
82
    }
83
84
    /**
85
     * Shows the settings general view.
86
     *
87
     * @return \Illuminate\View\View
88
     */
89 View Code Duplication
    public function showGeneralView()
90
    {
91
        $this->subMenu['general']['active'] = true;
92
93
        Session::flash('redirect_to', $this->subMenu['general']['url']);
94
95
        return View::make('admin.settings.general')
96
            ->withPageTitle(trans('admin.settings.general.general').' - '.trans('admin.admin'))
97
            ->withSubMenu($this->subMenu);
98
    }
99
100
    /**
101
     * Shows the settings localization view.
102
     *
103
     * @return \Illuminate\View\View
104
     */
105 View Code Duplication
    public function showLocalizationView()
106
    {
107
        $this->subMenu['localization']['active'] = true;
108
109
        Session::flash('redirect_to', $this->subMenu['localization']['url']);
110
111
        return View::make('admin.settings.localization')
112
            ->withPageTitle(trans('admin.settings.localization.localization').' - '.trans('admin.admin'))
113
            ->withSubMenu($this->subMenu);
114
    }
115
116
    /**
117
     * Shows the settings theme view.
118
     *
119
     * @return \Illuminate\View\View
120
     */
121 View Code Duplication
    public function showThemeView()
122
    {
123
        $this->subMenu['theme']['active'] = true;
124
125
        Session::flash('redirect_to', $this->subMenu['theme']['url']);
126
127
        return View::make('admin.settings.theme')
128
            ->withPageTitle(trans('admin.settings.theme.theme').' - '.trans('admin.admin'))
129
            ->withSubMenu($this->subMenu);
130
    }
131
132
    /**
133
     * Shows the settings timezone view.
134
     *
135
     * @return \Illuminate\View\View
136
     */
137 View Code Duplication
    public function showTimezoneView()
138
    {
139
        $this->subMenu['timezone']['active'] = true;
140
141
        Session::flash('redirect_to', $this->subMenu['timezone']['url']);
142
143
        return View::make('admin.settings.timezone')
144
            ->withPageTitle(trans('admin.settings.timezone.timezone').' - '.trans('admin.admin'))
145
            ->withSubMenu($this->subMenu);
146
    }
147
148
    /**
149
     * Shows the settings stylesheet view.
150
     *
151
     * @return \Illuminate\View\View
152
     */
153 View Code Duplication
    public function showStylesheetView()
154
    {
155
        $this->subMenu['stylesheet']['active'] = true;
156
157
        Session::flash('redirect_to', $this->subMenu['stylesheet']['url']);
158
159
        return View::make('admin.settings.stylesheet')
160
            ->withPageTitle(trans('admin.settings.stylesheet.stylesheet').' - '.trans('admin.admin'))
161
            ->withSubMenu($this->subMenu);
162
    }
163
164
    /**
165
     * Updates the system settings.
166
     *
167
     * @return \Illuminate\View\View
168
     */
169
    public function postSettings()
170
    {
171
        $redirectUrl = Session::get('redirect_to', route('admin.settings.general'));
172
173
        if (Binput::get('remove_banner') === '1') {
174
            $setting = Setting::where('name', 'app_banner');
175
            $setting->delete();
176
        }
177
178
        if (Binput::hasFile('app_banner')) {
179
            $file = Binput::file('app_banner');
180
181
            // Image Validation.
182
            // Image size in bytes.
183
            $maxSize = $file->getMaxFilesize();
184
185
            if ($file->getSize() > $maxSize) {
186
                return Redirect::to($redirectUrl)->withErrors(trans('admin.settings.general.too-big', ['size' => $maxSize]));
187
            }
188
189
            if (!$file->isValid() || $file->getError()) {
190
                return Redirect::to($redirectUrl)->withErrors($file->getErrorMessage());
191
            }
192
193
            if (!starts_with($file->getMimeType(), 'image/')) {
194
                return Redirect::to($redirectUrl)->withErrors(trans('admin.settings.general.images-only'));
195
            }
196
197
            // Store the banner.
198
            Setting::firstOrCreate(['name' => 'app_banner'])->update(['value' => base64_encode(file_get_contents($file->getRealPath()))]);
199
200
            // Store the banner type
201
            Setting::firstOrCreate(['name' => 'app_banner_type'])->update(['value' => $file->getMimeType()]);
202
        }
203
204
        try {
205
            foreach (Binput::except(['app_banner', 'remove_banner']) as $settingName => $settingValue) {
206
                Setting::firstOrCreate(['name' => $settingName])->update(['value' => $settingValue]);
207
            }
208
        } catch (Exception $e) {
209
            return Redirect::to($redirectUrl)->withErrors(trans('admin.settings.edit.failure'));
210
        }
211
212
        if (Binput::has('app_locale')) {
213
            Lang::setLocale(Binput::get('app_locale'));
214
        }
215
216
        return Redirect::to($redirectUrl)->withSuccess(trans('admin.settings.edit.success'));
217
    }
218
}
219