v1.5.11 - Event, News & Reservation deletion in sql db - And deletion of files in uploads when you go onto home pages and they're not used in DB

This commit is contained in:
Anders 2021-04-19 09:11:51 +02:00
parent b024d6e821
commit 40d082291f
7 changed files with 113 additions and 11 deletions

View File

@ -33,11 +33,6 @@ class NewsController extends Controller
*/
public function index(Request $request)
{
$news = News::query()->where('news_expiration_date', '<=', date('Y-m-d').'T'.date('H:i') )->get();
foreach ($news as $new) {
$new->delete();
}
$news = News::query()->orderBy('id', 'desc')->get();
return Response::detect("news.index", [ "news" => $news ]);

View File

@ -3,9 +3,13 @@
namespace App\Http\Controllers;
use App\Event;
use App\Guide;
use App\News;
use App\Resource;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\File;
date_default_timezone_set('Europe/Copenhagen');
@ -20,12 +24,23 @@ class RootController extends Controller
public function index() {
$perPage = 5;
//Delete news Articels
$news = News::query()->where('news_expiration_date', '<=', date('Y-m-d').'T'.date('H:i'))->get();
foreach ($news as $new) {
$new->delete();
}
foreach (File::allFiles(public_path('uploads')) as $file) {
$resource = Resource::query()->where('filename', '=', '/uploads/' . $file->getFilename())->get();
if(count($resource) < 1)
unlink(public_path() . '/uploads/' . $file->getFilename());
else {
$events = Event::query()->where('resource_id', '=', $resource[0]->id)->get();
$news = News::query()->where('resource_id', '=', $resource[0]->id)->get();
$guides = Guide::query()->where('resource_id', '=', $resource[0]->id)->get();
$users = User::query()->where('resource_id', '=', $resource[0]->id)->get();
if(count($events) < 1 && count($news) < 1 && count($guides) < 1 && count($users) < 1) {
$resource[0]->delete();
unlink(public_path() . '/uploads/' . $file->getFilename());
}
}
}
//All news
$newsCollection = News::query()->orderBy('id', 'desc')->get();

View File

@ -18,7 +18,7 @@ class CreateNewsTable extends Migration
$table->string("name");
$table->string('subname');
$table->longText("content");
$table->date('news_expiration_date')->nullable();
$table->dateTime('news_expiration_date')->nullable();
$table->foreignId('resource_id')->nullable()->constrained('resources', 'id');
$table->integer("arrangement_id")->nullable(); //Gets ID from given event, menu, guide or news
$table->foreignid("type_id")->nullable()->constrained("news_types", "id");

View File

@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class RemoveEvent extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
\Illuminate\Support\Facades\DB::unprepared("CREATE OR REPLACE EVENT `remove_event` ON SCHEDULE EVERY 1 HOUR STARTS '2000-01-01 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO
DELETE FROM events WHERE HOUR(TIMEDIFF(NOW(), events.date)) >= 24 AND date(events.date) < CURDATE()");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}

View File

@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class RemoveNews extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
\Illuminate\Support\Facades\DB::unprepared("CREATE OR REPLACE EVENT `remove_news` ON SCHEDULE EVERY 1 HOUR STARTS '2000-01-01 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO
DELETE FROM news WHERE news_expiration_date < NOW();");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}

View File

@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class RemoveReservation extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
\Illuminate\Support\Facades\DB::unprepared("CREATE OR REPLACE EVENT `remove_reservation` ON SCHEDULE EVERY 1 HOUR STARTS '2000-01-01 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO
DELETE FROM washing_reservations WHERE time <= DATE_SUB(NOW(), INTERVAL 1 HOUR)");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}

View File

@ -18,4 +18,9 @@
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
#Rewrite everything to https
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>