114 lines
4.3 KiB
PHP
114 lines
4.3 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Pages;
|
||
|
||
use App\Enums\UserRole;
|
||
use App\Filament\Concerns\FollowsAdminMode;
|
||
use App\Support\Settings;
|
||
use BackedEnum;
|
||
use Filament\Actions\Action;
|
||
use Filament\Forms\Components\Radio;
|
||
use Filament\Forms\Components\TextInput;
|
||
use Filament\Notifications\Notification;
|
||
use Filament\Pages\Page;
|
||
use Filament\Schemas\Components\Actions;
|
||
use Filament\Schemas\Components\EmbeddedSchema;
|
||
use Filament\Schemas\Components\Form;
|
||
use Filament\Schemas\Components\Section;
|
||
use Filament\Schemas\Components\Text;
|
||
use Filament\Schemas\Components\Utilities\Get;
|
||
use Filament\Schemas\Schema;
|
||
use Filament\Support\Icons\Heroicon;
|
||
use UnitEnum;
|
||
|
||
/**
|
||
* Operating mode: test mode sends every e-mail to one test recipient only; switching live delivers to the
|
||
* real recipients (guests, landlord). Administrators only.
|
||
*/
|
||
class Operations extends Page
|
||
{
|
||
use FollowsAdminMode;
|
||
|
||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedPower;
|
||
|
||
protected static string|UnitEnum|null $navigationGroup = 'System';
|
||
|
||
protected static ?string $navigationLabel = 'Betrieb';
|
||
|
||
protected static ?string $title = 'Betrieb';
|
||
|
||
protected static ?string $slug = 'betrieb';
|
||
|
||
protected static ?int $navigationSort = 1;
|
||
|
||
/** @var array<string, ?string> */
|
||
public array $data = [];
|
||
|
||
public static function canAccess(): bool
|
||
{
|
||
return (bool) auth()->user()?->hasRole(UserRole::Admin);
|
||
}
|
||
|
||
public function mount(): void
|
||
{
|
||
$this->form->fill([
|
||
'mode' => Settings::get(Settings::MODE, Settings::MODE_TEST),
|
||
'test_recipient' => Settings::get(Settings::TEST_RECIPIENT) ?? auth()->user()?->email,
|
||
]);
|
||
}
|
||
|
||
public function form(Schema $schema): Schema
|
||
{
|
||
return $schema
|
||
->statePath('data')
|
||
->components([
|
||
Section::make('E-Mail-Versand')
|
||
->description('Im Testbetrieb gehen alle E-Mails – Eingangsbestätigungen, Angebote, Bestätigungen, Erinnerungen, Benachrichtigungen – ausschließlich an die Testadresse, egal welche Adresse auf der Webseite eingetragen wurde.')
|
||
->schema([
|
||
Radio::make('mode')
|
||
->label('Betriebsart')
|
||
->options([
|
||
Settings::MODE_TEST => 'Testbetrieb – alle E-Mails an die Testadresse',
|
||
Settings::MODE_LIVE => 'Scharf geschaltet – E-Mails an Gäste und Vermieterin',
|
||
])
|
||
->required()
|
||
->live(),
|
||
TextInput::make('test_recipient')
|
||
->label('Testadresse')
|
||
->email()
|
||
->required(fn (Get $get): bool => $get('mode') === Settings::MODE_TEST)
|
||
->visible(fn (Get $get): bool => $get('mode') === Settings::MODE_TEST),
|
||
Text::make(fn (): string => filled(config('fewo.mail_redirect_to'))
|
||
? 'Hinweis: Auf diesem Server ist zusätzlich FEWO_MAIL_REDIRECT_TO='.config('fewo.mail_redirect_to').' gesetzt – diese Umleitung gilt immer, auch wenn hier scharf geschaltet ist.'
|
||
: ''),
|
||
]),
|
||
]);
|
||
}
|
||
|
||
public function content(Schema $schema): Schema
|
||
{
|
||
return $schema->components([
|
||
Form::make([EmbeddedSchema::make('form')])
|
||
->id('form')
|
||
->livewireSubmitHandler('save')
|
||
->footer([Actions::make([Action::make('save')->label('Speichern')->submit('save')])]),
|
||
]);
|
||
}
|
||
|
||
public function save(): void
|
||
{
|
||
$data = $this->form->getState();
|
||
|
||
if ($data['mode'] === Settings::MODE_LIVE && Settings::get(Settings::MODE) !== Settings::MODE_LIVE) {
|
||
Notification::make()->title('Scharf geschaltet')->body('Ab jetzt erhalten Gäste und Vermieterin die E-Mails direkt.')->warning()->send();
|
||
} else {
|
||
Notification::make()->title('Gespeichert')->success()->send();
|
||
}
|
||
|
||
Settings::save([
|
||
Settings::MODE => $data['mode'],
|
||
Settings::TEST_RECIPIENT => $data['test_recipient'] ?? Settings::get(Settings::TEST_RECIPIENT),
|
||
]);
|
||
}
|
||
}
|