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

317 lines
13 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\MultipleEvents;
use App\MultipleEventsParent;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Barryvdh\DomPDF\Facade as PDF;
//Not in use yet
use App\Event;
use App\News;
use App\Notification;
use App\Resource;
use App\UserEvent;
class MultipleEventsController 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\Http\Response
*/
public function index()
{
$multiEvents = MultipleEventsParent::join("multiple_events", "multiple_events.multiple_events_parent_id", '=', 'multiple_events_parent.id')->groupBy('multiple_events.multiple_events_parent_id', 'multiple_events_parent.id', 'multiple_events_parent.week', 'multiple_events_parent.resource_id', 'multiple_events_parent.created_at', 'multiple_events_parent.updated_at')->selectRaw('multiple_events_parent.*, max(date) as date')->get();
/*
$multiEvents = MultipleEventsParent::join("multiple_events", "multiple_events.multiple_events_parent_id", '=', 'multiple_events_parent.id')
->groupBy('multiple_events_parent_id')->orderBy('multiple_events.date', 'desc')
->selectRaw('multiple_events_parent.*, max(date) as date')
->get();
*/
return Response::detect("multiple-events.index", [ "multiEvents" => $multiEvents]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//returns create multiple events blade file from the view folder
return Response::detect("multiple-events.create");
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$requestBody = $request->validate([
"week" => "required",
"name" => "array",
"description" => "array",
"accountable" => "array",
"date" => "array",
]);
$multipleParent = new MultipleEventsParent();
$multipleParent->week = $request->week;
if($request->file("resource")) {
$multipleParent->resource_id = ResourceController::store($request)->id;
}
$multipleParent->save();
for ($i = 0; $i < count($requestBody["name"]); $i++) {
$multi_event = new MultipleEvents();
$multi_event->date = $requestBody['date'][$i];
$multi_event->description = $requestBody['description'][$i];
$multi_event->accountable = $requestBody['accountable'][$i];
$multi_event->name = $requestBody['name'][$i];
$multi_event->multiple_events_parent_id = $multipleParent->id;
$multi_event->save();
}
//creates a new Event model with the given parameter
$multiEventsGetEndDate = MultipleEventsParent::join("multiple_events", "multiple_events.multiple_events_parent_id", '=', 'multiple_events_parent.id')
->groupBy('multiple_events_parent_id')
->orderBy('multiple_events.date', 'desc')
->selectRaw('multiple_events_parent.*, max(date) as date')
->get();
$multiEvents = MultipleEventsParent::query()->get();
if($request->newsoption == true){
$news = new News();
$news->name = "Ny ugentlig aktivitet";
$news->subname = $request->week;
$news->arrangement_id = $multipleParent->id;
$news->type_id = '5';
$news->content = "Den ugentlige aktivitets plan for uge ".$request->week." er her!";
$news->resource_id = $request->resource_id;
$news->news_expiration_date = $multiEventsGetEndDate[0]->date;
NewsController::storeAndGet($news);
}
return redirect()->route('events.index', ['multiEvents' => $multiEvents]);
}
/**
* Display the specified resource.
*
* @param \App\MultipleEvents $multipleeventsController
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$multiEvents = MultipleEvents::where('multiple_events.multiple_events_parent_id', '=', $id)->orderBy('date')->get();
$multiEventParent = MultipleEventsParent::find($id);
return Response::detect("multiple-events.show", [ "week" => $multiEventParent->week, "multiEvents" => $multiEvents]);
}
/**
* 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.
$events = UserEvent::join('users', 'users.id', '=', 'user_events.user_id')->where('multiple_event_id', $request->multiEvent)->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("multiple-events.signups", [ "multiEvent" => $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)
{
$multiEvents = MultipleEvents::where('multiple_events_parent_id', '=', $id)->get();
$parentEvent = MultipleEventsParent::find($id);
return Response::detect("multiple-events.edit", ["multiEvents" => $multiEvents, "parentEvent" => $parentEvent]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$requestBody = $request->validate([
"week" => "required",
"name" => "array",
"description" => "array",
"accountable" => "array",
"date" => "array",
]);
$parentEvent = MultipleEventsParent::find($id);
if($request->file("resource")) {
$parentEvent->update(["week" => $request->week, "resource_id" => ResourceController::store($request)->id]) ;
} else
$parentEvent->update(["week" => $request->week]);
if (!empty($requestBody["name"])) {
foreach ($requestBody["name"] as $key => $singleEvent) {
if ($key == "new") {
//dd(count($s));
for ($i = 0; $i < count($singleEvent); $i++) {
$multi_event = new MultipleEvents();
$multi_event->name = $requestBody["name"]["new"][$i];
$multi_event->description = $requestBody["description"]["new"][$i];
$multi_event->accountable = $requestBody["accountable"]["new"][$i];
$multi_event->date = $requestBody["date"]["new"][$i];
$multi_event->multiple_events_parent_id = $id;
$multi_event->save();
}
} else {
$event = MultipleEvents::find($key);
$event->update(["name" => $requestBody["name"][$key][0], "description" => $requestBody["description"][$key][0], "accountable" => $requestBody["accountable"][$key][0], "date" => date("Y-m-d H:i:s", strtotime($requestBody["date"][$key][0]))]);
}
}
$multiEventsGetEndDate = MultipleEventsParent::join("multiple_events", "multiple_events.multiple_events_parent_id", '=', 'multiple_events_parent.id')
->groupBy('multiple_events_parent_id')
->orderBy('multiple_events.date', 'desc')
->selectRaw('multiple_events_parent.*, max(date) as date')
->get();
if ($request->newsoption == true) {
$news = new News();
$news->name = "Opdateret ugentlig aktivitet";
$news->subname = $request->week;
$news->arrangement_id = $parentEvent->id;
$news->type_id = '5';
$news->content = "Den ugentlige aktivitets plan for uge " . $request->week . " er opdateret!";
$news->resource_id = $request->resource_id;
$news->news_expiration_date = $multiEventsGetEndDate[0]->date;
NewsController::storeAndGet($news);
}
}
$events = Event::query()->orderBY('date' , 'asc')->get();
return Response::detect("events.index", [ "events" => $events]);
}
/**
* Remove the specified resource from storage.
*
* @param \App\MultipleEvents $multipleeventsController
* @return \Illuminate\Http\Response
*/
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 specific week event
$UserEvent = UserEvent::find($id);
$UserEvent->delete();
return 1;
} elseif ($request->onlyMultiEvent != null) { // Delete a single event from multiple events, and all its signups
$userEvents = UserEvent::where('multiple_event_id', '=', $id)->get();
$multiEvent = MultipleEvents::find($id);
foreach ($userEvents as $ue) {
$ue->delete();
}
$multiEvent->delete();
return 1;
} else { // Delete all multiple events and signups, from the parent/weekly event
$multiparent = MultipleEventsParent::find($id);
$multievent = MultipleEvents::where('multiple_events_parent_id', '=', $id)->get();
foreach ($multievent as $me) {
foreach (UserEvent::where('multiple_event_id', '=', $me->id)->get() as $ue) {
$ue->delete();
}
$me->delete();
}
$news = News::query()->join('news_types', 'news_types.id', '=', 'news.type_id')->where("type", "=", "MultipleEvent")->where("arrangement_id", "=", $id);
$news->delete();
$multiparent->delete();
return 1;
}
}
public function previewPages(Request $request){
/*if(!Resource::find($request->preview)){
$multipleEventsParent = MultipleEventsParent::find($request->preview);
$multipleEvents = MultipleEvents::where('multiple_events_parent_id', '=', $multipleEventsParent->id)->get();
} else {*/
//$multipleEventsParent = MultipleEventsParent::find($request->preview);
$multipleEventsParent = MultipleEventsParent::leftJoin('resources', 'resources.id', '=', 'multiple_events_parent.resource_id')->where('multiple_events_parent.id', '=', $request->preview)->select('multiple_events_parent.*', 'resources.filename')->first();
$multipleEvents = MultipleEvents::where('multiple_events_parent_id', '=', $multipleEventsParent->id)->get();
//}
return [$multipleEventsParent, $multipleEvents];
}
public function genPDF($Id){
$multiEvent = MultipleEventsParent::query()->join("multiple_events", "multiple_events.multiple_events_parent_id", "=", "multiple_events_parent.id")->where("multiple_events_parent.id", "=", $Id)->orderBy('multiple_events.date', 'asc')->get();
$pdf = PDF::loadView('multipdf.multipleevent', ["multiEvent" => $multiEvent]);
$pdf->setPaper('A4', 'landscape');
return $pdf->stream("ugentligt_aktivitet_uge_".$multiEvent[0]->week.".pdf");
}
public function signups(Request $request)
{
// Find every event you have clicked on. And find all users to that event, and the event name itself.
//$multiEvents = UserEvent::join('users', 'users.id', '=', 'user_events.user_id')->join('multiple_events', 'multiple_events.id', '=', 'user_events.multiple_event_id')->where('multiple_event_id', '=', $request->multipleEvent)->get();
$multiEvents = MultipleEvents::where('multiple_events_parent_id', '=', $request->multipleEvent)->get();
//if (count($multiEvents) == 0)
// $multiEvents = MultipleEvents::where('id', '=', $request->multipleEvent)->get();
return Response::detect("multiple-events.signups", [ "multiEvent" => $multiEvents ]);
}
}