From 9e0f449bd355b60ba7a0c58206d59514961b7ae3 Mon Sep 17 00:00:00 2001 From: frederikpyt Date: Fri, 14 Aug 2020 07:36:42 +0200 Subject: [PATCH] v0.9.13 - Added notifications in events, when an event you're signed up to gets canceled. --- .../app/Http/Controllers/EventController.php | 27 ++++++++++----- skolehjem/app/Notification.php | 12 +++++++ ...2020_08_13_111844_create_notifications.php | 33 +++++++++++++++++++ skolehjem/resources/lang/dk/msg.php | 3 +- skolehjem/resources/lang/en/msg.php | 3 +- .../views/app/events/index.blade.php | 29 +++++++++++++++- .../views/app/events/signups.blade.php | 2 +- skolehjem/routes/web.php | 2 +- 8 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 skolehjem/app/Notification.php create mode 100644 skolehjem/database/migrations/2020_08_13_111844_create_notifications.php diff --git a/skolehjem/app/Http/Controllers/EventController.php b/skolehjem/app/Http/Controllers/EventController.php index 448565d..38112ce 100644 --- a/skolehjem/app/Http/Controllers/EventController.php +++ b/skolehjem/app/Http/Controllers/EventController.php @@ -210,24 +210,24 @@ class EventController extends Controller public function destroy(Request $request, $id) { if ($request->signup != null) { // If input signup is not empty, which has been set when you look at the individuel signup, then delete the user who have signed up for the event - $UserEvent = UserEvent::where('user_id', $request->signup)->where('event_id', $id); + $UserEvent = UserEvent::query()->where('user_id', "=", $request->signup)->where('event_id', "=", $id); + $UserEvent->delete(); + return redirect()->route("events.signups", [ "event" => $id ]); } else { // Else if you are deleting an event. Then delete all the sign ups AND the event - $UserEvents = UserEvent::where('event_id', $id); - $event = Event::find($id); + $userEvents = UserEvent::query()->where('event_id', "=", $id)->get(); + $event = Event::query()->find($id); - foreach ($UserEvents as $userEvent) { + foreach ($userEvents as $userEvent) { $notification = new Notification(); $notification->user_id = $userEvent->user_id; $notification->message = $event->name . " - "; + $notification->save(); + $userEvent->delete(); } - - - - $UserEvents->delete(); $event->delete(); return redirect()->route("events.index"); @@ -298,6 +298,17 @@ class EventController extends Controller } return $html; } + + public function deleteNotifications(Request $request){ + if($request->ajax()){ + foreach (Notification::query()->where("user_id", "=", $request->user_id)->get() as $notification) { + $notification->delete(); + } + return "Done"; + } else { + return "ERROR"; + } + } } diff --git a/skolehjem/app/Notification.php b/skolehjem/app/Notification.php new file mode 100644 index 0000000..7f38d09 --- /dev/null +++ b/skolehjem/app/Notification.php @@ -0,0 +1,12 @@ +id(); + $table->foreignId("user_id")->constrained("users", "id"); + $table->string("message"); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('notifications'); + } +} diff --git a/skolehjem/resources/lang/dk/msg.php b/skolehjem/resources/lang/dk/msg.php index 448b660..964b59c 100644 --- a/skolehjem/resources/lang/dk/msg.php +++ b/skolehjem/resources/lang/dk/msg.php @@ -58,7 +58,8 @@ return[ "af" => "Af", "programmedby" => "Programmeret af", "error" => "Fejl", - "404" => "Denne side findes ikke." + "404" => "Denne side findes ikke.", + "canceled" => "Aflyst" diff --git a/skolehjem/resources/lang/en/msg.php b/skolehjem/resources/lang/en/msg.php index 63e7677..0aa43e8 100644 --- a/skolehjem/resources/lang/en/msg.php +++ b/skolehjem/resources/lang/en/msg.php @@ -67,5 +67,6 @@ return[ "af" => "By", "programmedby" => "Programmed by", "error" => "Error", - "404" => "This page doesn't exist." + "404" => "This page doesn't exist.", + "canceled" => "Canceled" ]; diff --git a/skolehjem/resources/views/app/events/index.blade.php b/skolehjem/resources/views/app/events/index.blade.php index 8041a1d..c41177e 100644 --- a/skolehjem/resources/views/app/events/index.blade.php +++ b/skolehjem/resources/views/app/events/index.blade.php @@ -34,6 +34,18 @@

{{ __('msg.aktiviteter') }}

+ + @if(count(\App\Notification::query()->where("user_id", "=", auth()->user()->id)->get()) > 0) +
+
+ + @foreach(\App\Notification::query()->where("user_id", "=", auth()->user()->id)->get() as $notification) +

{{ $notification->message }}{{ __("msg.canceled") }}

+ @endforeach +
+
+ @endif + @if(!$events->isEmpty()) @foreach($events as $event)
@@ -59,7 +71,7 @@ @if (count(\App\UserEvent::query()->where('event_id', '=', $event->id)->where('user_id', '=', Auth::user()->id)->get()) > 0) {{__('msg.afmeld')}} @else {{-- ^ If you're already participating in the event, then show a ´cancel´ button - v Else show a ´participate´ button --}} - {{__('msg.tilmeld')}} + {{__('msg.tilmeld')}} @endif $event->id ])}}">{{__('msg.læsmere')}} $event->id ])}}">{{__('msg.sedeltagere')}} @@ -115,6 +127,21 @@ }); } } + + function deleteNotifications(el) { + el.remove(); + } + + window.onload = function () { + setMain(); + axios({ + method: 'delete', + url: '{{route("notifications.delete")}}', + data: { + user_id: {{ auth()->user()->id }} + } + }); + }; @endsection diff --git a/skolehjem/resources/views/app/events/signups.blade.php b/skolehjem/resources/views/app/events/signups.blade.php index 107edb8..e381071 100644 --- a/skolehjem/resources/views/app/events/signups.blade.php +++ b/skolehjem/resources/views/app/events/signups.blade.php @@ -67,6 +67,6 @@ @endif @endforeach - +
@endsection diff --git a/skolehjem/routes/web.php b/skolehjem/routes/web.php index 45887b8..907a37d 100644 --- a/skolehjem/routes/web.php +++ b/skolehjem/routes/web.php @@ -35,7 +35,7 @@ Route::get("/settings", "SettingsController@index")->name("settings.index"); Route::post("/events/signup", "UserEventController@createajax")->name("userevents.createajax"); Route::get("/about", "AboutController@index")->name("about.index"); Route::post("/events/cancelsignup", "UserEventController@createajaxcancel")->name("userevents.createajaxcancel"); - +Route::delete("/notifications/delete", "EventController@deleteNotifications")->name("notifications.delete"); //Search/Filter Route::get("/contactsapi", "ContactController@search")->name("contacts.search");