Skip to content
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 47 additions & 12 deletions database/factories/PageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,67 @@

namespace Eclipse\Cms\Factories;

use Eclipse\Cms\Enums\PageStatus;
use Eclipse\Cms\Models\Page;
use Eclipse\Cms\Models\Section;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;

/**
* @experimental
*/
class PageFactory extends Factory
{
protected $model = Page::class;

public function definition(): array
{
$englishTitle = $this->faker->sentence(3);
$slovenianTitle = "SI: {$englishTitle}";

$englishShortText = $this->faker->text(200);
$slovenianShortText = "SI: {$englishShortText}";

$englishLongText = $this->faker->text(500);
$slovenianLongText = "SI: {$englishLongText}";

$slug = $this->faker->slug();

return [
'title' => $this->faker->word(),
'short_text' => $this->faker->text(),
'long_text' => $this->faker->text(),
'sef_key' => $this->faker->word(),
'code' => $this->faker->word(),
'status' => $this->faker->word(),
'type' => $this->faker->word(),
'title' => [
'en' => $englishTitle,
'sl' => $slovenianTitle,
],
'short_text' => [
'en' => $englishShortText,
'sl' => $slovenianShortText,
],
'long_text' => [
'en' => $englishLongText,
'sl' => $slovenianLongText,
],
'sef_key' => [
'en' => $slug,
'sl' => "{$slug}-si",
],
'code' => $this->faker->unique()->numerify('page-###-####'),
'status' => $this->faker->randomElement([PageStatus::Draft, PageStatus::Published]),
'type' => 'page',
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),

'section_id' => Section::factory(),
];
}

public function configure()
{
return $this->afterMaking(function (Page $page) {
if (! $page->section_id) {
$page->section_id = Section::factory()->create()->id;
}
});
}

public function forSection($section): static
{
return $this->state([
'section_id' => $section->id,
]);
}
}
43 changes: 35 additions & 8 deletions database/factories/SectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Eclipse\Cms\Enums\SectionType;
use Eclipse\Cms\Models\Section;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;

Expand All @@ -15,18 +14,46 @@ class SectionFactory extends Factory

public function definition(): array
{
$attrs = [
'name' => Str::of($this->faker->words(asText: true))->ucwords(),
'type' => $this->faker->randomElement(Arr::pluck(SectionType::cases(), 'name')),
$englishName = Str::of($this->faker->words(asText: true))->ucwords();
$slovenianName = "SI: {$englishName}";

return [
'name' => [
'en' => $englishName,
'sl' => $slovenianName,
],
'type' => $this->faker->randomElement(SectionType::cases()),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
];
}

public function configure()
{
return $this->afterMaking(function (Section $section) {
if (config('eclipse-cms.tenancy.enabled')) {
$foreignKey = config('eclipse-cms.tenancy.foreign_key');
$currentValue = $section->getAttribute($foreignKey);

if (config('eclipse-cms.tenancy.enabled') && empty($attrs[config('eclipse-cms.tenancy.foreign_key')])) {
$class = config('eclipse-cms.tenancy.model');
$attrs[config('eclipse-cms.tenancy.foreign_key')] = $class::inRandomOrder()->first()?->id ?? $class::factory()->create()->id;
if (! $currentValue || $currentValue === null) {
$class = config('eclipse-cms.tenancy.model');
if (class_exists($class)) {
$newValue = $class::inRandomOrder()->first()?->id ?? $class::factory()->create()->id;
$section->setAttribute($foreignKey, $newValue);
}
}
}
});
}

public function forSite($site): static
{
if (config('eclipse-cms.tenancy.enabled')) {
return $this->state([
config('eclipse-cms.tenancy.foreign_key') => $site->id,
]);
}

return $attrs;
return $this;
}
}
49 changes: 47 additions & 2 deletions database/seeders/CmsSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,62 @@

namespace Eclipse\Cms\Seeders;

use Eclipse\Cms\Models\Page;
use Eclipse\Cms\Models\Section;
use Illuminate\Database\Seeder;

class CmsSeeder extends Seeder
{
public function run(): void
{
Section::factory()
->count(3)
if (config('eclipse-cms.tenancy.enabled')) {
$tenantModel = config('eclipse-cms.tenancy.model');
$tenants = $tenantModel::all();

if ($tenants->isEmpty()) {
$tenants = collect([$tenantModel::factory()->create()]);
}

$tenants->each(function ($tenant): void {
$this->seedForTenant($tenant);
});
} else {
$this->seedWithoutTenancy();
}
}

protected function seedForTenant($tenant): void
{
$sections = Section::factory()
->forSite($tenant)
->count(5)
->create();

$sections->each(function (Section $section): void {
Page::factory()
->count(rand(2, 5))
->forSection($section)
->create();
});

$this
->call(BannerSeeder::class)
->call(MenuSeeder::class);
}

protected function seedWithoutTenancy(): void
{
$sections = Section::factory()
->count(5)
->create();

$sections->each(function (Section $section): void {
Page::factory()
->count(rand(2, 5))
->forSection($section)
->create();
});

$this
->call(BannerSeeder::class)
->call(MenuSeeder::class);
Expand Down
2 changes: 2 additions & 0 deletions src/Admin/Filament/Resources/BannerPositionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class BannerPositionResource extends Resource implements HasShieldPermissions

protected static ?string $pluralModelLabel = 'Banners';

protected static ?int $navigationSort = 202;

public static function getPermissionPrefixes(): array
{
return [
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/Filament/Resources/MenuResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MenuResource extends Resource implements HasShieldPermissions

protected static string|\UnitEnum|null $navigationGroup = 'CMS';

protected static ?int $navigationSort = 3;
protected static ?int $navigationSort = 201;

public static function form(Schema $schema): Schema
{
Expand Down
Loading