🆙 More refactored 🆙

This commit is contained in:
Remco
2026-01-20 20:17:04 +01:00
parent 1e84468d49
commit fccf4c2116
14 changed files with 169 additions and 83 deletions
+58 -35
View File
@@ -31,15 +31,18 @@ class BadgePage extends Page
protected static string $translateIdentifier = 'badge-resource';
public $badgeWasPreviouslyCreated;
public bool $badgeWasPreviouslyCreated = false;
public ?array $data = [];
/**
* @var array<string, mixed>
*/
public array $data = [];
public static string $roleName = 'badge_page';
public static function canAccess(): bool
{
return auth()->user()->can('view::admin::' . static::$roleName);
return auth()->check() && auth()->user()?->can('view::admin::' . static::$roleName) === true;
}
#[\Override]
@@ -60,7 +63,7 @@ class BadgePage extends Page
->label(__('filament::resources.inputs.badge_code'))
->helperText(__('filament::resources.helpers.badge_code_helper'))
->afterStateUpdated(function (?string $state, Set $set): void {
$set('code', strtoupper($state));
$set('code', strtoupper($state ?? ''));
})
->suffixAction(fn (): PageAction => PageAction::make('search')->icon('heroicon-o-magnifying-glass')->action(fn () => $this->searchBadgesByCode()),
),
@@ -69,7 +72,7 @@ class BadgePage extends Page
->label(__('filament::resources.inputs.badge_image'))
->placeholder('...')
->autocomplete()
->visible(fn (Get $get) => isset($this->data['image']) ?? false)
->visible(fn (Get $get) => isset($this->data['image']))
->prefixAction(
fn (?string $state): PageAction => PageAction::make('visit')
->icon('heroicon-s-arrow-top-right-on-square')
@@ -87,12 +90,12 @@ class BadgePage extends Page
TextInput::make('nitro.title')
->label(__('filament::resources.inputs.badge_title'))
->placeholder('...')
->visible(fn () => isset($this->data['nitro']['title']) ?? false),
->visible(fn () => is_array($this->data['nitro']) && array_key_exists('title', $this->data['nitro'])),
TextInput::make('nitro.description')
->label(__('filament::resources.inputs.badge_description'))
->placeholder('...')
->visible(fn () => isset($this->data['nitro']['description']) ?? false),
->visible(fn () => is_array($this->data['nitro']) && array_key_exists('description', $this->data['nitro'])),
]),
Section::make('Flash Texts')
@@ -102,12 +105,12 @@ class BadgePage extends Page
TextInput::make('flash.title')
->label(__('filament::resources.inputs.badge_title'))
->placeholder('...')
->visible(fn () => isset($this->data['flash']['title']) ?? false),
->visible(fn () => is_array($this->data['flash']) && array_key_exists('title', $this->data['flash'])),
TextInput::make('flash.description')
->label(__('filament::resources.inputs.badge_description'))
->placeholder('...')
->visible(fn () => isset($this->data['flash']['description']) ?? false),
->visible(fn () => is_array($this->data['flash']) && array_key_exists('description', $this->data['flash'])),
]),
])
->statePath('data');
@@ -115,16 +118,22 @@ class BadgePage extends Page
private function searchBadgesByCode(): void
{
$badgeCode = $this->form->getState()['code'] ?? null;
$badgeCode = is_string($this->data['code'] ?? null) ? $this->data['code'] : null;
if (empty($badgeCode)) {
$this->notify('danger', __('filament::resources.notifications.badge_code_required'));
Notification::make()
->color('danger')
->icon('heroicon-o-exclamation-triangle')
->title(__('filament::resources.notifications.badge_code_required'))
->send();
return;
}
$badgeData = app(ExternalTextsParser::class)->getBadgeData($badgeCode);
$this->badgeWasPreviouslyCreated = is_array($badgeData['nitro']) || is_array($badgeData['flash']);
$badgeData = app(ExternalTextsParser::class)->getBadgeData((string) $badgeCode);
$nitro = is_array($badgeData['nitro'] ?? null) ? $badgeData['nitro'] : [];
$flash = is_array($badgeData['flash'] ?? null) ? $badgeData['flash'] : [];
$this->badgeWasPreviouslyCreated = ! empty($nitro) || ! empty($flash);
if ($this->badgeWasPreviouslyCreated) {
Notification::make()
@@ -135,13 +144,13 @@ class BadgePage extends Page
->send();
$this->data = [
'code' => $badgeCode,
'code' => (string) $badgeCode,
...$this->getDefaultDataBehavior(
$badgeData['image'] ?? null,
$badgeData['nitro']['title'] ?? null,
$badgeData['nitro']['description'] ?? null,
$badgeData['flash']['title'] ?? null,
$badgeData['flash']['description'] ?? null,
is_string($badgeData['image'] ?? null) ? $badgeData['image'] : null,
is_string($nitro['title'] ?? null) ? $nitro['title'] : null,
is_string($nitro['description'] ?? null) ? $nitro['description'] : null,
is_string($flash['title'] ?? null) ? $flash['title'] : null,
is_string($flash['description'] ?? null) ? $flash['description'] : null,
),
];
@@ -156,11 +165,14 @@ class BadgePage extends Page
->send();
$this->data = [
'code' => $badgeCode,
'code' => (string) $badgeCode,
...$this->getDefaultDataBehavior(),
];
}
/**
* @return array<string, mixed>
*/
private function getDefaultDataBehavior(
?string $badgeImageUrl = null,
?string $nitroTitle = null,
@@ -181,7 +193,7 @@ class BadgePage extends Page
];
}
public function create()
public function create(): void
{
$nitroEnabled = config('hotel.client.nitro.enabled');
$flashEnabled = config('hotel.client.flash.enabled');
@@ -219,10 +231,18 @@ class BadgePage extends Page
$this->uploadBadgeImage($externalTextsParser);
if (! empty($this->data['nitro']) && $nitroEnabled) {
$externalTextsParser->updateNitroBadgeTexts($this->data['code'], ...$this->data['nitro']);
$code = is_string($this->data['code'] ?? null) ? $this->data['code'] : '';
$nitro = is_array($this->data['nitro']) ? $this->data['nitro'] : [];
$title = is_string($nitro['title'] ?? null) ? $nitro['title'] : '';
$desc = is_string($nitro['description'] ?? null) ? $nitro['description'] : '';
$externalTextsParser->updateNitroBadgeTexts($code, $title, $desc);
}
if (! empty($this->data['flash']) && $flashEnabled) {
$externalTextsParser->updateFlashBadgeTexts($this->data['code'], ...$this->data['flash']);
$code = is_string($this->data['code'] ?? null) ? $this->data['code'] : '';
$flash = is_array($this->data['flash']) ? $this->data['flash'] : [];
$title = is_string($flash['title'] ?? null) ? $flash['title'] : '';
$desc = is_string($flash['description'] ?? null) ? $flash['description'] : '';
$externalTextsParser->updateFlashBadgeTexts($code, $title, $desc);
}
} catch (Throwable $exception) {
Log::channel('badge')->error('[ORION BADGE RESOURCE] - ERROR: ' . $exception->getMessage());
@@ -237,7 +257,7 @@ class BadgePage extends Page
return;
}
$this->data['image'] = $externalTextsParser->getBadgeImageUrl($this->data['code']);
$this->data['image'] = $externalTextsParser->getBadgeImageUrl(is_string($this->data['code'] ?? null) ? $this->data['code'] : '');
$this->badgeWasPreviouslyCreated = true;
Notification::make()
@@ -250,15 +270,17 @@ class BadgePage extends Page
protected function uploadBadgeImage(ExternalTextsParser $parser): void
{
if (empty($this->data['image']) || ! filter_var($this->data['image'], FILTER_VALIDATE_URL)) {
$imageUrl = is_string($this->data['image'] ?? null) ? $this->data['image'] : '';
$code = is_string($this->data['code'] ?? null) ? $this->data['code'] : '';
if ($imageUrl === '' || ! filter_var($imageUrl, FILTER_VALIDATE_URL)) {
return;
}
if ($this->data['image'] == $parser->getBadgeImageUrl($this->data['code'])) {
if ($imageUrl === $parser->getBadgeImageUrl($code)) {
return;
}
$image = Http::get($this->data['image']);
$image = Http::get($imageUrl);
if (! $image->successful()) {
return;
@@ -267,9 +289,9 @@ class BadgePage extends Page
$contentType = $image->header('content-type');
$gdImage = match ($contentType) {
'image/png' => imagecreatefrompng($this->data['image']),
'image/gif' => imagecreatefromgif($this->data['image']),
'image/jpeg' => imagecreatefromjpeg($this->data['image']),
'image/png' => imagecreatefrompng($imageUrl),
'image/gif' => imagecreatefromgif($imageUrl),
'image/jpeg' => imagecreatefromjpeg($imageUrl),
default => false
};
@@ -284,15 +306,16 @@ class BadgePage extends Page
return;
}
$uploadPath = public_path(sprintf('%s%s%s.gif',
rtrim((string) config('hotel.client.flash.relative_files_path'), '\//'),
'/c_images/album1584/',
$this->data['code'],
));
$basePath = config('hotel.client.flash.relative_files_path');
$basePathStr = is_string($basePath) ? $basePath : '';
$uploadPath = public_path(sprintf('%s%s%s.gif', rtrim($basePathStr, '\//'), '/c_images/album1584/', $code));
imagegif($gdImage, $uploadPath);
}
/**
* @return array<\Filament\Actions\Action|ActionGroup>
*/
/**
* @return array<\Filament\Actions\Action|ActionGroup>
*/
+1 -1
View File
@@ -21,6 +21,6 @@ class Dashboard extends FilamentDashboard
public static function canAccess(): bool
{
return auth()->user()->can('view::admin::' . static::$roleName);
return auth()->check() && auth()->user()?->can('view::admin::' . static::$roleName) === true;
}
}
+21 -9
View File
@@ -14,7 +14,7 @@ use Illuminate\Validation\ValidationException;
class Login extends \Filament\Auth\Pages\Login
{
public $username = '';
public string $username = '';
#[\Override]
public function authenticate(): ?LoginResponse
@@ -22,15 +22,23 @@ class Login extends \Filament\Auth\Pages\Login
try {
$this->rateLimit(5);
} catch (TooManyRequestsException $exception) {
$seconds = is_numeric($exception->secondsUntilAvailable) ? (int) $exception->secondsUntilAvailable : 0;
$minutes = ceil($seconds / 60);
$body = null;
$throttled = __('filament-panels::pages/auth/login.notifications.throttled');
if (is_array($throttled)) {
$bodyText = __('filament-panels::pages/auth/login.notifications.throttled.body', [
'seconds' => $seconds,
'minutes' => $minutes,
]);
$body = is_string($bodyText) ? $bodyText : null;
}
Notification::make()
->title(__('filament-panels::pages/auth/login.notifications.throttled.title', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
'seconds' => $seconds,
'minutes' => $minutes,
]))
->body(array_key_exists('body', __('filament-panels::pages/auth/login.notifications.throttled') ?: []) ? __('filament-panels::pages/auth/login.notifications.throttled.body', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]) : null)
->body($body)
->danger()
->send();
@@ -39,7 +47,7 @@ class Login extends \Filament\Auth\Pages\Login
$data = $this->form->getState();
if (! Filament::auth()->attempt($this->getCredentialsFromFormData($data), $data['remember'] ?? false)) {
if (! Filament::auth()->attempt($this->getCredentialsFromFormData($data), (bool) ($data['remember'] ?? false))) {
$this->throwFailureValidationException();
}
@@ -47,7 +55,8 @@ class Login extends \Filament\Auth\Pages\Login
if (
($user instanceof FilamentUser) &&
(! $user->canAccessPanel(Filament::getCurrentOrDefaultPanel()))
($panel = Filament::getCurrentOrDefaultPanel()) instanceof \Filament\Panel &&
(! $user->canAccessPanel($panel))
) {
Filament::auth()->logout();
@@ -66,6 +75,9 @@ class Login extends \Filament\Auth\Pages\Login
]);
}
/**
* @return array<\Illuminate\Contracts\Support\Htmlable|string>
*/
protected function getFormSchema(): array
{
return [