|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Competitor; |
|
6
|
|
|
use App\Exceptions\InvitationExpiredException; |
|
7
|
|
|
use App\Exceptions\InvitationNeededException; |
|
8
|
|
|
use App\Exceptions\InvitationNotActiveException; |
|
9
|
|
|
use App\Grade; |
|
10
|
|
|
use App\Http\Requests\ChampionshipRequest; |
|
11
|
|
|
use App\Invite; |
|
12
|
|
|
use App\Notifications\RegisteredToChampionship; |
|
13
|
|
|
use App\Tournament; |
|
14
|
|
|
use App\User; |
|
15
|
|
|
use Auth; |
|
16
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
|
17
|
|
|
use Illuminate\Support\Facades\URL; |
|
18
|
|
|
use Illuminate\Support\Facades\View; |
|
19
|
|
|
|
|
20
|
|
|
class ChampionshipController extends Controller |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Register a User to a Tournament |
|
24
|
|
|
* Triggered when User click Activation Link received in mail |
|
25
|
|
|
* |
|
26
|
|
|
* @param $tournamentSlug |
|
27
|
|
|
* @param $token |
|
28
|
|
|
* @return View |
|
29
|
|
|
* @throws AuthorizationException |
|
30
|
|
|
* @throws InvitationExpiredException |
|
31
|
|
|
* @throws InvitationNeededException |
|
32
|
|
|
* @throws InvitationNotActiveException |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function create($tournamentSlug, $token) //TODO Should not put token as part as URL, but as param |
|
35
|
|
|
{ |
|
36
|
1 |
|
$tournament = Tournament::where('slug', $tournamentSlug)->first(); |
|
37
|
1 |
|
$grades = Grade::getAllPlucked(); |
|
38
|
1 |
|
$invite = Invite::getInviteFromToken($token); |
|
39
|
|
|
|
|
40
|
1 |
|
if (is_null($invite)) throw new InvitationNeededException(); |
|
41
|
|
|
if ($invite->hasExpired()) throw new InvitationExpiredException(); |
|
42
|
|
|
if ($invite->active != 1) throw new InvitationNotActiveException(); |
|
43
|
|
|
|
|
44
|
|
|
// Check if user is already registered |
|
45
|
|
|
$user = User::where('email', $invite->email)->first(); |
|
46
|
|
|
if (is_null($user)) { // Redirect to user creation |
|
47
|
|
|
return view('auth/invite', compact('token')); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
// If user is not confirmed, auto confirm him |
|
50
|
|
|
if ($user->verified == 0) { |
|
51
|
|
|
$user->verified = 1; |
|
52
|
|
|
$user->save(); |
|
53
|
|
|
} |
|
54
|
|
|
// Redirect to register category Screen |
|
55
|
|
|
|
|
56
|
|
|
Auth::loginUsingId($user->id); |
|
57
|
|
|
return view("categories.register", compact('tournament', 'invite', 'grades')); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Store a new championship, when registering categories |
|
62
|
|
|
* Invoked on save() after create() is called |
|
63
|
|
|
* |
|
64
|
|
|
* @param Tournament $tournament |
|
65
|
|
|
* @param ChampionshipRequest $request |
|
66
|
|
|
* @return \Illuminate\Http\Response |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function store(Tournament $tournament, ChampionshipRequest $request) |
|
69
|
|
|
{ |
|
70
|
1 |
|
$categories = $request->get('cat'); |
|
71
|
|
|
|
|
72
|
1 |
|
if ($categories == null) { |
|
73
|
|
|
flash()->error(trans('msg.you_must_choose_at_least_one_championship')); |
|
|
|
|
|
|
74
|
|
|
return redirect()->back(); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
1 |
|
Auth::user()->updateUserFullName($request->firstname, $request->lastname); |
|
77
|
1 |
|
Auth::user()->championships()->detach(); |
|
78
|
|
|
|
|
79
|
1 |
|
$shortId = Competitor::getShortId($categories, $tournament); |
|
80
|
|
|
|
|
81
|
1 |
|
foreach ($categories as $category) { |
|
82
|
1 |
|
Auth::user()->championships()->attach($category, ['confirmed' => 0, 'short_id' => $shortId]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
$tournament->owner->notify(new RegisteredToChampionship(Auth::user(), $tournament)); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
1 |
|
flash()->success(trans('msg.tx_for_register_tournament', ['tournament' => $tournament->name])); |
|
|
|
|
|
|
88
|
1 |
|
return redirect(URL::action('UserController@getMyTournaments', Auth::user())); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|