Initial commit

This commit is contained in:
root
2026-05-09 17:28:23 +02:00
commit 9d73f82529
5575 changed files with 281989 additions and 0 deletions
@@ -0,0 +1,115 @@
<?php
declare(strict_types=1);
namespace App\Filament\Resources\Hotel\CatalogEditors;
use App\Models\Game\Furniture\CatalogPage;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
class CatalogEditorResource extends Resource
{
#[\Override]
protected static ?string $model = CatalogPage::class;
#[\Override]
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-rectangle-stack';
#[\Override]
protected static string|\UnitEnum|null $navigationGroup = 'Hotel';
#[\Override]
protected static ?string $navigationLabel = 'Catalog Editor';
#[\Override]
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id')->label('ID')->sortable(),
TextColumn::make('caption')->label('Page Name')->searchable(),
TextColumn::make('parent_id')->label('Parent ID'),
TextColumn::make('order_num')->label('Order'),
IconColumn::make('visible')->boolean()->label('Visible'),
IconColumn::make('enabled')->boolean()->label('Enabled'),
])
->recordActions([])
->toolbarActions([]);
}
#[\Override]
public static function form(Schema $schema): Schema
{
return $schema
->components([
TextInput::make('caption')
->label('Page Name')
->required()
->maxLength(128),
TextInput::make('caption_save')
->label('Name TAG')
->maxLength(25)
->helperText('Lowercase letters only (a-z), no spaces.'),
Select::make('parent_id')
->label('Parent Page')
->options(fn () => CatalogPage::all()
->pluck('caption', 'id')
->toArray())
->default(-1),
TextInput::make('order_num')
->label('Order')
->numeric()
->default(1),
TextInput::make('icon_image')
->label('Icon Number')
->numeric()
->default(1),
TextInput::make('page_layout')
->label('Page Layout')
->default('default_3x3'),
TextInput::make('min_rank')
->label('Min Rank')
->numeric()
->default(1),
Toggle::make('visible')
->label('Visible')
->default(true),
Toggle::make('enabled')
->label('Enabled')
->default(true),
Toggle::make('club_only')
->label('Club Only (HC)')
->default(false),
Toggle::make('vip_only')
->label('VIP Only')
->default(false),
]);
}
#[\Override]
public static function getPages(): array
{
return [
'index' => Pages\ManageCatalogEditor::route('/'),
'create' => Pages\CreateCatalogEditor::route('/create'),
'view' => Pages\ViewCatalogEditor::route('/{record}/view'),
];
}
}
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace App\Filament\Resources\Hotel\CatalogEditors\Pages;
use App\Filament\Resources\Hotel\CatalogEditors\CatalogEditorResource;
use Filament\Resources\Pages\CreateRecord;
class CreateCatalogEditor extends CreateRecord
{
#[\Override]
protected static string $resource = CatalogEditorResource::class;
}
@@ -0,0 +1,896 @@
<?php
namespace App\Filament\Resources\Hotel\CatalogEditors\Pages;
use App\Filament\Resources\Hotel\CatalogEditors\CatalogEditorResource;
use App\Models\Game\Furniture\CatalogItem;
use App\Models\Game\Furniture\CatalogPage;
use App\Models\Miscellaneous\WebsiteSetting;
use Filament\Actions\Action as FilamentAction;
use Filament\Actions\EditAction;
use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\Page;
use Filament\Tables;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Table;
use Illuminate\Support\Facades\DB;
class ManageCatalogEditor extends Page implements HasTable
{
use InteractsWithTable;
#[\Override]
protected static string $resource = CatalogEditorResource::class;
#[\Override]
protected string $view = 'filament.resources.hotel.catalog-editors.pages.manage-catalog-editor';
public string $search = '';
public string $pageSearch = '';
public ?CatalogPage $selectedPage = null;
/** @var array<mixed> */
public array $expandedPages = [];
/** @var array<mixed> */
public array $selectedItemIds = [];
#[\Override]
protected function getHeaderActions(): array
{
return [
FilamentAction::make('createPage')
->label('New Page')
->icon('heroicon-m-plus')
->url(fn () => static::getResource()::getUrl('create')),
FilamentAction::make('editPageHeader')
->label('Edit Page')
->icon('heroicon-m-pencil')
->button()
->action(function () {
if ($this->selectedPage instanceof CatalogPage) {
$this->selectedPage->refresh();
$this->dispatch('open-edit-page', pageId: $this->selectedPage->id);
}
})
->modalHeading('Edit page')
->modalSubmitActionLabel('Save')
->modalWidth('3xl')
->form([
Forms\Components\TextInput::make('caption')->label('Name')->maxLength(128)->required(),
Forms\Components\TextInput::make('caption_save')->label('Name TAG')->maxLength(25)->nullable(),
Forms\Components\TextInput::make('order_num')->label('Order')->numeric()->minValue(0)->required(),
Forms\Components\TextInput::make('icon_image')->label('Icon number')->numeric()->minValue(1)->required(),
])
->fillForm(function () {
if (! $this->selectedPage instanceof CatalogPage) {
return [];
}
return [
'caption' => $this->selectedPage->caption ?? '',
'caption_save' => $this->selectedPage->caption_save ?? '',
'order_num' => $this->selectedPage->order_num ?? 1,
'icon_image' => $this->selectedPage->icon_image ?? 1,
];
})
->action(function (array $data) {
if ($this->selectedPage instanceof CatalogPage) {
$this->selectedPage->update($data);
Notification::make()->title('Page updated')->success()->send();
}
}),
];
}
protected function escapeLike(string $value, string $escapeChar = '\\'): string
{
return str_replace(
[$escapeChar, '%', '_'],
[$escapeChar . $escapeChar, $escapeChar . '%', $escapeChar . '_'],
$value,
);
}
public function selectPage(int $pageId): void
{
$this->selectedPage = CatalogPage::find($pageId);
$this->selectedItemIds = [];
if ($this->pageSearch !== '') {
$this->pageSearch = '';
}
$this->expandedPages = $this->collectParentIds($pageId);
$this->resetTable();
}
protected function collectParentIds(int $pageId): array
{
$pages = CatalogPage::pluck('parent_id', 'id');
$ids = [$pageId];
$parentId = $pages[$pageId] ?? null;
while ($parentId && $parentId > 0) {
$ids[] = $parentId;
$parentId = $pages[$parentId] ?? null;
}
return array_unique($ids);
}
#[\Override]
public function getMaxContentWidth(): ?string
{
return 'full';
}
public function resetView(): void
{
$this->pageSearch = '';
$this->selectedPage = null;
$this->expandedPages = [];
$this->selectedItemIds = [];
$this->resetTable();
Notification::make()
->title('View reset')
->body('Catalog view restored to default.')
->success()
->send();
}
public function toggleExpand(int $pageId): void
{
if (in_array($pageId, $this->expandedPages, true)) {
$this->expandedPages = array_values(array_diff($this->expandedPages, [$pageId]));
} else {
$this->expandedPages[] = $pageId;
}
}
public function isExpanded(int $pageId): bool
{
return in_array($pageId, $this->expandedPages, true);
}
public function toggleSelectItem(int $itemId, bool $ctrl = false): void
{
if ($ctrl) {
if (in_array($itemId, $this->selectedItemIds, true)) {
$this->selectedItemIds = array_values(array_diff($this->selectedItemIds, [$itemId]));
} else {
$this->selectedItemIds[] = $itemId;
}
} else {
$this->selectedItemIds = [$itemId];
}
$this->resetTable();
}
public function updatedPageSearch(): void
{
$this->resetTable();
$needle = trim($this->pageSearch);
if ($needle === '') {
return;
}
$like = '%' . $this->escapeLike($needle) . '%';
$matchingPage = CatalogPage::query()
->whereRaw("caption LIKE ? ESCAPE '\\\\'", [$like])
->first();
if ($matchingPage) {
$this->selectedPage = $matchingPage;
$this->expandedPages[] = $matchingPage->id;
$this->resetTable();
$this->dispatch('scroll-to-page', id: $matchingPage->id);
return;
}
$matchingItem = CatalogItem::query()
->whereRaw("catalog_name LIKE ? ESCAPE '\\\\'", [$like])
->orWhere('id', ctype_digit($needle) ? (int) $needle : -1)
->first();
if ($matchingItem) {
$page = CatalogPage::find($matchingItem->page_id);
if ($page) {
$this->selectedPage = $page;
$this->expandedPages[] = $page->id;
$this->selectedItemIds = [$matchingItem->id];
$this->resetTable();
$this->dispatch('scroll-to-page', id: $page->id);
Notification::make()
->title('Item found')
->body("Opened page: {$page->caption}")
->success()
->send();
}
}
}
public function getTableQuery()
{
if (! $this->selectedPage instanceof CatalogPage) {
return CatalogItem::query()->whereRaw('1=0');
}
$query = CatalogItem::query()
->where('page_id', $this->selectedPage->id);
if (filled($this->pageSearch)) {
$needle = trim($this->pageSearch);
$like = '%' . $this->escapeLike($needle) . '%';
$isNumeric = ctype_digit($needle);
$query->where(function ($q) use ($like, $needle, $isNumeric) {
$q->whereRaw("catalog_name LIKE ? ESCAPE '\\\\'", [$like]);
if ($isNumeric) {
$n = (int) $needle;
$q->orWhere('id', $n)
->orWhere('cost_credits', $n)
->orWhere('cost_points', $n)
->orWhere('points_type', $n);
}
});
}
if (! $this->getTableSortColumn()) {
$query->orderBy('order_number')->orderBy('catalog_name')->orderBy('id');
}
return $query;
}
protected function findPrevNeighbor(CatalogItem $record): ?CatalogItem
{
return CatalogItem::query()
->where('page_id', $record->page_id)
->where('order_number', '!=', -1)
->where(function ($q) use ($record) {
$q->where('order_number', '<', $record->order_number)
->orWhere(function ($q2) use ($record) {
$q2->where('order_number', $record->order_number)
->where('id', '<', $record->id);
});
})
->orderBy('order_number', 'desc')
->orderBy('id', 'desc')
->first();
}
protected function findNextNeighbor(CatalogItem $record): ?CatalogItem
{
return CatalogItem::query()
->where('page_id', $record->page_id)
->where('order_number', '!=', -1)
->where(function ($q) use ($record) {
$q->where('order_number', '>', $record->order_number)
->orWhere(function ($q2) use ($record) {
$q2->where('order_number', $record->order_number)
->where('id', '>', $record->id);
});
})
->orderBy('order_number', 'asc')
->orderBy('id', 'asc')
->first();
}
protected function canMoveUp(CatalogItem $record): bool
{
if ($record->order_number === -1) {
return false;
}
return (bool) $this->findPrevNeighbor($record);
}
protected function canMoveDown(CatalogItem $record): bool
{
if ($record->order_number === -1) {
return false;
}
return (bool) $this->findNextNeighbor($record);
}
protected function nudgeRecord(CatalogItem $record, string $direction): void
{
if ($record->order_number === -1) {
Notification::make()->title('Locked')->body('This item is locked (order = -1).')->danger()->send();
return;
}
$neighbor = $direction === 'up'
? $this->findPrevNeighbor($record)
: $this->findNextNeighbor($record);
if (! $neighbor instanceof CatalogItem) {
return;
}
DB::transaction(function () use ($record, $neighbor) {
$a = $record->order_number;
$b = $neighbor->order_number;
$record->update(['order_number' => $b]);
$neighbor->update(['order_number' => $a]);
});
$this->normalizeOrderForSelectedPage();
Notification::make()->title('Order updated')->success()->send();
}
protected function normalizeOrderForSelectedPage(): void
{
if (! $this->selectedPage?->id) {
return;
}
$items = CatalogItem::query()
->where('page_id', $this->selectedPage->id)
->where('order_number', '!=', -1)
->orderBy('order_number')
->orderBy('id')
->get(['id']);
DB::transaction(function () use ($items) {
foreach ($items->values() as $index => $item) {
CatalogItem::whereKey($item->id)
->update(['order_number' => ($index + 1) * 10]);
}
});
$this->resetTable();
}
public function pageHasLockedItems(): bool
{
if (! $this->selectedPage?->id) {
return false;
}
return CatalogItem::query()
->where('page_id', $this->selectedPage->id)
->where('order_number', -1)
->exists();
}
public function autoOrderItems(): void
{
if (! $this->selectedPage?->id) {
Notification::make()->title('Select a page first')->warning()->send();
return;
}
if ($this->pageHasLockedItems()) {
Notification::make()
->title('Action not allowed')
->body('This page contains item(s) with order_number = -1. Remove or change them before auto-ordering.')
->danger()
->send();
return;
}
$affected = CatalogItem::query()
->where('page_id', $this->selectedPage->id)
->where('order_number', '!=', -1)
->update(['order_number' => 99]);
$this->resetTable();
if ($affected > 0) {
Notification::make()->title('Items auto-ordered')->body("Updated {$affected} item(s).")->success()->send();
} else {
Notification::make()->title('Nothing to update')->body('No items were changed (none on this page or all are set to -1).')->warning()->send();
}
}
public function manualOrderItems(): void
{
if (! $this->selectedPage?->id) {
Notification::make()->title('Select a page first')->warning()->send();
return;
}
if ($this->pageHasLockedItems()) {
Notification::make()->title('Action not allowed')->body('This page contains item(s) with order_number = -1. Change/remove them before manual ordering.')->danger()->send();
return;
}
$items = CatalogItem::query()
->where('page_id', $this->selectedPage->id)
->where('order_number', '!=', -1)
->orderBy('catalog_name', 'asc')
->orderBy('id', 'asc')
->get(['id']);
if ($items->isEmpty()) {
Notification::make()->title('Nothing to update')->body('No items on this page (or all are locked to -1).')->warning()->send();
return;
}
DB::transaction(function () use ($items) {
foreach ($items->values() as $index => $item) {
CatalogItem::whereKey($item->id)->update(['order_number' => ($index + 1) * 10]);
}
});
$this->resetTable();
Notification::make()->title('Items manually ordered')->body('Items sorted A→Z and numbered 10, 20, 30, …')->success()->send();
}
public function table(Table $table): Table
{
return $table->paginated(false);
}
protected function getTableColumns(): array
{
return [
Tables\Columns\ViewColumn::make('select_item')
->label('')
->view('filament.tables.columns.catalog-item-select')
->viewData([
'itemId' => fn ($record) => $record->id,
'isSelected' => fn ($record) => in_array($record->id, $this->selectedItemIds, true),
])
->width('36px')
->sortable(false)
->searchable(false),
Tables\Columns\ViewColumn::make('item_display')
->label('Item')
->view('filament.tables.columns.catalog-item-draggable')
->viewData([
'icon' => fn ($record) => $this->buildFurniIconUrl($record->catalog_name),
'name' => fn ($record) => $record->catalog_name,
'itemId' => fn ($record) => $record->id,
'isSelected' => fn ($record) => in_array($record->id, $this->selectedItemIds, true),
])
->sortable(false)
->searchable(false),
Tables\Columns\TextColumn::make('cost_credits')
->label('Credits')
->sortable(),
Tables\Columns\TextColumn::make('cost_points')
->label('Points')
->sortable(),
Tables\Columns\TextColumn::make('points_type')
->label('Type')
->sortable(),
Tables\Columns\TextColumn::make('amount')
->label('Amount')
->sortable(),
Tables\Columns\TextColumn::make('order_number')
->label('Order')
->sortable()
->toggleable(),
Tables\Columns\IconColumn::make('club_only')
->boolean()
->label('Club Only')
->sortable(),
];
}
protected function getTableActions(): array
{
return [
FilamentAction::make('move_up')
->label('')
->icon('heroicon-m-chevron-up')
->color('gray')
->tooltip('Move up')
->action(fn (CatalogItem $record) => $this->nudgeRecord($record, 'up'))
->visible(fn (CatalogItem $record) => $this->pageSearch === '' && $this->canMoveUp($record))
->size('sm'),
FilamentAction::make('move_down')
->label('')
->icon('heroicon-m-chevron-down')
->color('gray')
->tooltip('Move down')
->action(fn (CatalogItem $record) => $this->nudgeRecord($record, 'down'))
->visible(fn (CatalogItem $record) => $this->pageSearch === '' && $this->canMoveDown($record))
->size('sm'),
EditAction::make('edit')
->label('Edit')
->icon('heroicon-m-pencil-square')
->modalHeading('Edit catalog item')
->modalSubmitActionLabel('Save')
->modalWidth('md')
->form([
Forms\Components\TextInput::make('cost_credits')->label('Credits')->numeric()->minValue(0)->required(),
Forms\Components\TextInput::make('cost_points')->label('Points')->numeric()->minValue(0)->required(),
Forms\Components\TextInput::make('points_type')->label('Type')->numeric()->minValue(0)->maxValue(999)->maxLength(50),
Forms\Components\TextInput::make('amount')->label('Amount')->numeric()->minValue(1)->default(1)->required(),
Forms\Components\TextInput::make('order_number')
->label('Order')
->numeric()
->minValue(-1)
->step(1)
->helperText('Use -1 to lock, or a non-negative number to sort (lower = earlier).')
->required(),
Forms\Components\Toggle::make('club_only')->label('Club only'),
])
->fillForm(fn (CatalogItem $record) => [
'cost_credits' => $record->cost_credits,
'cost_points' => $record->cost_points,
'points_type' => $record->points_type,
'amount' => $record->amount,
'order_number' => $record->order_number,
'club_only' => $record->club_only === '1',
])
->action(function (CatalogItem $record, array $data): void {
$record->update([
'cost_credits' => (int) $data['cost_credits'],
'cost_points' => (int) $data['cost_points'],
'points_type' => $data['points_type'] ?? null,
'amount' => (int) $data['amount'],
'order_number' => (int) $data['order_number'],
'club_only' => empty($data['club_only']) ? '0' : '1',
]);
$this->resetTable();
Notification::make()->title('Item updated')->success()->send();
}),
];
}
#[\Override]
protected function getActions(): array
{
return [
FilamentAction::make('editPage')
->label('Edit page')
->modalHeading(function (array $arguments): string {
$page = CatalogPage::find($arguments['pageId'] ?? null);
return $page ? 'Edit: ' . $page->caption : 'Edit page';
})
->modalSubmitActionLabel('Save')
->modalWidth('3xl')
->form([
Forms\Components\TextInput::make('caption')->label('Name')->maxLength(128)->required(),
Forms\Components\TextInput::make('caption_save')
->label('Name TAG')
->maxLength(25)
->nullable(),
Forms\Components\TextInput::make('order_num')
->label('Order')
->numeric()
->minValue(0)
->step(1)
->required()
->helperText('Lower number appears earlier in the menu.'),
Forms\Components\TextInput::make('icon_image')
->label('Icon number')
->numeric()
->minValue(1)
->required()
->default(1),
])
->fillForm(function (array $arguments): array {
$page = CatalogPage::find($arguments['pageId'] ?? null);
return [
'caption' => $page?->caption ?? '',
'caption_save' => $page?->caption_save ?? '',
'order_num' => $page?->order_num ?? 1,
'icon_image' => $page?->icon_image ?? 1,
];
})
->action(function (array $data, array $arguments): void {
$page = CatalogPage::find($arguments['pageId'] ?? null);
if (! $page) {
Notification::make()->title('Page not found')->danger()->send();
return;
}
$icon = max(1, (int) ($data['icon_image'] ?: 1));
$page->update([
'caption' => $data['caption'],
'caption_save' => $data['caption_save'] ?? '',
'order_num' => (int) ($data['order_num'] ?? 1),
'icon_image' => $icon,
]);
$this->selectPage($page->id);
Notification::make()->title('Page updated')->success()->send();
}),
];
}
public function openEditPage(int $pageId): void
{
$this->mountAction('editPage', ['pageId' => $pageId]);
}
public function moveItemToPage(int $itemId, int $targetPageId): void
{
$this->moveItemsToPage((string) $itemId, $targetPageId);
}
public function moveItemsToPage(string $itemIdsCsv, int $targetPageId): void
{
$target = CatalogPage::find($targetPageId);
$ids = collect(explode(',', $itemIdsCsv))
->map(fn ($v) => (int) trim($v))
->filter(fn ($v) => $v > 0)
->unique()
->values()
->all();
if (empty($ids) || ! $target) {
Notification::make()->title('Move failed')->body('No items selected or target page not found.')->danger()->send();
return;
}
DB::transaction(function () use ($ids, $targetPageId) {
$maxOrder = (int) (CatalogItem::where('page_id', $targetPageId)->max('order_number') ?? 0);
foreach ($ids as $i => $id) {
CatalogItem::whereKey($id)->update([
'page_id' => $targetPageId,
'order_number' => $maxOrder + 1 + $i,
]);
}
});
$this->resetTable();
$this->selectedItemIds = [];
$this->dispatch('$refresh');
Notification::make()
->title('Items moved')
->body('Moved ' . count($ids) . ' item(s) to: ' . ($target->caption ?? ('#' . $targetPageId)))
->success()
->send();
}
protected function buildFurniIconUrl(string $catalogName): string
{
$base = $this->getFurniIconBasePath();
$safeName = str_replace('*', '_', $catalogName);
$path = rtrim($base, '/') . '/' . $safeName . '_icon.png';
if (preg_match('#^(https?:)?//#', $path)) {
return $path;
}
return asset($path);
}
protected function getFurniIconBasePath(): string
{
$setting = WebsiteSetting::where('key', 'furniture_icons_path')->first();
return $setting && $setting->value ? rtrim($setting->value, '/') : '/images/furniture';
}
protected function getCatalogIconBasePath(): string
{
$setting = WebsiteSetting::where('key', 'catalog_icons_path')->first();
return $setting && $setting->value ? rtrim($setting->value, '/') : '/gamedata/c_images/catalogue';
}
protected function buildCatalogIconUrl(int $iconImage): string
{
$iconImage = $iconImage > 0 ? $iconImage : 1;
$base = $this->getCatalogIconBasePath();
$path = $base . '/icon_' . $iconImage . '.png';
if (preg_match('#^(https?:)?//#', $path)) {
return $path;
}
return asset($path);
}
public function reorderItems(array $orderedIds): void
{
if (filled($this->pageSearch)) {
Notification::make()
->title('Ordering disabled in search mode')
->body('You cannot reorder items while viewing search results.')
->warning()
->send();
return;
}
if (! $this->selectedPage?->id) {
return;
}
DB::transaction(function () use ($orderedIds) {
foreach ($orderedIds as $index => $id) {
CatalogItem::whereKey($id)->update([
'order_number' => ($index + 1) * 10,
]);
}
});
$this->normalizeOrderForSelectedPage();
Notification::make()
->title('Items reordered')
->success()
->send();
$this->resetTable();
}
protected function getTableHeaderActions(): array
{
return [
FilamentAction::make('massEdit')
->label('Mass edit selected')
->icon('heroicon-m-pencil-square')
->color('primary')
->disabled(fn () => $this->selectedItemIds === [])
->modalHeading('Edit selected catalog items')
->modalSubmitActionLabel('Apply changes')
->modalWidth('lg')
->form([
Forms\Components\TextInput::make('cost_credits')->label('Credits')->numeric()->minValue(0)->nullable()->helperText('Leave empty to keep unchanged'),
Forms\Components\TextInput::make('cost_points')->label('Points')->numeric()->minValue(0)->nullable()->helperText('Leave empty to keep unchanged'),
Forms\Components\TextInput::make('points_type')->label('Type (points_type)')->numeric()->minValue(0)->maxValue(999)->nullable()->helperText('Leave empty to keep unchanged'),
Forms\Components\TextInput::make('amount')->label('Amount')->numeric()->minValue(1)->nullable()->helperText('Leave empty to keep unchanged'),
Forms\Components\TextInput::make('order_number')->label('Order')->numeric()->minValue(-1)->nullable()->helperText('Leave empty to keep unchanged'),
Forms\Components\Select::make('club_only')
->label('Club only')
->options(['' => '— No change —', '1' => 'Yes', '0' => 'No'])
->native(false)
->nullable()
->default('')
->helperText('Choose Yes/No, or leave as "No change"'),
])
->action(function (array $data): void {
$ids = collect($this->selectedItemIds)
->filter(fn ($v) => (int) $v > 0)
->map(fn ($v) => (int) $v)
->values()
->all();
if (empty($ids)) {
Notification::make()->title('No items selected')->warning()->send();
return;
}
$updates = [];
if ($data['cost_credits'] !== null && $data['cost_credits'] !== '') {
$updates['cost_credits'] = (int) $data['cost_credits'];
}
if ($data['cost_points'] !== null && $data['cost_points'] !== '') {
$updates['cost_points'] = (int) $data['cost_points'];
}
if ($data['points_type'] !== null && $data['points_type'] !== '') {
$updates['points_type'] = (int) $data['points_type'];
}
if ($data['amount'] !== null && $data['amount'] !== '') {
$updates['amount'] = (int) $data['amount'];
}
if ($data['order_number'] !== null && $data['order_number'] !== '') {
$updates['order_number'] = (int) $data['order_number'];
}
if ($data['club_only'] !== null && $data['club_only'] !== '') {
$updates['club_only'] = $data['club_only'] === '1' ? '1' : '0';
}
if ($updates === []) {
Notification::make()->title('Nothing to update')->body('Fill at least one field to apply to the selected items.')->warning()->send();
return;
}
CatalogItem::whereIn('id', $ids)->update($updates);
$count = count($ids);
$this->resetTable();
$this->selectedItemIds = [];
Notification::make()->title('Updated items')->body("Applied changes to {$count} item(s).")->success()->send();
}),
FilamentAction::make('updateOrder')
->label('Update Order')
->icon('heroicon-o-arrow-path')
->color('secondary')
->visible(fn () => $this->selectedPage && $this->pageSearch === '')
->requiresConfirmation()
->modalHeading('Confirm Update Order')
->modalDescription('This will save the current item order (as currently sorted) into the database. Continue?')
->modalSubmitActionLabel('Update Order')
->action(function (): void {
if (! $this->selectedPage?->id) {
Notification::make()->title('No page selected')->warning()->send();
return;
}
if ($this->pageSearch !== '') {
Notification::make()
->title('Disabled in search mode')
->body('Cannot update order while search results are active.')
->warning()
->send();
return;
}
$sortColumn = $this->getTableSortColumn();
$sortDirection = $this->getTableSortDirection() ?? 'asc';
$query = $this->getTableQuery();
if ($sortColumn) {
$query->orderBy($sortColumn, $sortDirection);
} else {
$query->orderBy('order_number')->orderBy('id');
}
$items = $query->get(['id']);
if ($items->isEmpty()) {
Notification::make()->title('No items')->warning()->send();
return;
}
DB::transaction(function () use ($items) {
foreach ($items->values() as $index => $item) {
CatalogItem::whereKey($item->id)->update([
'order_number' => ($index + 1) * 10,
]);
}
});
$this->resetTable();
Notification::make()->title('Order updated')->success()->send();
}),
];
}
}
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Filament\Resources\Hotel\CatalogEditors\Pages;
use App\Filament\Resources\Hotel\CatalogEditors\CatalogEditorResource;
use Filament\Actions\DeleteAction;
use Filament\Resources\Pages\ViewRecord;
class ViewCatalogEditor extends ViewRecord
{
#[\Override]
protected static string $resource = CatalogEditorResource::class;
#[\Override]
protected function getHeaderActions(): array
{
return [
DeleteAction::make(),
];
}
}