|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\TechTips; |
|
4
|
|
|
|
|
5
|
|
|
use Inertia\Inertia; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
|
|
9
|
|
|
use App\Http\Controllers\Controller; |
|
10
|
|
|
use App\Http\Requests\TechTips\NewTipRequest; |
|
11
|
|
|
use App\Models\EquipmentCategory; |
|
12
|
|
|
use App\Models\EquipmentType; |
|
13
|
|
|
use App\Models\FileUploads; |
|
14
|
|
|
use App\Models\TechTip; |
|
15
|
|
|
use App\Models\TechTipFile; |
|
16
|
|
|
use App\Models\TechTipType; |
|
17
|
|
|
use App\Traits\FileTrait; |
|
18
|
|
|
use Illuminate\Support\Facades\Log; |
|
19
|
|
|
use Illuminate\Support\Str; |
|
20
|
|
|
|
|
21
|
|
|
class TechTipsController extends Controller |
|
22
|
|
|
{ |
|
23
|
|
|
use FileTrait; |
|
24
|
|
|
|
|
25
|
|
|
protected $disk; |
|
26
|
|
|
protected $tmpFolder; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->disk = 'tips'; |
|
31
|
|
|
$this->tmpFolder = '_tmp'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Show all of the Tech Tips |
|
36
|
|
|
*/ |
|
37
|
|
|
public function index() |
|
38
|
|
|
{ |
|
39
|
|
|
return Inertia::render('TechTips/index'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Form to create a new Tech Tip |
|
44
|
|
|
*/ |
|
45
|
|
|
public function create() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->authorize('create', TechTip::class); |
|
48
|
|
|
|
|
49
|
|
|
return Inertia::render('TechTips/create', [ |
|
50
|
|
|
'tipTypes' => TechTipType::all(), |
|
51
|
|
|
'equipment' => EquipmentCategory::with('EquipmentType')->get(), |
|
52
|
|
|
]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Create the new Tech Tip |
|
57
|
|
|
*/ |
|
58
|
|
|
public function store(NewTipRequest $request) |
|
59
|
|
|
{ |
|
60
|
|
|
// Determine if we are in the process of uploading a file, or if this is the initial request |
|
61
|
|
|
if($request->file) |
|
62
|
|
|
{ |
|
63
|
|
|
$status = $this->getChunk($request, $this->disk, $this->tmpFolder); |
|
64
|
|
|
|
|
65
|
|
|
// If the file is completely uploaded, save the name and location in session data and move onto the next file |
|
66
|
|
|
if($status['done'] === 100) |
|
67
|
|
|
{ |
|
68
|
|
|
$newFile = FileUploads::create([ |
|
69
|
|
|
'disk' => $this->disk, |
|
70
|
|
|
'folder' => $this->tmpFolder, |
|
71
|
|
|
'file_name' => $status['filename'], |
|
72
|
|
|
'public' => 1, |
|
73
|
|
|
]); |
|
74
|
|
|
|
|
75
|
|
|
$fileArr = session('new-file-upload') !== null ? session('new-file-upload') : []; |
|
76
|
|
|
$fileArr[] = $newFile; |
|
77
|
|
|
|
|
78
|
|
|
session(['new-file-upload' => $fileArr]); |
|
79
|
|
|
|
|
80
|
|
|
return response()->noContent(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Continue the file upload |
|
84
|
|
|
return response($status); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// All file uploads are done, create the new Tech Tip |
|
88
|
|
|
$newTip = TechTip::create([ |
|
89
|
|
|
'user_id' => $request->user()->user_id, |
|
90
|
|
|
'tip_type_id' => $request->tip_type_id, |
|
91
|
|
|
'sticky' => $request->sticky, |
|
92
|
|
|
'subject' => $request->subject, |
|
93
|
|
|
'slug' => Str::slug($request->subject), |
|
94
|
|
|
'details' => $request->details, |
|
95
|
|
|
]); |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
// Move the files into a folder with the name of the tip_id |
|
99
|
|
|
if(session('new-file-upload') !== null) |
|
100
|
|
|
{ |
|
101
|
|
|
$files = session('new-file-upload'); |
|
102
|
|
|
foreach($files as $file) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->moveFile($file->file_id, $this->disk, $newTip->tip_id); |
|
105
|
|
|
TechTipFile::create([ |
|
106
|
|
|
'tip_id' => $newTip->tip_id, |
|
107
|
|
|
'file_id' => $file->file_id, |
|
108
|
|
|
]); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
session()->forget('new-file-upload'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
Log::channel('user')->info('New Tech Tip - '.$request->subject.' was created by '.$request->user()->username); |
|
115
|
|
|
return redirect(route('tech-tips.show', $newTip->slug)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Display the specified resource. |
|
120
|
|
|
* |
|
121
|
|
|
* @param int $id |
|
122
|
|
|
* @return \Illuminate\Http\Response |
|
123
|
|
|
*/ |
|
124
|
|
|
public function show($id) |
|
|
|
|
|
|
125
|
|
|
{ |
|
126
|
|
|
// |
|
127
|
|
|
return 'show new tip'; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Show the form for editing the specified resource. |
|
132
|
|
|
* |
|
133
|
|
|
* @param int $id |
|
134
|
|
|
* @return \Illuminate\Http\Response |
|
135
|
|
|
*/ |
|
136
|
|
|
public function edit($id) |
|
|
|
|
|
|
137
|
|
|
{ |
|
138
|
|
|
// |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Update the specified resource in storage. |
|
143
|
|
|
* |
|
144
|
|
|
* @param \Illuminate\Http\Request $request |
|
145
|
|
|
* @param int $id |
|
146
|
|
|
* @return \Illuminate\Http\Response |
|
147
|
|
|
*/ |
|
148
|
|
|
public function update(Request $request, $id) |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
// |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Remove the specified resource from storage. |
|
155
|
|
|
* |
|
156
|
|
|
* @param int $id |
|
157
|
|
|
* @return \Illuminate\Http\Response |
|
158
|
|
|
*/ |
|
159
|
|
|
public function destroy($id) |
|
|
|
|
|
|
160
|
|
|
{ |
|
161
|
|
|
// |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.