Completed
Push — dev5 ( 222021...7dd80f )
by Ron
08:28
created

SystemsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace App\Http\Controllers\Installer;
4
5
use App\SystemTypes;
6
use App\SystemCategories;
7
use App\SystemCustDataTypes;
8
use Illuminate\Http\Request;
9
use App\SystemCustDataFields;
10
use Illuminate\Validation\Rule;
11
use Illuminate\Support\Facades\Log;
12
use Illuminate\Support\Facades\Auth;
13
use App\Http\Controllers\Controller;
14
15
class SystemsController extends Controller
16
{
17
    public function __construct()
18
    {
19
        $this->middleware('auth');
20
    }
21
    
22
    //  List the systems that can be modified
23
    public function index()
24
    {
25
        $systems = SystemCategories::with('SystemTypes')->get();
26
        
27
        return view('installer.systemsList', [
28
            'systems' => $systems
29
        ]);
30
    }
31
32
    //  Open the form to create a new system
33
    public function create()
34
    {
35
        $categories = SystemCategories::all();
36
        $dataTypes  = SystemCustDataTypes::orderBy('name', 'ASC')->get();
37
        
38
        $dropDown = [];
39
        foreach($dataTypes as $type)
40
        {
41
            $dropDown[] = [
42
                'value' => $type->data_type_id,
43
                'label' => $type->name
44
            ];
45
        }
46
        
47
        return view('installer.newSystem', [
48
            'categories' => $categories,
49
            'dropDown'   => $dropDown
50
        ]);
51
    }
52
53
    //  Store the new system type
54
    public function store(Request $request)
55
    {
56
        $request->validate([
57
            'category' => 'required|numeric',
58
            'name' => [
59
                    'required',
60
                    'string',
61
                    Rule::unique('system_types'),
62
                    'regex:/^[a-zA-Z0-9_ ]*$/'
63
                ],
64
            'dataOptions' => 'required'
65
            
66
        ]);
67
        
68
        $sysData = SystemTypes::create([
69
            'cat_id'          => $request->category,
70
            'name'            => $request->name,
71
            'parent_id'       => null,
72
            'folder_location' => str_replace(' ', '_', $request->name)
73
        ]);
74
        $sysID = $sysData->sys_id;
75
        $i = 0;
76
        
77
        foreach($request->dataOptions as $field)
78
        {
79
            if(!empty($field))
80
            {
81
                if(isset($field['value']))
82
                {
83
                    $id = $field['value'];
84
                }
85
                else
86
                {
87
                    $newField = SystemCustDataTypes::create([
88
                        'name' => $field['label']
89
                    ]);
90
                    $id = $newField->data_type_id;
91
                }
92
93
                SystemCustDataFields::create([
94
                    'sys_id' => $sysID,
95
                    'data_type_id' => $id,
96
                    'order' => $i
97
                ]);
98
                $i++;
99
            }
100
        }
101
        
102
        Log::info('New System Created', ['cat_id' => $request->catName, 'sys_name' => $request->name, 'user_id' => Auth::user()->user_id]);
103
        $request->session()->flash('success', 'New System Created');
104
        
105
        return response()->json(['success' => true]);
106
    }
107
108
    //  Get a JSON array of the system to be edited
109
    public function show($id)
110
    {
111
        $system = SystemTypes::find($id);
112
        $data   = SystemCustDataFields::where('sys_id', $id)
113
            ->join('system_cust_data_types', 'system_cust_data_fields.data_type_id', '=', 'system_cust_data_types.data_type_id')
114
            ->orderBy('order', 'ASC')
115
            ->pluck('name');
116
        
117
        $sysData = [
118
            'name' => $system->name,
119
            'data' => $data
120
        ];
121
        
122
        return response()->json($sysData);
123
    }
124
125
    //  Edit an existing system
126
    public function edit($id)
127
    {
128
        $system = SystemTypes::find($id);
129
        $dataTypes  = SystemCustDataTypes::orderBy('name', 'ASC')->get();
130
        
131
        $dropDown = [];
132
        foreach($dataTypes as $type)
133
        {
134
            $dropDown[] = [
135
                'value' => $type->data_type_id,
136
                'label' => $type->name
137
            ];
138
        }
139
        
140
        return view('installer.editSystem', [
141
            'sys_id'   => $id,
142
            'name'     => $system->name,
143
            'dropDown' => $dropDown
144
        ]);
145
    }
146
147
    //  Update the system data
148
    public function update(Request $request, $id)
149
    {
150
        $request->validate([
151
            'name'     => [
152
                    'required',
153
                    'string',
154
                    Rule::unique('system_types')->ignore($id, 'sys_id'),
155
                    'regex:/^[a-zA-Z0-9_ ]*$/'
156
                ]
157
        ]);
158
        
159
        //  Update the system name
160
        $sys = SystemTypes::find($id)->update([
0 ignored issues
show
Unused Code introduced by
The assignment to $sys is dead and can be removed.
Loading history...
161
            'name' => $request->name
162
        ]);
163
        
164
        //  Update the order of the existing data fields
165
        $i = 0;
166
        foreach($request->dataOptions as $data)
167
        {
168
            $dataID = SystemCustDataTypes::where('name', $data)->first();
169
            
170
            $dataType = SystemCustDataFields::where('sys_id', $id)->where('data_type_id', $dataID->data_type_id)->update([
0 ignored issues
show
Unused Code introduced by
The assignment to $dataType is dead and can be removed.
Loading history...
171
                'order' => $i
172
            ]);
173
            
174
            $i++;
175
        }
176
        
177
        //  Process any new data fields
178
        if(!empty($request->newDataOptions))
179
        {
180
            foreach($request->newDataOptions as $field)
181
            {
182
                if(!empty($field))
183
                {
184
                    if(isset($field['value']))
185
                    {
186
                        $dataID = $field['value'];
187
                    }
188
                    else
189
                    {
190
                        $newField = SystemCustDataTypes::create([
191
                            'name' => $field['label']
192
                        ]);
193
                        $dataID = $newField->data_type_id;
194
                    }
195
196
                    SystemCustDataFields::create([
197
                        'sys_id' => $id,
198
                        'data_type_id' => $dataID,
199
                        'order' => $i
200
                    ]);
201
                    $i++;
202
                }
203
            }
204
        }
205
        
206
        Log::info('System Updated', ['sys_name' => $request->name, 'user_id' => Auth::user()->user_id]);
207
        $request->session()->flash('success', 'System Updated');
208
        
209
        return response()->json(['success' => true]);
210
    }
211
212
    /**
213
     * Remove the specified resource from storage.
214
     *
215
     * @param  int  $id
216
     * @return \Illuminate\Http\Response
217
     */
218
//    public function destroy($id)
219
//    {
220
//        //
221
//    }
222
}
223