Skip to content

Commit

Permalink
feat: enhance admin panel and resources functionality
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
frederic moras committed Dec 30, 2024
1 parent 9ebc0dc commit 345fdf0
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 70 deletions.
4 changes: 4 additions & 0 deletions app/Filament/Resources/BannerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion app/Filament/Resources/PartnerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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() ?? '-' ),
Expand All @@ -51,6 +60,8 @@ public static function table(Table $table): Table
{
return $table
->columns( [
SpatieMediaLibraryImageColumn::make( 'illustration' )->collection( 'illustration' ),

TextColumn::make( 'name' )
->searchable()
->sortable(),
Expand Down
118 changes: 75 additions & 43 deletions app/Filament/Resources/RealisationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
29 changes: 21 additions & 8 deletions app/Filament/Resources/TestimonialResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);
Expand All @@ -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')
Expand Down
14 changes: 12 additions & 2 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
3 changes: 1 addition & 2 deletions app/Models/CustomMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Spatie\MediaLibrary\MediaCollections\Models\Media;

/**
*
*
*
* @property-read mixed $extension
* @property-read mixed $human_readable_size
Expand Down Expand Up @@ -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];

Expand Down
12 changes: 8 additions & 4 deletions app/Models/Realisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,7 +13,7 @@
use Spatie\MediaLibrary\ResponsiveImages\ResponsiveImageGenerator;

/**
*
*
*
* @property-read mixed $gallery_images
* @property-read mixed $image
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -153,7 +159,6 @@ public function image(): Attribute
get: function() {
if ( $this->illustration ) {
$args = $this->illustration;
ray($args->orientation())->red();
return $args?->toHtml();
}
return '<img class="rounded-full" src="'.url("/assets/images/custom/default/portfolio_banner.jpeg").'" width="960" height="960">';
Expand Down Expand Up @@ -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');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/Filament/AdminPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions app/Scopes/OrderByScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace App\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class OrderByScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->orderBy( 'ordre' ); // Replace 'order' with your column name.
}
}
Loading

0 comments on commit 345fdf0

Please sign in to comment.