Completed
Push — master ( d9ee7a...0393a1 )
by Iman
14s
created

AdminSettingsController::hookBeforeAdd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\SettingModule;
4
5
use crocodicstudio\crudbooster\controllers\CBController;
6
use Illuminate\Support\Facades\Storage;
7
use Illuminate\Support\Facades\Request;
8
use Illuminate\Support\Facades\Cache;
9
use CRUDBooster;
0 ignored issues
show
Bug introduced by
The type CRUDBooster was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
class AdminSettingsController extends CBController
12
{
13
    /**
14
     * AdminSettingsController constructor.
15
     */
16
    public function __construct()
17
    {
18
        $this->table = 'cms_settings';
19
        $this->title_field = "name";
20
    }
21
22
    public function cbInit()
23
    {
24
        $this->index_orderby = ['name' => 'asc'];
0 ignored issues
show
Bug Best Practice introduced by
The property index_orderby does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25
26
        $this->setButtons();
27
28
        $this->col = [];
29
30
        $this->form = SettingsForm::makeForm(request('group_setting', 'General Setting'));
31
    }
32
33
    function getShow()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
34
    {
35
        $this->cbLoader();
36
37
        $this->allowOnlySuperAdmin();
38
39
        $data['page_title'] = urldecode(request('group'));
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
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

39
        $data['page_title'] = urldecode(/** @scrutinizer ignore-type */ request('group'));
Loading history...
40
41
        return view('CbSettings::setting', $data);
42
    }
43
44
    function hookBeforeEdit(&$posdata, $id)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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

44
    function hookBeforeEdit(&$posdata, /** @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...
45
    {
46
        $this->return_url = CRUDBooster::mainpath("show")."?group=".$posdata['group_setting'];
47
    }
48
49
    function getDeleteFileSetting()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
50
    {
51
        $id = request('id');
52
        $content = CRUDBooster::first($this->table, $id)->content;
53
54
        Storage::delete($content);
55
56
        $this->table()->where('id', $id)->update(['content' => null]);
57
58
        CRUDBooster::redirect(Request::server('HTTP_REFERER'), cbTrans('alert_delete_data_success'), 'success');
59
    }
60
61
    function postSaveSetting()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
62
    {
63
        $this->allowOnlySuperAdmin();
64
65
        $group = request('group_setting');
66
67
        $settings = $this->table()->where('group_setting', $group)->get();
68
69
        foreach ($settings as $setting) {
70
71
            $name = $setting->name;
72
73
            $content = request($name);
74
            if (Request::hasFile($name)) {
75
                $content = $this->uploadFile($setting);
76
            }
77
78
            $this->table()->where('name', $name)->update(['content' => $content]);
79
80
            Cache::forget('setting_'.$name);
81
        }
82
83
        backWithMsg(cbTrans('Update_Setting'));
84
    }
85
86
    function hookBeforeAdd(&$arr)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
87
    {
88
        $arr['name'] = str_slug($arr['label'], '_');
89
        $this->return_url = CRUDBooster::mainpath("show")."?group=".$arr['group_setting'];
90
    }
91
92
    function hookAfterEdit($id)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
93
    {
94
        $row = $this->table()->where($this->primary_key, $id)->first();
95
96
        /* REMOVE CACHE */
97
        Cache::forget('setting_'.$row->name);
98
    }
99
100
    /**
101
     * @param $name
102
     * @param $set
103
     */
104
    private function validateFileType($set)
105
    {
106
        $name = $set->name;
107
        $rules = [$name => 'image|max:10000'];
108
109
        if ($set->content_input_type !== 'upload_image') {
110
            $rules = [$name => 'mimes:doc,docx,xls,xlsx,ppt,pptx,pdf,zip,rar|max:20000'];
111
        }
112
113
        CRUDBooster::valid($rules, 'view');
114
    }
115
116
    private function allowOnlySuperAdmin()
117
    {
118
        if (CRUDBooster::isSuperadmin()) {
119
            return true;
120
        }
121
122
        CRUDBooster::insertTryLog('view', 'Setting');
123
        CRUDBooster::denyAccess();
124
    }
125
126
    /**
127
     * @param $set
128
     * @param $name
129
     * @return string
130
     */
131
    private function uploadFile($set)
132
    {
133
        $this->validateFileType($set);
134
        $month = date('Y-m');
135
136
        $file = Request::file($set->name);
137
        //Create Directory Monthly
138
        Storage::makeDirectory($month);
139
140
        //Move file to storage
141
        $filename = md5(str_random(5)).'.'.$file->getClientOriginalExtension();
142
        if ($file->move(storage_path('app'.DIRECTORY_SEPARATOR.$month), $filename)) {
143
            $content = 'uploads/'.$month.'/'.$filename;
144
        }
145
146
        return $content;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $content does not seem to be defined for all execution paths leading up to this point.
Loading history...
147
    }
148
149
    private function setButtons()
150
    {
151
        $this->button_delete = true;
152
        $this->button_show = false;
153
        $this->button_cancel = false;
154
        $this->button_import = false;
155
        $this->button_export = false;
156
    }
157
}
158