Skip to content

Commit 53a1041

Browse files
committed
feat: add Page and Section prototypes
1 parent 0305739 commit 53a1041

21 files changed

Lines changed: 625 additions & 31 deletions

config/eclipse-cms.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Multi-tenancy config
8+
|--------------------------------------------------------------------------
9+
*/
10+
'tenancy' => [
11+
'enabled' => false,
12+
'model' => null,
13+
'foreign_key' => null,
14+
],
15+
];

database/factories/PageFactory.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Eclipse\Cms\Factories;
4+
5+
use Eclipse\Cms\Models\Page;
6+
use Eclipse\Cms\Models\Section;
7+
use Illuminate\Database\Eloquent\Factories\Factory;
8+
use Illuminate\Support\Carbon;
9+
10+
/**
11+
* @experimental
12+
*/
13+
class PageFactory extends Factory
14+
{
15+
protected $model = Page::class;
16+
17+
public function definition(): array
18+
{
19+
return [
20+
'title' => $this->faker->word(),
21+
'short_text' => $this->faker->text(),
22+
'long_text' => $this->faker->text(),
23+
'sef_key' => $this->faker->word(),
24+
'code' => $this->faker->word(),
25+
'status' => $this->faker->word(),
26+
'type' => $this->faker->word(),
27+
'created_at' => Carbon::now(),
28+
'updated_at' => Carbon::now(),
29+
30+
'section_id' => Section::factory(),
31+
];
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Eclipse\Cms\Factories;
4+
5+
use Eclipse\Cms\Enums\SectionType;
6+
use Eclipse\Cms\Models\Section;
7+
use Illuminate\Database\Eloquent\Factories\Factory;
8+
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Carbon;
10+
use Illuminate\Support\Str;
11+
12+
class SectionFactory extends Factory
13+
{
14+
protected $model = Section::class;
15+
16+
public function definition(): array
17+
{
18+
$attrs = [
19+
'name' => Str::of($this->faker->words(asText: true))->ucwords(),
20+
'type' => $this->faker->randomElement(Arr::pluck(SectionType::cases(), 'name')),
21+
'created_at' => Carbon::now(),
22+
'updated_at' => Carbon::now(),
23+
];
24+
25+
if (config('eclipse-cms.tenancy.enabled') && empty($attrs[config('eclipse-cms.tenancy.foreign_key')])) {
26+
$class = config('eclipse-cms.tenancy.model');
27+
$attrs[config('eclipse-cms.tenancy.foreign_key')] = $class::inRandomOrder()->first()?->id ?? $class::factory()->create()->id;
28+
}
29+
30+
return $attrs;
31+
}
32+
}

database/migrations/.gitkeep

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration {
8+
public function up(): void
9+
{
10+
Schema::create('cms_sections', function (Blueprint $table) {
11+
$table->id();
12+
$table->string('type');
13+
14+
if (config('eclipse-cms.tenancy.enabled')) {
15+
$tenantClass = config('eclipse-cms.tenancy.model');
16+
/** @var \Illuminate\Database\Eloquent\Model $tenant */
17+
$tenant = new $tenantClass;
18+
$table->foreignId(config('eclipse-cms.tenancy.foreign_key'))
19+
->constrained($tenant->getTable(), $tenant->getKeyName())
20+
->cascadeOnUpdate()
21+
->cascadeOnDelete();
22+
}
23+
24+
$table->string('name');
25+
$table->timestamps();
26+
$table->softDeletes();
27+
});
28+
}
29+
30+
public function down(): void
31+
{
32+
Schema::dropIfExists('cms_sections');
33+
}
34+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration {
8+
public function up(): void
9+
{
10+
Schema::create('cms_pages', function (Blueprint $table) {
11+
$table->id();
12+
$table->string('title');
13+
$table->foreignId('section_id')
14+
->constrained('cms_sections', 'id')
15+
->cascadeOnUpdate()
16+
->cascadeOnDelete();
17+
$table->text('short_text')->nullable();
18+
$table->mediumText('long_text')->nullable();
19+
$table->string('sef_key');
20+
$table->string('code')->nullable();
21+
$table->string('status');
22+
$table->string('type');
23+
$table->timestamps();
24+
$table->softDeletes();
25+
});
26+
}
27+
28+
public function down(): void
29+
{
30+
Schema::dropIfExists('cms_pages');
31+
}
32+
};

database/seeders/CmsSeeder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Eclipse\Cms\Seeders;
4+
5+
use Eclipse\Cms\Models\Section;
6+
use Illuminate\Database\Seeder;
7+
8+
class CmsSeeder extends Seeder
9+
{
10+
public function run(): void
11+
{
12+
Section::factory()
13+
->count(3)
14+
->create();
15+
}
16+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
namespace Eclipse\Cms\Admin\Filament\Resources;
4+
5+
use Eclipse\Cms\Admin\Filament\Resources\PageResource\Pages;
6+
use Eclipse\Cms\Models\Page;
7+
use Filament\Forms\Components\MarkdownEditor;
8+
use Filament\Forms\Components\Placeholder;
9+
use Filament\Forms\Components\TextInput;
10+
use Filament\Forms\Form;
11+
use Filament\Resources\Resource;
12+
use Filament\Tables\Actions\BulkActionGroup;
13+
use Filament\Tables\Actions\DeleteAction;
14+
use Filament\Tables\Actions\DeleteBulkAction;
15+
use Filament\Tables\Actions\EditAction;
16+
use Filament\Tables\Actions\ForceDeleteAction;
17+
use Filament\Tables\Actions\ForceDeleteBulkAction;
18+
use Filament\Tables\Actions\RestoreAction;
19+
use Filament\Tables\Actions\RestoreBulkAction;
20+
use Filament\Tables\Columns\TextColumn;
21+
use Filament\Tables\Filters\TrashedFilter;
22+
use Filament\Tables\Table;
23+
use Illuminate\Database\Eloquent\Builder;
24+
use Illuminate\Database\Eloquent\SoftDeletingScope;
25+
26+
class PageResource extends Resource
27+
{
28+
protected static ?string $model = Page::class;
29+
30+
protected static ?string $slug = 'pages';
31+
32+
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
33+
34+
protected static ?string $navigationGroup = 'CMS';
35+
36+
public static function form(Form $form): Form
37+
{
38+
return $form
39+
->schema([
40+
TextInput::make('title')
41+
->required(),
42+
43+
TextInput::make('section_id')
44+
->required()
45+
->integer(),
46+
47+
MarkdownEditor::make('short_text'),
48+
49+
MarkdownEditor::make('long_text'),
50+
51+
TextInput::make('sef_key')
52+
->required(),
53+
54+
TextInput::make('code'),
55+
56+
TextInput::make('status')
57+
->required(),
58+
59+
TextInput::make('type')
60+
->required(),
61+
62+
Placeholder::make('created_at')
63+
->label('Created Date')
64+
->content(fn(?Page $record): string => $record?->created_at?->diffForHumans() ?? '-'),
65+
66+
Placeholder::make('updated_at')
67+
->label('Last Modified Date')
68+
->content(fn(?Page $record): string => $record?->updated_at?->diffForHumans() ?? '-'),
69+
]);
70+
}
71+
72+
public static function table(Table $table): Table
73+
{
74+
return $table
75+
->columns([
76+
TextColumn::make('title')
77+
->searchable()
78+
->sortable(),
79+
80+
TextColumn::make('section_id'),
81+
82+
TextColumn::make('sef_key'),
83+
84+
TextColumn::make('code'),
85+
86+
TextColumn::make('status'),
87+
88+
TextColumn::make('type'),
89+
])
90+
->filters([
91+
TrashedFilter::make(),
92+
])
93+
->actions([
94+
EditAction::make(),
95+
DeleteAction::make(),
96+
RestoreAction::make(),
97+
ForceDeleteAction::make(),
98+
])
99+
->bulkActions([
100+
BulkActionGroup::make([
101+
DeleteBulkAction::make(),
102+
RestoreBulkAction::make(),
103+
ForceDeleteBulkAction::make(),
104+
]),
105+
]);
106+
}
107+
108+
public static function getPages(): array
109+
{
110+
return [
111+
'index' => Pages\ListPages::route('/'),
112+
'create' => Pages\CreatePage::route('/create'),
113+
'edit' => Pages\EditPage::route('/{record}/edit'),
114+
];
115+
}
116+
117+
public static function getEloquentQuery(): Builder
118+
{
119+
return parent::getEloquentQuery()
120+
->withoutGlobalScopes([
121+
SoftDeletingScope::class,
122+
]);
123+
}
124+
125+
public static function getGloballySearchableAttributes(): array
126+
{
127+
return ['title'];
128+
}
129+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Eclipse\Cms\Admin\Filament\Resources\PageResource\Pages;
4+
5+
use Eclipse\Cms\Admin\Filament\Resources\PageResource;
6+
use Filament\Resources\Pages\CreateRecord;
7+
8+
class CreatePage extends CreateRecord
9+
{
10+
protected static string $resource = PageResource::class;
11+
12+
protected function getHeaderActions(): array
13+
{
14+
return [
15+
16+
];
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Eclipse\Cms\Admin\Filament\Resources\PageResource\Pages;
4+
5+
use Eclipse\Cms\Admin\Filament\Resources\PageResource;
6+
use Filament\Actions\DeleteAction;
7+
use Filament\Actions\ForceDeleteAction;
8+
use Filament\Actions\RestoreAction;
9+
use Filament\Resources\Pages\EditRecord;
10+
11+
class EditPage extends EditRecord
12+
{
13+
protected static string $resource = PageResource::class;
14+
15+
protected function getHeaderActions(): array
16+
{
17+
return [
18+
DeleteAction::make(),
19+
ForceDeleteAction::make(),
20+
RestoreAction::make(),
21+
];
22+
}
23+
}

0 commit comments

Comments
 (0)