|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use App\Enums\ProjectInvolvement; |
| 6 | +use App\Enums\UserContext; |
| 7 | +use App\Models\User; |
| 8 | +use Illuminate\Contracts\View\View; |
| 9 | +use Illuminate\Http\RedirectResponse; |
| 10 | +use Illuminate\Http\Response; |
| 11 | +use Illuminate\Support\Facades\Auth; |
| 12 | + |
| 13 | +class UserEngagementsController extends Controller |
| 14 | +{ |
| 15 | + public function show(): Response|View|RedirectResponse |
| 16 | + { |
| 17 | + $user = Auth::user(); |
| 18 | + |
| 19 | + if ($user->context === UserContext::Organization->value && ! $user->organization) { |
| 20 | + return redirect(localized_route('organizations.show-type-selection')); |
| 21 | + } |
| 22 | + |
| 23 | + if ($this->isParticipant($user)) { |
| 24 | + $section = ProjectInvolvement::Participating->value; |
| 25 | + $activeEngagements = $user->{$user->context}->engagements()->active()->get(); |
| 26 | + $completeEngagements = $user->{$user->context}->engagements()->complete()->get(); |
| 27 | + } elseif ($this->isConnector($user)) { |
| 28 | + $section = ProjectInvolvement::Contracted->value; |
| 29 | + $activeEngagements = $user->{$user->context}->connectingEngagements()->active()->get(); |
| 30 | + $completeEngagements = $user->{$user->context}->connectingEngagements()->complete()->get(); |
| 31 | + } else { |
| 32 | + abort(404); |
| 33 | + } |
| 34 | + |
| 35 | + return view('engagements.joined', [ |
| 36 | + 'section' => $section ?? '', |
| 37 | + 'showParticipating' => $this->isParticipant($user), |
| 38 | + 'showConnecting' => $this->isConnector($user), |
| 39 | + 'activeEngagements' => $activeEngagements ?? [], |
| 40 | + 'completeEngagements' => $completeEngagements ?? [], |
| 41 | + ]); |
| 42 | + } |
| 43 | + |
| 44 | + public function showContracted(): Response|View|RedirectResponse |
| 45 | + { |
| 46 | + $user = Auth::user(); |
| 47 | + |
| 48 | + if ($user->context === UserContext::Organization->value && ! $user->organization) { |
| 49 | + return redirect(localized_route('organizations.show-type-selection')); |
| 50 | + } |
| 51 | + |
| 52 | + if ($this->isConnector($user)) { |
| 53 | + $activeEngagements = $user->{$user->context}->connectingEngagements()->active()->get(); |
| 54 | + $completeEngagements = $user->{$user->context}->connectingEngagements()->complete()->get(); |
| 55 | + |
| 56 | + return view('engagements.joined', [ |
| 57 | + 'title' => __('Engagements I’ve joined as a Community Connector'), |
| 58 | + 'section' => 'contracted', |
| 59 | + 'showParticipating' => $this->isParticipant($user), |
| 60 | + 'showConnecting' => true, |
| 61 | + 'activeEngagements' => $activeEngagements, |
| 62 | + 'completeEngagements' => $completeEngagements, |
| 63 | + ]); |
| 64 | + } |
| 65 | + |
| 66 | + abort(404); |
| 67 | + } |
| 68 | + |
| 69 | + public function showParticipating(): Response|View|RedirectResponse |
| 70 | + { |
| 71 | + $user = Auth::user(); |
| 72 | + |
| 73 | + if ($user->context === UserContext::Organization->value && ! $user->organization) { |
| 74 | + return redirect(localized_route('organizations.show-type-selection')); |
| 75 | + } |
| 76 | + |
| 77 | + if ($this->isParticipant($user)) { |
| 78 | + $activeEngagements = $user->{$user->context}->engagements()->active()->get(); |
| 79 | + $completeEngagements = $user->{$user->context}->engagements()->complete()->get(); |
| 80 | + |
| 81 | + return view('engagements.joined', [ |
| 82 | + 'title' => __('Engagements I’ve joined as a Consultation Participant'), |
| 83 | + 'section' => 'participating', |
| 84 | + 'showParticipating' => true, |
| 85 | + 'showConnecting' => $this->isConnector($user), |
| 86 | + 'activeEngagements' => $activeEngagements, |
| 87 | + 'completeEngagements' => $completeEngagements, |
| 88 | + ]); |
| 89 | + } |
| 90 | + |
| 91 | + abort(404); |
| 92 | + } |
| 93 | + |
| 94 | + public function isParticipant(User $user): bool |
| 95 | + { |
| 96 | + $userContext = $user->{$user->context}; |
| 97 | + |
| 98 | + return $userContext && ($userContext->isParticipant() || $userContext->engagements()->count()); |
| 99 | + } |
| 100 | + |
| 101 | + public function isConnector(User $user): bool |
| 102 | + { |
| 103 | + $userContext = $user->{$user->context}; |
| 104 | + |
| 105 | + return $userContext && ($userContext->isConnector() || $userContext->connectingEngagements()->count()); |
| 106 | + } |
| 107 | +} |
0 commit comments