182 lines
3.8 KiB
PHP
182 lines
3.8 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 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\MediaLibrary\HasMedia;
|
|
use Spatie\MediaLibrary\InteractsWithMedia;
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
|
use Spatie\Translatable\HasTranslations;
|
|
|
|
class Carousel extends Model implements HasMedia
|
|
{
|
|
use HasFactory;
|
|
use HasSchemalessAttributes;
|
|
use HasTranslations;
|
|
use InteractsWithMedia;
|
|
use SortableTrait;
|
|
|
|
/**
|
|
* Table
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table = 'carousels';
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<string>
|
|
*/
|
|
protected $fillable = [
|
|
'id',
|
|
'app',
|
|
'title',
|
|
'description',
|
|
'place',
|
|
'link',
|
|
'is_visible',
|
|
'resource_id',
|
|
'resource_type',
|
|
'sort_order',
|
|
'options',
|
|
];
|
|
|
|
/**
|
|
* Transtable fields
|
|
*
|
|
* @var array<string>
|
|
*/
|
|
public array $translatable = ['title', 'description'];
|
|
|
|
/**
|
|
* Sortable attributes
|
|
*
|
|
* @var array<string, mixed>
|
|
*/
|
|
public $sortable = [
|
|
'order_column_name' => 'sort_order',
|
|
'sort_when_creating' => true,
|
|
];
|
|
|
|
/**
|
|
* Category
|
|
*/
|
|
public function category(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Category::class, 'resource_id');
|
|
}
|
|
|
|
/**
|
|
* Collection
|
|
*/
|
|
public function collection(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Collection::class, 'resource_id');
|
|
}
|
|
|
|
/**
|
|
* Brand
|
|
*/
|
|
public function brand(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Brand::class, 'resource_id');
|
|
}
|
|
|
|
/**
|
|
* Media collections
|
|
*/
|
|
public function registerMediaCollections(): void
|
|
{
|
|
$this->addMediaCollection('main')
|
|
->singleFile();
|
|
}
|
|
|
|
/**
|
|
* Media conversations
|
|
*/
|
|
public function registerMediaConversions(?Media $media = null): void
|
|
{
|
|
$this->addMediaConversion('thumb50x50')
|
|
->width(50)
|
|
->height(50);
|
|
|
|
$this->addMediaConversion('thumb350x350')
|
|
->width(350)
|
|
->height(350);
|
|
|
|
$this->addMediaConversion('thumb650x650')
|
|
->width(650)
|
|
->height(650);
|
|
}
|
|
|
|
/**
|
|
* Thumbnail
|
|
*/
|
|
public function thumbnail(string $resolution = '50x50'): string
|
|
{
|
|
return $this->getFirstMediaUrl('main', 'thumb'.$resolution);
|
|
}
|
|
|
|
/**
|
|
* Scope to filter
|
|
*
|
|
* @param mixed $query
|
|
* @param string $attribute
|
|
* @param string $value
|
|
* @return $query
|
|
*/
|
|
public function scopeFilter($query, $attribute, $value): Builder
|
|
{
|
|
return $value ? $query->where($attribute, $value) : $query;
|
|
}
|
|
|
|
/**
|
|
* Main image
|
|
*/
|
|
public function image(): string
|
|
{
|
|
return $this->getFirstMediaUrl('main');
|
|
}
|
|
|
|
/**
|
|
* Text color
|
|
*/
|
|
public function textColor(): string
|
|
{
|
|
return $this->options->get('color') ?? '';
|
|
}
|
|
|
|
/**
|
|
* Places for banner
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function places(): array
|
|
{
|
|
return [
|
|
'homepage' => __('Homepage'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Attachable resourcec to banner
|
|
*/
|
|
public static function attachableResources(): array
|
|
{
|
|
return [
|
|
'brand' => __('Brand'),
|
|
'category' => __('Category'),
|
|
'collection' => __('Collection'),
|
|
];
|
|
}
|
|
}
|