🆙 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
@@ -77,6 +77,9 @@ class WebsiteDrawBadgeResource extends Resource
->limit(35)
->tooltip(function (TextColumn $column): ?string {
$state = $column->getState();
if (! is_string($state)) {
return null;
}
if (strlen($state) <= $column->getCharacterLimit()) {
return null;
}
@@ -91,7 +94,7 @@ class WebsiteDrawBadgeResource extends Resource
->getStateUsing(function (WebsiteDrawBadgeModel $record): string {
$appUrl = config('app.url');
$appUrl = is_string($appUrl) ? $appUrl : '';
$badgeUrl = is_string($record->badge_url) ? $record->badge_url : '';
$badgeUrl = (string) $record->badge_url;
return $appUrl . $badgeUrl;
})
->extraAttributes(['style' => 'image-rendering: pixelated'])
@@ -102,7 +105,8 @@ class WebsiteDrawBadgeResource extends Resource
->recordActions([
DeleteAction::make()
->before(function (DeleteAction $action, WebsiteDrawBadge $record): void {
$badgeCode = pathinfo($record->badge_path, PATHINFO_FILENAME);
$badgePath = (string) $record->badge_path;
$badgeCode = pathinfo($badgePath, PATHINFO_FILENAME);
// Remove the badge from any user before deleting it.
if ($record->published) {
@@ -113,27 +117,34 @@ class WebsiteDrawBadgeResource extends Resource
}
// Remove from JSON
$filePath = DB::table('website_settings')->where('key', 'nitro_external_texts_file')->value('value');
$filePathValue = DB::table('website_settings')->where('key', 'nitro_external_texts_file')->value('value');
$filePath = is_string($filePathValue) ? $filePathValue : '';
if ($filePath && file_exists($filePath) && is_writable($filePath)) {
$json = json_decode(file_get_contents($filePath), true);
unset($json["badge_name_{$badgeCode}"]);
unset($json["badge_desc_{$badgeCode}"]);
file_put_contents($filePath, json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
if ($filePath !== '' && file_exists($filePath) && is_writable($filePath)) {
$jsonRaw = @file_get_contents($filePath);
$json = is_string($jsonRaw) ? json_decode($jsonRaw, true) : [];
$data = is_array($json) ? $json : [];
unset($data["badge_name_{$badgeCode}"]);
unset($data["badge_desc_{$badgeCode}"]);
file_put_contents($filePath, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
// Delete the badge file from the filesystem
$badgePath = $record->badge_path;
if ($badgePath && file_exists($badgePath)) {
unlink($badgePath);
$badgePathFs = (string) $record->badge_path;
if ($badgePathFs !== '' && file_exists($badgePathFs)) {
unlink($badgePathFs);
}
}),
])
->toolbarActions([
DeleteBulkAction::make()
->before(function (DeleteBulkAction $action, $records): void {
->before(function (DeleteBulkAction $action, iterable $records): void {
foreach ($records as $record) {
$badgeCode = pathinfo((string) $record->badge_path, PATHINFO_FILENAME);
if (! ($record instanceof WebsiteDrawBadgeModel)) {
continue;
}
$badgePath = (string) $record->badge_path;
$badgeCode = pathinfo($badgePath, PATHINFO_FILENAME);
// Remove the badge from any user before deleting it.
if ($record->published) {
@@ -143,18 +154,21 @@ class WebsiteDrawBadgeResource extends Resource
->delete();
}
$filePath = DB::table('website_settings')->where('key', 'nitro_external_texts_file')->value('value');
$filePathValue = DB::table('website_settings')->where('key', 'nitro_external_texts_file')->value('value');
$filePath = is_string($filePathValue) ? $filePathValue : '';
if ($filePath && file_exists($filePath) && is_writable($filePath)) {
$json = json_decode(file_get_contents($filePath), true);
unset($json["badge_name_{$badgeCode}"]);
unset($json["badge_desc_{$badgeCode}"]);
file_put_contents($filePath, json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
if ($filePath !== '' && file_exists($filePath) && is_writable($filePath)) {
$jsonRaw = @file_get_contents($filePath);
$json = is_string($jsonRaw) ? json_decode($jsonRaw, true) : [];
$data = is_array($json) ? $json : [];
unset($data["badge_name_{$badgeCode}"]);
unset($data["badge_desc_{$badgeCode}"]);
file_put_contents($filePath, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
$badgePath = $record->badge_path;
if ($badgePath && file_exists($badgePath)) {
unlink($badgePath);
$badgePathFs = (string) $record->badge_path;
if ($badgePathFs !== '' && file_exists($badgePathFs)) {
unlink($badgePathFs);
}
}
}),
@@ -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 {
+3
View File
@@ -99,3 +99,6 @@
"}
[2026-01-20 17:19:05] production.ERROR: RCON connection failed: Kan geen verbinding maken omdat de doelcomputer de verbinding actief heeft geweigerd
[2026-01-20 17:19:06] production.ERROR: RCON connection failed: Kan geen verbinding maken omdat de doelcomputer de verbinding actief heeft geweigerd
[2026-01-20 17:32:06] production.ERROR: RCON connection failed: Kan geen verbinding maken omdat de doelcomputer de verbinding actief heeft geweigerd
[2026-01-20 17:55:31] production.ERROR: RCON connection failed: Kan geen verbinding maken omdat de doelcomputer de verbinding actief heeft geweigerd
[2026-01-20 17:55:31] production.ERROR: RCON connection failed: Kan geen verbinding maken omdat de doelcomputer de verbinding actief heeft geweigerd