Skip to content

Commit

Permalink
add badge category in admin / wip on badges
Browse files Browse the repository at this point in the history
  • Loading branch information
gheorghelupu17 committed Oct 23, 2023
1 parent aefb674 commit ee8ed30
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 6 deletions.
95 changes: 95 additions & 0 deletions app/Filament/Resources/BadgeCategoryResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace App\Filament\Resources;

use App\Enums\VolunteerStatus;
use App\Filament\Resources\BadgeCategoryResource\Pages;
use App\Filament\Resources\BadgeCategoryResource\RelationManagers;
use App\Models\BadgeCategory;
use App\Models\VolunteerRequest;
use Filament\Forms;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TernaryFilter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class BadgeCategoryResource extends Resource
{
protected static ?string $model = BadgeCategory::class;

protected static ?string $navigationIcon = 'heroicon-o-collection';

protected static ?int $navigationSort = 6;

public static function getPluralLabel(): ?string
{
return __('badge.category.labels.plural');
}

public static function getModelLabel(): string
{
return __('badge.category.labels.singular');
}

protected static function getNavigationGroup(): ?string
{
return __('navigation.group.manage');
}

protected static function getNavigationBadge(): ?string
{
return (string) BadgeCategory::query()->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('/'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Filament\Resources\BadgeCategoryResource\Pages;

use App\Filament\Resources\BadgeCategoryResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\CreateRecord;

class CreateBadgeCategory extends CreateRecord
{
protected static string $resource = BadgeCategoryResource::class;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\BadgeCategoryResource\Pages;

use App\Filament\Resources\BadgeCategoryResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\EditRecord;

class EditBadgeCategory extends EditRecord
{
protected static string $resource = BadgeCategoryResource::class;

protected function getActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\BadgeCategoryResource\Pages;

use App\Filament\Resources\BadgeCategoryResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\ListRecords;

class ListBadgeCategories extends ListRecords
{
protected static string $resource = BadgeCategoryResource::class;

protected function getActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
47 changes: 44 additions & 3 deletions app/Filament/Resources/BadgeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Filament\Resources\BadgeResource\Pages;
use App\Filament\Resources\BadgeResource\RelationManagers\UsersRelationManager;
use App\Models\Badge;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
Expand All @@ -16,17 +17,38 @@
use Filament\Tables;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Illuminate\Database\Eloquent\Builder;

class BadgeResource extends Resource
{
protected static ?string $model = Badge::class;

protected static ?string $navigationGroup = 'Administrează';

protected static ?int $navigationSort = 5;

protected static ?string $navigationIcon = 'heroicon-o-badge-check';

public static function getPluralLabel(): ?string
{
return __('badge.labels.plural');
}

public static function getModelLabel(): string
{
return __('badge.labels.singular');
}

protected static function getNavigationGroup(): ?string
{
return __('navigation.group.manage');
}

protected static function getNavigationBadge(): ?string
{
return (string) Badge::query()->whereHas('users')->count();
}

public static function form(Form $form): Form
{
return $form
Expand All @@ -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()
Expand All @@ -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(),
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,4 +51,8 @@ public function getImageAttribute(): string
{
return $this->getFirstMediaUrl(conversionName: 'thumb');
}
public function badgeCategory(): BelongsTo
{
return $this->belongsTo(BadgeCategory::class);
}
}
23 changes: 23 additions & 0 deletions app/Models/BadgeCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class BadgeCategory extends Model
{
use HasFactory;

protected $fillable = [
'title',
];

public function badges(): HasMany
{
return $this->hasMany(Badge::class);
}
}
25 changes: 25 additions & 0 deletions database/factories/BadgeCategoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\BadgeCategory>
*/
class BadgeCategoryFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'title' => $this->faker->title ,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('badge_categories', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('badge_categories');
}
};
2 changes: 2 additions & 0 deletions database/migrations/2023_07_04_005236_create_badges_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
});
Expand Down
22 changes: 19 additions & 3 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,9 +75,7 @@ public function run(): void
->pending()
->create();

Badge::factory()
->count(30)
->create();
$this->seedBadges();

$this->seedArticleCategories();
}
Expand Down Expand Up @@ -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();
}
}
}
Loading

0 comments on commit ee8ed30

Please sign in to comment.