Ekapp/skolehjem/app/Http/Controllers/UserEventController.php

229 lines
10 KiB
PHP
Raw Normal View History

2020-07-28 06:44:25 +00:00
<?php
namespace App\Http\Controllers;
use App\UserEvent;
use Illuminate\Database\Eloquent\Model;
2020-07-28 06:44:25 +00:00
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class UserEventController extends Controller
{
2020-08-06 13:31:38 +00:00
public function __construct()
{
$this->middleware(["auth"]);
2020-08-10 07:18:00 +00:00
$this->middleware(["lang"]);
2020-09-02 09:40:36 +00:00
$this->middleware([ "check.auth:userevent.create" ])->only("create");
$this->middleware([ "check.auth:userevent.delete" ])->only("destroy");
2020-08-06 13:31:38 +00:00
}
/**
* Update the specified resource in storage.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
2020-08-07 08:25:46 +00:00
public function index(Request $request)
{
2021-05-04 06:15:21 +00:00
$userevents = [];
$userevents['events'] = UserEvent::join('events', 'events.id', '=', 'user_events.event_id')->get();
$userevents['multiple'] = UserEvent::join('multiple_events', 'multiple_events.id', '=', 'user_events.multiple_event_id')->get();
2020-07-28 06:44:25 +00:00
return Response::detect("events.yourevents", [ "userevents" => $userevents ]);
}
2020-07-28 06:44:25 +00:00
public function store() {
}
2020-07-28 06:44:25 +00:00
/**
* Update the specified resource in storage.
*
* @param UserEvent $eventid
2020-07-28 06:44:25 +00:00
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function create(Request $request)
2020-07-28 06:44:25 +00:00
{
// Get written data from events.index
$data = $request->validate([
"event_id" => "required|max:255"
]);
2020-07-28 06:44:25 +00:00
// Check the UserEvent table if there is a row that has the user_id AND the event_id
$getTableInfo = UserEvent::where('user_id', auth()->user()->id)->where('event_id', $request->event_id)->get();
// If the row has both, then go back and show an error - Cause you're not allowed to be on the same event twice.
if (count($getTableInfo) > 0) {
if (request()->cookie('languagesSetting') == "dk")
return redirect()->route("events.index")->with('error#' . $request->event_id, '<p class="text-center"><b>Du har allerede tilmeldt dig denne Aktivitet!</b></p>');
if (request()->cookie('languagesSetting') == "en")
return redirect()->route("events.index")->with('error#' . $request->event_id, '<p class="text-center"><b>You are already participating in this event!</b></p>');
return redirect()->route("events.index")->with('error#' . $request->event_id, '<p class="text-center"><b>Du har allerede tilmeldt dig denne Aktivitet!</b></p>');
}
// If not, then it keeps going and saves and shows a success message
$UserEvent = new UserEvent($data);
$UserEvent->user_id = auth()->user()->id;
$UserEvent->save();
if (request()->cookie('languagesSetting') == "dk")
return redirect()->route("events.index")->with('signup#' . $request->event_id, '<p class="text-center"><b>Du er hermed tilmeldt denne aktivitet!</b></p>');
if (request()->cookie('languagesSetting') == "en")
return redirect()->route("events.index")->with('signup#' . $request->event_id, '<p class="text-center"><b>You are now participating in this event!</b></p>');
return redirect()->route("events.index")->with('signup#' . $request->event_id, '<p class="text-center"><b>Du er hermed tilmeldt denne aktivitet!</b></p>');
2020-07-28 06:44:25 +00:00
}
/**
* Update the specified resource in storage.
*
* @param UserEvent $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function destroy($id)
{
// Check the UserEvent table if there is a row that has the user_id AND the event_id
$UserEvent = UserEvent::query()->where('user_id', "=", auth()->user()->id)->where('event_id', "=", $id);
$UserEvent->delete();
$JoinedEvents = UserEvent::query()->join('events', 'events.id', '=', 'user_events.event_id')->where('user_id', "=", auth()->user()->id)->get();
if (request()->cookie('languagesSetting') == "dk")
return redirect()->route("userevents.index", [ "userevents" => $JoinedEvents ])->with('eventunsubscribed', '<p class="text-center"><b>Du er hermed afmeldt aktiviteten!</b></p>');
if (request()->cookie('languagesSetting') == "en")
return redirect()->route("userevents.index", [ "userevents" => $JoinedEvents ])->with('eventunsubscribed', '<p class="text-center"><b>You are no longer participating in this event!</b></p>');
return redirect()->route("userevents.index", [ "userevents" => $JoinedEvents ])->with('eventunsubscribed', '<p class="text-center"><b>Du er hermed afmeldt aktiviteten!</b></p>');
}
public function show()
{
}
public function edit()
{
}
public function update()
{
}
2020-08-11 13:08:09 +00:00
//Signs a user up to an event via ajax
2020-08-11 13:08:09 +00:00
public function createajax(Request $request)
{
// Get written data from events.index
$data = $request->validate([
"event_id" => "required|max:255"
]);
// Check the UserEvent table if there is a row that has the user_id AND the event_id
$getTableInfo = UserEvent::where('user_id', auth()->user()->id)
->where('event_id', $request->event_id)->get();
// If the row has both, then go back and show an error - Cause you're not allowed to be on the same event twice.
if (count($getTableInfo) > 0) {
if (request()->cookie('languagesSetting') == "dk")
return '<p class="text-center"><b>Du har allerede tilmeldt dig denne Aktivitet!</b></p>';
if (request()->cookie('languagesSetting') == "en")
return '<p class="text-center"><b>You are already participating in this event!</b></p>';
return '<p class="text-center"><b>Du har allerede tilmeldt dig denne Aktivitet!</b></p>';
}
// If not, then it keeps going and saves and shows a success message
$UserEvent = new UserEvent();
$UserEvent->event_id = $request->event_id;
$UserEvent->user_id = auth()->user()->id;
$UserEvent->save();
if (request()->cookie('languagesSetting') == "dk")
return '<p class="text-center"><b>Du er hermed tilmeldt denne aktivitet!</b></p>';
if (request()->cookie('languagesSetting') == "en")
return '<p class="text-center"><b>You are now participating in this event!</b></p>';
return '<p class="text-center"><b>Du er hermed tilmeldt denne aktivitet!</b></p>';
}
//Signs a user up to a multiple event via ajax
public function createmultiajax(Request $request)
{
// Get written data from events.index
$data = $request->validate([
"multiple_event_id" => "required|max:255"
]);
// Check the UserEvent table if there is a row that has the user_id AND the event_id
$getTableInfo = UserEvent::where('user_id', auth()->user()->id)
2021-05-04 06:15:21 +00:00
->where('multiple_event_id', $request->multiple_event_id)->get();
// If the row has both, then go back and show an error - Cause you're not allowed to be on the same event twice.
if (count($getTableInfo) > 0) {
if (request()->cookie('languagesSetting') == "dk")
return '<p class="text-center"><b>Du har allerede tilmeldt dig denne Aktivitet!</b></p>';
if (request()->cookie('languagesSetting') == "en")
return '<p class="text-center"><b>You are already participating in this event!</b></p>';
return '<p class="text-center"><b>Du har allerede tilmeldt dig denne Aktivitet!</b></p>';
}
// If not, then it keeps going and saves and shows a success message
$UserEvent = new UserEvent();
$UserEvent->multiple_event_id = $request->multiple_event_id;
2020-08-11 13:08:09 +00:00
$UserEvent->user_id = auth()->user()->id;
$UserEvent->save();
if (request()->cookie('languagesSetting') == "dk")
return '<p class="text-center"><b>Du er hermed tilmeldt denne aktivitet!</b></p>';
if (request()->cookie('languagesSetting') == "en")
return '<p class="text-center"><b>You are now participating in this event!</b></p>';
return '<p class="text-center"><b>Du er hermed tilmeldt denne aktivitet!</b></p>';
}
//Removes a users participation in an event via ajax
public function createajaxcancel(Request $request)
{
// Check the UserEvent table if there is a row that has the user_id AND the event_id
$UserEvent = UserEvent::where('user_id', auth()->user()->id)
->where('event_id', $request->event_id);
// If you are in the Event, then remove yourself.
if (count($UserEvent->get()) > 0) {
// If not, then it keeps going and saves and shows a success message
$UserEvent->delete();
if (request()->cookie('languagesSetting') == "dk")
return '<p class="text-center"><b>Du er hermed afmeldt aktiviteten!</b></p>';
if (request()->cookie('languagesSetting') == "en")
return '<p class="text-center"><b>You are no longer participating in this event!</b></p>';
return '<p class="text-center"><b>Du er hermed afmeldt aktiviteten!</b></p>';
}
}
//Removes a users participation in an event via ajax
public function createmultiajaxcancel(Request $request)
{
// Check the UserEvent table if there is a row that has the user_id AND the event_id
$UserEvent = UserEvent::where('user_id', auth()->user()->id)
->where('multiple_event_id', $request->multiple_event_id);
// If you are in the Event, then remove yourself.
if (count($UserEvent->get()) > 0) {
// If not, then it keeps going and saves and shows a success message
$UserEvent->delete();
if (request()->cookie('languagesSetting') == "dk")
return '<p class="text-center"><b>Du er hermed afmeldt aktiviteten!</b></p>';
if (request()->cookie('languagesSetting') == "en")
return '<p class="text-center"><b>You are no longer participating in this event!</b></p>';
return '<p class="text-center"><b>Du er hermed afmeldt aktiviteten!</b></p>';
}
}
2020-07-28 06:44:25 +00:00
}