-
Notifications
You must be signed in to change notification settings - Fork 2
/
PresentationModal.php
138 lines (116 loc) · 4.39 KB
/
PresentationModal.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
135
136
137
138
<?php
namespace App\Livewire\Schedule;
use App\Actions\Schedule\PresentationAllocationHelper;
use App\Actions\Schedule\PresentationConflictChecker;
use App\Mail\CancelledPresentationMailable;
use App\Models\Edition;
use App\Models\Room;
use App\Models\Timeslot;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Mail;
use Illuminate\View\View;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Illuminate\Support\Facades\Validator;
use LivewireUI\Modal\ModalComponent;
use App\Models\DefaultPresentation;
class PresentationModal extends ModalComponent
{
public $presentation;
public $rooms;
#[Validate(['required', 'exists:rooms,id'])]
public $room_id;
#[Validate(['required', 'date_format:H:i'])]
public $start;
public $errorMessage;
/**
* Initializes the component
* @param $presentation
* @return void
*/
public function mount($presentationId)
{
$this->presentation = \App\Models\Presentation::find($presentationId);
$this->rooms = Room::all();
$this->room_id = $this->presentation->room->id;
$this->start = Carbon::parse($this->presentation->start)->format('H:i');
}
/**
* Processes the new data submitted by the user and sends move event to the parent
* to handle the moving of presentation
* @return void
*/
public function save()
{
$this->authorize('edit-schedule');
$this->validate();
$this->additionalStartTimeValidation($this->start);
$presentationChecker = new PresentationConflictChecker();
$allocationHelper = new PresentationAllocationHelper();
$timeslot = $allocationHelper->findTimeslotByStartingTime($this->start);
$room = Room::find($this->room_id);
if ($presentationChecker->isClearOfConflicts($this->presentation, $room, $this->start)) {
$this->dispatch('move-presentation', data: [
'presentation_id' => $this->presentation->id,
'new_room_id' => $this->room_id,
'new_timeslot_id' => $timeslot->id,
'new_time' => $this->start
]);
$this->closeModal();
} else {
$this->errorMessage = 'Error: Scheduling conflict has occurred and the presentation cannot
be moved to the desired spot. Try to set different starting time';
}
}
/**
* Ensures that the time given is between the starting time of the conference day and before the end
* @param $start
* @return void
* @throws \Illuminate\Validation\ValidationException
*/
public function additionalStartTimeValidation($start)
{
$validator = Validator::make(
['start' => $this->start],
['start' => ['required', 'date_format:H:i', function ($attribute, $value, $fail) {
$start = Carbon::parse($value);
$openingEndTime = Carbon::parse(DefaultPresentation::opening()->end);
$closingStartTime = Carbon::parse(DefaultPresentation::closing()->start);
if ($start->lessThan($openingEndTime) || $start->greaterThanOrEqualTo($closingStartTime)) {
$fail('The starting time must be after ' . $openingEndTime->format('H:i') . ' and before ' . $closingStartTime->format('H:i') . '.');
}
}]]
);
$validator->validate();
}
/**
* Handles removing of participant of presentation if needed and sends an
* event to the parent to handle removing of the presentation from the schedule
*
* @return void
*/
public function remove()
{
if (Edition::current()->is_final_programme_released) {
$participants = $this->presentation->participants;
foreach ($participants as $participant) {
$participant->leavePresentation($this->presentation);
if ($participant->receive_emails) {
Mail::to($participant->email)->send(new CancelledPresentationMailable($participant, $this->presentation));
}
}
}
$this->dispatch('remove-presentation', data: [
'presentation_id' => $this->presentation->id
]);
$this->closeModal();
}
/**
* Renders the component
* @return View
*/
public function render(): View
{
return view('livewire.schedule.presentation-modal');
}
}