Passed
Push — master ( d1c6ec...66cfc7 )
by Iman
06:37
created

AdminSettingsController::uploadFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\SettingModule;
4
5
use crocodicstudio\crudbooster\controllers\CBController;
6
use crocodicstudio\crudbooster\helpers\CbValidator;
7
use crocodicstudio\crudbooster\helpers\CRUDBooster;
8
use Illuminate\Support\Facades\Cache;
9
use Illuminate\Support\Facades\Request;
10
use Illuminate\Support\Facades\Storage;
11
12
class AdminSettingsController extends CBController
13
{
14
    /**
15
     * AdminSettingsController constructor.
16
     */
17
    public function __construct()
18
    {
19
        $this->table = 'cms_settings';
20
        $this->titleField = "name";
21
    }
22
23
    public function cbInit()
24
    {
25
        $this->orderby = ['name' => 'asc'];
26
27
        $this->setButtons();
28
29
        $this->col = [];
30
31
        $this->form = SettingsForm::makeForm(request('group_setting', 'General Setting'));
32
    }
33
34
    public function getShow()
35
    {
36
        CRUDBooster::allowOnlySuperAdmin();
37
38
        $this->cbLoader();
39
40
        $data = ['page_title' => urldecode(request('group'))];
0 ignored issues
show
Bug introduced by
It seems like request('group') can also be of type array; however, parameter $str of urldecode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        $data = ['page_title' => urldecode(/** @scrutinizer ignore-type */ request('group'))];
Loading history...
41
42
        return view('CbSettings::setting', $data);
43
    }
44
45
    public function hookBeforeEdit($postData, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

45
    public function hookBeforeEdit($postData, /** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
    {
47
        $this->return_url = CRUDBooster::mainpath("show")."?group=".$postData['group_setting'];
48
        return $postData;
49
    }
50
51
    public function getDeleteFileSetting()
52
    {
53
        $this->genericLoader();
54
        $id = request('id');
55
        $content = CRUDBooster::first($this->table, $id)->content;
56
57
        Storage::delete($content);
58
59
        $this->findRow($id)->update(['content' => null]);
60
61
        backWithMsg(cbTrans('alert_delete_data_success'));
62
    }
63
64
    public function postSaveSetting()
65
    {
66
        CRUDBooster::allowOnlySuperAdmin();
67
68
        $group = request('group_setting');
69
70
        $settings = $this->table()->where('group_setting', $group)->get();
71
72
        foreach ($settings as $setting) {
73
74
            $name = $setting->name;
75
76
            $content = request($name);
77
            if (Request::hasFile($name)) {
78
                $content = $this->uploadFile($setting);
79
            }
80
81
            $this->table()->where('name', $name)->update(['content' => $content]);
82
83
            Cache::forget('setting_'.$name);
84
        }
85
86
        backWithMsg(cbTrans('Update_Setting'));
87
    }
88
89
    public function hookBeforeAdd($arr)
90
    {
91
        $arr['name'] = str_slug($arr['label'], '_');
92
        $this->return_url = CRUDBooster::mainpath("show")."?group=".$arr['group_setting'];
93
        return $arr;
94
    }
95
96
    public function hookAfterEdit($id)
97
    {
98
        $row = $this->findRow($id)->first();
99
100
        /* REMOVE CACHE */
101
        Cache::forget('setting_'.$row->name);
102
    }
103
104
    /**
105
     * @param $set
106
     */
107
    private function validateFileType($set)
108
    {
109
        $name = $set->name;
110
        $rules = [$name => 'image|max:10000'];
111
112
        if ($set->content_input_type !== 'upload_image') {
113
            $rules = [$name => 'mimes:doc,docx,xls,xlsx,ppt,pptx,pdf,zip,rar|max:20000'];
114
        }
115
116
        CbValidator::valid($rules, 'view');
117
    }
118
119
    /**
120
     * @param $set
121
     * @return string
122
     */
123
    private function uploadFile($set)
124
    {
125
        $this->validateFileType($set);
126
        $month = date('Y-m');
127
128
        $file = Request::file($set->name);
129
        //Create Directory Monthly
130
        Storage::makeDirectory($month);
131
132
        //Move file to storage
133
        $filename = md5(str_random(5)).'.'.$file->getClientOriginalExtension();
134
        if ($file->move(storage_path('app'.DIRECTORY_SEPARATOR.$month), $filename)) {
135
            return 'uploads/'.$month.'/'.$filename;
136
        }
137
    }
138
139
    private function setButtons()
140
    {
141
        $this->buttonShow = false;
142
        $this->buttonCancel = false;
143
        $this->buttonImport = false;
144
        $this->buttonExport = false;
145
    }
146
}
147