Files
2026-05-09 17:32:17 +02:00

150 lines
5.9 KiB
PHP
Executable File

<?php
namespace App\Livewire;
use App\Models\Miscellaneous\WebsiteSetting;
use Carbon\Carbon;
use Filament\Actions\Action;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Livewire\Component;
class MaintenanceToggle extends Component implements HasActions, HasForms
{
use InteractsWithActions;
use InteractsWithForms;
public function render()
{
return view('livewire.maintenance-toggle');
}
public function toggleMaintenanceAction(): Action
{
$isEnabled = (bool) setting('maintenance_enabled');
return Action::make('toggleMaintenance')
->label($isEnabled ? __('Disable Maintenance') : __('Enable Maintenance'))
->color($isEnabled ? 'danger' : 'success')
->icon($isEnabled ? 'heroicon-o-lock-open' : 'heroicon-o-lock-closed')
->requiresConfirmation()
->modalHeading($isEnabled ? __('Disable Maintenance Mode?') : __('Enable Maintenance Mode?'))
->modalDescription($isEnabled
? __('Are you sure you want to disable maintenance mode? Users will be able to access the site.')
: __('Are you sure you want to enable maintenance mode? Users will not be able to access the site.'))
->action(function () use ($isEnabled) {
$newState = ! $isEnabled;
WebsiteSetting::updateOrCreate(
['key' => 'maintenance_enabled'],
['value' => $newState ? '1' : '0'],
);
Notification::make()
->title($newState ? __('Maintenance mode enabled') : __('Maintenance mode disabled'))
->success()
->send();
$this->redirect(request()->header('Referer') ?? route('filament.housekeeping.pages.dashboard'));
});
}
public function previewMaintenanceAction(): Action
{
return Action::make('previewMaintenance')
->label(__('Preview Page'))
->icon('heroicon-o-eye')
->color('info')
->url(route('maintenance.show', ['preview' => '1']), true);
}
public function editMaintenanceMessageAction(): Action
{
return Action::make('editMaintenanceMessage')
->label(__('Edit Message'))
->icon('heroicon-o-pencil')
->color('warning')
->form([
Textarea::make('message')
->label(__('Maintenance Message'))
->default(fn () => setting('maintenance_message'))
->rows(5)
->required(),
])
->action(function (array $data) {
WebsiteSetting::updateOrCreate(
['key' => 'maintenance_message'],
['value' => $data['message']],
);
Notification::make()
->title(__('Maintenance message updated'))
->success()
->send();
});
}
public function scheduleMaintenanceAction(): Action
{
$scheduledAt = setting('maintenance_scheduled_at');
$isScheduled = $scheduledAt && strtotime((string) $scheduledAt) > time();
return Action::make('scheduleMaintenance')
->label($isScheduled ? __('Cancel Schedule') : __('Schedule'))
->icon($isScheduled ? 'heroicon-o-x-circle' : 'heroicon-o-clock')
->color($isScheduled ? 'danger' : 'info')
->form($isScheduled ? [] : [
DateTimePicker::make('scheduled_at')
->label(__('Start Time'))
->required()
->minDate(now())
->helperText(__('When should maintenance mode start?')),
TextInput::make('duration')
->label(__('Duration (minutes)'))
->numeric()
->default(30)
->minValue(1)
->helperText(__('How long should maintenance last?')),
])
->modalHeading($isScheduled ? __('Cancel Scheduled Maintenance?') : __('Schedule Maintenance'))
->modalDescription($isScheduled
? __('Are you sure you want to cancel the scheduled maintenance?')
: __('Schedule when maintenance mode should automatically start.'))
->action(function (array $data) use ($isScheduled) {
if ($isScheduled) {
WebsiteSetting::where('key', 'maintenance_scheduled_at')->delete();
WebsiteSetting::where('key', 'maintenance_duration_minutes')->delete();
Notification::make()
->title(__('Scheduled maintenance cancelled'))
->success()
->send();
} else {
WebsiteSetting::updateOrCreate(
['key' => 'maintenance_scheduled_at'],
['value' => $data['scheduled_at']],
);
WebsiteSetting::updateOrCreate(
['key' => 'maintenance_duration_minutes'],
['value' => $data['duration']],
);
$startTime = Carbon::parse($data['scheduled_at'])->format('M d, Y H:i');
Notification::make()
->title(__('Maintenance scheduled'))
->body(__('Maintenance will start at :time for :duration minutes', ['time' => $startTime, 'duration' => $data['duration']]))
->success()
->send();
}
});
}
}