🆙 Refactor Cleanup started 🆙

This commit is contained in:
Remco
2026-01-20 18:58:15 +01:00
parent d57e97bb42
commit a71a634dee
8 changed files with 86 additions and 73 deletions
@@ -65,7 +65,7 @@ class AchievementResource extends Resource
Select::make('category')
->native(false)
->label(__('filament::resources.inputs.category'))
->options(AchievementCategory::toInput()),
->options(fn () => AchievementCategory::toInput()),
]),
Tab::make(__('filament::resources.tabs.Configurations'))
@@ -74,7 +74,7 @@ class AchievementResource extends Resource
Select::make('visible')
->native(false)
->label(__('filament::resources.inputs.visible'))
->options([
->options(fn () => [
'1' => __('filament::resources.common.Yes'),
'0' => __('filament::resources.common.No'),
]),
@@ -82,7 +82,7 @@ class AchievementResource extends Resource
Select::make('reward_type')
->native(false)
->label(__('filament::resources.inputs.reward_type'))
->options(CurrencyTypes::toInput()),
->options(fn () => CurrencyTypes::toInput()),
TextInput::make('reward_amount')
->label(__('filament::resources.inputs.reward_amount'))
@@ -137,7 +137,7 @@ class AchievementResource extends Resource
])
->filters([
SelectFilter::make('visible')
->options([
->options(fn () => [
'1' => __('filament::resources.common.Yes'),
'0' => __('filament::resources.common.No'),
])
@@ -145,7 +145,7 @@ class AchievementResource extends Resource
->placeholder(__('filament::resources.common.All')),
SelectFilter::make('category')
->options(AchievementCategory::toInput())
->options(fn () => AchievementCategory::toInput())
->label(__('filament::resources.columns.category'))
->placeholder(__('filament::resources.common.All')),
])
@@ -63,8 +63,7 @@ class BadgeTextEditorResource extends Resource
ImageColumn::make('badge_key')
->label('Badge Image')
->getStateUsing(function (WebsiteBadge $record) use ($badgesPath): string {
$badgeKey = is_string($record->badge_key) ? $record->badge_key : '';
$badgeName = str_replace('badge_desc_', '', $badgeKey);
$badgeName = str_replace('badge_desc_', '', (string) $record->badge_key);
return asset($badgesPath . $badgeName . '.gif');
})
@@ -73,21 +72,16 @@ class BadgeTextEditorResource extends Resource
TextColumn::make('badge_name')
->label('Badge Code & Name')
->formatStateUsing(function (WebsiteBadge $record): string {
$key = is_string($record->badge_key) ? $record->badge_key : '';
$name = is_string($record->badge_name) ? $record->badge_name : '';
return $key . ' : ' . $name;
return (string) $record->badge_key . ' : ' . (string) $record->badge_name;
})
->searchable(query: function ($query, $search): void {
->searchable(query: function (\Illuminate\Database\Eloquent\Builder $query, string $search): void {
$query->where('badge_key', 'like', "%{$search}%")
->orWhere('badge_name', 'like', "%{$search}%");
})
->sortable(),
TextColumn::make('badge_description')
->label('Badge Description')
->getStateUsing(function (WebsiteBadge $record): string {
$desc = is_string($record->badge_description) ? $record->badge_description : '';
return Str::limit($desc, 65);
})
->getStateUsing(fn (WebsiteBadge $record): string => Str::limit((string) $record->badge_description, 65))
->searchable(),
])
->filters([])
@@ -41,10 +41,9 @@ class ListBadgeTextEditors extends ListRecords
];
}
public function exportToJson(SettingsService $settingsService)
public function exportToJson(SettingsService $settingsService): void
{
$jsonPath = $settingsService->getOrDefault('nitro_external_texts_file');
if ($jsonPath === '' || $jsonPath === '0') {
Notification::make()
->title('Export Failed')
@@ -65,16 +64,19 @@ class ListBadgeTextEditors extends ListRecords
return;
}
$jsonData = json_decode(file_get_contents($jsonPath), true);
$raw = @file_get_contents($jsonPath);
$jsonData = is_string($raw) ? json_decode($raw, true) : [];
$jsonData = is_array($jsonData) ? $jsonData : [];
$badges = WebsiteBadge::all();
$badgeKeys = $badges->pluck('badge_key')->toArray();
foreach ($jsonData as $key => $value) {
if (
(str_starts_with((string) $key, 'badge_desc_') || str_starts_with((string) $key, 'badge_name_')) &&
! in_array(str_replace(['badge_desc_', 'badge_name_'], '', $key), $badgeKeys)
) {
if (! is_string($key)) {
continue;
}
$isBadgeKey = str_starts_with($key, 'badge_desc_') || str_starts_with($key, 'badge_name_');
if ($isBadgeKey && ! in_array(str_replace(['badge_desc_', 'badge_name_'], '', $key), $badgeKeys, true)) {
unset($jsonData[$key]);
}
}
@@ -110,7 +112,7 @@ class ListBadgeTextEditors extends ListRecords
}
}
public function createBackup(SettingsService $settingsService)
public function createBackup(SettingsService $settingsService): void
{
$jsonPath = $settingsService->getOrDefault('nitro_external_texts_file');
@@ -56,14 +56,20 @@ class BadgeUploadResource extends Resource
];
}
public static function getFiles(): array
public static function getFiles(): mixed
{
$badgePath = env('BadgePath', 'badges');
$settings = app(\App\Services\SettingsService::class);
$badgePath = $settings->getOrDefault('badge_path_filesystem', 'badges') ?: 'badges';
$files = Storage::disk('local')->files($badgePath);
return collect($files)->map(fn ($file) => [
'filename' => basename($file),
'path' => $file,
])->toArray();
$result = [];
foreach ($files as $file) {
$path = is_string($file) ? $file : '';
$result[] = [
'filename' => $path !== '' ? basename($path) : '',
'path' => $path,
];
}
return $result;
}
}
@@ -5,27 +5,25 @@ namespace App\Filament\Resources\Hotel\BadgeUploads\Pages;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\Page; // Import the Notification class
use Filament\Resources\Pages\Page;
class ManageBadgeUploads extends Page implements HasForms
{
use InteractsWithForms;
public $badge_file;
public ?string $badge_file = null;
protected static string $resource = \App\Filament\Resources\Hotel\BadgeUploads\BadgeUploadResource::class;
protected string $view = 'filament.pages.manage-badge-uploads';
public function mount(): void
{
$this->form->fill([]);
}
public function mount(): void {}
protected function getFormSchema(): array
public function form(Form $form): Form
{
return [
return $form->schema([
FileUpload::make('badge_file')
->label('Upload Badge')
->disk('badges')
@@ -33,13 +31,11 @@ class ManageBadgeUploads extends Page implements HasForms
->acceptedFileTypes(['image/gif'])
->rules(['mimes:gif'])
->required(),
];
]);
}
public function save(): void
{
$this->form->getState();
Notification::make()
->title('Badge uploaded successfully!')
->success()
@@ -451,20 +451,18 @@ class CatalogItemsRelationManager extends RelationManager
])
->action(function (array $data, CatalogItem $record): void {
// Transform any null or empty values to empty strings
$data = collect($data)->map(function ($value) {
if ($value === null || $value === '') {
return '';
}
if (is_bool($value)) {
return $value ? '1' : '0';
}
return $value;
})->toArray();
$normalized = [];
foreach ($data as $key => $value) {
$normalized[(string) $key] = match (true) {
$value === null, $value === '' => '',
is_bool($value) => $value ? '1' : '0',
default => $value,
};
}
$itemBase = $record->itemBase;
if ($itemBase) {
$itemBase->forceFill($data)->save();
$itemBase->forceFill($normalized)->save();
}
})
->visible(function (CatalogItem $record): bool {