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

218 lines
8.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2020-06-18 06:01:42 +00:00
use App\Event;
use App\UserEvent;
use App\User;
use Illuminate\Http\Request;
2020-06-24 06:23:15 +00:00
use Illuminate\Http\Response;
2020-07-27 14:03:49 +00:00
use Illuminate\Support\Facades\DB;
2020-06-24 06:23:15 +00:00
class EventController extends Controller
{
2020-06-30 07:21:21 +00:00
public function __construct()
{
$this->middleware([ "auth" ]);
$this->middleware([ "check.auth:event.list" ])->only("index");
$this->middleware([ "check.auth:event.show" ])->only("show");
$this->middleware([ "check.auth:event.create" ])->only("create", "store");
$this->middleware([ "check.auth:event.edit" ])->only("edit", "update");
$this->middleware([ "check.auth:event.delete" ])->only("delete");
}
/**
* Display a listing of the resource..
*
2020-06-18 06:01:42 +00:00
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index(Request $request)
{
2020-07-01 09:29:45 +00:00
$events = Event::query()->orderBY('date' , 'asc')->paginate($request->input("limit", 20));
2020-07-01 09:29:45 +00:00
//returns the function with events index page and a parameter of events.
//also Response::detect checks screen size to determine if user is on a computer or mobile.
return Response::detect("events.index", [ "events" => $events]);
}
/**
* Show the form for creating a new resource.
*
2020-06-18 06:01:42 +00:00
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function create()
{
2020-07-01 09:29:45 +00:00
//returns "create event" blade
return Response::detect("events.create");
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
2020-06-18 06:01:42 +00:00
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function store(Request $request)
{
2020-06-18 06:01:42 +00:00
$requestBody = $request->validate([
"name" => "required|unique:events|max:255",
"description" => "required|max:255",
"date" => "required"
2020-06-18 06:01:42 +00:00
]);
2020-07-01 09:29:45 +00:00
//creates a new Event model with the given parameter
2020-06-18 06:01:42 +00:00
$event = new Event($requestBody);
2020-07-01 07:43:11 +00:00
$saved = $event->save();
if(!$saved){
return Response::detect("events.store");
}else{
$event = Event::query()->paginate($request->input("limit", 20));
return redirect()->route("events.index", ['events' => $event]);
2020-07-01 07:43:11 +00:00
}
2020-06-18 06:01:42 +00:00
}
/**
* Display the specified resource.
*
2020-06-18 06:01:42 +00:00
* @param Event $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
2020-06-18 06:01:42 +00:00
public function show(Event $id)
{
return Response::detect("events.show", [ "event" => $id ]);
}
/**
* Display signups for event.
*
* @param int $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function signups(Request $request)
{
// Find every event you have clicked on. And find all users to that event, and the event name itself.
$events = UserEvent::join('users', 'users.id', '=', 'user_events.user_id')->join('events', 'events.id', '=', 'user_events.event_id')->where('event_id', $request->event)->get();
if (count($events) == 0)
$events = Event::where('id', $request->event)->get();
return Response::detect("events.signups", [ "events" => $events ]);
}
/**
2020-06-09 08:45:16 +00:00
* Show the form for editing the specified resource.-
*
* @param int $id
2020-06-18 06:01:42 +00:00
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
2020-06-30 06:52:11 +00:00
public function edit($id)
{
2020-06-30 06:52:11 +00:00
$event = Event::find($id);
return Response::detect("events.edit", [ "event" => $event ]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
2020-06-19 06:15:24 +00:00
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
2020-06-30 06:52:11 +00:00
public function update(Request $request, $id)
{
2020-06-30 06:52:11 +00:00
$data = $request->all();
2020-06-19 06:15:24 +00:00
2020-06-30 06:52:11 +00:00
$event = Event::find($id);
$event->update($data);
2020-07-01 07:43:11 +00:00
$saved = $event->save();
if(!$saved){
return Response::detect("events.update", [ "event" => $event]);
}else{
$event = Event::query()->paginate($request->input("limit", 20));
return Response::detect("events.index", ['events' => $event]);
}
2020-06-19 06:15:24 +00:00
}
/**
* Remove the specified resource from storage.
*
2020-06-18 06:01:42 +00:00
* @param Event $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Exception
*/
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->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
$UserEvent = UserEvent::where('event_id', $id);
$UserEvent->delete();
$event = Event::find($id);
$event->delete();
return redirect()->route("events.index");
}
}
2020-07-27 14:03:49 +00:00
public function search(Request $request){
if($request->ajax()){
$output = "<tr>".
"<th>Event Navn</th>".
"<th>Event Beskrivelse</th>".
"<th>Event Dato</th>".
2020-07-29 07:49:54 +00:00
"<th style=\"width: 1em;\"><img class=\"w-100\" src=\"http://127.0.0.1:8000/images/icons/eye.svg\" alt=\"Update\"></th>".
2020-07-27 14:03:49 +00:00
"<th style=\"width: 1em;\"><img class=\"w-100\" src=\"http://127.0.0.1:8000/images/icons/pencil.svg\" alt=\"Update\"></th>".
"<th style=\"width: 1em;\"><img class=\"w-100\" src=\"http://127.0.0.1:8000/images/icons/trashcan.svg\" alt=\"Delete\"></th>".
"</tr>";
$events = DB::table('events')->orderBY('date' , 'asc')->where('name', 'LIKE',$request->search.'%')
2020-07-27 14:03:49 +00:00
->orWhere('date','LIKE', $request->search.'%')
2020-07-29 07:49:54 +00:00
->get();//Job is here dude :9
2020-07-27 14:03:49 +00:00
if(count($events) !== 0){
foreach ($events as $key => $event){
$output.='<tr>'.
'<td>' . $event->name . '</td>'.
'<td>' . $event->description .'</td>'.
'<td>' . $event->date .'</td>'.
2020-07-29 07:49:54 +00:00
'<td><a href="'. route("events.signups", [ "event" => $event->id ]).'"><img class="w-100" src="'. asset('/images/icons/eye-dark.svg') .'" alt="Update"></a></td>'.
2020-07-27 14:03:49 +00:00
'<td><a href="'. route("events.edit", [ "event" => $event->id ]) . '"><img class="w-100" src="'. asset('/images/icons/pencil-dark.svg') . '" alt="Update"></a></td>'.
'<td><form method="post" action="' .route("events.destroy", [ "event" => $event->id ]). '" class="w-100 nostyle">'.
csrf_field().
method_field("delete").
'<button class="w-100 nostyle" onclick="return confirm(\'Are you sure you want to delete?\');" type="submit"><img class="w-100 cursor-pointer" src="'. asset('/images/icons/trashcan-dark.svg') . '" alt="Delete"></button>'.
'</form>'.
'</td>'.
'</tr>';
}
2020-07-28 12:23:54 +00:00
}else{
$output.='<tr>'.
'<td>Intet match</td>'.
'<td></td>'.
'<td></td>'.
'<td></td>'.
'<td></td>'.
2020-07-29 07:49:54 +00:00
'<td></td>'.
2020-07-28 12:23:54 +00:00
'</tr>';
2020-07-27 14:03:49 +00:00
}
return Response($output);
}
}
}
2020-07-27 14:03:49 +00:00