diff --git a/app/Filament/Resources/BadgeCategoryResource.php b/app/Filament/Resources/BadgeCategoryResource.php new file mode 100644 index 00000000..6c448a6b --- /dev/null +++ b/app/Filament/Resources/BadgeCategoryResource.php @@ -0,0 +1,95 @@ +whereHas('badges')->count(); + } + + + public static function form(Form $form): Form + { + return $form + ->schema([ + TextInput::make('title') + ->label(__('badge.category.title')) + ->required() + ]); + } + + public static function table(Table $table): Table + { + return $table + ->columns([ + TextColumn::make('title') + ->label(__('badge.category.title')) + ->searchable() + ->sortable(), + TextColumn::make('badges_count') + ->counts('badges') + ->label(__('badge.category.badges_count')) + ->sortable(), + ]) + ->filters([ + TernaryFilter::make('has_badges') + ->label(__('badges.filters.has_badges')) + ->queries( + true: fn (Builder $query) => $query->whereHas('badges'), + false: fn (Builder $query) => $query->whereDoesntHave('badges') + ), + ]) + ->actions([ + Tables\Actions\EditAction::make(), + ]) + ->bulkActions([ + Tables\Actions\DeleteBulkAction::make(), + ]); + } + + public static function getPages(): array + { + return [ + 'index' => Pages\ListBadgeCategories::route('/'), + ]; + } +} diff --git a/app/Filament/Resources/BadgeCategoryResource/Pages/CreateBadgeCategory.php b/app/Filament/Resources/BadgeCategoryResource/Pages/CreateBadgeCategory.php new file mode 100644 index 00000000..aaf3ec64 --- /dev/null +++ b/app/Filament/Resources/BadgeCategoryResource/Pages/CreateBadgeCategory.php @@ -0,0 +1,12 @@ +whereHas('users')->count(); + } + public static function form(Form $form): Form { return $form @@ -38,6 +60,13 @@ public static function form(Form $form): Form ->maxLength(200) ->required(), + Select::make('badge_category_id') + ->label(__('badge.category.title')) + ->inlineLabel() + ->columnSpanFull() + ->relationship('badgeCategory', 'title') + ->required(), + Textarea::make('description') ->label(__('badge.description')) ->inlineLabel() @@ -59,18 +88,30 @@ public static function table(Table $table): Table return $table ->columns([ ImageColumn::make('image') + ->label(__('badge.image')) ->square(), TextColumn::make('title') + ->label(__('badge.title')) ->searchable() ->sortable(), TextColumn::make('users_count') + ->label(__('badge.users_count')) ->counts('users') ->sortable(), ]) ->filters([ - // + SelectFilter::make('badge_category_id') + ->label(__('badge.filters.category')) + ->relationship('badgeCategory', 'title') + ->multiple(), + TernaryFilter::make('has_users') + ->label(__('badge.filters.has_users')) + ->queries( + true: fn (Builder $query) => $query->whereHas('users'), + false: fn (Builder $query) => $query->whereDoesntHave('users') + ), ]) ->actions([ Tables\Actions\ViewAction::make(), diff --git a/app/Models/Badge.php b/app/Models/Badge.php index 88f1ee5b..627791cc 100644 --- a/app/Models/Badge.php +++ b/app/Models/Badge.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Support\Facades\Vite; use Spatie\Image\Manipulations; @@ -50,4 +51,8 @@ public function getImageAttribute(): string { return $this->getFirstMediaUrl(conversionName: 'thumb'); } + public function badgeCategory(): BelongsTo + { + return $this->belongsTo(BadgeCategory::class); + } } diff --git a/app/Models/BadgeCategory.php b/app/Models/BadgeCategory.php new file mode 100644 index 00000000..be296022 --- /dev/null +++ b/app/Models/BadgeCategory.php @@ -0,0 +1,23 @@ +hasMany(Badge::class); + } +} diff --git a/database/factories/BadgeCategoryFactory.php b/database/factories/BadgeCategoryFactory.php new file mode 100644 index 00000000..f185be55 --- /dev/null +++ b/database/factories/BadgeCategoryFactory.php @@ -0,0 +1,25 @@ + + */ +class BadgeCategoryFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'title' => $this->faker->title , + ]; + } +} diff --git a/database/migrations/2023_07_03_083038_create_badge_categories_table.php b/database/migrations/2023_07_03_083038_create_badge_categories_table.php new file mode 100644 index 00000000..fd5a3833 --- /dev/null +++ b/database/migrations/2023_07_03_083038_create_badge_categories_table.php @@ -0,0 +1,28 @@ +id(); + $table->string('title'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('badge_categories'); + } +}; diff --git a/database/migrations/2023_07_04_005236_create_badges_table.php b/database/migrations/2023_07_04_005236_create_badges_table.php index 32d25510..b10aaf82 100644 --- a/database/migrations/2023_07_04_005236_create_badges_table.php +++ b/database/migrations/2023_07_04_005236_create_badges_table.php @@ -3,6 +3,7 @@ declare(strict_types=1); use App\Models\Badge; +use App\Models\BadgeCategory; use App\Models\User; use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; @@ -18,6 +19,7 @@ public function up(): void Schema::create('badges', function (Blueprint $table) { $table->id(); $table->timestamps(); + $table->foreignIdFor(BadgeCategory::class)->constrained()->cascadeOnDelete(); $table->string('title'); $table->text('description')->nullable(); }); diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index ed9977b4..fe746ffc 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -8,6 +8,7 @@ use App\Models\ActivityDomain; use App\Models\ArticleCategory; use App\Models\Badge; +use App\Models\BadgeCategory; use App\Models\Championship; use App\Models\Organization; use App\Models\ProjectCategory; @@ -74,9 +75,7 @@ public function run(): void ->pending() ->create(); - Badge::factory() - ->count(30) - ->create(); + $this->seedBadges(); $this->seedArticleCategories(); } @@ -122,4 +121,21 @@ private function seedProjectCategories(): void ->create(); } } + + private function seedBadges(): void + { + $oldCategories = [ + 'Donatii', + 'Voluntariat', + 'Sharing', + 'Proiect ONG', + 'Activitate lunara', + ]; + foreach ($oldCategories as $category) { + BadgeCategory::factory() + ->state(['title' => $category]) + ->has(Badge::factory()->count(5)) + ->create(); + } + } } diff --git a/lang/ro/badge.php b/lang/ro/badge.php index d376f731..7a04f998 100644 --- a/lang/ro/badge.php +++ b/lang/ro/badge.php @@ -7,6 +7,7 @@ 'title' => 'Titlu badge', 'description' => 'Descriere badge', 'image' => 'Imagine badge', + 'users_count' => 'Număr de utilizatori cu accest badge', 'donor_count' => ':count donatori cu accest badge', @@ -14,4 +15,23 @@ 'attach' => 'Alocă badge', 'detach' => 'Elimină', ], + + 'category' => [ + 'title' => 'Categorie', + 'badges_count' => 'Număr de badge-uri', + 'labels' => [ + 'singular' => 'Categorie de badge', + 'plural' => 'Categorii de badge', + ], + ], + 'filters' => [ + 'has_badges' => 'Are badge-uri', + 'category' => 'Categorie', + 'has_users' => 'Are utilizatori alocati', + ], + + 'labels' => [ + 'singular' => 'Badge', + 'plural' => 'Badge-uri', + ], ];