1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\InviteRequest; |
6
|
|
|
use App\Invite; |
7
|
|
|
use App\Notifications\InviteCompetitor; |
8
|
|
|
use App\Tournament; |
9
|
|
|
use App\User; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
use Illuminate\Support\Facades\Auth; |
12
|
|
|
use Illuminate\Support\Facades\URL; |
13
|
|
|
use Illuminate\Support\Facades\View; |
14
|
|
|
use Maatwebsite\Excel\Facades\Excel; |
15
|
|
|
|
16
|
|
|
class InviteController extends Controller |
17
|
|
|
{ |
18
|
|
|
protected $emailBadFormat; |
19
|
|
|
protected $wrongEmail; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Display a listing of all invitations. |
24
|
|
|
* |
25
|
|
|
* @return View |
26
|
|
|
*/ |
27
|
|
|
public function index() |
28
|
|
|
{ |
29
|
|
|
$invites = Auth::user() |
30
|
|
|
->invites() |
31
|
|
|
->with('tournament.owner') |
32
|
|
|
->paginate(config('constants.PAGINATION')); |
33
|
|
|
return view('invitation.index', compact('invites')); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Display the UI for inviting competitors |
38
|
|
|
* |
39
|
|
|
* @param Tournament $tournament |
40
|
|
|
* @return View |
41
|
|
|
*/ |
42
|
1 |
|
public function create(Tournament $tournament) |
43
|
|
|
{ |
44
|
|
|
// Should dd $tournament |
45
|
1 |
|
return view('invitation.show', compact('tournament')); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Send an email to competitor and store invitation. |
51
|
|
|
* |
52
|
|
|
* @param InviteRequest|Request $request |
53
|
|
|
* @return \Illuminate\Http\Response |
54
|
|
|
*/ |
55
|
1 |
|
public function store(Request $request) |
56
|
|
|
{ |
57
|
|
|
//TODO check that recipient is list of emails |
58
|
1 |
|
$this->validate($request, [ |
59
|
1 |
|
'recipients' => 'required' |
60
|
|
|
]); |
61
|
|
|
|
62
|
1 |
|
$tournament = Tournament::where('slug', $request->tournamentSlug)->first(); |
63
|
1 |
|
$recipients = json_decode($request->get("recipients")); |
64
|
1 |
|
foreach ($recipients as $recipient) { |
65
|
|
|
// Mail to Recipients |
66
|
1 |
|
$code = resolve(Invite::class)->generateTournamentInvite($recipient, $tournament); |
67
|
1 |
|
$user = new User(); |
68
|
1 |
|
$user->email = $recipient; |
69
|
1 |
|
$user->notify(new InviteCompetitor($user, $tournament, $code, null)); |
70
|
|
|
} |
71
|
1 |
|
flash()->success(trans('msg.invitation_sent')); |
|
|
|
|
72
|
1 |
|
return redirect(URL::action('TournamentController@edit', $tournament->slug)); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Send an email to competitor and store invitation. |
78
|
|
|
* |
79
|
|
|
* @param InviteRequest|Request $request |
80
|
|
|
* @return \Illuminate\Http\Response |
81
|
|
|
*/ |
82
|
|
|
public function upload(Request $request) |
83
|
|
|
{ |
84
|
|
|
$this->emailBadFormat = false; |
85
|
|
|
$file = $request->file('invites')->store('invites'); |
86
|
|
|
$tournament = Tournament::where('slug', $request->tournamentSlug)->first(); |
87
|
|
|
// Parse Csv File |
88
|
|
|
|
89
|
|
|
$reader = Excel::load("storage/app/" . $file, function ($reader) { |
|
|
|
|
90
|
|
|
})->get(); |
91
|
|
|
|
92
|
|
|
// Checking if malformed email |
93
|
|
|
resolve(Invite::class)->checkBadEmailsInExcel($reader, $tournament); |
94
|
|
|
if ($this->emailBadFormat == true) { |
|
|
|
|
95
|
|
|
flash()->error(trans('msg.email_not_valid', ['email' => $this->wrongEmail])); |
96
|
|
|
return redirect(URL::action('InviteController@create', $tournament->slug)); |
97
|
|
|
} |
98
|
|
|
Invite::sendInvites($reader, $tournament); |
99
|
|
|
flash()->success(trans('msg.invitation_sent')); |
|
|
|
|
100
|
|
|
return redirect(URL::action('InviteController@create', $tournament->slug)); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|