From 6cda1fe183d614a5e90db25c11bf4ce6721e8f58 Mon Sep 17 00:00:00 2001 From: Miguel N Date: Tue, 20 Jul 2021 10:32:43 +0100 Subject: [PATCH] More refactoring Refactored some old code and added missing translation calls. --- app/Http/Controllers/ApiKeyController.php | 10 +- .../Controllers/ApplicationController.php | 149 +++++------------- .../Controllers/AppointmentController.php | 8 +- app/Http/Controllers/BanController.php | 8 +- app/Http/Controllers/CommentController.php | 6 +- app/Http/Controllers/UserController.php | 12 +- app/Http/Controllers/VoteController.php | 2 +- 7 files changed, 61 insertions(+), 134 deletions(-) diff --git a/app/Http/Controllers/ApiKeyController.php b/app/Http/Controllers/ApiKeyController.php index 86d942c..f725ec4 100644 --- a/app/Http/Controllers/ApiKeyController.php +++ b/app/Http/Controllers/ApiKeyController.php @@ -42,7 +42,7 @@ class ApiKeyController extends Controller if ($key) { - $request->session()->flash('success', 'Key successfully registered!'); + $request->session()->flash('success', __('Key successfully registered!')); $request->session()->flash('finalKey', $discriminator . '.' . $secret); return redirect() @@ -51,7 +51,7 @@ class ApiKeyController extends Controller return redirect() ->back() - ->with('error', 'An error occurred whilst trying to create an API key.'); + ->with('error', __('An error occurred whilst trying to create an API key.')); } @@ -68,12 +68,12 @@ class ApiKeyController extends Controller { return redirect() ->back() - ->with('error', 'Key already revoked.'); + ->with('error', __('Key already revoked.')); } return redirect() ->back() - ->with('success', 'Key revoked. Apps using this key will stop working.'); + ->with('success', __('Key revoked. Apps using this key will stop working.')); } @@ -89,7 +89,7 @@ class ApiKeyController extends Controller return redirect() ->back() - ->with('success', 'Key deleted successfully. Apps using this key will stop working.'); + ->with('success', __('Key deleted successfully. Apps using this key will stop working.')); } } diff --git a/app/Http/Controllers/ApplicationController.php b/app/Http/Controllers/ApplicationController.php index 7f52e72..1ec3400 100755 --- a/app/Http/Controllers/ApplicationController.php +++ b/app/Http/Controllers/ApplicationController.php @@ -24,7 +24,6 @@ namespace App\Http\Controllers; use App\Application; use App\Events\ApplicationDeniedEvent; use App\Facades\JSON; -use App\Http\Resources\ApplicationResource; use App\Notifications\ApplicationMoved; use App\Notifications\NewApplicant; use App\Response; @@ -49,7 +48,7 @@ class ApplicationController extends Controller } } - return ($allvotes->count() == 1) ? false : true; + return !(($allvotes->count() == 1)); } public function showUserApps() @@ -60,51 +59,34 @@ class ApplicationController extends Controller public function showUserApp(Request $request, Application $application) { - if (!$request->wantsJson()) { - $this->authorize('view', $application); + $this->authorize('view', $application); - if (!is_null($application)) { - return view('dashboard.user.viewapp') - ->with( - [ - 'application' => $application, - 'comments' => $application->comments, - 'structuredResponses' => json_decode($application->response->responseData, true), - 'formStructure' => $application->response->form, - 'vacancy' => $application->response->vacancy, - 'canVote' => $this->canVote($application->votes), - ] - ); - } else { - $request->session()->flash('error', 'The application you requested could not be found.'); - } - - return redirect()->back(); + if (!is_null($application)) { + return view('dashboard.user.viewapp') + ->with( + [ + 'application' => $application, + 'comments' => $application->comments, + 'structuredResponses' => json_decode($application->response->responseData, true), + 'formStructure' => $application->response->form, + 'vacancy' => $application->response->vacancy, + 'canVote' => $this->canVote($application->votes), + ] + ); + } else { + $request->session()->flash('error', __('The application you requested could not be found.')); } - return (new ApplicationResource($application))->additional([ - 'meta' => [ - 'code' => 200, - 'status' => 'success' - ] - ]); + return redirect()->back(); + } public function showAllApps(Request $request) { - if (!$request->wantsJson()) { - $this->authorize('viewAny', Application::class); + $this->authorize('viewAny', Application::class); - return view('dashboard.appmanagement.all') - ->with('applications', Application::paginate(6)); - } + return view('dashboard.appmanagement.all'); - - // todo: eager load all relationships used - return ApplicationResource::collection(Application::paginate(6))->additional([ - 'code' => '200', - 'status' => 'success', - ]); } @@ -121,7 +103,7 @@ class ApplicationController extends Controller 'preprocessedForm' => json_decode($vacancyWithForm->first()->forms->formStructure, true), ]); } else { - abort(404, 'The application you\'re looking for could not be found or it is currently unavailable.'); + abort(404, __('The application you\'re looking for could not be found or it is currently unavailable.')); } } @@ -130,38 +112,24 @@ class ApplicationController extends Controller $vacancy = Vacancy::with('forms')->where('vacancySlug', $vacancySlug)->get(); if ($vacancy->isEmpty()) { - if (!$request->wantsJson()) { - return redirect() - ->back() - ->with('error', 'This vacancy doesn\'t exist; Please use the proper buttons to apply to one.'); - } - return JSON::setResponseType('error') - ->setStatus('error') - ->setMessage('Can\'t apply to non-existent application!') - ->setCode(404) - ->build(); + + return redirect() + ->back() + ->with('error', __('This vacancy doesn\'t exist; Please use the proper buttons to apply to one.')); } if ($vacancy->first()->vacancyCount == 0 || $vacancy->first()->vacancyStatus !== 'OPEN') { - if ($request->wantsJson()) { - return JSON::setResponseType('error') - ->setStatus('error') - ->setMessage('This application is unavailable.') - ->setCode(404) - ->build(); - } - return redirect() ->back() - ->with('error', 'This application is unavailable'); + ->with('error', __('This application is unavailable')); } Log::info('Processing new application!'); $formStructure = json_decode($vacancy->first()->forms->formStructure, true); - $responseValidation = ($request->wantsJson()) ? ContextAwareValidator::getResponseValidator($request->json('payload'), $formStructure) : ContextAwareValidator::getResponseValidator($request->all(), $formStructure); - $applicant = ($request->wantsJson()) ? User::findOrFail($request->json('metadata.submittingUserID')) : Auth::user(); + $responseValidation = ContextAwareValidator::getResponseValidator($request->all(), $formStructure); + $applicant = Auth::user(); // API users may specify ID 1 for an anonymous application, but they'll have to submit contact details for it to become active. // User ID 1 is exempt from application rate limits @@ -197,55 +165,35 @@ class ApplicationController extends Controller } } - if ($request->wantsJson()) { - return JSON::setResponseType('success') - ->setStatus('accepted') - ->setMessage('Application submitted successfully. Please refer to the docs to add contact info if your submission was anonymous.') - ->setCode(201) - ->setAdditional([ - 'links' => [ - 'application-web' => route('showUserApp', ['application' => $application->id]) - ] - ])->build(); - } - - $request->session()->flash('success', 'Thank you for your application! It will be reviewed as soon as possible.'); + $request->session()->flash('success', __('Thank you for your application! It will be reviewed as soon as possible.')); return redirect(route('showUserApps')); - } elseif ($request->wantsJson()) { - return JSON::setResponseType('error') - ->setStatus('validation_error') - ->setMessage('There are one or more errors in this application. Please make sure all fields are present in "payload" according to this application\'s respective form structure; They\'re always required.') - ->setCode(400) - ->build(); } Log::warning('Application form for ' . $applicant->name . ' contained errors, resetting!'); return redirect() ->back() - ->with('error', 'There are one or more errors in your application. Please make sure none of your fields are empty, since they are all required.'); + ->with('error', __('There are one or more errors in your application. Please make sure none of your fields are empty, since they are all required.')); } public function updateApplicationStatus(Request $request, Application $application, $newStatus) { $messageIsError = false; - - if (!$request->wantsJson()) - $this->authorize('update', Application::class); + $this->authorize('update', Application::class); switch ($newStatus) { case 'deny': event(new ApplicationDeniedEvent($application)); - $message = "Application denied successfully."; + $message = __("Application denied successfully."); break; case 'interview': Log::info(' Moved application ID ' . $application->id . 'to interview stage!'); - $message = 'Application moved to interview stage! (:'; + $message = __('Application moved to interview stage!'); $application->setStatus('STAGE_INTERVIEW'); $application->user->notify(new ApplicationMoved()); @@ -253,40 +201,21 @@ class ApplicationController extends Controller break; default: - $message = "There are no suitable statuses to update to."; + $message = __("There are no suitable statuses to update to."); $messageIsError = true; } - if ($request->wantsJson()) - { - return JSON::setResponseType(($messageIsError) ? 'error' : 'success') - ->setStatus(($messageIsError) ? 'error' : 'success') - ->setMessage($message) - ->setCode(($messageIsError) ? 400 : 200) - ->build(); - } - return redirect()->back(); } public function delete(Request $request, Application $application) { - if (!$request->wantsJson()) { - $this->authorize('delete', $application); - $application->delete(); // observers will run, cleaning it up + $this->authorize('delete', $application); + $application->delete(); // observers will run, cleaning it up - return redirect() - ->back() - ->with('success', 'Application deleted. Comments, appointments and responses have also been deleted.'); - } + return redirect() + ->back() + ->with('success', __('Application deleted. Comments, appointments and responses have also been deleted.')); - - $application->delete(); - - return JSON::setResponseType('success') - ->setStatus('deleted') - ->setMessage('Application deleted. Relationships were also nuked.') - ->setCode(200) - ->build(); } } diff --git a/app/Http/Controllers/AppointmentController.php b/app/Http/Controllers/AppointmentController.php index 3931995..e1d1858 100755 --- a/app/Http/Controllers/AppointmentController.php +++ b/app/Http/Controllers/AppointmentController.php @@ -62,7 +62,7 @@ class AppointmentController extends Controller ]); $application->user->notify(new AppointmentScheduled($appointment)); - $request->session()->flash('success', 'Appointment successfully scheduled @ '.$appointmentDate->toDateTimeString()); + $request->session()->flash('success', __('Appointment successfully scheduled @ :appointmentTime', ['appointmentTime', $appointmentDate->toDateTimeString()])); return redirect()->back(); } @@ -83,7 +83,7 @@ class AppointmentController extends Controller $application->setStatus('STAGE_PEERAPPROVAL'); $application->user->notify(new ApplicationMoved()); - $request->session()->flash('success', 'Interview finished! Staff members can now vote on it.'); + $request->session()->flash('success', __('Interview finished! Staff members can now vote on it.')); return redirect()->back(); } @@ -97,9 +97,9 @@ class AppointmentController extends Controller $application->appointment->meetingNotes = $request->noteText; $application->appointment->save(); - $request->session()->flash('success', 'Meeting notes have been saved.'); + $request->session()->flash('success', __('Meeting notes have been saved.')); } else { - $request->session()->flash('error', 'There\'s no appointment to save notes to!'); + $request->session()->flash('error', __('There\'s no appointment to save notes to!')); } return redirect()->back(); diff --git a/app/Http/Controllers/BanController.php b/app/Http/Controllers/BanController.php index 7cac9ab..8c8acf4 100755 --- a/app/Http/Controllers/BanController.php +++ b/app/Http/Controllers/BanController.php @@ -34,6 +34,8 @@ class BanController extends Controller { $this->authorize('create', [Ban::class, $user]); + // FIXME: Needs refactoring to a simpler format, e.g. parse the user's given date directly. + if (is_null($user->bans)) { $reason = $request->reason; $duration = strtolower($request->durationOperator); @@ -61,7 +63,7 @@ class BanController extends Controller } } else { // Essentially permanent - $expiryDate->addYears(5); + $expiryDate->addYears(40); } $ban = Ban::create([ @@ -73,9 +75,9 @@ class BanController extends Controller ]); event(new UserBannedEvent($user, $ban)); - $request->session()->flash('success', 'User suspended successfully! Ban ID: #'.$ban->id); + $request->session()->flash('success', __('Account suspended. Suspension ID #:susId', ['susId', $ban->id])); } else { - $request->session()->flash('error', 'User already suspended!'); + $request->session()->flash('error', __('Account already suspended!')); } return redirect()->back(); diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php index b24ff46..ca2e9c6 100755 --- a/app/Http/Controllers/CommentController.php +++ b/app/Http/Controllers/CommentController.php @@ -29,10 +29,6 @@ use Illuminate\Support\Facades\Auth; class CommentController extends Controller { - public function index() - { - // - } public function insert(NewCommentRequest $request, Application $application) { @@ -45,7 +41,7 @@ class CommentController extends Controller ]); if ($comment) { - $request->session()->flash('success', __('Comment posted! (:')); + $request->session()->flash('success', __('Comment posted!')); } else { $request->session()->flash('error', __('Something went wrong while posting your comment!')); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 4f2bf05..0bba54a 100755 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -110,7 +110,7 @@ class UserController extends Controller ->get(); if (! $matchingUsers->isEmpty()) { - $request->session()->flash('success', 'There were '.$matchingUsers->count().' user(s) matching your search.'); + $request->session()->flash('success', __('There were :usersCount user(s) matching your search.', ['usersCount' => $matchingUsers->count()])); return view('dashboard.administration.players') ->with([ @@ -118,7 +118,7 @@ class UserController extends Controller 'bannedUserCount' => Ban::all()->count(), ]); } else { - $request->session()->flash('error', 'Your search term did not return any results.'); + $request->session()->flash('error', __('Your search term did not return any results.')); return redirect(route('registeredPlayerList')); } @@ -161,7 +161,7 @@ class UserController extends Controller 'timestamp' => now(), ]); - $request->session()->flash('success', 'Successfully logged out other devices. Remember to change your password if you think you\'ve been compromised.'); + $request->session()->flash('success', __('Successfully logged out other devices. Remember to change your password if you think you\'ve been compromised.')); return redirect()->back(); } @@ -236,8 +236,8 @@ class UserController extends Controller $user->uuid = $request->uuid; $existingRoles = Role::all() - ->pluck('name') - ->all(); + ->pluck('name') + ->all(); $roleDiff = array_diff($existingRoles, $request->roles); @@ -255,7 +255,7 @@ class UserController extends Controller } $user->save(); - $request->session()->flash('success', 'User updated successfully!'); + $request->session()->flash('success', __('User updated successfully!')); return redirect()->back(); } diff --git a/app/Http/Controllers/VoteController.php b/app/Http/Controllers/VoteController.php index 30eb2ea..e2d3bf2 100755 --- a/app/Http/Controllers/VoteController.php +++ b/app/Http/Controllers/VoteController.php @@ -42,7 +42,7 @@ class VoteController extends Controller Log::info('User '.Auth::user()->name.' has voted in applicant '.$application->user->name.'\'s application', [ 'voteType' => $voteRequest->voteType, ]); - $voteRequest->session()->flash('success', 'Your vote has been registered!'); + $voteRequest->session()->flash('success', __('Your vote has been counted!')); // Cron job will run command that processes votes return redirect()->back();