Passed
Push — master ( 70d075...35e9ea )
by Ferry
08:21 queued 12s
created

SettingsController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 15
eloc 74
dl 0
loc 146
rs 10
c 3
b 1
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDeleteFileSetting() 0 10 2
A getShow() 0 12 2
A hook_before_add() 0 4 1
A cbInit() 0 52 2
A hook_after_edit() 0 6 1
A hook_before_edit() 0 3 1
B postSaveSetting() 0 45 6
1
<?php namespace crocodicstudio\crudbooster\controllers;
2
3
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...
4
use Illuminate\Support\Facades\Cache;
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Support\Facades\Excel;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Excel 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...
7
use Illuminate\Support\Facades\PDF;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\PDF 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...
8
use Illuminate\Support\Facades\Request;
9
use Illuminate\Support\Facades\Storage;
10
11
class SettingsController extends CBController
12
{
13
    public function cbInit()
14
    {
15
        $this->module_name = "Settings";
16
        $this->table = 'cms_settings';
17
        $this->primary_key = 'id';
18
        $this->title_field = "name";
19
        $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...
20
        $this->button_delete = true;
21
        $this->button_show = false;
22
        $this->button_cancel = false;
23
        $this->button_import = false;
24
        $this->button_export = false;
25
26
        $this->col = [];
27
        $this->col[] = ["label" => "Nama", "name" => "name", "callback_php" => "ucwords(str_replace('_',' ',%field%))"];
28
        $this->col[] = ["label" => "Setting", "name" => "content"];
29
30
        $this->form = [];
31
32
        if (Request::get('group_setting')) {
33
            $value = Request::get('group_setting');
34
        } else {
35
            $value = 'General Setting';
36
        }
37
38
        $this->form[] = ['label' => 'Group', 'name' => 'group_setting', 'value' => $value];
39
        $this->form[] = ['label' => 'Label', 'name' => 'label'];
40
41
        $this->form[] = [
42
            "label" => "Type",
43
            "name" => "content_input_type",
44
            "type" => "select",
45
            "dataenum" => ["text", "number", "email", "textarea", "wysiwyg", "upload_image", "upload_document", "datepicker", "radio", "select"],
46
        ];
47
        $this->form[] = [
48
            "label" => "Radio / Select Data",
49
            "name" => "dataenum",
50
            "placeholder" => "Example : abc,def,ghi",
51
            "jquery" => "
52
			function show_radio_data() {
53
				var cit = $('#content_input_type').val();
54
				if(cit == 'radio' || cit == 'select') {
55
					$('#form-group-dataenum').show();	
56
				}else{
57
					$('#form-group-dataenum').hide();
58
				}					
59
			}
60
			$('#content_input_type').change(show_radio_data);
61
			show_radio_data();
62
			",
63
        ];
64
        $this->form[] = ["label" => "Helper Text", "name" => "helper", "type" => "text"];
65
    }
66
67
    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...
68
    {
69
        $this->cbLoader();
70
71
        if (! CRUDBooster::isSuperadmin()) {
72
            CRUDBooster::insertLog(trans("crudbooster.log_try_view", ['name' => 'Setting', 'module' => 'Setting']));
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

72
            CRUDBooster::insertLog(/** @scrutinizer ignore-call */ trans("crudbooster.log_try_view", ['name' => 'Setting', 'module' => 'Setting']));
Loading history...
73
            CRUDBooster::redirect(CRUDBooster::adminPath(), trans('crudbooster.denied_access'));
74
        }
75
76
        $data['page_title'] = urldecode(Request::get('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...
77
78
        return view('crudbooster::setting', $data);
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

78
        return /** @scrutinizer ignore-call */ view('crudbooster::setting', $data);
Loading history...
79
    }
80
81
    function hook_before_edit(&$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...
82
    {
83
        $this->return_url = CRUDBooster::mainpath("show")."?group=".$posdata['group_setting'];
84
    }
85
86
    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...
87
    {
88
        $id = g('id');
89
        $row = CRUDBooster::first('cms_settings', $id);
90
        Cache::forget('setting_'.$row->name);
91
        if (Storage::exists($row->content)) {
92
            Storage::delete($row->content);
93
        }
94
        DB::table('cms_settings')->where('id', $id)->update(['content' => null]);
95
        CRUDBooster::redirect(Request::server('HTTP_REFERER'), trans('alert_delete_data_success'), 'success');
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

95
        CRUDBooster::redirect(Request::server('HTTP_REFERER'), /** @scrutinizer ignore-call */ trans('alert_delete_data_success'), 'success');
Loading history...
96
    }
97
98
    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...
99
    {
100
101
        if (! CRUDBooster::isSuperadmin()) {
102
            CRUDBooster::insertLog(trans("crudbooster.log_try_view", ['name' => 'Setting', 'module' => 'Setting']));
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

102
            CRUDBooster::insertLog(/** @scrutinizer ignore-call */ trans("crudbooster.log_try_view", ['name' => 'Setting', 'module' => 'Setting']));
Loading history...
103
            CRUDBooster::redirect(CRUDBooster::adminPath(), trans('crudbooster.denied_access'));
104
        }
105
106
        $group = Request::get('group_setting');
107
        $setting = DB::table('cms_settings')->where('group_setting', $group)->get();
108
        foreach ($setting as $set) {
109
110
            $name = $set->name;
111
112
            $content = Request::get($set->name);
113
114
            if (Request::hasFile($name)) {
115
116
                if ($set->content_input_type == 'upload_image') {
117
                    CRUDBooster::valid([$name => 'image|max:10000'], 'view');
118
                } else {
119
                    CRUDBooster::valid([$name => 'mimes:doc,docx,xls,xlsx,ppt,pptx,pdf,zip,rar|max:20000'], 'view');
120
                }
121
122
                $file = Request::file($name);
123
                $ext = $file->getClientOriginalExtension();
124
125
                //Create Directory Monthly
126
                $directory = 'uploads/'.date('Y-m');
127
                Storage::makeDirectory($directory);
128
129
                //Move file to storage
130
                $filename = md5(str_random(5)).'.'.$ext;
131
                $storeFile = Storage::putFileAs($directory, $file, $filename);
132
                if ($storeFile) {
133
                    $content = $directory.'/'.$filename;
134
                }
135
            }
136
137
            DB::table('cms_settings')->where('name', $set->name)->update(['content' => $content]);
138
139
            Cache::forget('setting_'.$set->name);
140
        }
141
142
        return redirect()->back()->with(['message' => 'Your setting has been saved !', 'message_type' => 'success']);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

142
        return /** @scrutinizer ignore-call */ redirect()->back()->with(['message' => 'Your setting has been saved !', 'message_type' => 'success']);
Loading history...
143
    }
144
145
    function hook_before_add(&$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...
146
    {
147
        $arr['name'] = str_slug($arr['label'], '_');
148
        $this->return_url = CRUDBooster::mainpath("show")."?group=".$arr['group_setting'];
149
    }
150
151
    function hook_after_edit($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...
152
    {
153
        $row = DB::table($this->table)->where($this->primary_key, $id)->first();
154
155
        /* REMOVE CACHE */
156
        Cache::forget('setting_'.$row->name);
157
    }
158
}
159