2020-06-08 13:08:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-08-31 11:20:49 +00:00
|
|
|
use App\Event;
|
2021-04-19 07:11:51 +00:00
|
|
|
use App\Guide;
|
2021-04-27 10:27:46 +00:00
|
|
|
use App\MultipleEventsParent;
|
2020-08-19 07:54:26 +00:00
|
|
|
use App\News;
|
2021-04-19 07:11:51 +00:00
|
|
|
use App\Resource;
|
|
|
|
use App\User;
|
2020-06-08 13:08:46 +00:00
|
|
|
use Illuminate\Http\Request;
|
2020-06-24 06:23:15 +00:00
|
|
|
use Illuminate\Http\Response;
|
2021-04-19 07:11:51 +00:00
|
|
|
use Illuminate\Support\Facades\File;
|
2020-06-08 13:08:46 +00:00
|
|
|
|
2020-10-07 13:27:29 +00:00
|
|
|
date_default_timezone_set('Europe/Copenhagen');
|
|
|
|
|
2020-06-08 13:08:46 +00:00
|
|
|
class RootController extends Controller
|
|
|
|
{
|
2020-06-30 08:42:10 +00:00
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware("auth");
|
2020-08-06 13:31:38 +00:00
|
|
|
$this->middleware([ "lang" ]);
|
2020-06-30 08:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 13:08:46 +00:00
|
|
|
public function index() {
|
2021-04-19 12:42:03 +00:00
|
|
|
if (File::exists(public_path('uploads'))) {
|
|
|
|
foreach (File::allFiles(public_path('uploads')) as $file) {
|
2021-04-27 10:27:46 +00:00
|
|
|
$resource = Resource::where('filename', '=', '/uploads/' . $file->getFilename())->get();
|
2020-08-19 07:54:26 +00:00
|
|
|
|
2021-04-19 12:42:03 +00:00
|
|
|
if(count($resource) < 1)
|
2021-04-19 07:11:51 +00:00
|
|
|
unlink(public_path() . '/uploads/' . $file->getFilename());
|
2021-04-19 12:42:03 +00:00
|
|
|
else {
|
2021-04-27 10:27:46 +00:00
|
|
|
$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();
|
2021-04-19 12:42:03 +00:00
|
|
|
|
2021-04-27 10:27:46 +00:00
|
|
|
if(count($events) < 1 && count($news) < 1 && count($guides) < 1 && count($users) < 1 && count($multipleevent) < 1) {
|
2021-04-19 12:42:03 +00:00
|
|
|
$resource[0]->delete();
|
|
|
|
unlink(public_path() . '/uploads/' . $file->getFilename());
|
|
|
|
}
|
2021-04-19 07:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-19 12:42:03 +00:00
|
|
|
} else
|
|
|
|
File::makeDirectory(public_path('uploads'), 0777, true, true);
|
2020-10-07 13:27:29 +00:00
|
|
|
|
2020-09-22 06:56:03 +00:00
|
|
|
//All news
|
2020-09-22 08:45:27 +00:00
|
|
|
$newsCollection = News::query()->orderBy('id', 'desc')->get();
|
2020-08-31 11:20:49 +00:00
|
|
|
|
2020-09-22 06:56:03 +00:00
|
|
|
//Runs through all the news and deletes the old stuff
|
2020-08-31 11:20:49 +00:00
|
|
|
foreach ($newsCollection as $new)
|
|
|
|
{
|
|
|
|
if($new->type_id == 3)
|
|
|
|
if(Event::query()->where('id', '=', $new->arrangement_id)->first() == null)
|
|
|
|
$new->delete();
|
|
|
|
}
|
|
|
|
|
2020-09-22 06:56:03 +00:00
|
|
|
//Return either the admin or app index page without the old news
|
2020-08-19 07:54:26 +00:00
|
|
|
if(auth()->user()->can('admin.panel.show')) {
|
|
|
|
if(Response::detect("root.index")->name() == "admin.root.index")
|
2020-08-27 11:59:11 +00:00
|
|
|
return view("admin.root.index");
|
2020-08-19 07:54:26 +00:00
|
|
|
else {
|
2020-10-07 13:27:29 +00:00
|
|
|
$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();
|
2020-08-27 11:59:11 +00:00
|
|
|
return view("app.root.index", ["news" => $news]);
|
2020-08-19 07:54:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-10-07 13:27:29 +00:00
|
|
|
$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();
|
2020-08-19 07:54:26 +00:00
|
|
|
|
2020-08-27 11:59:11 +00:00
|
|
|
return Response::detect('root.index', ["news" => $news]);
|
2020-08-19 07:54:26 +00:00
|
|
|
}
|
2020-06-08 13:08:46 +00:00
|
|
|
}
|
|
|
|
}
|