1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use PDF; |
6
|
|
|
use Exception; |
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 App\SystemTypes; |
12
|
|
|
use App\SystemCategories; |
13
|
|
|
use App\User; |
14
|
|
|
use App\UserSettings; |
15
|
|
|
use App\Files; |
16
|
|
|
use App\TechTips; |
17
|
|
|
use App\TechTipFiles; |
18
|
|
|
use App\TechTipSystems; |
19
|
|
|
use App\TechTipComments; |
20
|
|
|
use App\TechTipFavs; |
21
|
|
|
use App\Mail\NewTechtip; |
22
|
|
|
|
23
|
|
|
class TechTipsController extends Controller |
24
|
|
|
{ |
25
|
|
|
// Only authorized users have access |
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->middleware('auth'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// Landing page brings up the tech tip search form |
32
|
|
|
public function index() |
33
|
|
|
{ |
34
|
|
|
$systems = SystemCategories::with('SystemTypes') |
35
|
|
|
->orderBy('cat_id', 'asc') |
36
|
|
|
->get(); |
37
|
|
|
|
38
|
|
|
$sysArr = []; |
39
|
|
|
foreach($systems as $sys) |
40
|
|
|
{ |
41
|
|
|
foreach($sys->SystemTypes as $s) |
42
|
|
|
{ |
43
|
|
|
$sysArr[$sys->name][$s->sys_id] = $s->name; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return view('tip.index', [ |
48
|
|
|
'systems' => $sysArr |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Search for a tech tip |
53
|
|
|
public function search(Request $request) |
54
|
|
|
{ |
55
|
|
|
// Run different request based on if system field is filled out or not |
56
|
|
|
if(!empty($request->system)) |
57
|
|
|
{ |
58
|
|
|
$tipData = TechTips::where('subject', 'like', '%'.$request->tipSearch.'%') |
59
|
|
|
->whereHas('TechTipSystems', function($q) use($request) |
60
|
|
|
{ |
61
|
|
|
$q->where('sys_id', $request->system); |
62
|
|
|
}) |
63
|
|
|
->get(); |
64
|
|
|
} |
65
|
|
|
else |
66
|
|
|
{ |
67
|
|
|
$tipData = TechTips::where('subject', 'like', '%'.$request->tipSearch.'%') |
68
|
|
|
->get(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return view('tip.searchResults', [ |
72
|
|
|
'results' => $tipData |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Create a new Tech Tip |
77
|
|
|
public function create() |
78
|
|
|
{ |
79
|
|
|
// Get system types for tip tagging |
80
|
|
|
$systems = SystemCategories::with('SystemTypes') |
81
|
|
|
->orderBy('cat_id', 'asc') |
82
|
|
|
->get(); |
83
|
|
|
|
84
|
|
|
$sysArr = []; |
85
|
|
|
foreach($systems as $sys) |
86
|
|
|
{ |
87
|
|
|
foreach($sys->SystemTypes as $s) |
88
|
|
|
{ |
89
|
|
|
$sysArr[$sys->name][$s->sys_id] = $s->name; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return view('tip.form.newTip', [ |
94
|
|
|
'systems' => $sysArr |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Submit the new tech tip |
99
|
|
|
public function store(Request $request) |
100
|
|
|
{ |
101
|
|
|
$request->validate([ |
102
|
|
|
'subject' => 'required', |
103
|
|
|
'details' => 'required', |
104
|
|
|
'sysTags' => 'required' |
105
|
|
|
]); |
106
|
|
|
|
107
|
|
|
// Remove any forward slash (/) from the Subject field |
108
|
|
|
$request->merge(['subject' => str_replace('/', '-', $request->subject)]); |
109
|
|
|
|
110
|
|
|
// Enter the tip details and get the tip ID |
111
|
|
|
$tip = TechTips::create([ |
112
|
|
|
'subject' => $request->subject, |
113
|
|
|
'description' => $request->details, |
114
|
|
|
'user_id' => Auth::user()->user_id |
115
|
|
|
]); |
116
|
|
|
$tipID = $tip->tip_id; |
117
|
|
|
|
118
|
|
|
// Enter all system tags associated with the tip |
119
|
|
|
if(is_array($request->sysTags)) |
120
|
|
|
{ |
121
|
|
|
foreach($request->sysTags as $tag) |
122
|
|
|
{ |
123
|
|
|
TechTipSystems::create([ |
124
|
|
|
'tip_id' => $tipID, |
125
|
|
|
'sys_id' => $tag |
126
|
|
|
]); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
else |
130
|
|
|
{ |
131
|
|
|
TechTipSystems::create([ |
132
|
|
|
'tip_id' => $tipID, |
133
|
|
|
'sys_id' => $request->sysTags |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// If there are any files, process them |
138
|
|
|
if(!empty($request->file)) |
139
|
|
|
{ |
140
|
|
|
$filePath = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$tipID; |
141
|
|
|
foreach($request->file as $file) |
142
|
|
|
{ |
143
|
|
|
// Clean the file and store it |
144
|
|
|
$fileName = Files::cleanFilename($filePath, $file->getClientOriginalName()); |
145
|
|
|
$file->storeAs($filePath, $fileName); |
146
|
|
|
|
147
|
|
|
// Place file in Files table of DB |
148
|
|
|
$newFile = Files::create([ |
149
|
|
|
'file_name' => $fileName, |
150
|
|
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
151
|
|
|
]); |
152
|
|
|
$fileID = $newFile->file_id; |
153
|
|
|
|
154
|
|
|
// Place the file in the tech tip files table of DB |
155
|
|
|
TechTipFiles::create([ |
156
|
|
|
'tip_id' => $tipID, |
157
|
|
|
'file_id' => $fileID |
158
|
|
|
]); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// Email the techs of the new tip |
163
|
|
|
$tipData = TechTips::find($tipID); |
164
|
|
|
$userList = UserSettings::where('em_tech_tip', 1)->join('users', 'user_settings.user_id', '=', 'users.user_id')->where('active', 1)->get(); |
165
|
|
|
try |
166
|
|
|
{ |
167
|
|
|
Mail::to($userList)->send(new newTechtip($tipData)); |
168
|
|
|
} |
169
|
|
|
catch(Exception $e) |
170
|
|
|
{ |
171
|
|
|
report($e); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
Log::info('Tech Tip ID-'.$tipID.' Created by Customer ID-'.Auth::user()->user_id); |
175
|
|
|
|
176
|
|
|
return $tipID; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// Process an uploaded image to the Tech Tip body |
180
|
|
|
public function processImage(Request $request) |
181
|
|
|
{ |
182
|
|
|
$request->validate([ |
183
|
|
|
'image' => 'mimes:jpeg,bmp,png' |
184
|
|
|
]); |
185
|
|
|
|
186
|
|
|
$file = $request->file; |
187
|
|
|
$fileName = $file->getClientOriginalName(); |
188
|
|
|
$file->storeAs('img/tip_img', $fileName, 'public'); |
189
|
|
|
|
190
|
|
|
return json_encode(['location' => '/storage/img/tip_img/'.$fileName]); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// Show the Tech Tip details |
194
|
|
|
public function details($id, $name) |
195
|
|
|
{ |
196
|
|
|
$tipData = TechTips::where('tip_id', $id)->with('user')->first(); |
197
|
|
|
if(empty($tipData)) |
198
|
|
|
{ |
199
|
|
|
Log::warning('User ID-'.Auth::user()->user_id.' tried to access invlaid Tech Tip ID-'.$id.' Name-'.$name); |
200
|
|
|
return view('errors.tipNotFound'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$tipFiles = TechTipFiles::where('tip_id', $id) |
204
|
|
|
->join('files', 'tech_tip_files.file_id', '=', 'files.file_id') |
205
|
|
|
->get(); |
206
|
|
|
$tipCmts = TechTipComments::where('tip_id', $id) |
207
|
|
|
->get(); |
208
|
|
|
$tipSys = TechTipSystems::where('tip_id', $id) |
209
|
|
|
->join('system_types', 'tech_tip_systems.sys_id', '=', 'system_types.sys_id') |
210
|
|
|
->get(); |
211
|
|
|
$tipFav = TechTipFavs::where('user_id', Auth::user()->user_id) |
212
|
|
|
->where('tip_id', $id) |
213
|
|
|
->first(); |
214
|
|
|
|
215
|
|
|
return view('tip.details', [ |
216
|
|
|
'data' => $tipData, |
217
|
|
|
'files' => $tipFiles, |
218
|
|
|
'systems' => $tipSys, |
219
|
|
|
'comments' => $tipCmts, |
220
|
|
|
'isFav' => $tipFav |
221
|
|
|
]); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// Download a note as a PDF file |
225
|
|
|
public function generatePDF($id) |
226
|
|
|
{ |
227
|
|
|
$tipData = TechTips::where('tip_id', $id)->with('user')->first(); |
228
|
|
|
$tipCmts = TechTipComments::where('tip_id', $id) |
229
|
|
|
->join('users', 'tech_tip_comments.user_id', '=', 'users.user_id') |
230
|
|
|
->get(); |
231
|
|
|
$tipSys = TechTipSystems::where('tip_id', $id) |
232
|
|
|
->join('system_types', 'tech_tip_systems.sys_id', '=', 'system_types.sys_id') |
233
|
|
|
->get(); |
234
|
|
|
|
235
|
|
|
$pdf = PDF::loadView('pdf.techTip', [ |
236
|
|
|
'data' => $tipData, |
237
|
|
|
'systems' => $tipSys, |
238
|
|
|
'comments' => $tipCmts, |
239
|
|
|
]); |
240
|
|
|
|
241
|
|
|
return $pdf->download('Tech Tip - '.$tipData->subject.'.pdf'); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
// Toggle whether or not the customer is listed as a user favorite |
245
|
|
|
public function toggleFav($action, $tipID) |
246
|
|
|
{ |
247
|
|
|
switch ($action) |
248
|
|
|
{ |
249
|
|
|
case 'add': |
250
|
|
|
TechTipFavs::create([ |
251
|
|
|
'user_id' => Auth::user()->user_id, |
252
|
|
|
'tip_id' => $tipID |
253
|
|
|
]); |
254
|
|
|
break; |
255
|
|
|
case 'remove': |
256
|
|
|
$tipFav = TechTipFavs::where('user_id', Auth::user()->user_id)->where('tip_id', $tipID)->first(); |
257
|
|
|
$tipFav->delete(); |
258
|
|
|
break; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
// Edit a Tech Tip |
263
|
|
|
public function edit($id) |
264
|
|
|
{ |
265
|
|
|
$tipData = TechTips::find($id); |
266
|
|
|
if(empty($tipData)) |
267
|
|
|
{ |
268
|
|
|
return 'tip not found'; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$tipFiles = TechTipFiles::where('tip_id', $id) |
272
|
|
|
->join('files', 'tech_tip_files.file_id', '=', 'files.file_id') |
273
|
|
|
->get(); |
274
|
|
|
$tipCmts = TechTipComments::where('tip_id', $id) |
275
|
|
|
->get(); |
276
|
|
|
$tipSys = TechTipSystems::where('tip_id', $id) |
277
|
|
|
->join('system_types', 'tech_tip_systems.sys_id', '=', 'system_types.sys_id') |
278
|
|
|
->get(); |
279
|
|
|
$tipFav = TechTipFavs::where('user_id', Auth::user()->user_id) |
280
|
|
|
->where('tip_id', $id) |
281
|
|
|
->first(); |
282
|
|
|
|
283
|
|
|
$tipS = []; |
284
|
|
|
foreach($tipSys as $s) |
285
|
|
|
{ |
286
|
|
|
$tipS[] = $s->sys_id; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
// Get system types for tip tagging |
290
|
|
|
$systems = SystemCategories::with('SystemTypes') |
291
|
|
|
->orderBy('cat_id', 'asc') |
292
|
|
|
->get(); |
293
|
|
|
|
294
|
|
|
$sysArr = []; |
295
|
|
|
foreach($systems as $sys) |
296
|
|
|
{ |
297
|
|
|
foreach($sys->SystemTypes as $s) |
298
|
|
|
{ |
299
|
|
|
$sysArr[$sys->name][$s->sys_id] = $s->name; |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
return view('tip.form.editTip', [ |
304
|
|
|
'data' => $tipData, |
305
|
|
|
'files' => $tipFiles, |
306
|
|
|
'systems' => $tipS, |
307
|
|
|
'comments' => $tipCmts, |
308
|
|
|
'isFav' => $tipFav, |
309
|
|
|
'sysToTag' => $sysArr |
310
|
|
|
]); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
// Update the tech tip |
314
|
|
|
public function update(Request $request, $id) |
315
|
|
|
{ |
316
|
|
|
$request->validate([ |
317
|
|
|
'subject' => 'required', |
318
|
|
|
'description' => 'required', |
319
|
|
|
'sysTags' => 'required' |
320
|
|
|
]); |
321
|
|
|
|
322
|
|
|
// Remove any forward slash (/) from the Subject field |
323
|
|
|
$request->merge(['subject' => str_replace('/', '-', $request->subject)]); |
324
|
|
|
|
325
|
|
|
// update tip details |
326
|
|
|
TechTips::find($id)->update([ |
327
|
|
|
'subject' => $request->subject, |
328
|
|
|
'description' => $request->description |
329
|
|
|
]); |
330
|
|
|
|
331
|
|
|
// Enter all system tags associated with the tip after destroying the existing systems |
332
|
|
|
TechTipSystems::where('tip_id', $id)->delete(); |
333
|
|
|
if(is_array($request->sysTags)) |
334
|
|
|
{ |
335
|
|
|
foreach($request->sysTags as $tag) |
336
|
|
|
{ |
337
|
|
|
TechTipSystems::create([ |
338
|
|
|
'tip_id' => $id, |
339
|
|
|
'sys_id' => $tag |
340
|
|
|
]); |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
else |
344
|
|
|
{ |
345
|
|
|
TechTipSystems::create([ |
346
|
|
|
'tip_id' => $id, |
347
|
|
|
'sys_id' => $request->sysTags |
348
|
|
|
]); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
// Determine if any files were removed |
352
|
|
|
$tipFiles = TechTipFiles::where('tip_id', $id)->get(); |
353
|
|
|
if(!$tipFiles->isEmpty()) |
354
|
|
|
{ |
355
|
|
|
if(!empty($request->existingFile)) |
356
|
|
|
{ |
357
|
|
|
foreach($tipFiles as $file) |
358
|
|
|
{ |
359
|
|
|
if(!in_array($file->file_id, $request->existingFile)) |
360
|
|
|
{ |
361
|
|
|
TechTipFiles::where('file_id', $file->file_id)->delete(); |
362
|
|
|
Files::deleteFile($file->file_id); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
else |
367
|
|
|
{ |
368
|
|
|
TechTipFiles::where('tip_id', $id)->delete(); |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
// Process any new files |
373
|
|
|
if(!empty($request->file)) |
374
|
|
|
{ |
375
|
|
|
$filePath = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$id; |
376
|
|
|
foreach($request->file as $file) |
377
|
|
|
{ |
378
|
|
|
// Clean the file and store it |
379
|
|
|
$fileName = Files::cleanFilename($filePath, $file->getClientOriginalName()); |
380
|
|
|
$file->storeAs($filePath, $fileName); |
381
|
|
|
|
382
|
|
|
// Place file in Files table of DB |
383
|
|
|
$newFile = Files::create([ |
384
|
|
|
'file_name' => $fileName, |
385
|
|
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
386
|
|
|
]); |
387
|
|
|
$fileID = $newFile->file_id; |
388
|
|
|
|
389
|
|
|
// Place the file in the tech tip files table of DB |
390
|
|
|
TechTipFiles::create([ |
391
|
|
|
'tip_id' => $id, |
392
|
|
|
'file_id' => $fileID |
393
|
|
|
]); |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
Log::info('Tech Tip ID-'.$id.' Updated by User ID-'.Auth::user()->user_id); |
398
|
|
|
|
399
|
|
|
return $id; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Remove the specified resource from storage. |
404
|
|
|
* |
405
|
|
|
* @param int $id |
406
|
|
|
* @return \Illuminate\Http\Response |
407
|
|
|
*/ |
408
|
|
|
public function destroy($id) |
|
|
|
|
409
|
|
|
{ |
410
|
|
|
// |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.