Test Failed
Push — master ( 24a1f5...1bb6c6 )
by Ron
01:55
created

InstallerController::customizeSystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Exception;
6
use Carbon\Carbon;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Log;
10
use Illuminate\Support\Facades\Mail;
11
use Illuminate\Support\Facades\Storage;
12
use App\SystemCustDataFields;
13
use App\SystemCustDataTypes;
14
use App\SystemCategories;
15
use App\SystemTypes;
16
use App\Settings;
17
use App\User;
18
use App\Mail\TestEmail;
19
20
class InstallerController extends Controller
21
{
22
    public function __construct()
23
    {
24
        $this->middleware('auth');
25
    }
26
    
27
    //  Home page for Installer Functions
28
    public function index()
29
    {
30
        //  Get the list of system categories
31
        $cats = SystemCategories::all();
32
        $sysArr = [];
33
        //  Populate that list with the matching systems
34
        foreach($cats as $cat)
35
        {
36
            $systems = SystemTypes::where('cat_id', $cat->cat_id)->get();
1 ignored issue
show
Bug introduced by
The property cat_id does not seem to exist on App\SystemCategories. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
37
            if(!$systems->isEmpty())
38
            {
39
                foreach($systems as $sys)
40
                {
41
                    $sysArr[$cat->name][] = $sys->name;
2 ignored issues
show
Bug introduced by
The property name does not seem to exist on App\SystemCategories. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property name does not seem to exist on App\SystemTypes. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
42
                }
43
            }
44
            else
45
            {
46
                $sysArr[$cat->name] = null;
47
            }
48
        }
49
        
50
        return view('installer.index', [
51
            'sysArr' => $sysArr
52
        ]); 
53
    }
54
    
55
    //  Server customization form
56
    public function customizeSystem()
57
    {
58
        return view('installer.form.customize');
59
    }
60
    
61
    //  Submit the server customization form
62
    public function submitCustom(Request $request)
63
    {
64
        $request->validate([
65
            'timezone' => 'required'
66
        ]);
67
        
68
        Settings::where('key', 'app.timezone')->update([
69
            'value' => $request->timezone
70
        ]);
71
        
72
        Log::info('Tech Bench Settings Updated', ['user_id' => Auth::user()->user_id]);
73
        
74
        return redirect()->back()->with('success', 'Tech Bench Successfully Updated');//
75
    }
76
    
77
    //  Upload and submit a new site logo
78
    public function submitLogo(Request $request)
79
    {
80
        $file = $request->file;
81
        $fileName = $file->getClientOriginalName();
82
        $filePath = $file->storeAs('img', $fileName, 'public');
0 ignored issues
show
Unused Code introduced by
The assignment to $filePath is dead and can be removed.
Loading history...
83
        
84
        Settings::where('key', 'app.logo')->update([
85
            'value' => '/storage/img/'.$fileName
86
        ]);
87
        
88
        Log::info('A new company logo has been uploaded by User ID-'.Auth::user()->user_id);
89
        
90
        return 'success';
91
    }
92
    
93
    //  Email settings form
94
    public function emailSettings()
95
    {
96
        return view('installer.form.email');
97
    }
98
    
99
    //  Send a test email
100
    public function sendTestEmail(Request $request)
101
    {
102
        //  Make sure that all of the information properly validates
103
        $request->validate([
104
            'host'       => 'required',
105
            'port'       => 'required|numeric',
106
            'encryption' => 'required',
107
            'username'   => 'required'
108
        ]);
109
        
110
        //  Temporarily set the email settings
111
        config([
112
            'mail.host'       => $request->host,
113
            'mail.port'       => $request->port,
114
            'mail.encryption' => $request->encryption,
115
            'mail.username'   => $request->username,
116
        ]);
117
        
118
        if(!empty($request->password))
119
        {
120
            config(['mail.password' => $request->password]);
121
        }
122
        
123
        //  Try and send the test email
124
        try
125
        {
126
            Log::info('Test Email Successfully Sent to '.Auth::user()->email);
127
            Mail::to(Auth::user()->email)->send(new TestEmail());
128
            return 'success';
129
        }
130
        catch(Exception $e)
131
        {
132
            Log::notice('Test Email Failed.  Message: '.$e);
133
            $msg = '['.$e->getCode().'] "'.$e->getMessage().'" on line '.$e->getTrace()[0]['line'].' of file '.$e->getTrace()[0]['file'];
134
            return $msg;
135
        }
136
    }
137
    
138
    //  Submit the email settings form
139
    public function submitEmailSettings(Request $request)
140
    {
141
        $request->validate([
142
            'host'       => 'required',
143
            'port'       => 'required|numeric',
144
            'encryption' => 'required',
145
            'username'   => 'required'
146
        ]);
147
        
148
        //  Update each setting
149
        Settings::where('key', 'mail.host')->update(['value' => $request->host]);
150
        Settings::where('key', 'mail.port')->update(['value' => $request->port]);
151
        Settings::where('key', 'mail.encryption')->update(['value' => $request->encryption]);
152
        Settings::where('key', 'mail.username')->update(['value' => $request->username]);
153
        if(!empty($request->password))
154
        {
155
            Settings::where('key', 'mail.password')->update(['value' => $request->password]);
156
        }
157
         
158
        Log::info('Email Settings have been changed by User ID-'.Auth::user()->user_id);
159
        return redirect()->back()->with('success', 'Tech Bench Successfully Updated');//
160
    }
161
    
162
    //  User settings form
163
    public function userSettings()
164
    {
165
        $passExpire = config('users.passExpires') != null ? config('users.passExpires') : 0;
166
        
167
        return view('installer.form.users', [
168
            'passExpire' => $passExpire
169
        ]);
170
    }
171
    
172
    //  Submit the user settings form
173
    public function submitUserSettings(Request $request)
174
    {
175
        $request->validate([
176
            'passExpire' => 'required|numeric'
177
        ]);
178
        
179
        //  Determine if the password expires field is updated
180
        $oldExpire = config('users.passExpires');
181
        if($request->passExpire != $oldExpire)
182
        {
183
            //  Update the setting in the database
184
            Settings::where('key', 'users.passExpires')->update([
185
                'value' => $request->passExpire
186
            ]);
187
            //  If the setting is changing from never to xx days, update all users
188
            if($request->passExpire == 0)
189
            {
190
                User::whereNotNull('password_expires')->update([
191
                    'password_expires' => null
192
                ]);
193
            }
194
            else
195
            {
196
                $newExpire = Carbon::now()->addDays($request->passExpire);
197
                User::whereNull('password_expires')->update([
198
                    'password_expires' => $newExpire
199
                ]);
200
            }
201
        }
202
        
203
        Log::info('User Settings have been changed by User ID-'.Auth::user()->user_id);
204
        return redirect()->back()->with('success', 'User Settings Updated');
205
    }
206
    
207
    //  Form to create a new Category
208
    public function newCat()
209
    {
210
        return view('installer.form.newCat');
211
    }
212
    
213
    //  Submit the new Category
214
    public function submitCat(Request $request)
215
    {
216
        $request->validate([
217
            'name' => 'required|string|unique:system_categories|regex:/^[a-zA-Z0-9_ ]*$/'
218
        ]);
219
        
220
        $cat = SystemCategories::create([
221
            'name' => $request->name
222
        ]);
223
        
224
        Log::info('New System Category Created', ['cat_name' => $request->name, 'user_id' => Auth::user()->user_id]);
225
        
226
        return redirect()->back()->with('success', 'Category Successfully Added. <a href="'.route('installer.newSys', urlencode($cat->name)).'">Add System</a>');
1 ignored issue
show
Bug introduced by
The property name does not seem to exist on App\SystemCategories. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
227
    }
228
     
229
    //  Form to add a new system
230
    public function newSystem($cat)
231
    {
232
        $dropDown = SystemCustDataTypes::orderBy('name', 'ASC')->get();
233
        
234
        return view('installer.form.newSystem', [
235
            'cat'      => $cat,
236
            'dropDown' => $dropDown
237
        ]);
238
    }
239
    
240
    //  Submit the new system
241
    public function submitSys($cat, Request $request)
242
    {
243
        $catName = SystemCategories::where('name', urldecode($cat))->first()->cat_id;
1 ignored issue
show
Bug introduced by
The property cat_id does not seem to exist on App\SystemCategories. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
244
        $sysData = SystemTypes::create([
245
            'cat_id' => $catName,
246
            'name' => $request->name,
247
            'parent_id' => null,
248
            'folder_location' => str_replace(' ', '_', $request->name)
249
        ]);
250
        $sysID = $sysData->sys_id;
1 ignored issue
show
Bug introduced by
The property sys_id does not seem to exist on App\SystemTypes. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
251
        $i = 0;
252
        
253
        foreach($request->custField as $field)
254
        {
255
            if(!empty($field))
256
            {
257
                $id = SystemCustDataTypes::where('name', $field)->first();
258
259
                if(is_null($id))
260
                {
261
                    $newField = SystemCustDataTypes::create([
262
                        'name' => $field
263
                    ]);
264
                    $id = $newField->data_type_id;
1 ignored issue
show
Bug introduced by
The property data_type_id does not seem to exist on App\SystemCustDataTypes. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
265
                }
266
                else
267
                {
268
                    $id = $id->data_type_id;
269
                }
270
271
                SystemCustDataFields::create([
272
                    'sys_id' => $sysID,
273
                    'data_type_id' => $id,
274
                    'order' => $i
275
                ]);
276
277
                $i++;
278
            }
279
        }
280
        
281
        Log::info('New System Created', ['cat_id' => $request->catName, 'sys_name' => $request->name, 'user_id' => Auth::user()->user_id]);
282
        
283
        return redirect()->back()->with('success', 'System Successfully Added');//
284
    }
285
    
286
    //  Form to edit an existing system
287
    public function editSystem($sysName)
288
    {        
289
        $sysData = SystemTypes::where('name', urldecode($sysName))->first();
290
        $dropDown = SystemCustDataTypes::orderBy('name', 'ASC')->get();
291
        $dataType = SystemCustDataFields::where('sys_id', $sysData->sys_id)
1 ignored issue
show
Bug introduced by
The property sys_id does not seem to exist on App\SystemTypes. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
292
            ->join('system_cust_data_types', 'system_cust_data_types.data_type_id', '=', 'system_cust_data_fields.data_type_id')
293
            ->orderBy('order', 'ASC')
294
            ->get();
295
        
296
        return view('installer.form.editSystem', [
297
            'dropDown' => $dropDown,
298
            'fields'   => $dataType,
299
            'name'     => $sysData->name,
1 ignored issue
show
Bug introduced by
The property name does not seem to exist on App\SystemTypes. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
300
            'sysID'    => $sysData->sys_id
301
        ]);
302
    }
303
    
304
    //  Submit the edited system
305
    public function submitEditSystem($sysID, Request $request)
306
    {
307
        $sysName = SystemTypes::find($sysID)->name;
1 ignored issue
show
Bug introduced by
The property name does not seem to exist on App\SystemTypes. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
308
        
309
        //  Change the name of the system if it has been modified
310
        if($sysName !== $request->name)
311
        {
312
            SystemTypes::find($sysID)->update([
313
                'name' => $request->name
314
            ]);
315
        }
316
        
317
        //  Update the Customer Information
318
        $dataType = SystemCustDataFields::where('sys_id', $sysID)
319
            ->join('system_cust_data_types', 'system_cust_data_types.data_type_id', '=', 'system_cust_data_fields.data_type_id')
320
            ->get();
321
        
322
        $i = 0;
323
        foreach($request->custField as $field)
324
        {
325
            $found = false;
326
            if(!empty($field))
327
            {
328
                //  Check if the field already exists
329
                foreach($dataType as $type)
330
                {
331
                    if($type->name === $field)
332
                    {
333
                        $found = true;
334
                        //  See if the order has changed
335
                        if($type->order != $i)
336
                        {
337
                            SystemCustDataFields::find($type->field_id)->update(
338
                            [
339
                                'order' => $i
340
                            ]);
341
                        }
342
                    }
343
                }
344
                //  If the field does not exist, create it
345
                if(!$found)
346
                {
347
                    $id = SystemCustDataTypes::where('name', $field)->first();
348
349
                    if(is_null($id))
350
                    {
351
                        $newField = SystemCustDataTypes::create([
352
                            'name' => $field
353
                        ]);
354
                        $id = $newField->data_type_id;
1 ignored issue
show
Bug introduced by
The property data_type_id does not seem to exist on App\SystemCustDataTypes. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
355
                    }
356
                    else
357
                    {
358
                        $id = $id->data_type_id;
359
                    }
360
361
                    SystemCustDataFields::create([
362
                        'sys_id' => $sysID,
363
                        'data_type_id' => $id,
364
                        'order' => $i
365
                    ]);
366
                }
367
            }
368
            
369
            $i++;
370
        }
371
        
372
        Log::info('System Updated', ['sys_id' => $sysID, 'user_id' => Auth::user()->user_id]);
373
        
374
        return redirect(route('installer.editSystem', $request->name))->with('success', 'System Successfully Updated');
375
    }
376
}
377