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

317 lines
12 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Event;
use App\News;
use App\Notification;
use App\Resource;
use App\UserEvent;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
date_default_timezone_set('Europe/Copenhagen');
class EventController extends Controller
{
public function __construct()
{
$this->middleware([ "auth" ]);
$this->middleware([ "lang" ]);
$this->middleware([ "check.auth:event.show" ])->only("show", "index");
$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..
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index(Request $request)
{
$events = Event::query()->orderBY('date' , 'asc')->get();
//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.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function create()
{
//returns "create event" blade
return Response::detect("events.create");
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request)
{
$requestBody = $request->validate([
"name" => "required|max:255",
"accountable" => "required|max:255",
"description" => "required",
"date" => "required"
]);
//creates a new Event model with the given parameter
$event = new Event($requestBody);
$allEvents = Event::query()->where('name', '=', $request->name)->get();
if(count($allEvents) > 0) {
return redirect()->route("events.index", ['events' => $event]);
} else {
if($request->file("resource")) {
$event->resource_id = ResourceController::store($request)->id;
}
$event->save();
$events = Event::query()->get();
//If the check on the create pages that is was a news and has to be displayed on the news pages
if($request->newsoption == true){
//Get all events from the database
$events = Event::query()->get();
//make a new object of the news calls
$news = new News();
//Get and save the data
$news->name = "Ny aktivitet";
$news->subname = $event->name;
$news->arrangement_id = $event->id;
$news->type_id = '3';
$news->content = $event->description;
$news->resource_id = $event->resource_id;
$news->news_expiration_date = $events[0]->date;
//Call the news controller function store and get, pass $news to be saved
NewsController::storeAndGet($news);
}
return redirect()->route('events.index', ['events' => $events]);
}
}
/**
* Display the specified resource.
*
* @param Event $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show($id)
{
//Get the entity that match with the passed id.
$event = Event::query()->where("id", "=", $id)->first();
//return the fetch data to the show pages
return Response::detect("events.show", [ "event" => $event ]);
}
/**
* 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)->select('user_events.*', 'users.name_first', 'users.name_last', 'users.phone')->get();
if (count($events) == 0)
$events = Event::where('id', $request->event)->get();
return Response::detect("events.signups", [ "events" => $events ]);
}
/**
* Display signups for event.
*
* @param int $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function accountsignups(Request $request)
{
// Find every event you have clicked on. And find all users to that event, and the event name itself. - This is only being showed on App
$events = UserEvent::join('users', 'users.id', '=', 'user_events.user_id')->join('events', 'events.id', '=', 'user_events.event_id')->where('event_id', $request->event)->get();
return Response::detect("events.signups", [ "events" => $events ]);
}
/**
* Show the form for editing the specified resource.-
*
* @param int $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit($id)
{
$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
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function update(Request $request, $id)
{
//Set $data to holde the value of request->all
$data = $request->all();
//Get the event with matching id
$event = Event::find($id);
//get all events that has a match with the passed id
$allEvents = Event::query()->where('name', '=', $request->name)->where('id', '!=', $id)->get();
//If the amount of $allevents is equal to 0 redirect to index
if(count($allEvents) > 0){
return redirect()->route("events.index", ['events' => $event]);
}else{
//Call the update function
$event->update($data);
//If the events has a images call the Resource Controllers store function
if($request->file("resource")) {
$event->resource_id = ResourceController::store($request)->id;
}
$event->save();
$events = Event::query()->get();
if($request->newsoption == true){
$news = new News();
$news->name = "Aktivitet opdateret";
$news->subname = $event->name;
$news->arrangement_id = $event->id;
$news->type_id = '3';
$news->resource_id = $event->resource_id;
$news->content = "<p>" . $this->closetags(substr($event->description, 0, 300));
$news->news_expiration_date = $event->date;
NewsController::storeAndGet($news);
}
return redirect()->route("events.index", ['events' => $events]);
}
}
/**
* Remove the specified resource from storage.
*
* @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::find($id);
$UserEvent->delete();
return 1;
} else { // Else if you are deleting an event. Then delete all the sign ups AND the event
$userEvents = UserEvent::query()->where('event_id', "=", $id)->get();
$event = Event::query()->find($id);
foreach ($userEvents as $userEvent) {
$notification = new Notification();
$notification->user_id = $userEvent->user_id;
$notification->message = $event->name . " - ";
$notification->save();
$userEvent->delete();
}
$news = News::query()->join('news_types', 'news_types.id', '=', 'news.type_id')->where("type", "=", "Activity")->where("arrangement_id", "=", $id);
$news->delete();
$event->delete();
return redirect()->route("events.index");
}
}
//Fixes tags that have been substringed
//idk yoink this bitch from stackoverflow, you never going to use it, trust me.
public function closetags($html) {
preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
$openedtags = $result[1];
preg_match_all('#</([a-z]+)>#iU', $html, $result);
$closedtags = $result[1];
$len_opened = count($openedtags);
if (count($closedtags) == $len_opened) {
return $html;
}
$openedtags = array_reverse($openedtags);
for ($i=0; $i < $len_opened; $i++) {
if (!in_array($openedtags[$i], $closedtags)) {
$html .= '</'.$openedtags[$i].'>';
} else {
unset($closedtags[array_search($openedtags[$i], $closedtags)]);
}
}
return $html;
}
public function deleteNotifications(Request $request){
//If the request is equal to ajax, delete every notification where the user_id is = to the $request->user_id
if($request->ajax()){
foreach (Notification::query()->where("user_id", "=", $request->user_id)->get() as $notification) {
$notification->delete();
}
return "Done";
} else {
return "ERROR";
}
}
//Used for checking if the currently typed event name is unique. Create version
public function nameCheck(Request $request){
//Search the database to check that the name they are typing is uniq
$event = Event::query()->where('name', 'LIKE',$request->nameCheck)->get();
if(count($event) > 0 && $request->nameCheck !== NULL){
return 1;
}
}
//Used for checking if the currently typed event name is unique. Edit version
public function nameCheckUpdate(Request $request){
$event = Event::query()->where('name', 'LIKE',$request->nameCheck)->where('id', '!=', $request->id)->get();
if(count($event) > 0 && $request->nameCheck !== NULL){
return 1;
}
}
public function previewPages(Request $request){
//get the data from the database that match the request and leftjoin the resources table
$event = Event::where('events.id', '=', $request->preview)->leftJoin('resources', 'resources.id', '=', 'events.resource_id')->first();
//convert dababy to a convertible
$convertToJsonArray = json_encode($event);
return $convertToJsonArray;
}
}