Skip to content

Commit ab1584b

Browse files
scott graysonscott grayson
authored andcommitted
feat: add basic Filament resource for sidebar navigation
- Create LibraryItemResource with proper Filament 4 syntax - Add resource pages (List, Create, Edit) - Register resource in FilamentLibraryPlugin - Fix type hints for navigationGroup and navigationIcon - Update form method to use Schema instead of Form - Add Library navigation group to admin panel
1 parent 3e8b1b1 commit ab1584b

11 files changed

+175
-15
lines changed

database/migrations/2024_01_01_000000_create_library_items_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function up(): void
2020
$table->foreignId('created_by')->constrained('users');
2121
$table->timestamps();
2222
$table->softDeletes();
23-
23+
2424
$table->index(['parent_id', 'type']);
2525
$table->unique(['parent_id', 'slug'], 'lib_items_parent_slug_unique');
2626
});

database/migrations/2024_01_01_000001_create_library_item_permissions_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function up(): void
1717
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
1818
$table->enum('permission', ['view', 'edit']);
1919
$table->timestamps();
20-
20+
2121
$table->unique(['library_item_id', 'user_id', 'permission'], 'lib_item_perms_unique');
2222
});
2323
}

database/seeders/LibrarySeeder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function run(): void
1515
{
1616
// Get the first user as the creator
1717
$user = \App\Models\User::first();
18-
18+
1919
if (!$user) {
2020
$this->command->warn('No users found. Please create a user first.');
2121
return;
@@ -116,10 +116,10 @@ public function run(): void
116116

117117
// Create some sample permissions if there are other users
118118
$otherUsers = \App\Models\User::where('id', '!=', $user->id)->take(2)->get();
119-
119+
120120
if ($otherUsers->count() > 0) {
121121
$this->command->info('Creating sample permissions...');
122-
122+
123123
// Give first other user view access to Documents folder
124124
if ($otherUsers->count() >= 1) {
125125
LibraryItemPermission::create([
@@ -128,7 +128,7 @@ public function run(): void
128128
'permission' => 'view',
129129
]);
130130
}
131-
131+
132132
// Give second other user edit access to Project A
133133
if ($otherUsers->count() >= 2) {
134134
LibraryItemPermission::create([
@@ -150,7 +150,7 @@ public function run(): void
150150
$this->command->info('- Images/');
151151
$this->command->info(' - Photos/');
152152
$this->command->info(' - Graphics/');
153-
153+
154154
if ($otherUsers->count() > 0) {
155155
$this->command->info('Sample permissions created for other users.');
156156
}

src/FilamentLibraryPlugin.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ public function getId(): string
1414

1515
public function register(Panel $panel): void
1616
{
17-
// Register resources, pages, widgets, etc.
17+
$panel
18+
->resources([
19+
\Tapp\FilamentLibrary\Resources\LibraryItemResource::class,
20+
]);
1821
}
1922

2023
public function boot(Panel $panel): void

src/FilamentLibraryServiceProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ protected function getAssetPackageName(): ?string
101101
protected function getAssets(): array
102102
{
103103
$assets = [];
104-
104+
105105
// Only register assets if they exist
106106
if (file_exists(__DIR__ . '/../resources/dist/filament-library.css')) {
107107
$assets[] = Css::make('filament-library-styles', __DIR__ . '/../resources/dist/filament-library.css');
108108
}
109-
109+
110110
if (file_exists(__DIR__ . '/../resources/dist/filament-library.js')) {
111111
$assets[] = Js::make('filament-library-scripts', __DIR__ . '/../resources/dist/filament-library.js');
112112
}
113-
113+
114114
return $assets;
115115
}
116116

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace Tapp\FilamentLibrary\Resources;
4+
5+
use Filament\Resources\Resource;
6+
use Filament\Schemas\Schema;
7+
use Filament\Tables;
8+
use Filament\Tables\Table;
9+
use Tapp\FilamentLibrary\Models\LibraryItem;
10+
11+
class LibraryItemResource extends Resource
12+
{
13+
protected static ?string $model = LibraryItem::class;
14+
15+
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-folder';
16+
17+
protected static string|\UnitEnum|null $navigationGroup = 'Library';
18+
19+
protected static ?int $navigationSort = 10;
20+
21+
protected static ?string $navigationLabel = 'Library';
22+
23+
protected static ?string $modelLabel = 'Library Item';
24+
25+
protected static ?string $pluralModelLabel = 'Library Items';
26+
27+
public static function form(Schema $schema): Schema
28+
{
29+
return $schema
30+
->components([
31+
\Filament\Forms\Components\TextInput::make('name')
32+
->required()
33+
->maxLength(255),
34+
\Filament\Forms\Components\Select::make('type')
35+
->options([
36+
'folder' => 'Folder',
37+
'file' => 'File',
38+
])
39+
->required(),
40+
\Filament\Forms\Components\Select::make('parent_id')
41+
->relationship('parent', 'name')
42+
->searchable()
43+
->preload(),
44+
]);
45+
}
46+
47+
public static function table(Table $table): Table
48+
{
49+
return $table
50+
->columns([
51+
Tables\Columns\TextColumn::make('name')
52+
->searchable()
53+
->sortable(),
54+
Tables\Columns\TextColumn::make('type')
55+
->badge()
56+
->color(fn (string $state): string => match ($state) {
57+
'folder' => 'success',
58+
'file' => 'info',
59+
}),
60+
Tables\Columns\TextColumn::make('parent.name')
61+
->label('Parent Folder')
62+
->searchable()
63+
->sortable(),
64+
Tables\Columns\TextColumn::make('creator.name')
65+
->label('Created By')
66+
->searchable()
67+
->sortable(),
68+
Tables\Columns\TextColumn::make('created_at')
69+
->dateTime()
70+
->sortable()
71+
->toggleable(isToggledHiddenByDefault: true),
72+
Tables\Columns\TextColumn::make('updated_at')
73+
->dateTime()
74+
->sortable()
75+
->toggleable(isToggledHiddenByDefault: true),
76+
])
77+
->filters([
78+
Tables\Filters\SelectFilter::make('type')
79+
->options([
80+
'folder' => 'Folder',
81+
'file' => 'File',
82+
]),
83+
])
84+
->actions([
85+
Tables\Actions\EditAction::make(),
86+
Tables\Actions\DeleteAction::make(),
87+
])
88+
->bulkActions([
89+
Tables\Actions\BulkActionGroup::make([
90+
Tables\Actions\DeleteBulkAction::make(),
91+
]),
92+
]);
93+
}
94+
95+
public static function getRelations(): array
96+
{
97+
return [
98+
//
99+
];
100+
}
101+
102+
public static function getPages(): array
103+
{
104+
return [
105+
'index' => Pages\ListLibraryItems::route('/'),
106+
'create' => Pages\CreateLibraryItem::route('/create'),
107+
'edit' => Pages\EditLibraryItem::route('/{record}/edit'),
108+
];
109+
}
110+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Tapp\FilamentLibrary\Resources\Pages;
4+
5+
use Filament\Resources\Pages\CreateRecord;
6+
use Tapp\FilamentLibrary\Resources\LibraryItemResource;
7+
8+
class CreateLibraryItem extends CreateRecord
9+
{
10+
protected static string $resource = LibraryItemResource::class;
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Tapp\FilamentLibrary\Resources\Pages;
4+
5+
use Filament\Resources\Pages\EditRecord;
6+
use Tapp\FilamentLibrary\Resources\LibraryItemResource;
7+
8+
class EditLibraryItem extends EditRecord
9+
{
10+
protected static string $resource = LibraryItemResource::class;
11+
12+
protected function getHeaderActions(): array
13+
{
14+
return [
15+
//
16+
];
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Tapp\FilamentLibrary\Resources\Pages;
4+
5+
use Filament\Resources\Pages\ListRecords;
6+
use Tapp\FilamentLibrary\Resources\LibraryItemResource;
7+
8+
class ListLibraryItems extends ListRecords
9+
{
10+
protected static string $resource = LibraryItemResource::class;
11+
12+
protected function getHeaderActions(): array
13+
{
14+
return [
15+
//
16+
];
17+
}
18+
}

src/Services/PermissionService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class PermissionService
2525
public function hasPermission($user, LibraryItem $item, string $permission): bool
2626
{
2727
$cacheKey = $this->getCacheKey($user->id, $item->id, $permission);
28-
28+
2929
return Cache::remember($cacheKey, self::CACHE_TTL, function () use ($user, $item, $permission) {
3030
return $this->checkPermissionRecursive($user, $item, $permission);
3131
});

0 commit comments

Comments
 (0)