neayi /
insights
| 1 | <?php |
||
| 2 | |||
| 3 | |||
| 4 | namespace App\Http\Controllers; |
||
| 5 | |||
| 6 | use App\Http\Common\Form\OrganizationForm; |
||
| 7 | use App\Src\UseCases\Domain\Organizations\CreateOrganization; |
||
| 8 | use App\Src\UseCases\Domain\Organizations\EditOrganization; |
||
| 9 | use App\Src\UseCases\Domain\Organizations\GetOrganization; |
||
| 10 | use App\Src\UseCases\Domain\Organizations\Invitation\AttachUserToAnOrganization; |
||
| 11 | use App\Src\UseCases\Domain\Organizations\Invitation\InviteUsersInOrganization; |
||
| 12 | use App\Src\UseCases\Domain\Organizations\Invitation\PrepareInvitationUsersInOrganization; |
||
| 13 | use App\Src\UseCases\Domain\Organizations\Invitation\RespondInvitationToAnOrganization; |
||
| 14 | use App\Src\UseCases\Domain\Organizations\ListOrganizations; |
||
| 15 | use Illuminate\Http\Request; |
||
| 16 | use Illuminate\Support\Facades\Auth; |
||
| 17 | |||
| 18 | class OrganizationsController extends Controller |
||
| 19 | { |
||
| 20 | public function showAddForm() |
||
| 21 | { |
||
| 22 | return view('organizations/add_form'); |
||
| 23 | } |
||
| 24 | |||
| 25 | public function processAdd(Request $request, CreateOrganization $createOrganization, OrganizationForm $form) |
||
| 26 | { |
||
| 27 | list($name, $address, $picture) = $form->process(); |
||
| 28 | $createOrganization->create($name, $picture, $address); |
||
| 29 | return redirect()->route('organization.list'); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function showEditForm(string $organisationId, GetOrganization $getOrganization) |
||
| 33 | { |
||
| 34 | $organization = $getOrganization->get($organisationId); |
||
| 35 | return view('organizations/edit_form', [ |
||
| 36 | 'organization' => $organization->toArray() |
||
| 37 | ]); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function processEdit(string $organizationId, Request $request, EditOrganization $editOrganization, OrganizationForm $form) |
||
| 41 | { |
||
| 42 | list($name, $address, $picture) = $form->process(); |
||
| 43 | $editOrganization->edit($organizationId, $name, $picture, $address); |
||
| 44 | $request->session()->flash('notif_msg', __('organizations.message.organization.updated')); |
||
| 45 | return redirect()->back(); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function list() |
||
| 49 | { |
||
| 50 | return view('organizations/list'); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function listOrganizations(Request $request, ListOrganizations $listOrganizations) |
||
| 54 | { |
||
| 55 | $page = $request->input('start')/10 + 1; |
||
| 56 | $organizations = $listOrganizations->list($page, 10); |
||
| 57 | $total = isset($organizations['total']) ? $organizations['total'] : 0; |
||
| 58 | $list = []; |
||
| 59 | foreach ($organizations['list'] as $organization){ |
||
| 60 | $org = $organization->toArray(); |
||
| 61 | $list[] = [ |
||
| 62 | $org['name'], |
||
| 63 | $org['url_picture'], |
||
| 64 | '', |
||
| 65 | $org['uuid'], |
||
| 66 | route('organization.edit.form', ['id' => $org['uuid']]) |
||
| 67 | ]; |
||
| 68 | } |
||
| 69 | |||
| 70 | return format($total, $list); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function prepareInvitation(Request $request, PrepareInvitationUsersInOrganization $prepareInvitationUsersInOrganization) |
||
| 74 | { |
||
| 75 | $users = $request->input('users'); |
||
| 76 | $users = explode(PHP_EOL, $users); |
||
| 77 | $organizationId = $request->input('organization_id'); |
||
| 78 | |||
| 79 | $usersToProcess = $prepareInvitationUsersInOrganization->prepare($organizationId, $users); |
||
| 80 | |||
| 81 | return view('organizations.prepare-invitation', [ |
||
| 82 | 'organization_id' => $organizationId, |
||
| 83 | 'usersToProcess' => $usersToProcess |
||
| 84 | ]); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function sendInvitations(Request $request, InviteUsersInOrganization $inviteUsersInOrganization) |
||
| 88 | { |
||
| 89 | $users = json_decode($request->input('users'), true); |
||
| 90 | $organizationId = $request->input('organization_id'); |
||
| 91 | |||
| 92 | $inviteUsersInOrganization->invite($organizationId, $users['users']); |
||
| 93 | $request->session()->flash('notif_msg', __('organizations.message.organization.invitation_send')); |
||
| 94 | return redirect()->route('organization.list'); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function acceptInvite(Request $request, RespondInvitationToAnOrganization $respondInvitationToAnOrganization) |
||
| 98 | { |
||
| 99 | $action = $respondInvitationToAnOrganization->respond($token = $request->input('token')); |
||
| 100 | switch($action['action']){ |
||
| 101 | case 'register': |
||
| 102 | return $this->redirectToRegisterPage($request, $action); |
||
| 103 | case 'accept_or_decline': |
||
| 104 | return $this->showAcceptOrDeclinePage($request, $action); |
||
| 105 | case 'logout-login': |
||
| 106 | return $this->showLogoutToAcceptInvitation($request, $action, $token); |
||
| 107 | case 'login': |
||
| 108 | return $this->redirectToLogin($request, $action, $token); |
||
| 109 | case 'logout-register': |
||
| 110 | return $this->showLogoutToRegister($request, $action, $token); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | public function joinOrganization(Request $request, AttachUserToAnOrganization $attachUserToAnOrganization) |
||
| 115 | { |
||
| 116 | $userId = Auth::user()->uuid; |
||
| 117 | $organizationId = $request->session()->get('should_attach_to_organization'); |
||
| 118 | $attachUserToAnOrganization->attach($userId, $organizationId); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 119 | $request->session()->flash('notif_msg', __('organizations.message.organization.joined')); |
||
| 120 | return redirect()->route('home'); |
||
| 121 | } |
||
| 122 | |||
| 123 | private function redirectToRegisterPage(Request $request, array $action): \Illuminate\Http\RedirectResponse |
||
| 124 | { |
||
| 125 | $request->session()->flash('should_attach_to_organization', $action['organization_id']); |
||
| 126 | $request->session()->flash('user_to_register', $action['user']); |
||
| 127 | return redirect()->route('register'); |
||
| 128 | } |
||
| 129 | |||
| 130 | private function showAcceptOrDeclinePage(Request $request, array $action) |
||
| 131 | { |
||
| 132 | $request->session()->flash('should_attach_to_organization', $action['organization_to_join']->id()); |
||
| 133 | return view('organizations.accept-or-decline-invitation', [ |
||
| 134 | 'old_organisation' => isset($action['old_organisation']) ? $action['old_organisation']->toArray() : null, |
||
| 135 | 'organization_to_join' => isset($action['organization_to_join']) ? $action['organization_to_join']->toArray() : null |
||
| 136 | ]); |
||
| 137 | } |
||
| 138 | |||
| 139 | private function showLogoutToAcceptInvitation(Request $request, array $action, $token) |
||
| 140 | { |
||
| 141 | $request->session()->flash('should_attach_to_organization', $action['organization_to_join']->id()); |
||
| 142 | $request->session()->flash('should_attach_to_organization_token', $token); |
||
| 143 | $request->session()->flash('should_attach_to_organization_redirect', route('login')); |
||
| 144 | return view('organizations.logout-to-accept-or-decline-invitation', [ |
||
| 145 | 'organization_to_join' => isset($action['organization_to_join']) ? $action['organization_to_join']->toArray() : null |
||
| 146 | ]); |
||
| 147 | } |
||
| 148 | |||
| 149 | private function redirectToLogin(Request $request, array $action, $token): \Illuminate\Http\RedirectResponse |
||
| 150 | { |
||
| 151 | $request->session()->flash('should_attach_to_organization', $action['organization_to_join']->id()); |
||
| 152 | $request->session()->flash('should_attach_to_organization_token', $token); |
||
| 153 | $request->session()->flash('should_attach_to_organization_redirect', route('login')); |
||
| 154 | return redirect()->route('login'); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param Request $request |
||
| 159 | * @param array $action |
||
| 160 | * @param $token |
||
| 161 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 162 | */ |
||
| 163 | private function showLogoutToRegister(Request $request, array $action, $token) |
||
| 164 | { |
||
| 165 | $request->session()->flash('should_attach_to_organization', $action['organization_to_join']->id()); |
||
| 166 | $request->session()->flash('should_attach_to_organization_token', $token); |
||
| 167 | $request->session()->flash('should_attach_to_organization_redirect', route('register')); |
||
| 168 | $request->session()->flash('user_to_register', $action['user']); |
||
| 169 | return view('organizations.logout-to-accept-or-decline-invitation', [ |
||
| 170 | 'organization_to_join' => isset($action['organization_to_join']) ? $action['organization_to_join']->toArray() : null |
||
| 171 | ]); |
||
| 172 | } |
||
| 173 | } |
||
| 174 |