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

99 lines
3.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Event;
use App\Guide;
use App\MultipleEvents;
use App\MultipleEventsParent;
use App\News;
use App\Resource;
use App\User;
use App\WashingReservation;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\File;
date_default_timezone_set('Europe/Copenhagen');
class RootController extends Controller
{
function __construct()
{
$this->middleware("auth");
$this->middleware([ "lang" ]);
}
public function index() {
if (File::exists(public_path('uploads'))) {
foreach (File::allFiles(public_path('uploads')) as $file) {
$resource = Resource::where('filename', '=', '/uploads/' . $file->getFilename())->get();
if(count($resource) < 1)
unlink(public_path() . '/uploads/' . $file->getFilename());
else {
$events = Event::where('resource_id', '=', $resource[0]->id)->get();
$multipleevent = MultipleEventsParent::where('resource_id', '=', $resource[0]->id)->get();
$news = News::where('resource_id', '=', $resource[0]->id)->get();
$guides = Guide::where('resource_id', '=', $resource[0]->id)->get();
$users = User::where('resource_id', '=', $resource[0]->id)->get();
if(count($events) < 1 && count($news) < 1 && count($guides) < 1 && count($users) < 1 && count($multipleevent) < 1) {
$resource[0]->delete();
unlink(public_path() . '/uploads/' . $file->getFilename());
}
}
}
} else
File::makeDirectory(public_path('uploads'), 0777, true, true);
// Delete events, reservations, news and multiple events (not parent) - If they have been in the DB too long
$deleteevents = Event::where('date', '<', date("Y-m-d H:i:s", strtotime("-1 month")))->get();
$deletereservations = WashingReservation::where('time', '<', date("Y-m-d H:i:s", strtotime("-1 hour")))->get();
$deletenews = News::where('news_expiration_date', '<', date("Y-m-d H:i:s"))->get();
$deletemultievent = MultipleEvents::where('date', '<', strtotime("-1 month"))->get();
foreach ($deleteevents as $event) {
$event->delete();
}
foreach ($deletereservations as $reservation) {
$reservation->delete();
}
foreach ($deletenews as $new) {
$new->delete();
}
foreach ($deletemultievent as $multievent) {
$multievent->delete();
}
//All news
$newsCollection = News::query()->orderBy('id', 'desc')->get();
//Runs through all the news and deletes the old stuff
foreach ($newsCollection as $new)
{
if($new->type_id == 3)
if(Event::query()->where('id', '=', $new->arrangement_id)->first() == null)
$new->delete();
}
//Return either the admin or app index page without the old news
if(auth()->user()->can('admin.panel.show')) {
if(Response::detect("root.index")->name() == "admin.root.index")
return view("admin.root.index");
else {
$news = News::query()->select(['news_types.type', 'news.subname', 'news.id', 'news.resource_id', 'news.created_at', 'news.arrangement_id', 'news.content' ])->join('news_types', 'news_types.id', '=', 'news.type_id')->orderBy('news.id', 'desc')->get();
return view("app.root.index", ["news" => $news]);
}
}
else {
$news = News::query()->select(['news_types.type', 'news.subname', 'news.id', 'news.resource_id', 'news.created_at' ])->join('news_types', 'news_types.id', '=', 'news.type_id')->orderBy('news.id', 'desc')->get();
return Response::detect('root.index', ["news" => $news]);
}
}
}