|
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
|
|
|
|
|
19
|
|
|
class SetTechTips extends SetFiles |
|
20
|
|
|
{ |
|
21
|
|
|
// Attach a single image that can be used in the html field of the tech tip body |
|
22
|
4 |
|
public function processImage($request) |
|
23
|
|
|
{ |
|
24
|
|
|
// Generate a unique hash as the file name and store it in a publicly accessable folder |
|
25
|
4 |
|
$path = 'images/tip_img'; |
|
26
|
4 |
|
$location = Storage::disk('public')->putFile($path, new File($request->file)); |
|
27
|
4 |
|
Log::info('Image uploaded for tech tip. Image Data - ', $request->toArray()); |
|
28
|
|
|
|
|
29
|
|
|
// Return the full url path to the image |
|
30
|
4 |
|
return Storage::url($location); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
// Process the submitted data of a new tech tip - if a file chunk is sent, store that first |
|
34
|
6 |
|
public function processNewTip($request) |
|
35
|
|
|
{ |
|
36
|
|
|
// Determine if a file is being uploaded |
|
37
|
6 |
|
if(isset($request->file)) |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->uploadFile($request); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
6 |
|
return $this->createTip($request); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// Process the file chunk - if a full file is uploaded, set the file ID into session data |
|
46
|
|
|
protected function uploadFile($request) |
|
47
|
|
|
{ |
|
48
|
|
|
$filename = $this->getChunk($request); |
|
49
|
|
|
if(!$filename) |
|
50
|
|
|
{ |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$fileID = $this->addDatabaseRow($filename, $this->path); |
|
55
|
|
|
$fileArr = session('newTipFile') != null ? session('newTipFile') : []; |
|
56
|
|
|
$fileArr[] = $fileID; |
|
57
|
|
|
session(['newTipFile' => $fileArr]); |
|
58
|
|
|
|
|
59
|
|
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Create a new Tech Tip |
|
63
|
6 |
|
protected function createTip($tipData) |
|
64
|
|
|
{ |
|
65
|
6 |
|
$tip = TechTips::create([ |
|
66
|
6 |
|
'tip_type_id' => $tipData->tip_type_id, |
|
67
|
6 |
|
'subject' => $tipData->subject, |
|
68
|
6 |
|
'description' => $tipData->description, |
|
69
|
6 |
|
'user_id' => Auth::user()->user_id, |
|
70
|
6 |
|
'sticky' => $tipData->sticky, |
|
71
|
|
|
]); |
|
72
|
|
|
|
|
73
|
6 |
|
$this->processTipEquipment($tipData->equipment, $tip->tip_id); |
|
74
|
6 |
|
$this->processTipFiles($tip->tip_id); |
|
75
|
6 |
|
if(!$tipData->noEmail) |
|
76
|
|
|
{ |
|
77
|
4 |
|
$this->sendNotification($tip); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
6 |
|
return $tip->tip_id; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Add any equipment types to the tech tip |
|
84
|
6 |
|
protected function processTipEquipment($equipArr, $tipID) |
|
85
|
|
|
{ |
|
86
|
6 |
|
foreach($equipArr as $equip) |
|
87
|
|
|
{ |
|
88
|
6 |
|
TechTipSystems::create([ |
|
89
|
6 |
|
'tip_id' => $tipID, |
|
90
|
6 |
|
'sys_id' => $equip['sys_id'], |
|
91
|
|
|
]); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
6 |
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Move any uploaded files from the default location, to the Tech Tip folder |
|
98
|
6 |
|
protected function processTipFiles($tipID) |
|
99
|
|
|
{ |
|
100
|
6 |
|
$path = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$tipID.DIRECTORY_SEPARATOR; |
|
101
|
6 |
|
if(session('newTipFile') != null) |
|
102
|
|
|
{ |
|
103
|
|
|
$fileArr = session('newTipFile'); |
|
104
|
|
|
foreach($fileArr as $file) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->moveFile($file, $path); |
|
107
|
|
|
TechTipFiles::create([ |
|
108
|
|
|
'tip_id' => $tipID, |
|
109
|
|
|
'file_id' => $file, |
|
110
|
|
|
]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
session()->forget('newTipFile'); |
|
114
|
|
|
return true; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
6 |
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// Send a notification of the new/edited tech tip |
|
121
|
4 |
|
protected function sendNotification($tip, $edit = false) |
|
122
|
|
|
{ |
|
123
|
|
|
// $details = TechTips::find($tipID); |
|
124
|
|
|
$users = User::whereHas('UserSettings', function($q) |
|
125
|
|
|
{ |
|
126
|
4 |
|
$q->where('em_tech_tip', true); |
|
127
|
4 |
|
})->get(); |
|
128
|
|
|
|
|
129
|
4 |
|
if(!$edit) |
|
130
|
|
|
{ |
|
131
|
4 |
|
Notification::send($users, new NewTechTipNotification($tip)); |
|
132
|
|
|
} |
|
133
|
|
|
else |
|
134
|
|
|
{ |
|
135
|
|
|
abort(500); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
4 |
|
return true; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|