-
Notifications
You must be signed in to change notification settings - Fork 2
/
DefaultPresentationForm.php
111 lines (93 loc) · 2.76 KB
/
DefaultPresentationForm.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
<?php
namespace App\Livewire\Forms;
use App\Models\Company;
use App\Models\DefaultPresentation;
use App\Models\Presentation;
use App\Models\Room;
use Carbon\Carbon;
use Livewire\Attributes\Validate;
use Livewire\Form;
class DefaultPresentationForm extends Form
{
#[Validate('')]
public $name;
#[Validate('')]
public $description;
#[Validate(['required', 'integer', 'exists:rooms,id'])]
public $room_id;
#[Validate(['required', 'in:opening,closing'])]
public $type;
#[Validate('required')]
public $start;
#[Validate(['required', 'after:start'])]
public $end;
/**
* Sets the type of the presentation
* @param $type
* @return void
*/
public function setType($type)
{
$this->type = $type;
$this->room_id = optional(Room::where('name', 'GW027')->first())->id;
}
/**
* Sets the details of the default presentation that can be editted
* @return void
*/
public function setDetails($presentation): void
{
$this->name = $presentation->name;
$this->description = $presentation->description;
$this->room_id = $presentation->room_id;
}
/**
* Stores the presentation
* @return void
*/
public function save()
{
$this->validate();
if ($this->type == 'closing') {
$startTime = Carbon::parse($this->start);
$openingEndTime = Carbon::parse(DefaultPresentation::opening()->end);
if ($openingEndTime->greaterThan($startTime)) {
$this->addError('start', 'Closing presentation must start after the opening presentation ends.');
return;
}
}
if (DefaultPresentation::where('type', $this->type)->doesntExist()) {
DefaultPresentation::create(
$this->all()
);
}
}
/**
* Processes the updates of the default presentation
* @return void
* @throws \Illuminate\Validation\ValidationException
*/
public function update()
{
foreach (['name', 'description', 'room_id'] as $field) {
$this->validateOnly($field);
}
$presentation = DefaultPresentation::query()->where('type', '=', $this->type)->first();
$presentation->update($this->only('name', 'description', 'room_id'));
}
/**
* Determines whether the entity that was supposed to be created using this form
* is actually created
* @return bool
*/
public function isEntityCreated()
{
if ($this->type == 'opening') {
return !is_null(DefaultPresentation::opening());
}
if ($this->type == 'closing') {
return !is_null(DefaultPresentation::closing());
}
return false;
}
}