238 lines
5.3 KiB
PHP
238 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models\CMS\Media;
|
|
|
|
use App\Models\Concerns\HasSchemalessAttributes;
|
|
use App\Models\Ecommerce\Product\Brand\Brand;
|
|
use App\Models\Ecommerce\Product\Category\Category;
|
|
use App\Models\Ecommerce\Product\Collection\Collection;
|
|
use App\Models\Ecommerce\Product\Product\Product;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Spatie\EloquentSortable\SortableTrait;
|
|
use Spatie\Image\Manipulations;
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
use Spatie\MediaLibrary\InteractsWithMedia;
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
|
use Spatie\Translatable\HasTranslations;
|
|
|
|
class Banner extends Model implements HasMedia
|
|
{
|
|
use HasFactory;
|
|
use HasSchemalessAttributes;
|
|
use HasTranslations;
|
|
use InteractsWithMedia;
|
|
use SortableTrait;
|
|
|
|
/**
|
|
* Table
|
|
*/
|
|
protected $table = 'banners';
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'id',
|
|
'app',
|
|
'title',
|
|
'description',
|
|
'place',
|
|
'link',
|
|
'is_visible',
|
|
'resource_id',
|
|
'resource_type',
|
|
'sort_order',
|
|
'options',
|
|
];
|
|
|
|
/**
|
|
* Translatable attributes
|
|
*
|
|
* @var array<string>
|
|
*/
|
|
public $translatable = ['title', 'description'];
|
|
|
|
/**
|
|
* Sortable attributes
|
|
*
|
|
* @var array<string, mixed>
|
|
*/
|
|
public $sortable = [
|
|
'order_column_name' => 'sort_order',
|
|
'sort_when_creating' => true,
|
|
];
|
|
|
|
/**
|
|
* Banner Position: First
|
|
*/
|
|
public const PLACE_FIRST = 'first';
|
|
|
|
/**
|
|
* Banner Position: Market
|
|
*/
|
|
public const MARKET = 'market';
|
|
|
|
/**
|
|
* Banner Position: Store
|
|
*/
|
|
public const STORE = 'store';
|
|
|
|
/**
|
|
* Banner Position: Homepage Grids
|
|
*/
|
|
public const HOMEPAGE_2_GRIDS = 'homepage_2_grids';
|
|
|
|
/**
|
|
* Banner Position: Homepage categories
|
|
*/
|
|
public const HOMEPAGE_CATEGORIES = 'homepage_categories';
|
|
|
|
/**
|
|
* Media collections
|
|
*/
|
|
public function registerMediaCollections(): void
|
|
{
|
|
$this->addMediaCollection('main')
|
|
->singleFile();
|
|
}
|
|
|
|
/**
|
|
* Media conversations
|
|
*/
|
|
public function registerMediaConversions(?Media $media = null): void
|
|
{
|
|
$this->addMediaConversion('thumb400x400')
|
|
->fit(Manipulations::FIT_CONTAIN, 400, 400);
|
|
|
|
$this->addMediaConversion('thumb350x350')
|
|
->fit(Manipulations::FIT_CONTAIN, 350, 350);
|
|
|
|
$this->addMediaConversion('thumb800x800')
|
|
->fit(Manipulations::FIT_CONTAIN, 800, 800);
|
|
|
|
$this->addMediaConversion('thumb288x431')
|
|
->fit(Manipulations::FIT_CONTAIN, 288, 431);
|
|
|
|
$this->addMediaConversion('thumb270x350')
|
|
->fit(Manipulations::FIT_CONTAIN, 270, 350);
|
|
}
|
|
|
|
/**
|
|
* Category
|
|
*/
|
|
public function category(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Category::class, 'resource_id');
|
|
}
|
|
|
|
/**
|
|
* Collection
|
|
*/
|
|
public function collection(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Collection::class, 'resource_id');
|
|
}
|
|
|
|
/**
|
|
* Product
|
|
*/
|
|
public function product(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Product::class, 'resource_id');
|
|
}
|
|
|
|
/**
|
|
* Brand
|
|
*/
|
|
public function brand(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Brand::class, 'resource_id');
|
|
}
|
|
|
|
/**
|
|
* Thumbnail URL
|
|
*/
|
|
public function thumbnail(string $resolution = '50x50'): string
|
|
{
|
|
return $this->getFirstMediaUrl('main', 'thumb'.$resolution);
|
|
}
|
|
|
|
/**
|
|
* Original image
|
|
*/
|
|
public function image(): string
|
|
{
|
|
return $this->getFirstMediaUrl('main');
|
|
}
|
|
|
|
/**
|
|
* Scope to filter
|
|
*
|
|
* @param Builder $query
|
|
* @param string $attribute
|
|
* @param string $value
|
|
* @return Builder $query
|
|
*/
|
|
public function scopeFilter($query, $attribute, $value): Builder
|
|
{
|
|
return $value ? $query->where($attribute, $value) : $query;
|
|
}
|
|
|
|
/**
|
|
* Text color
|
|
*/
|
|
public function textColor(): ?string
|
|
{
|
|
return $this->options->get('color');
|
|
}
|
|
|
|
/**
|
|
* Places for banner
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function places(): array
|
|
{
|
|
return [
|
|
'first' => __('First banner'),
|
|
'market' => 'Market Baş sahypa',
|
|
'store' => 'Store Baş sahypa',
|
|
'homepage_2_grid' => __('Homepage 2 grid'),
|
|
'homepage_categories' => __('Homepage categories'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Attachable resourcec to banner
|
|
*/
|
|
public static function attachableResources(): array
|
|
{
|
|
return [
|
|
'brand' => __('Brand'),
|
|
'category' => __('Category'),
|
|
'collection' => __('Collection'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Related resources
|
|
*/
|
|
public function relatedResources()
|
|
{
|
|
if ($this->place === 'homepage_categories') {
|
|
if ($this->resource_type === 'category') {
|
|
$this->load('category');
|
|
|
|
return $this->category->children()->enabled()->ordered()->get();
|
|
}
|
|
}
|
|
|
|
return [];
|
|
}
|
|
}
|