From ad451af609e601b61a38636da7ee5472ae4dbc45 Mon Sep 17 00:00:00 2001 From: Nils Fischer Date: Fri, 19 Apr 2024 06:08:47 +0000 Subject: [PATCH] v.1.6 --- app/Console/Commands/PresentationProcess.php | 8 +---- .../Controllers/PresentationController.php | 15 ++++---- app/Http/Controllers/ScheduleController.php | 36 ++++++++++--------- app/Models/Presentation.php | 7 ++++ resources/views/presentations/index.blade.php | 15 ++++---- resources/views/schedules/create.blade.php | 4 +-- 6 files changed, 43 insertions(+), 42 deletions(-) diff --git a/app/Console/Commands/PresentationProcess.php b/app/Console/Commands/PresentationProcess.php index 1451e66..4b0c1b5 100644 --- a/app/Console/Commands/PresentationProcess.php +++ b/app/Console/Commands/PresentationProcess.php @@ -35,13 +35,7 @@ public function handle() $presentation = Presentation::where('id', $id)->firstOrFail(); - $type = $this->argument('type'); - - if ($type == 'pdf') { - $this->processPdf($presentation); - } else if ($type == 'video') { - // Deprecated - } + $this->processPdf($presentation); } public function processPdf($presentation) diff --git a/app/Http/Controllers/PresentationController.php b/app/Http/Controllers/PresentationController.php index 0fd51e6..f5f101b 100644 --- a/app/Http/Controllers/PresentationController.php +++ b/app/Http/Controllers/PresentationController.php @@ -20,6 +20,11 @@ public function index() { $presentations = Presentation::all()->sortBy('name'); + // Sort by inUse + $presentations = $presentations->sortBy(function ($presentation) { + return !$presentation->in_use(); + }); + $groups = Group::all()->sortBy('name'); $devices = Device::all()->sortBy('name'); @@ -28,15 +33,7 @@ public function index() // Loop through groups and devices and see which presentations are unused foreach($presentations as $presentation) { - $devices = $presentation->devices->count(); - $groups = $presentation->groups->count(); - $schedules = $presentation->schedules->count(); - - if($devices == 0 && $groups == 0 && $schedules == 0) { - $countUnused++; - } else { - $countUsed++; - } + $presentation->in_use() ? $countUsed++ : $countUnused++; } return view('presentations.index', compact('presentations', 'countUsed', 'countUnused')); diff --git a/app/Http/Controllers/ScheduleController.php b/app/Http/Controllers/ScheduleController.php index c06f073..e510fb0 100644 --- a/app/Http/Controllers/ScheduleController.php +++ b/app/Http/Controllers/ScheduleController.php @@ -9,6 +9,7 @@ use App\Models\Schedule; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\File; class ScheduleController extends Controller { @@ -160,34 +161,37 @@ public function destroy(string $id) static function checkForExpiredSchedules() { $currentTimestamp = now(); - $expiredSchedules = Schedule::where('end_time', '<', $currentTimestamp)->get(); - - // dd($expiredSchedules); + $expiredSchedules = Schedule::where('end_time', '<', $currentTimestamp)->where('delete_presentation', 1)->get(); foreach($expiredSchedules as $schedule) { - if($schedule->delete_presentation == 1) { $presentation = $schedule->presentation; if($presentation) { if($presentation->devices()->count() == 0 && $presentation->groups()->count() == 0) { - Log::create([ - 'ip_address' => "127.0.0.1", - 'username' => 'System', - 'action' => __('log.presentation_deleted_because_schedule', ['name' => $presentation->name, 'schedule' => $schedule->name,]), - ]); + try { + $presentation->slides()->delete(); + + File::delete(storage_path('app/public/presentations/' . $presentation->id . '/' . $presentation->id) . '.pdf'); + File::deleteDirectory(storage_path('app/public/presentations/' . $presentation->id . '/')); - $presentation->delete(); + $oldName = $presentation->name; + + $presentation->delete(); + + Log::create([ + 'ip_address' => "127.0.0.1", + 'username' => 'System', + 'action' => __('log.presentation_deleted_because_schedule', ['name' => $oldName, 'schedule' => $schedule->name,]), + ]); + } catch (\Exception $e) { + + } } else { Log::create([ 'ip_address' => "127.0.0.1", 'username' => 'System', 'action' => __('log.presentation_not_deleted_because_schedule', ['name' => $presentation->name, 'schedule' => $schedule->name,]), ]); - - $schedule->delete_presentation = false; - $schedule->save(); - - return false; } } @@ -196,8 +200,6 @@ static function checkForExpiredSchedules() { $schedule->save(); return true; - } - } return false; diff --git a/app/Models/Presentation.php b/app/Models/Presentation.php index d78ff75..cd9ee8e 100644 --- a/app/Models/Presentation.php +++ b/app/Models/Presentation.php @@ -35,6 +35,8 @@ protected static function booted(): void 'total_slides' ]; + protected $appends = ['in_use']; + public function devices() { return $this->hasMany(Device::class); @@ -65,4 +67,9 @@ public function getSchedules() return $schedules; } + + public function in_use() + { + return $this->devices->count() > 0 || $this->groups->count() > 0 || $this->getSchedules()->count() > 0; + } } diff --git a/resources/views/presentations/index.blade.php b/resources/views/presentations/index.blade.php index 85856ce..393dbb8 100644 --- a/resources/views/presentations/index.blade.php +++ b/resources/views/presentations/index.blade.php @@ -46,20 +46,21 @@ class="img-thumbnail" style="max-height: 100px;"> {{ $presentation->slides->count() }} {{ $presentation->slides->first()?->created_at?->format('d.m.Y H:i') ?? 'N/A' }} - + @php $list = []; @endphp @if ($presentation->devices->count() > 0) - {{ $presentation->devices->count() }} - {{ trans_choice('Device|Devices', $presentation->devices->count()) }}, + @php $list[] = $presentation->devices->count() . " " . trans_choice('Device|Devices', $presentation->devices->count()); @endphp @endif @if ($presentation->groups->count() > 0) - {{ $presentation->groups->count() }} - {{ trans_choice('Group|Groups', $presentation->groups->count()) }}, + @php $list[] = $presentation->groups->count() . " " . trans_choice('Group|Groups', $presentation->groups->count()); @endphp @endif @if ($presentation->getSchedules()->count() > 0) - {{ $presentation->getSchedules()->count() }} - {{ trans_choice('Schedule|Schedules', $presentation->getSchedules()->count()) }}, + @php $list[] = $presentation->getSchedules()->count() . " " . trans_choice('Schedule|Schedules', $presentation->getSchedules()->count()); @endphp + @endif + + @if (count($list) > 0) + {{ implode(', ', $list) }} @endif {{ $presentation->author }} diff --git a/resources/views/schedules/create.blade.php b/resources/views/schedules/create.blade.php index a465f77..4dbee65 100644 --- a/resources/views/schedules/create.blade.php +++ b/resources/views/schedules/create.blade.php @@ -72,7 +72,7 @@ function validate() { + value="{{ now()->format('Y-m-d H:00') }}"> @@ -81,7 +81,7 @@ class="text-danger">* + value="{{ now()->addHours(1)->format('Y-m-d H:00') }}">