mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
Add availability field to URLs for tracking product stock status (in stock, pre-order, back order, special order, out of stock, discontinued). Includes configurable match rules per store with exact match and regex support, priority-based matching, and a default status fallback. Out-of-stock products can now be added with price set to 0. Added The Tech Geeks store with availability detection and seeded Ubiquiti products.
228 lines
10 KiB
PHP
228 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Enums\Icons;
|
|
use App\Enums\ScraperService;
|
|
use App\Enums\StockStatus;
|
|
use App\Filament\Concerns\HasScraperTrait;
|
|
use App\Filament\Pages\AppSettingsPage;
|
|
use App\Filament\Resources\StoreResource\Pages\CreateStore;
|
|
use App\Filament\Resources\StoreResource\Pages\EditStore;
|
|
use App\Filament\Resources\StoreResource\Pages\ListStores;
|
|
use App\Filament\Resources\StoreResource\Pages\TestStore;
|
|
use App\Models\Store;
|
|
use App\Providers\Filament\AdminPanelProvider;
|
|
use App\Rules\StoreUrl;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Components\Section;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Form;
|
|
use Filament\Forms\Get;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Support\Enums\FontWeight;
|
|
use Filament\Tables\Actions\BulkActionGroup;
|
|
use Filament\Tables\Actions\DeleteBulkAction;
|
|
use Filament\Tables\Actions\EditAction;
|
|
use Filament\Tables\Columns\Layout\Split;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Filters\SelectFilter;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\HtmlString;
|
|
|
|
class StoreResource extends Resource
|
|
{
|
|
use HasScraperTrait;
|
|
|
|
public const array DEFAULT_SELECTORS = [
|
|
'title' => 'meta[property=og:title]|content',
|
|
'price' => 'meta[property=og:price:amount]|content',
|
|
'image' => 'meta[property=og:image]|content',
|
|
];
|
|
|
|
public const string API_GROUP = 'Store';
|
|
|
|
protected static ?string $model = Store::class;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-building-storefront';
|
|
|
|
protected static ?string $recordTitleAttribute = 'name';
|
|
|
|
protected static ?int $navigationSort = 20;
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Forms\Components\Section::make('Basics')->schema([
|
|
TextInput::make('name')
|
|
->label('Name')
|
|
->hintIcon(Icons::Help->value, 'The name of the store')
|
|
->required(),
|
|
])
|
|
->columns(1)
|
|
->description(__('Stores are shared between all users in :name', ['name' => config('app.name')]))
|
|
->live(),
|
|
|
|
Forms\Components\Section::make('Domains')->schema([
|
|
Forms\Components\Repeater::make('domains')
|
|
->schema([
|
|
TextInput::make('domain')->label('Domain'),
|
|
])->required(),
|
|
])
|
|
->description('What domains does this store apply to'),
|
|
|
|
Forms\Components\Group::make([
|
|
Forms\Components\Section::make('Title strategy')->schema([
|
|
Forms\Components\Group::make(self::makeStrategyInput('title', self::DEFAULT_SELECTORS['title']))->columns(2),
|
|
])->description('How to get the product title'),
|
|
Forms\Components\Section::make('Price strategy')->schema([
|
|
Forms\Components\Group::make(self::makeStrategyInput('price', self::DEFAULT_SELECTORS['price']))->columns(2),
|
|
])->description('How to get the product price'),
|
|
Forms\Components\Section::make('Image strategy')->schema([
|
|
Forms\Components\Group::make(self::makeStrategyInput('image', self::DEFAULT_SELECTORS['image']))->columns(2),
|
|
])->description('How to get the product image'),
|
|
Forms\Components\Section::make('Availability strategy')->schema([
|
|
Forms\Components\Group::make(self::makeStrategyInput('availability', required: false))->columns(2),
|
|
Forms\Components\Section::make('Match values')
|
|
->schema(
|
|
collect(StockStatus::nonInStockCases())->map(
|
|
fn (StockStatus $status) => Forms\Components\Group::make([
|
|
Forms\Components\Select::make('availability.match.'.$status->value.'.type')
|
|
->label('Type')
|
|
->options([
|
|
'match' => 'Exact match',
|
|
'regex' => 'Regex',
|
|
])
|
|
->default('match')
|
|
->afterStateHydrated(fn (Forms\Components\Select $component, ?string $state) => $component->state($state ?? 'match'))
|
|
->required(),
|
|
TextInput::make('availability.match.'.$status->value.'.value')
|
|
->label($status->getLabel())
|
|
->hintIcon($status->getIcon(), 'If the scraped text matches this value, the product will be marked as "'.$status->getLabel().'"'),
|
|
])->columns(2)
|
|
)->toArray()
|
|
)
|
|
->description('Map scraped text values to stock statuses. Order is priority (first match wins).')
|
|
->columns(1)
|
|
->collapsed(fn (Get $get): bool => empty(array_filter(
|
|
(array) $get('availability.match'),
|
|
fn ($entry, $key) => $key !== 'default' && (is_array($entry) ? ($entry['value'] ?? '') !== '' : ($entry !== '' && $entry !== null)),
|
|
ARRAY_FILTER_USE_BOTH,
|
|
))),
|
|
Forms\Components\Select::make('availability.match.default')
|
|
->label('Default status')
|
|
->options(StockStatus::class)
|
|
->default(StockStatus::InStock->value)
|
|
->afterStateHydrated(fn (Forms\Components\Select $component, ?string $state) => $component->state($state ?? StockStatus::InStock->value))
|
|
->required()
|
|
->hintIcon(Icons::Help->value, 'The status to use when the scraped text does not match any of the values above'),
|
|
])->description('Optional: a selector that matches product availability.')
|
|
->collapsed(fn (Get $get): bool => ($get('availability.value') ?? '') === ''),
|
|
])
|
|
->label('Scrape Strategy')
|
|
->statePath('scrape_strategy'),
|
|
|
|
self::getScraperSettings(),
|
|
|
|
Section::make('Locale')
|
|
->description(__('Override region and locale settings for this store'))
|
|
->columns(2)
|
|
->schema(AppSettingsPage::getLocaleFormFields('settings.locale_settings')),
|
|
|
|
Forms\Components\Section::make('Cookies')->schema([
|
|
TextInput::make('cookies')
|
|
->label('Cookies')
|
|
->hintIcon(Icons::Help->value, 'Any cookies to include in scrape requests for this store. Format as you would in an HTTP header, e.g. "cookie1=value; cookie2=value"'),
|
|
])->description('Optional cookies to include in scrape requests for this store'),
|
|
|
|
Forms\Components\Section::make('Notes')->schema([
|
|
Forms\Components\RichEditor::make('notes')
|
|
->hiddenLabel(true),
|
|
])->description('Additional notes regarding this store and how to scrape its content'),
|
|
])
|
|
->columns(1);
|
|
}
|
|
|
|
public static function testForm(Form $form): Form
|
|
{
|
|
return $form->schema([
|
|
Forms\Components\Section::make('Test url scrape')->schema([
|
|
TextInput::make('test_url')
|
|
->label('Product URL')
|
|
->hintIcon(Icons::Help->value, 'The URL to scrape')
|
|
->required()
|
|
->rules([new StoreUrl]),
|
|
])
|
|
->description('See the results of scraping a url using the current store settings')
|
|
->columns(1),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
Split::make([
|
|
Split::make([
|
|
TextColumn::make('name')
|
|
->searchable()
|
|
->sortable()
|
|
->weight(FontWeight::Bold)
|
|
->description(fn (Store $record): HtmlString => $record->domains_html),
|
|
]),
|
|
TextColumn::make('products_count')
|
|
->sortable()
|
|
->formatStateUsing(fn (string $state) => $state.' products')
|
|
->extraAttributes(['class' => 'min-w-36 md:flex md:justify-end pr-4'])
|
|
->grow(false),
|
|
TextColumn::make('settings.scraper_service')
|
|
->label('Scraper')
|
|
->badge()
|
|
->sortable()
|
|
->extraAttributes(['class' => 'min-w-16'])
|
|
->formatStateUsing(fn (string $state) => strtoupper($state))
|
|
->color(fn (Store $record): array => ScraperService::tryFrom($record->scraper_service)->getColor())
|
|
->grow(false),
|
|
])->from('sm'),
|
|
|
|
])
|
|
->paginated(AdminPanelProvider::DEFAULT_PAGINATION)
|
|
->defaultSort('name')
|
|
->filters([
|
|
SelectFilter::make('settings->scraper_service')
|
|
->options(ScraperService::class)
|
|
->label('Scraper'),
|
|
])
|
|
->actions([
|
|
EditAction::make(),
|
|
])
|
|
->bulkActions([
|
|
BulkActionGroup::make([
|
|
DeleteBulkAction::make(),
|
|
]),
|
|
])
|
|
->modifyQueryUsing(function (Builder $query) {
|
|
$query->withCount('products');
|
|
});
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
//
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => ListStores::route('/'),
|
|
'create' => CreateStore::route('/create'),
|
|
'edit' => EditStore::route('/{record}/edit'),
|
|
'test' => TestStore::route('/{record}/test'),
|
|
];
|
|
}
|
|
}
|