-
Notifications
You must be signed in to change notification settings - Fork 0
/
PresentationController.php
134 lines (108 loc) · 3.69 KB
/
PresentationController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace App\Http\Controllers\Crew;
use App\Http\Controllers\Controller;
use App\Http\Requests\StorePresentationRequest;
use App\Models\Company;
use App\Models\Presentation;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
class PresentationController extends Controller
{
/**
* Display a listing of the resource.
*
* @return View
*/
public function index(): View
{
if (Auth::user()->cannot('viewAny', Presentation::class)) {
abort(403);
}
$presentations = Presentation::orderBy('is_approved')
->orderBy('created_at', 'desc')
->paginate(15);
return view('crew.presentations.index', compact('presentations'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
if (Auth::user()->cannot('create', Presentation::class)) {
abort(403);
}
// Also possible to use the relationship with UserPresentation,
// but as a personal choice I'd rather stick to isolating that
$users = User::all()->filter(function ($user) {
return is_null($user->presenter_of) && $user->hasRole('participant');
});
return view('crew.presentations.create', compact('users'));
}
/**
* Store a newly created resource in storage.
*/
public function store(StorePresentationRequest $request)
{
if (Auth::user()->cannot('create', Presentation::class)) {
abort(403);
}
$validated = $request->validated();
$presentation = Presentation::create($request->validate(Presentation::rules()));
$user = User::find($validated['user_id']);
$user->joinPresentation($presentation, 'speaker');
if ($user->company) {
$presentation->company_id = $user->company->id;
$presentation->save();
$presentation->fresh();
}
return redirect(route('moderator.presentations.index'))
->banner('You successfully added a new presentation');
}
/**
* Display the specified resource.
*
*/
public function show(Presentation $presentation): View
{
if (Auth::user()->cannot('view', $presentation)) {
abort(403);
}
return view('crew.presentations.show', compact('presentation'));
}
/**
* Approve or reject the specified resource in storage.
*/
public function approve(Request $request, Presentation $presentation)
{
if (Auth::user()->cannot('approve', $presentation)) {
abort(403);
}
$validated = $request->validate([
'approved' => 'required|boolean'
]);
$isApproved = $validated['approved'];
$presentation->handleApproval($isApproved);
$template = $isApproved ? 'You approved the presentation to take place during the IT Conference!'
: 'You refused the request to host this presentation during the IT conference';
if ($isApproved) {
return redirect(route('moderator.presentations.show', $presentation))
->banner(__($template));
}
return redirect(route('moderator.presentations.index'))
->banner(__($template));
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Presentation $presentation)
{
if (Auth::user()->cannot('delete', $presentation)) {
abort(403);
}
$presentation->delete();
return redirect(route('moderator.presentations.index'))
->banner('You deleted the presentation successfully');
}
}