From 345fdf04d08df287b41344b3c9c21be137c42743 Mon Sep 17 00:00:00 2001 From: frederic moras Date: Mon, 30 Dec 2024 17:17:27 +0100 Subject: [PATCH] feat: enhance admin panel and resources functionality - Updated `PartnerResource`, `BannerResource`, `RealisationResource`, and `TestimonialResource` to improve forms, labels, sorting, and field options. - Added `OrderByScope` to globally sort models like `Realisation`. - Improved categorization and fallback logic for `serviceRealisations` in `HomeController`. - Updated application locale and timezone to French and Europe/Brussels in `config/app.php`. - Adjusted navigation path for the Admin Panel and refined resource visibility in the Filament configuration. - Simplified `composer.json` post-autoload commands and added an `ide` command group for development utilities. --- app/Filament/Resources/BannerResource.php | 4 + app/Filament/Resources/PartnerResource.php | 13 +- .../Resources/RealisationResource.php | 118 +++++++++++------- .../Resources/TestimonialResource.php | 29 +++-- app/Http/Controllers/HomeController.php | 14 ++- app/Models/CustomMedia.php | 3 +- app/Models/Realisation.php | 12 +- app/Providers/Filament/AdminPanelProvider.php | 2 +- app/Scopes/OrderByScope.php | 14 +++ composer.json | 10 +- config/app.php | 8 +- .../components/home/testimonials.blade.php | 2 +- resources/views/home.blade.php | 3 + 13 files changed, 162 insertions(+), 70 deletions(-) create mode 100644 app/Scopes/OrderByScope.php diff --git a/app/Filament/Resources/BannerResource.php b/app/Filament/Resources/BannerResource.php index 0d59d98..1a43b00 100644 --- a/app/Filament/Resources/BannerResource.php +++ b/app/Filament/Resources/BannerResource.php @@ -28,6 +28,10 @@ class BannerResource extends Resource protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; + public static function shouldRegisterNavigation(): bool + { + return false; // Hides the resource from the navigation menu + } public static function form(Form $form): Form { return $form diff --git a/app/Filament/Resources/PartnerResource.php b/app/Filament/Resources/PartnerResource.php index be9f421..153d401 100644 --- a/app/Filament/Resources/PartnerResource.php +++ b/app/Filament/Resources/PartnerResource.php @@ -5,6 +5,7 @@ use App\Filament\Resources\PartnerResource\Pages; use App\Models\Partner; use Filament\Forms\Components\Placeholder; +use Filament\Forms\Components\SpatieMediaLibraryFileUpload; use Filament\Forms\Components\TextInput; use Filament\Forms\Form; use Filament\Resources\Resource; @@ -16,6 +17,7 @@ use Filament\Tables\Actions\ForceDeleteBulkAction; use Filament\Tables\Actions\RestoreAction; use Filament\Tables\Actions\RestoreBulkAction; +use Filament\Tables\Columns\SpatieMediaLibraryImageColumn; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Filters\TrashedFilter; use Filament\Tables\Table; @@ -27,16 +29,23 @@ class PartnerResource extends Resource protected static ?string $model = Partner::class; protected static ?string $slug = 'partners'; + protected static ?string $label = 'partenaires'; - protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; + protected static ?int $navigationSort = 4; + protected static ?string $navigationIcon = 'heroicon-o-users'; public static function form(Form $form): Form { return $form ->schema( [ + TextInput::make( 'name' ) ->required(), + SpatieMediaLibraryFileUpload::make( 'illustration' ) + ->responsiveImages() + ->collection( 'illustration' ), + Placeholder::make( 'created_at' ) ->label( 'Created Date' ) ->content( fn(?Partner $record): string => $record?->created_at?->diffForHumans() ?? '-' ), @@ -51,6 +60,8 @@ public static function table(Table $table): Table { return $table ->columns( [ + SpatieMediaLibraryImageColumn::make( 'illustration' )->collection( 'illustration' ), + TextColumn::make( 'name' ) ->searchable() ->sortable(), diff --git a/app/Filament/Resources/RealisationResource.php b/app/Filament/Resources/RealisationResource.php index 24e6f95..e3abe1d 100644 --- a/app/Filament/Resources/RealisationResource.php +++ b/app/Filament/Resources/RealisationResource.php @@ -2,9 +2,11 @@ namespace App\Filament\Resources; +use App\Enums\Categories; use App\Filament\Resources\RealisationResource\Pages; use App\Models\Realisation; use Filament\Forms\Components\SpatieMediaLibraryFileUpload; +use Filament\Forms\Components\Toggle; use Filament\Forms\Form; use Filament\Resources\Resource; use Filament\Tables\Actions\Action; @@ -24,70 +26,100 @@ class RealisationResource extends Resource { protected static ?string $model = Realisation::class; -// protected static ?string $navigationIcon = 'heroicon-o-collection'; + protected static ?string $navigationIcon = 'heroicon-o-wrench-screwdriver'; + protected static ?string $pluralLabel = 'Realisations'; - protected static ?string $navigationGroup = 'Content Management'; + public static function form(Form $form): Form { return $form - ->schema([ - TextInput::make('title') + ->schema( [ + + Toggle::make('published') + ->label('Publier') + ->default(true), + + Toggle::make('favorite') + ->label('Favoris') + ->default(false), + + TextInput::make( 'title' ) + ->label( 'Titre') ->required() - ->maxLength(255), + ->maxLength( 255 ), - Textarea::make('description'), + Textarea::make( 'description' ), - TextInput::make('category') - ->required(), + Select::make( 'category' ) + ->label( 'Catégorie') + ->required() + ->multiple() + ->options( + collect( Categories::all() ) + ->filter( fn($item) => $item['key'] != 'tout' ) + ->mapWithKeys( function($category) { + return [$category['key'] => $category['label']]; + } ) + ->toArray() + ), - TextInput::make('place'), + TextInput::make( 'place' )->label( 'Lieu'), - DatePicker::make('date'), + DatePicker::make( 'date' ), - TextInput::make('customer'), + TextInput::make( 'customer' ) + ->placeholder( 'anonyme') + ->label( 'Client'), - SpatieMediaLibraryFileUpload::make('illustration') + SpatieMediaLibraryFileUpload::make( 'illustration' ) ->responsiveImages() - ->collection( 'illustration'), + ->collection( 'illustration' ), - SpatieMediaLibraryFileUpload::make('gallery') + SpatieMediaLibraryFileUpload::make( 'gallery' ) + ->label( 'Gallerie') ->responsiveImages() ->multiple() - ->collection( 'gallery'), - - Select::make('published') - ->label('Published') - ->options([0 => 'No', 1 => 'Yes']) - ->default(0), - - Select::make('favorite') - ->label('Favorite') - ->options([0 => 'No', 1 => 'Yes']) - ->default(0), - ]); + ->collection( 'gallery' ), + + ] ); } public static function table(Table $table): Table { return $table - ->columns([ - SpatieMediaLibraryImageColumn::make('illustration')->collection( 'illustration'), - - TextColumn::make('title')->sortable()->searchable(), - TextColumn::make('category')->sortable()->searchable(), - TextColumn::make('place')->sortable(), - TextColumn::make('date')->date(), - BooleanColumn::make('published'), - BooleanColumn::make('favorite'), - - ]) - ->defaultSort('ordre', 'asc') - ->reorderable('ordre') - ->filters([ - Filter::make('published')->query(fn ($query) => $query->where('published', true)), - Filter::make('favorite')->query(fn ($query) => $query->where('favorite', true)), - ]); + ->columns( [ + SpatieMediaLibraryImageColumn::make( 'illustration' ) + ->collection( 'illustration' ), + + TextColumn::make( 'title' ) + ->label('Titre') + ->sortable() + ->searchable(), + TextColumn::make( 'category' ) + ->label('Catégorie') + ->sortable() + ->searchable() + ->formatStateUsing( function($state, $record) { + return Categories::getCategoryLabels( $record->category ); + } ), + TextColumn::make( 'place' ) + ->label('Lieu') + ->sortable(), + TextColumn::make( 'date' ) + ->dateTime('j F Y'), + BooleanColumn::make( 'published' ) + ->label('Publié'), + BooleanColumn::make( 'favorite' ) + ->label('Favoris'), + + ] ) + ->defaultSort( 'ordre', 'asc' ) + ->reorderable( 'ordre' ) + ->filters( [ + Filter::make( 'published' )->query( fn($query) => $query->where( 'published', true ) ), + Filter::make( 'favorite' )->query( fn($query) => $query->where( 'favorite', true ) ), + ] ); } public static function getPages(): array diff --git a/app/Filament/Resources/TestimonialResource.php b/app/Filament/Resources/TestimonialResource.php index 8a27318..070a3ea 100644 --- a/app/Filament/Resources/TestimonialResource.php +++ b/app/Filament/Resources/TestimonialResource.php @@ -18,29 +18,35 @@ class TestimonialResource extends Resource { protected static ?string $model = Testimonial::class; -// protected static ?string $navigationIcon = 'heroicon-o-annotation'; + protected static ?string $navigationIcon = 'heroicon-o-megaphone'; + + protected static ?string $pluralLabel = 'Témoignages'; - protected static ?string $navigationGroup = 'Content Management'; public static function form(Form $form): Form { return $form ->schema([ Forms\Components\TextInput::make('author') + ->label('Auteur') ->required() ->maxLength(255), Forms\Components\Textarea::make('content') + ->label('Contenu') ->required(), Forms\Components\TextInput::make('city') + ->label('Ville') ->required() ->maxLength(255), Forms\Components\Toggle::make('published') - ->label('Published') + ->label('Publier') ->default(false), Forms\Components\DateTimePicker::make('created_at') + ->label('Date') ->label('Created At'), Forms\Components\Select::make('realisation_id') + ->label('Rélisation') ->relationship('realisation', 'title') ->nullable(), ]); @@ -50,16 +56,23 @@ public static function table(Table $table): Table { return $table ->columns([ - TextColumn::make('author')->sortable()->searchable(), + TextColumn::make('author') + ->label('Auteur') + ->sortable() + ->searchable(), TextColumn::make('content') + ->label('Contenu') ->limit(50) ->sortable(), - TextColumn::make('city')->sortable()->searchable(), + TextColumn::make('city') + ->label('Ville') + ->sortable() + ->searchable(), BooleanColumn::make('published') - ->label('Published'), + ->label('Publier'), TextColumn::make('created_at') - ->dateTime() - ->label('Created At'), + ->label('Date') + ->dateTime('j F Y') ]) ->filters([ SelectFilter::make('published') diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 92ce720..e40cb26 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -19,8 +19,18 @@ public function __invoke() $serviceRealisations = Realisation::with(['media']) ->favorites($favoritesRealisations->pluck('id')) - ->get(); - + ->get() + ->groupBy(fn ($item) => $item->category[0] ?? null); + + + if($serviceRealisations->count() < 4){ + $serviceRealisations = Realisation::query() + ->whereNotNull('category') + ->whereHas( 'media') + ->get() + ->groupBy(fn ($item) => $item->category[0] ?? null) // Group by the first category + ->map(fn ($items) => $items->first()); // Pick the first realization for each group + } return view('home', compact('themeColor','customerData', 'favoritesRealisations', 'serviceRealisations')); } diff --git a/app/Models/CustomMedia.php b/app/Models/CustomMedia.php index 3a02c08..dd103b7 100755 --- a/app/Models/CustomMedia.php +++ b/app/Models/CustomMedia.php @@ -8,7 +8,7 @@ use Spatie\MediaLibrary\MediaCollections\Models\Media; /** - * + * * * @property-read mixed $extension * @property-read mixed $human_readable_size @@ -132,7 +132,6 @@ public function orientation():string if(!str_starts_with($this->mime_type, 'image/')) return 'not an image'; $image = getimagesize($this->getPath()); - ray($image); $width = $image[0]; $height = $image[1]; diff --git a/app/Models/Realisation.php b/app/Models/Realisation.php index 74f8721..02f8cdd 100755 --- a/app/Models/Realisation.php +++ b/app/Models/Realisation.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Scopes\OrderByScope; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -12,7 +13,7 @@ use Spatie\MediaLibrary\ResponsiveImages\ResponsiveImageGenerator; /** - * + * * * @property-read mixed $gallery_images * @property-read mixed $image @@ -72,6 +73,11 @@ class Realisation extends Model implements HasMedia protected $casts = ['category' => 'array']; + protected static function booted() + { + static::addGlobalScope(new OrderByScope()); + } + public function registerMediaCollections(): void { $this->addMediaCollection('illustration')->singleFile(); @@ -153,7 +159,6 @@ public function image(): Attribute get: function() { if ( $this->illustration ) { $args = $this->illustration; - ray($args->orientation())->red(); return $args?->toHtml(); } return ''; @@ -206,8 +211,7 @@ public function scopeFavorites($query,$blackListIds = null ) $query->where('favorite', true) ->orWhereNotNull('media.id'); }) - /* ->groupBy('category') - ->orderByRaw('CASE WHEN favorite = 1 THEN 1 ELSE 2 END, media.created_at DESC')*/; + ->orderByRaw('CASE WHEN favorite = 1 THEN 1 ELSE 2 END, media.created_at DESC'); } /** diff --git a/app/Providers/Filament/AdminPanelProvider.php b/app/Providers/Filament/AdminPanelProvider.php index 058efbb..ff61923 100644 --- a/app/Providers/Filament/AdminPanelProvider.php +++ b/app/Providers/Filament/AdminPanelProvider.php @@ -25,7 +25,7 @@ public function panel(Panel $panel): Panel return $panel ->default() ->id('admin') - ->path('admin') + ->path('mp_admin') ->login() ->colors([ 'primary' => Color::Amber, diff --git a/app/Scopes/OrderByScope.php b/app/Scopes/OrderByScope.php new file mode 100644 index 0000000..26ff946 --- /dev/null +++ b/app/Scopes/OrderByScope.php @@ -0,0 +1,14 @@ +orderBy( 'ordre' ); // Replace 'order' with your column name. + } +} diff --git a/composer.json b/composer.json index eca2e95..926fee0 100644 --- a/composer.json +++ b/composer.json @@ -41,10 +41,7 @@ "post-autoload-dump": [ "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", "@php artisan package:discover --ansi", - "@php artisan filament:upgrade", - "@php artisan ide-helper:generate --env=local || true", - "@php artisan ide-helper:meta --env=local || true", - "@php artisan ide-helper:models --env=local --write || true" + "@php artisan filament:upgrade" ], "post-update-cmd": [ "@php artisan vendor:publish --tag=laravel-assets --ansi --force" @@ -56,6 +53,11 @@ "@php artisan key:generate --ansi", "@php -r \"file_exists('database/database.sqlite') || touch('database/database.sqlite');\"", "@php artisan migrate --ansi" + ], + "ide": [ + "@php artisan ide-helper:generate", + "@php artisan ide-helper:meta", + "@php artisan ide-helper:models --write" ] }, "extra": { diff --git a/config/app.php b/config/app.php index f467267..c5881c5 100644 --- a/config/app.php +++ b/config/app.php @@ -65,7 +65,7 @@ | */ - 'timezone' => env('APP_TIMEZONE', 'UTC'), + 'timezone' => env('APP_TIMEZONE', 'EUROPE/BRUSSELS'), /* |-------------------------------------------------------------------------- @@ -78,11 +78,11 @@ | */ - 'locale' => env('APP_LOCALE', 'en'), + 'locale' => env('APP_LOCALE', 'fr'), - 'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'), + 'fallback_locale' => env('APP_FALLBACK_LOCALE', 'fr'), - 'faker_locale' => env('APP_FAKER_LOCALE', 'en_US'), + 'faker_locale' => env('APP_FAKER_LOCALE', 'fr_FR'), /* |-------------------------------------------------------------------------- diff --git a/resources/views/components/home/testimonials.blade.php b/resources/views/components/home/testimonials.blade.php index fdf7e5d..084f2a5 100644 --- a/resources/views/components/home/testimonials.blade.php +++ b/resources/views/components/home/testimonials.blade.php @@ -18,7 +18,7 @@ @endforeach -
+{{--
--}} diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index d6e85dc..4fd0e0a 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -23,6 +23,9 @@ @include('components.cta-contact') + @php + ray($serviceRealisations); + @endphp @include('components.home.services',['serviceListe'=>$serviceRealisations])