main

filamentphp/demo

Last updated at: 29/12/2023 09:53

CategoryImporter.php

TLDR

This file, CategoryImporter.php, is a part of the Demo Projects project. It is located at app/Filament/Imports/Shop/CategoryImporter.php. It contains a class called CategoryImporter that extends the Importer class. This class is responsible for importing shop categories and provides methods for retrieving import columns, resolving records, and generating a completed notification body.

Methods

getColumns

This method returns an array of import columns for shop categories. Each import column represents a specific attribute of a shop category, such as name, slug, parent, description, position, visibility, SEO title, and SEO description. It provides various rules and examples for each column.

resolveRecord

This method resolves the record of a category based on the provided data. It uses the slug attribute to find an existing category or create a new one if no matching category is found. It returns the resolved category.

getCompletedNotificationBody

This method generates the body of a completed notification message for a shop category import. It takes an Import object as a parameter and constructs the notification body based on the number of successful rows and failed rows. It returns the completed notification body as a string.

<?php

namespace App\Filament\Imports\Shop;

use App\Models\Shop\Category;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;

class CategoryImporter extends Importer
{
    protected static ?string $model = Category::class;

    public static function getColumns(): array
    {
        return [
            ImportColumn::make('name')
                ->requiredMapping()
                ->rules(['required', 'max:255'])
                ->example('Category A'),
            ImportColumn::make('slug')
                ->requiredMapping()
                ->rules(['required', 'max:255'])
                ->example('category-a'),
            ImportColumn::make('parent')
                ->relationship(resolveUsing: ['name', 'slug'])
                ->example('Category B'),
            ImportColumn::make('description')
                ->example('This is the description for Category A.'),
            ImportColumn::make('position')
                ->requiredMapping()
                ->numeric()
                ->rules(['required', 'integer'])
                ->example('1'),
            ImportColumn::make('is_visible')
                ->label('Visibility')
                ->requiredMapping()
                ->boolean()
                ->rules(['required', 'boolean'])
                ->example('yes'),
            ImportColumn::make('seo_title')
                ->label('SEO title')
                ->rules(['max:60'])
                ->example('Awesome Category A'),
            ImportColumn::make('seo_description')
                ->label('SEO description')
                ->rules(['max:160'])
                ->example('Wow! It\'s just so amazing.'),
        ];
    }

    public function resolveRecord(): ?Category
    {
        return Category::firstOrNew([
            'slug' => $this->data['slug'],
        ]);
    }

    public static function getCompletedNotificationBody(Import $import): string
    {
        $body = 'Your shop category import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.';

        if ($failedRowsCount = $import->getFailedRowsCount()) {
            $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.';
        }

        return $body;
    }
}