1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Requests; |
4
|
|
|
|
5
|
|
|
use App\Tournament; |
6
|
|
|
use App\Venue; |
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
|
10
|
|
|
class TournamentRequest extends Request |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Determine if the user is authorized to make this request. |
14
|
|
|
* |
15
|
|
|
* @return bool |
16
|
|
|
*/ |
17
|
7 |
|
public function authorize() |
18
|
|
|
{ |
19
|
7 |
|
return true; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Get the validation rules that apply to the request. |
24
|
|
|
* |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
7 |
|
public function rules() |
28
|
|
|
{ |
29
|
|
|
switch (true) { |
30
|
|
|
|
31
|
7 |
|
case $this->exists('dateIni'): |
32
|
|
|
return [ |
33
|
6 |
|
'name' => 'required|min:6', |
34
|
|
|
'dateIni' => 'required|date', |
35
|
|
|
'dateFin' => 'required|date', |
36
|
|
|
]; |
37
|
4 |
|
case $this->exists('longitude'): |
38
|
|
|
return [ |
39
|
1 |
|
'venue_name' => 'required|min:3' |
40
|
|
|
]; |
41
|
3 |
|
case $this->has('category'): |
42
|
|
|
return [ |
43
|
|
|
'category' => 'required' |
44
|
|
|
]; |
45
|
|
|
|
46
|
3 |
|
case $this->rule_id == 0: |
47
|
|
|
return [ |
48
|
3 |
|
'name' => 'required|min:6', |
49
|
|
|
'dateIni' => 'required|date', |
50
|
|
|
'dateFin' => 'required|date', |
51
|
|
|
'category' => 'required', |
52
|
|
|
]; |
53
|
|
|
default: |
54
|
|
|
return [ |
55
|
|
|
'name' => 'required|min:6', |
56
|
|
|
'dateIni' => 'required|date', |
57
|
|
|
'dateFin' => 'required|date', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return Tournament $tournament |
67
|
|
|
*/ |
68
|
2 |
|
public function persist() |
69
|
|
|
{ |
70
|
2 |
|
$request = $this->except('category', 'config'); |
71
|
2 |
|
$request['registerDateLimit'] = Carbon::now()->addMonth(3); |
72
|
|
|
|
73
|
2 |
|
$tournament = Auth::user()->tournaments()->create($request); |
74
|
2 |
|
if ($this->rule_id == 0) { |
75
|
1 |
|
$tournament->categories()->sync($this->input('category')); |
76
|
1 |
|
return $tournament; |
77
|
|
|
} |
78
|
1 |
|
$tournament->setAndConfigureCategories($this->rule_id); |
79
|
1 |
|
return $tournament; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Tournament $tournament |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
4 |
|
public function update(Tournament $tournament, Venue $venue) |
87
|
|
|
{ |
88
|
4 |
|
$res = null; |
89
|
|
|
switch (true) { |
90
|
4 |
|
case $this->exists('dateIni'): |
91
|
3 |
|
$res = $tournament->update($this->all()); |
92
|
3 |
|
break; |
93
|
1 |
|
case $this->exists('longitude'): |
94
|
1 |
|
$tournament->venue_id = $venue->id; |
95
|
1 |
|
$tournament->update(); |
96
|
1 |
|
$res = true; |
97
|
1 |
|
break; |
98
|
|
|
case $this->has('category'): |
99
|
|
|
$res = $tournament->categories()->sync($this->input('category')); |
100
|
|
|
break; |
101
|
|
|
default: |
102
|
|
|
$tournament->setAndConfigureCategories($this->rule_id); |
103
|
|
|
$res = $tournament->update($this->all()); |
104
|
|
|
break; |
105
|
|
|
} |
106
|
4 |
|
return $res; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|