%PDF- %PDF-
Direktori : /www/loslex/demo/app/Http/Controllers/ |
Current File : //www/loslex/demo/app/Http/Controllers/RegistrationController.php |
<?php namespace App\Http\Controllers; use App\Enums\RegistrationDeletionReason; use App\Http\Requests\RegistrationUpdateRequest; use App\Mail\RegistrationCompleted; use App\Mail\RegistrationCompletedMail; use App\Mail\RegistrationDeletedMail; use App\Mail\RegistrationPaymentMail; use App\Models\Contest; use App\Models\Registration; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Str; use Illuminate\View\View; class RegistrationController extends Controller { /** Display the specified registration. */ public function show(Registration $registration): View { $this->authorize('view', $registration); $registration->load(['contest', 'contest.range', 'division']); return view('registration.show', [ 'registration' => $registration ]); } /** Store a newly created registration in storage. */ public function store(RegistrationUpdateRequest $request): RedirectResponse { $this->authorize('create', Registration::class); $regData = new Registration; $this->fillRegistrationData($regData, $request); $regData->canceltoken = Str::orderedUuid(); // if shooter added manually by org, try fill licence number form user's profile if ($request->has('maintain')) { $regData->licence_number = $regData->user->licence_number ?? ""; $regData->lexmember = !is_null($regData->user->lex_hash); } $squad = $regData->squad; if ($squad > 0) { // non R squad, check squad capacity $contest = Contest::where('id', $regData->contest_id)->with('registrations')->first(); // if squad is full, only organizer or admin can save if ($contest->registrations->where('squad', $squad)->count() >= $contest->squadSize[$squad] && !Auth::user()->is_organizer_member($contest->organizer_group_id) && !Auth::user()->is_admin ) { return back()->withErrors(['squad' => __('validation.custom.squad.capacity')])->withInput(); } } // if any entered value was not stored to the user profile, do so. $user = $regData->user; if ($request->has('lex_hash') && empty($user->lex_hash)) { $user->lex_hash = $request->input('lex_hash'); } if ($request->has('licence_number') && $user->licence_number !== $request->input('licence_number')) { $user->licence_number = $request->input('licence_number'); } if ($user->isDirty()) { $user->save(); } $regData->save(); Log::info("User {$regData->user->username} registered to contest by {$request->user()->username}", [ 'contest' => (int)$regData->contest_id, 'registration' => $regData->id, 'user' => $regData->user->id, 'org' => $request->user()->id ]); Mail::to($regData->user->email)->send(new RegistrationCompletedMail($regData)); return $request->has('maintain') ? redirect(route('contest.maintain', [$regData->contest_id])) : redirect(route('registration.show', $regData->id)); } /** Show the form for editing the specified resource. */ public function edit(Registration $registration): View { $this->authorize('update', $registration); // we eill force NotCompeting flag if there is already competing registration and it's not this one. $forcenotcompete = $registration->notcomp && $registration->contest->registrations->where('user_id', Auth::user()->id)->where('notcomp', 0)->count() > 0; return view('registration.edit', [ 'registration' => $registration, 'contest' => $registration->contest, 'forcenotcompete' => !Auth::user()->is_admin && $forcenotcompete ]); } /** Update the specified registration in storage. */ public function update(RegistrationUpdateRequest $request, Registration $registration): RedirectResponse { $this->authorize('update', $registration); $this->fillRegistrationData($registration, $request); $registration->save(); $user = $registration->user; if ($request->has('lex_hash') && empty($registration->user->lex_hash)) { $user->lex_hash = $request->input('lex_hash'); } if ($request->has('licence_number') && $user->licence_number !== $request->input('licence_number')) { $user->licence_number = $request->input('licence_number'); } if ($user->isDirty()) { $user->save(); } Log::info("User " . Auth::user()->username . " updated contest registration", [ 'contest' => (int)$registration->contest_id, 'registration' => $registration->id, 'shooter' => $registration->user->id, 'changed' => $registration->getChanges() ]); Mail::to($registration->user->email)->send(new RegistrationCompletedMail($registration)); return $request->has('maintain') ? redirect(route('contest.maintain', [$registration->contest_id])) : redirect(route('registration.show', $registration->id)); } /* Remove the specified registration from storage. */ public function destroy(Request $request, Registration $registration): RedirectResponse { $this->authorize('delete', $registration); $contestid = $registration->contest->id; Log::info("Registration to contest for {$registration->user->username} deleted by {$request->user()->username}.", [ 'contest' => (int)$contestid, 'registration' => $registration->id, 'user' => $registration->user->id, 'org' => $request->user()->id ]); if ($request->canceltoken == $registration->canceltoken) { $registration->delete(); } Mail::to($registration->user->email)->send(new RegistrationDeletedMail($registration, $request->has('maintain') ? RegistrationDeletionReason::MAINTAIN : RegistrationDeletionReason::USER)); return $request->has('maintain') ? back() : redirect(route('contest.show', $contestid)); } /* Switches single registration flags back and forth */ public function switch(Registration $registration, string $action): RedirectResponse { $this->authorize('maintain', $registration); switch ($action) { case "paid": $registration->paid = !$registration->paid; Mail::to($registration->user->email)->send(new RegistrationPaymentMail($registration)); break; case "present": $registration->present = !$registration->present; break; default: break; } $registration->save(); $logmsg = "User " . Auth::user()->username . " set registration {$registration->id} ({$registration->user->username}) as " . ($registration->{$action} ? "" : "NOT ") . $action; Log::info($logmsg, [ 'user' => $registration->user_id, 'org' => Auth::user()->id, 'registration' => $registration->id, 'contest' => (int)$registration->contest_id ]); return back(); } private function fillRegistrationData(Registration $regData, RegistrationUpdateRequest $request): void { $regData->fill($request->validated()); $regData->notcomp = $request->boolean('notcomp'); $regData->lexmember = $request->boolean('lexmember'); $regData->rookie = $request->boolean('rookie'); $regData->builder = $request->boolean('builder'); $regData->referee = $request->boolean('referee'); } }