Skip to content
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
"datalinx/php-utils": "^2.5",
"eclipsephp/common": "dev-main",
"filament/filament": "^3.3",
"filament/spatie-laravel-translatable-plugin": "^3.3",
"spatie/image": "^3.8",
"spatie/laravel-package-tools": "^1.19"
},
"require-dev": {
Expand Down
53 changes: 53 additions & 0 deletions database/factories/BannerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Eclipse\Cms\Factories;

use Eclipse\Cms\Models\Banner;
use Illuminate\Database\Eloquent\Factories\Factory;

class BannerFactory extends Factory
{
protected $model = Banner::class;

public function definition(): array
{
$names = [
'en' => ['Summer Sale', 'Winter Sale', 'Special Offer', 'New Arrivals', 'Featured Deal'],
'sl' => ['Poletna Razprodaja', 'Zimska Razprodaja', 'Posebna Ponudba', 'Nove Stvari', 'Izpostavljena Ponudba'],
];

$nameIndex = $this->faker->numberBetween(0, count($names['en']) - 1);

return [
'name' => [
'en' => $names['en'][$nameIndex],
'sl' => $names['sl'][$nameIndex],
],
'link' => 'https://example.com/'.$this->faker->slug(2),
'is_active' => $this->faker->boolean(80),
'new_tab' => $this->faker->boolean(30),
'sort' => $this->faker->numberBetween(1, 10),
];
}

public function active(): static
{
return $this->state(fn (array $attributes): array => [
'is_active' => true,
]);
}

public function inactive(): static
{
return $this->state(fn (array $attributes): array => [
'is_active' => false,
]);
}

public function newTab(): static
{
return $this->state(fn (array $attributes): array => [
'new_tab' => true,
]);
}
}
83 changes: 83 additions & 0 deletions database/factories/BannerImageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Eclipse\Cms\Factories;

use Eclipse\Cms\Models\Banner\Image;
use Illuminate\Database\Eloquent\Factories\Factory;

class BannerImageFactory extends Factory
{
protected $model = Image::class;

public function definition(): array
{
$sampleImages = [
'banners/summer-sale-desktop.jpg',
'banners/winter-collection-mobile.jpg',
'banners/spring-deals-tablet.jpg',
'banners/black-friday-hero.jpg',
'banners/new-arrivals-sidebar.jpg',
'banners/limited-offer-footer.jpg',
'banners/featured-product-square.jpg',
'banners/special-promo-wide.jpg',
'banners/holiday-sale-banner.jpg',
'banners/end-season-deals.jpg',
];

return [
'file' => $this->faker->randomElement($sampleImages),
'image_width' => $this->faker->randomElement([400, 800, 1200, 1920]),
'image_height' => $this->faker->randomElement([200, 400, 600, 800]),
'is_hidpi' => $this->faker->boolean(30),
];
}

public function desktop(): static
{
return $this->state(fn (array $attributes): array => [
'file' => 'banners/desktop-'.$this->faker->slug().'.jpg',
'image_width' => 1920,
'image_height' => 600,
'is_hidpi' => false,
]);
}

public function mobile(): static
{
return $this->state(fn (array $attributes): array => [
'file' => 'banners/mobile-'.$this->faker->slug().'.jpg',
'image_width' => 800,
'image_height' => 400,
'is_hidpi' => true,
]);
}

public function tablet(): static
{
return $this->state(fn (array $attributes): array => [
'file' => 'banners/tablet-'.$this->faker->slug().'.jpg',
'image_width' => 1024,
'image_height' => 500,
'is_hidpi' => false,
]);
}

public function square(): static
{
return $this->state(fn (array $attributes): array => [
'file' => 'banners/square-'.$this->faker->slug().'.jpg',
'image_width' => 400,
'image_height' => 400,
'is_hidpi' => false,
]);
}

public function hidpi(): static
{
return $this->state(fn (array $attributes): array => [
'is_hidpi' => true,
'image_width' => $attributes['image_width'] * 2,
'image_height' => $attributes['image_height'] * 2,
]);
}
}
60 changes: 60 additions & 0 deletions database/factories/BannerImageTypeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Eclipse\Cms\Factories;

use Eclipse\Cms\Models\Banner\ImageType;
use Illuminate\Database\Eloquent\Factories\Factory;

class BannerImageTypeFactory extends Factory
{
protected $model = ImageType::class;

public function definition(): array
{
return [
'name' => [
'en' => 'Desktop',
'sl' => 'Namizje',
],
'code' => 'desktop',
'image_width' => 1200,
'image_height' => 400,
'is_hidpi' => false,
];
}

public function desktop(): static
{
return $this->state(fn (array $attributes) => [
'name' => [
'en' => 'Desktop',
'sl' => 'Namizje',
],
'code' => 'desktop',
'image_width' => 1200,
'image_height' => 400,
'is_hidpi' => false,
]);
}

public function mobile(): static
{
return $this->state(fn (array $attributes) => [
'name' => [
'en' => 'Mobile',
'sl' => 'Mobilni',
],
'code' => 'mobile',
'image_width' => 800,
'image_height' => 400,
'is_hidpi' => true,
]);
}

public function hidpi(): static
{
return $this->state(fn (array $attributes) => [
'is_hidpi' => true,
]);
}
}
22 changes: 22 additions & 0 deletions database/factories/BannerPositionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Eclipse\Cms\Factories;

use Eclipse\Cms\Models\Banner\Position;
use Illuminate\Database\Eloquent\Factories\Factory;

class BannerPositionFactory extends Factory
{
protected $model = Position::class;

public function definition(): array
{
return [
'name' => [
'en' => 'Website Banners',
'sl' => 'Spletni Bannerji',
],
'code' => 'website',
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

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

return new class extends Migration
{
public function up(): void
{
Schema::table('cms_banner_images', function (Blueprint $table) {
$table->json('file')->change();
});
}

public function down(): void
{
Schema::table('cms_banner_images', function (Blueprint $table) {
$table->string('file')->change();
});
}
};
129 changes: 129 additions & 0 deletions database/seeders/BannerSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace Eclipse\Cms\Seeders;

use Eclipse\Cms\Models\Banner;
use Eclipse\Cms\Models\Banner\ImageType;
use Eclipse\Cms\Models\Banner\Position;
use Exception;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Storage;

class BannerSeeder extends Seeder
{
public function run(): void
{
$tenantData = $this->getTenantData();

$position = Position::factory()->create(['code' => 'website', ...$tenantData]);

$desktop = ImageType::factory()->for($position)->desktop()->create();
$mobile = ImageType::factory()->for($position)->mobile()->create();

$banners = [
['name' => ['en' => 'Summer Sale', 'sl' => 'Poletna Razprodaja'], 'active' => true, 'new_tab' => false],
['name' => ['en' => 'Winter Sale', 'sl' => 'Zimska Razprodaja'], 'active' => true, 'new_tab' => false],
['name' => ['en' => 'Special Offer', 'sl' => 'Posebna Ponudba'], 'active' => false, 'new_tab' => true],
];

foreach ($banners as $index => $bannerData) {
$banner = Banner::factory()->create([
'position_id' => $position->id,
'name' => $bannerData['name'],
'is_active' => $bannerData['active'],
'new_tab' => $bannerData['new_tab'],
'sort' => $index + 1,
]);

$this->addImages($banner, $desktop, 'desktop');
$this->addImages($banner, $mobile, 'mobile');
}
}

private function addImages($banner, $type, $suffix): void
{
$bannerName = is_array($banner->name) ? $banner->name['en'] : $banner->name;

if ($type->is_hidpi) {
$hidpiFilename = "banner-{$banner->id}-{$suffix}@2x.png";
$hidpiWidth = $type->image_width * 2;
$hidpiHeight = $type->image_height * 2;

$this->createBannerImage(
$hidpiWidth,
$hidpiHeight,
strtoupper($suffix)." @2x - {$bannerName}",
$hidpiFilename
);

$banner->images()->create([
'type_id' => $type->id,
'file' => $hidpiFilename,
'image_width' => $hidpiWidth,
'image_height' => $hidpiHeight,
'is_hidpi' => true,
]);
} else {
$regularFilename = "banner-{$banner->id}-{$suffix}.png";
$this->createBannerImage(
$type->image_width,
$type->image_height,
strtoupper($suffix)." - {$bannerName}",
$regularFilename
);

$banner->images()->create([
'type_id' => $type->id,
'file' => $regularFilename,
'image_width' => $type->image_width,
'image_height' => $type->image_height,
'is_hidpi' => false,
]);
}
}

private function getTenantData(): array
{
if (config('eclipse-cms.tenancy.enabled')) {
$tenantModel = config('eclipse-cms.tenancy.model');
if ($tenantModel && class_exists($tenantModel)) {
$tenantFK = config('eclipse-cms.tenancy.foreign_key', 'site_id');
$tenant = $tenantModel::first() ?: $tenantModel::create([
'name' => 'Default Site',
'domain' => 'localhost',
]);
if ($tenant) {
return [$tenantFK => $tenant->id];
}
}
}

return [];
}

protected function createBannerImage(int $width, int $height, string $text, string $filePath): bool
{
if (Storage::exists($filePath)) {
return true;
}

try {
$url = "https://dummyimage.com/{$width}x{$height}/ffffff/000000.png?text=".urlencode($text);
$context = stream_context_create(['http' => ['timeout' => 10]]);
$imageData = file_get_contents($url, false, $context);

if (! $imageData) {
return false;
}

$directory = dirname($filePath);
if (! Storage::exists($directory)) {
Storage::makeDirectory($directory);
}

return Storage::put($filePath, $imageData);
} catch (Exception) {
return false;
}
}
}
3 changes: 3 additions & 0 deletions database/seeders/CmsSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ public function run(): void
Section::factory()
->count(3)
->create();

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