1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Championship; |
6
|
|
|
use App\Competitor; |
7
|
|
|
use App\Country; |
8
|
|
|
use App\Grade; |
9
|
|
|
use App\Http\Requests\CompetitorRequest; |
10
|
|
|
use App\Invite; |
11
|
|
|
use App\Notifications\InviteCompetitor; |
12
|
|
|
use App\Tournament; |
13
|
|
|
use App\User; |
14
|
|
|
use Illuminate\Http\Request; |
15
|
|
|
use Illuminate\Support\Facades\Auth; |
16
|
|
|
use Illuminate\Support\Facades\View; |
17
|
|
|
use Response; |
18
|
|
|
use URL; |
19
|
|
|
|
20
|
|
|
class CompetitorController extends Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Display a listing of the resource. |
24
|
|
|
* |
25
|
|
|
* @param Tournament $tournament |
26
|
|
|
* @return View |
27
|
|
|
*/ |
28
|
3 |
|
public function index(Tournament $tournament) |
29
|
|
|
{ |
30
|
3 |
|
$tournament = Tournament::with('championships.users', 'championships.teams', 'championships.category')->find($tournament->id); |
31
|
3 |
|
$settingSize = $tournament->championshipSettings()->count(); |
32
|
3 |
|
$categorySize = $tournament->categories->count(); |
33
|
3 |
|
$grades = Grade::getAllPlucked(); |
34
|
3 |
|
$countries = Country::getAll(); |
35
|
3 |
|
return view("tournaments.users", compact('tournament', 'settingSize', 'categorySize', 'grades', 'countries')); |
|
|
|
|
36
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Show the form for creating a new competitor. |
41
|
|
|
* |
42
|
|
|
* @param Request $request |
43
|
|
|
* @param Tournament $tournament |
44
|
|
|
* @return View |
45
|
|
|
*/ |
46
|
|
|
public function create(Request $request, Tournament $tournament) |
47
|
|
|
{ |
48
|
|
|
$championshipId = $request->get('categoryId'); |
49
|
|
|
|
50
|
|
|
return view("tournaments/users/create", compact('tournament', 'championshipId')); //, compact() |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Store a newly created resource in storage. |
55
|
|
|
* |
56
|
|
|
* @param CompetitorRequest $request |
57
|
|
|
* @param Tournament $tournament |
58
|
|
|
* @return \Illuminate\Http\Response |
59
|
|
|
*/ |
60
|
2 |
|
public function store(CompetitorRequest $request, Tournament $tournament) |
61
|
|
|
{ |
62
|
|
|
|
63
|
2 |
|
$championshipId = $request->championshipId; |
64
|
2 |
|
$championship = Championship::findOrFail($championshipId); |
65
|
|
|
|
66
|
2 |
|
foreach ($request->firstnames as $id => $firstname) { |
67
|
2 |
|
$email = $request->emails[$id] ?? Auth::user()->id . sha1(rand(1, 999999999999)) . (User::count() + 1) . "@kendozone.com"; |
68
|
2 |
|
$lastname = $request->lastnames[$id] ?? ''; |
69
|
|
|
|
70
|
2 |
|
$user = Competitor::createUser([ |
71
|
2 |
|
'firstname' => $firstname, |
72
|
2 |
|
'lastname' => $lastname, |
73
|
2 |
|
'name' => $firstname . " " . $lastname, |
74
|
2 |
|
'email' => $email |
75
|
|
|
]); |
76
|
|
|
|
77
|
2 |
|
$championships = $user->championships(); |
78
|
|
|
// If user has not registered yet this championship |
79
|
2 |
|
if (!$championships->get()->contains($championship)) { |
80
|
|
|
// Get Competitor Short ID |
81
|
2 |
|
$categories = $tournament->championships->pluck('id'); |
82
|
2 |
|
$shortId = Competitor::getShortId($categories, $tournament); |
83
|
2 |
|
$championships->attach($championshipId, ['confirmed' => 0, 'short_id' => $shortId]); |
84
|
|
|
} |
85
|
|
|
//TODO Should add a test for this |
86
|
|
|
// We send him an email with detail (and user /password if new) |
87
|
2 |
|
if (strpos($email, '@kendozone.com') === -1) { // Substring is not present |
88
|
|
|
$code = resolve(Invite::class)->generateTournamentInvite($user->email, $tournament); |
89
|
2 |
|
$user->notify(new InviteCompetitor($user, $tournament, $code, $championship->category->name)); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
2 |
|
flash()->success(trans('msg.user_registered_successful', ['tournament' => $tournament->name])); |
|
|
|
|
93
|
2 |
|
return redirect(URL::action('CompetitorController@index', $tournament->slug)); |
|
|
|
|
94
|
|
|
|
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $tournamentSlug |
100
|
|
|
* @param $tcId |
101
|
|
|
* @param $userSlug |
102
|
|
|
* @return \Illuminate\Http\JsonResponse |
103
|
|
|
*/ |
104
|
1 |
|
public function confirmUser($tournamentSlug, $tcId, $userSlug) |
|
|
|
|
105
|
|
|
{ |
106
|
1 |
|
$user = User::where('slug', $userSlug)->first(); |
107
|
1 |
|
$ctu = Competitor::where('championship_id', $tcId) |
108
|
1 |
|
->where('user_id', $user->id)->first(); |
109
|
|
|
|
110
|
1 |
|
$ctu->confirmed ? $ctu->confirmed = 0 : $ctu->confirmed = 1; |
|
|
|
|
111
|
1 |
|
if ($ctu->save()) { |
112
|
1 |
|
return Response::json(['msg' => trans('msg.user_status_successful'), 'status' => 'success']); |
113
|
|
|
} else { |
114
|
|
|
return Response::json(['msg' => trans('msg.user_status_error'), 'status' => 'error']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $tournamentSlug |
121
|
|
|
* @param $tcId |
122
|
|
|
* @param $userSlug |
123
|
|
|
* @return \Illuminate\Http\JsonResponse |
124
|
|
|
*/ |
125
|
2 |
|
public function deleteUser($tournamentSlug, $tcId, $userSlug) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
|
128
|
2 |
|
$user = User::where('slug', $userSlug)->first(); |
129
|
2 |
|
$ctu = Competitor::where('championship_id', $tcId) |
130
|
2 |
|
->where('user_id', $user->id); |
131
|
|
|
|
132
|
2 |
|
if ($ctu->forceDelete()) { |
133
|
2 |
|
return Response::json(['msg' => trans('msg.user_delete_successful'), 'status' => 'success']); |
134
|
|
|
} else { |
135
|
|
|
return Response::json(['msg' => trans('msg.user_delete_error'), 'status' => 'error']); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Display the specified resource. |
142
|
|
|
* |
143
|
|
|
* @param Tournament $tournament |
144
|
|
|
* @param User $user |
145
|
|
|
* @return View |
146
|
|
|
*/ |
147
|
|
|
public function show(Tournament $tournament, User $user) |
148
|
|
|
{ |
149
|
|
|
return view('users.show', compact('tournament', 'user')); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|