|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Domains\TechTips; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\File; |
|
6
|
|
|
use Illuminate\Support\Facades\Log; |
|
7
|
|
|
use Illuminate\Support\Facades\Auth; |
|
8
|
|
|
use Illuminate\Support\Facades\Storage; |
|
9
|
|
|
use Illuminate\Support\Facades\Notification; |
|
10
|
|
|
|
|
11
|
|
|
use App\User; |
|
12
|
|
|
use App\TechTips; |
|
13
|
|
|
use App\TechTipFiles; |
|
14
|
|
|
use App\TechTipSystems; |
|
15
|
|
|
|
|
16
|
|
|
use App\Domains\Files\SetFiles; |
|
17
|
|
|
use App\Notifications\NewTechTipNotification; |
|
18
|
|
|
use App\Notifications\UpdateTechTipNotification; |
|
19
|
|
|
|
|
20
|
|
|
class SetTechTips extends SetFiles |
|
21
|
|
|
{ |
|
22
|
|
|
// Attach a single image that can be used in the html field of the tech tip body |
|
23
|
4 |
|
public function processImage($request) |
|
24
|
|
|
{ |
|
25
|
|
|
// Generate a unique hash as the file name and store it in a publicly accessable folder |
|
26
|
4 |
|
$path = 'images/tip_img'; |
|
27
|
4 |
|
$location = Storage::disk('public')->putFile($path, new File($request->file)); |
|
28
|
4 |
|
Log::info('Image uploaded for tech tip. Image Data - ', $request->toArray()); |
|
29
|
|
|
|
|
30
|
|
|
// Return the full url path to the image |
|
31
|
4 |
|
return Storage::url($location); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// Process the submitted data of a new tech tip - if a file chunk is sent, store that first |
|
35
|
6 |
|
public function processNewTip($request, $userID) |
|
36
|
|
|
{ |
|
37
|
|
|
// Determine if a file is being uploaded |
|
38
|
6 |
|
if(isset($request->file)) |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->uploadFile($request); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
6 |
|
return $this->createTip($request, $userID); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Edit an existing tech tips information |
|
47
|
6 |
|
public function processEditTip($request, $tipID, $userID) |
|
48
|
|
|
{ |
|
49
|
|
|
// Determine if a file is being uploaded |
|
50
|
6 |
|
if(isset($request->file)) |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->uploadFile($request); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
6 |
|
return $this->updateTip($request, $tipID, $userID); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Soft delete tech tip |
|
59
|
4 |
|
public function deactivateTip($tipID) |
|
60
|
|
|
{ |
|
61
|
4 |
|
TechTips::find($tipID)->delete(); |
|
62
|
4 |
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// Process the file chunk - if a full file is uploaded, set the file ID into session data |
|
66
|
|
|
protected function uploadFile($request) |
|
67
|
|
|
{ |
|
68
|
|
|
$filename = $this->getChunk($request); |
|
69
|
|
|
if(!$filename) |
|
70
|
|
|
{ |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$fileID = $this->addDatabaseRow($filename, $this->path); |
|
75
|
|
|
$fileArr = session('newTipFile') != null ? session('newTipFile') : []; |
|
76
|
|
|
$fileArr[] = $fileID; |
|
77
|
|
|
session(['newTipFile' => $fileArr]); |
|
78
|
|
|
|
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// Create a new Tech Tip |
|
83
|
6 |
|
protected function createTip($tipData, $userID) |
|
84
|
|
|
{ |
|
85
|
6 |
|
$tip = TechTips::create([ |
|
86
|
6 |
|
'tip_type_id' => $tipData->tip_type_id, |
|
87
|
6 |
|
'subject' => $tipData->subject, |
|
88
|
6 |
|
'description' => $tipData->description, |
|
89
|
6 |
|
'user_id' => $userID, |
|
90
|
6 |
|
'sticky' => $tipData->sticky, |
|
91
|
|
|
]); |
|
92
|
|
|
|
|
93
|
6 |
|
$this->processTipEquipment($tipData->equipment, $tip->tip_id); |
|
94
|
6 |
|
$this->processTipFiles($tip->tip_id); |
|
95
|
6 |
|
if(!$tipData->noEmail) |
|
96
|
|
|
{ |
|
97
|
4 |
|
$this->sendNotification($tip); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
6 |
|
return $tip->tip_id; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Update an existing Tech Tip |
|
104
|
6 |
|
protected function updateTip($tipData, $tipID, $userID) |
|
105
|
|
|
{ |
|
106
|
6 |
|
TechTips::find($tipID)->update([ |
|
107
|
6 |
|
'tip_type_id' => $tipData->tip_type_id, |
|
108
|
6 |
|
'subject' => $tipData->subject, |
|
109
|
6 |
|
'description' => $tipData->description, |
|
110
|
6 |
|
'updated_id' => $userID, |
|
111
|
6 |
|
'sticky' => $tipData->sticky, |
|
112
|
|
|
]); |
|
113
|
|
|
|
|
114
|
6 |
|
$this->checkTipEquipment($tipData->equipment, $tipID); |
|
115
|
6 |
|
$this->processTipFiles($tipID); |
|
116
|
6 |
|
if(isset($tipData->deletedFileList)) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->removeTipFiles($tipData->deletedFileList, $tipID); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
// Send an updated notification if necessary |
|
122
|
6 |
|
if($tipData->notify) |
|
123
|
|
|
{ |
|
124
|
4 |
|
$tip = TechTips::find($tipID); |
|
125
|
4 |
|
$this->sendNotification($tip, true); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
6 |
|
return true; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// Determine if any equipment types have been removed from the tip |
|
132
|
6 |
|
protected function checkTipEquipment($equipArr, $tipID) |
|
133
|
|
|
{ |
|
134
|
6 |
|
$current = TechTipSystems::where('tip_id', $tipID)->get(); |
|
135
|
6 |
|
$newEquip = []; |
|
136
|
|
|
|
|
137
|
|
|
// Cycle through all equipment to see if it is new or existing |
|
138
|
6 |
|
foreach($equipArr as $equip) |
|
139
|
|
|
{ |
|
140
|
6 |
|
if(isset($equip['laravel_through_key'])) |
|
141
|
|
|
{ |
|
142
|
|
|
$current = $current->filter(function($item) use ($equip) |
|
143
|
|
|
{ |
|
144
|
|
|
return $item->sys_id != $equip['sys_id']; |
|
145
|
|
|
}); |
|
146
|
|
|
} |
|
147
|
|
|
else |
|
148
|
|
|
{ |
|
149
|
6 |
|
$newEquip[] = $equip; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// Remove anything left in the current field as it has been removed |
|
154
|
6 |
|
foreach($current as $cur) |
|
155
|
|
|
{ |
|
156
|
4 |
|
TechTipSystems::find($cur->tip_tag_id)->delete(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
// Process all new equipment |
|
160
|
6 |
|
$this->processTipEquipment($newEquip, $tipID); |
|
161
|
6 |
|
return true; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
// Add any equipment types to the tech tip |
|
165
|
12 |
|
protected function processTipEquipment($equipArr, $tipID) |
|
166
|
|
|
{ |
|
167
|
12 |
|
foreach($equipArr as $equip) |
|
168
|
|
|
{ |
|
169
|
12 |
|
TechTipSystems::create([ |
|
170
|
12 |
|
'tip_id' => $tipID, |
|
171
|
12 |
|
'sys_id' => $equip['sys_id'], |
|
172
|
|
|
]); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
12 |
|
return true; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
// Move any uploaded files from the default location, to the Tech Tip folder |
|
179
|
12 |
|
protected function processTipFiles($tipID) |
|
180
|
|
|
{ |
|
181
|
12 |
|
$path = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$tipID.DIRECTORY_SEPARATOR; |
|
182
|
12 |
|
if(session('newTipFile') != null) |
|
183
|
|
|
{ |
|
184
|
|
|
$fileArr = session('newTipFile'); |
|
185
|
|
|
foreach($fileArr as $file) |
|
186
|
|
|
{ |
|
187
|
|
|
$this->moveFile($file, $path); |
|
188
|
|
|
TechTipFiles::create([ |
|
189
|
|
|
'tip_id' => $tipID, |
|
190
|
|
|
'file_id' => $file, |
|
191
|
|
|
]); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
session()->forget('newTipFile'); |
|
195
|
|
|
return true; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
12 |
|
return false; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
// Remove any files that are no longer needed for the tip |
|
202
|
|
|
protected function removeTipFiles($fileList, $tipID) |
|
|
|
|
|
|
203
|
|
|
{ |
|
204
|
|
|
foreach($fileList as $file) |
|
205
|
|
|
{ |
|
206
|
|
|
$details = TechTipFiles::find($file); |
|
207
|
|
|
$fileID = $details->file_id; |
|
208
|
|
|
$details->delete(); |
|
209
|
|
|
|
|
210
|
|
|
$this->deleteFile($fileID); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return true; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
// Send a notification of the new/edited tech tip |
|
217
|
8 |
|
protected function sendNotification($tip, $edit = false) |
|
218
|
|
|
{ |
|
219
|
|
|
$users = User::whereHas('UserSettings', function($q) |
|
220
|
|
|
{ |
|
221
|
8 |
|
$q->where('em_tech_tip', true); |
|
222
|
8 |
|
})->get(); |
|
223
|
|
|
|
|
224
|
8 |
|
if(!$edit) |
|
225
|
|
|
{ |
|
226
|
4 |
|
Notification::send($users, new NewTechTipNotification($tip)); |
|
227
|
|
|
} |
|
228
|
|
|
else |
|
229
|
|
|
{ |
|
230
|
4 |
|
Notification::send($users, new UpdateTechTipNotification($tip)); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
8 |
|
return true; |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|