1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Domains\TechTips; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\File; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Log; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
use Illuminate\Support\Facades\Storage; |
10
|
|
|
use Illuminate\Support\Facades\Notification; |
11
|
|
|
|
12
|
|
|
use App\Domains\FilesDomain; |
13
|
|
|
|
14
|
|
|
use App\User; |
15
|
|
|
use App\Files; |
16
|
|
|
use App\TechTips; |
17
|
|
|
use App\TechTipFavs; |
18
|
|
|
use App\TechTipFiles; |
19
|
|
|
use App\TechTipSystems; |
20
|
|
|
|
21
|
|
|
use App\Http\Requests\TechTipNewTipRequest; |
22
|
|
|
use App\Http\Requests\TechTipEditTipRequest; |
23
|
|
|
use App\Http\Requests\TechTipProcessImageRequest; |
24
|
|
|
|
25
|
|
|
use App\Notifications\NewTechTip; |
26
|
|
|
use App\Notifications\TechTipUpdated; |
27
|
|
|
|
28
|
|
|
class SetTechTips extends FilesDomain |
29
|
|
|
{ |
30
|
|
|
// Store an image that has been uploaded to assign to a Tech Tip |
31
|
2 |
|
public function processTipImage(TechTipProcessImageRequest $request) |
32
|
|
|
{ |
33
|
|
|
// Generate a unique hash as the file name and store it in a publicly accessable folder |
34
|
2 |
|
$path = 'img/tip_img'; |
35
|
2 |
|
$location = Storage::disk('public')->putFile($path, new File($request->file)); |
36
|
2 |
|
Log::info('Image uploaded for tech tip. Image Data - ', array($request)); |
37
|
|
|
|
38
|
|
|
// Return the full url path to the image |
39
|
2 |
|
return Storage::url($location); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Process the new tip form to see if a file is being uploaded, or upload is completed |
43
|
4 |
|
public function processNewTip(TechTipNewTipRequest $request) |
44
|
|
|
{ |
45
|
4 |
|
if(isset($request->file)) |
46
|
|
|
{ |
47
|
2 |
|
$fileID = $this->processFileChunk($request); |
48
|
2 |
|
if($fileID) |
49
|
|
|
{ |
50
|
2 |
|
$fileArr = session('newTipFile') != null ? session('newTipFile') : []; |
51
|
2 |
|
$fileArr[] = $fileID; |
52
|
2 |
|
session(['newTipFile' => $fileArr]); |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
return $this->createTip($request); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Update an existin gTech Tip |
62
|
4 |
|
public function processEditTip(TechTipEditTipRequest $request, $tipID) |
63
|
|
|
{ |
64
|
4 |
|
if(isset($request->file)) |
65
|
|
|
{ |
66
|
2 |
|
$this->path = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$tipID; |
67
|
2 |
|
$fileID = $this->processFileChunk($request); |
68
|
2 |
|
if($fileID) |
69
|
|
|
{ |
70
|
|
|
// Attach file to Tech Tip |
71
|
2 |
|
TechTipFiles::create([ |
72
|
2 |
|
'tip_id' => $tipID, |
73
|
2 |
|
'file_id' => $fileID, |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
$this->updateTipDetails($request); |
81
|
2 |
|
$this->updateTipEquipment($request->system_types, $tipID); |
82
|
2 |
|
if(isset($request->deletedFileList)) |
83
|
|
|
{ |
84
|
|
|
$this->updateTipFiles($request->deletedFileList); |
85
|
|
|
} |
86
|
2 |
|
if(isset($request->resendNotification) && $request->resendNotification) |
87
|
|
|
{ |
88
|
|
|
$this->sendNotification($tipID, true); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Delete the Tech Tip |
95
|
2 |
|
public function deleteTip($tipID) |
96
|
|
|
{ |
97
|
2 |
|
TechTipFavs::where('tip_id', $tipID)->delete(); |
98
|
|
|
|
99
|
|
|
// Disable the tip |
100
|
2 |
|
TechTips::find($tipID)->delete(); |
101
|
2 |
|
Log::warning('User - '.Auth::user()->full_name.' deleted Tech Tip ID - '.$tipID); |
102
|
|
|
|
103
|
2 |
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Create the Tech Tip in the datbase |
107
|
4 |
|
protected function createTip($tipData) |
108
|
|
|
{ |
109
|
|
|
// Remove any forward slash (/) from the Subject Field |
110
|
4 |
|
$tipData->merge(['subject' => str_replace('/', '-', $tipData->subject)]); |
111
|
|
|
|
112
|
|
|
// Enter the tip details and return the tip ID |
113
|
4 |
|
$tip = TechTips::create([ |
114
|
4 |
|
'tip_type_id' => $tipData->tip_type_id, |
115
|
4 |
|
'subject' => $tipData->subject, |
116
|
4 |
|
'description' => $tipData->description, |
117
|
4 |
|
'user_id' => Auth::user()->user_id, |
118
|
4 |
|
'sticky' => $tipData->sticky, |
119
|
|
|
]); |
120
|
4 |
|
$tipID = $tip->tip_id; |
121
|
|
|
|
122
|
4 |
|
$this->processTipEquipment($tipData->equipment, $tipID); |
123
|
4 |
|
$this->processFiles($tipID); |
124
|
4 |
|
if(!$tipData->noEmail) |
125
|
|
|
{ |
126
|
4 |
|
$this->sendNotification($tipID); |
127
|
|
|
} |
128
|
|
|
|
129
|
4 |
|
return $tipID; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Update the tip details |
133
|
2 |
|
protected function updateTipDetails($data) |
134
|
|
|
{ |
135
|
|
|
// Update the base information |
136
|
2 |
|
TechTips::find($data->tip_id)->update([ |
137
|
2 |
|
'tip_type_id' => $data->tip_type_id, |
138
|
2 |
|
'subject' => $data->subject, |
139
|
2 |
|
'description' => $data->description, |
140
|
2 |
|
'sticky' => $data->sticky, |
141
|
2 |
|
'updated_id' => Auth::user()->user_id, |
142
|
|
|
]); |
143
|
|
|
|
144
|
2 |
|
return true; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// Add or remove any system types attached to the tip |
148
|
2 |
|
protected function updateTipEquipment($equipTypes, $tipID) |
149
|
|
|
{ |
150
|
2 |
|
$current = TechTipSystems::where('tip_id', $tipID)->get(); |
151
|
2 |
|
$newEquip = []; |
152
|
|
|
|
153
|
2 |
|
foreach($equipTypes as $equip) |
154
|
|
|
{ |
155
|
2 |
|
if(isset($equip['laravel_through_key'])) |
156
|
|
|
{ |
157
|
|
|
$current = $current->filter(function($item) use ($equip) { |
158
|
|
|
return $item->sys_id != $equip['sys_id']; |
159
|
|
|
}); |
160
|
|
|
} |
161
|
|
|
else |
162
|
|
|
{ |
163
|
2 |
|
$newEquip[] = $equip; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
2 |
|
$this->processTipEquipment($newEquip, $tipID); |
168
|
|
|
|
169
|
2 |
|
foreach($current as $cur) |
170
|
|
|
{ |
171
|
2 |
|
TechTipSystems::find($cur->tip_tag_id)->delete(); |
172
|
|
|
} |
173
|
|
|
|
174
|
2 |
|
return true; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// Remove any files that were deleted from the tip |
178
|
|
|
protected function updateTipFiles($deletedList) |
179
|
|
|
{ |
180
|
|
|
foreach($deletedList as $delFile) |
181
|
|
|
{ |
182
|
|
|
$details = TechTipFiles::find($delFile); |
183
|
|
|
$fileID = $details->file_id; |
184
|
|
|
$details->delete(); |
185
|
|
|
|
186
|
|
|
$this->deleteFile($fileID); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// Store any equipment types that were attached to the tip |
191
|
6 |
|
protected function processTipEquipment($equipment, $tipID) |
192
|
|
|
{ |
193
|
6 |
|
foreach($equipment as $sys) |
194
|
|
|
{ |
195
|
6 |
|
TechTipSystems::create([ |
196
|
6 |
|
'tip_id' => $tipID, |
197
|
6 |
|
'sys_id' => $sys['sys_id'] |
198
|
|
|
]); |
199
|
|
|
} |
200
|
|
|
|
201
|
6 |
|
return true; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// For all files that were uploaded, move to the proper folder and attach to the tip |
205
|
4 |
|
protected function processFiles($tipID) |
206
|
|
|
{ |
207
|
4 |
|
if(session('newTipFile') != null) |
208
|
|
|
{ |
209
|
2 |
|
$files = session('newTipFile'); |
210
|
2 |
|
$this->path = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$tipID; |
211
|
2 |
|
foreach($files as $file) |
212
|
|
|
{ |
213
|
|
|
// Move the file to the proper folder |
214
|
2 |
|
$data = Files::find($file); |
215
|
2 |
|
Storage::move($data->file_link.$data->file_name, $this->path.DIRECTORY_SEPARATOR.$data->file_name); |
216
|
2 |
|
$data->update([ |
217
|
2 |
|
'file_link' => $this->path.DIRECTORY_SEPARATOR |
218
|
|
|
]); |
219
|
|
|
|
220
|
|
|
// Attach file to Tech Tip |
221
|
2 |
|
TechTipFiles::create([ |
222
|
2 |
|
'tip_id' => $tipID, |
223
|
2 |
|
'file_id' => $data->file_id |
224
|
|
|
]); |
225
|
|
|
} |
226
|
|
|
|
227
|
2 |
|
session()->forget('newTipFile'); |
228
|
|
|
} |
229
|
|
|
|
230
|
4 |
|
return true; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// Send the notifications for the new tip |
234
|
4 |
|
protected function sendNotification($tipID, $edit = false) |
235
|
|
|
{ |
236
|
4 |
|
$details = TechTips::find($tipID); |
237
|
|
|
$users = User::whereHas('UserSettings', function($query) { |
238
|
4 |
|
$query->where('em_tech_tip', 1); |
239
|
4 |
|
})->get(); |
240
|
|
|
|
241
|
4 |
|
if(!$edit) |
242
|
|
|
{ |
243
|
|
|
|
244
|
4 |
|
Notification::send($users, new NewTechTip($details)); |
245
|
|
|
} |
246
|
|
|
else |
247
|
|
|
{ |
248
|
|
|
Notification::send($users, new TechTipUpdated($details)); |
249
|
|
|
} |
250
|
4 |
|
} |
251
|
|
|
} |
252
|
|
|
|