Files
ai-stylegallery/app/Filament/Pages/GlobalSettings.php

97 lines
3.3 KiB
PHP

<?php
namespace App\Filament\Pages;
use App\Models\Setting;
use App\Services\PrinterService;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Illuminate\Support\Arr;
class GlobalSettings extends Page implements HasForms
{
use InteractsWithForms;
protected static ?string $navigationIcon = 'heroicon-o-cog';
protected static string $view = 'filament.pages.global-settings';
protected static ?string $navigationGroup = 'Admin';
public ?array $data = [];
public function mount(): void
{
$settings = Setting::all()->pluck('value', 'key')->toArray();
$this->form->fill($settings);
}
public function form(Form $form): Form
{
$printerService = new PrinterService();
$printers = $printerService->getPrinters();
$printerOptions = array_merge($printers, ['__custom__' => __('filament.resource.setting.form.custom_printer')]);
return $form
->schema([
TextInput::make('gallery_heading')
->label(__('filament.resource.setting.form.gallery_heading'))
->required(),
TextInput::make('new_image_timespan_minutes')
->label(__('filament.resource.setting.form.new_image_timespan_minutes'))
->numeric()
->required(),
TextInput::make('image_refresh_interval')
->label(__('filament.resource.setting.form.image_refresh_interval'))
->numeric()
->required(),
TextInput::make('max_number_of_copies')
->label(__('filament.resource.setting.form.max_number_of_copies'))
->numeric()
->required(),
Toggle::make('show_print_button')
->label(__('filament.resource.setting.form.show_print_button')),
Select::make('selected_printer')
->label(__('filament.resource.setting.form.printer'))
->options($printerOptions)
->reactive(),
TextInput::make('custom_printer_address')
->label(__('filament.resource.setting.form.custom_printer_address'))
->visible(fn ($get) => $get('selected_printer') === '__custom__'),
])
->statePath('data');
}
public function save(): void
{
$data = $this->form->getState();
// if a non-custom printer is selected, clear the custom address
if (Arr::get($data, 'selected_printer') !== '__custom__') {
$data['custom_printer_address'] = '';
}
foreach ($data as $key => $value) {
Setting::where('key', $key)->update(['value' => $value]);
}
Notification::make()
->title(__('settings.saved_successfully'))
->success()
->send();
}
protected function getFormActions(): array
{
return [
\Filament\Actions\Action::make('save')
->label(__('settings.save_button'))
->submit('save'),
];
}
}