Files
einundzwanzig-portal/app/Models/LibraryItem.php
Benjamin Takats d8086d9201 ->hideFromIndex()
2023-01-24 20:13:33 +01:00

129 lines
3.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Facades\Cookie;
use Spatie\Comments\Models\Concerns\HasComments;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\ModelStatus\HasStatuses;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Spatie\Tags\HasTags;
class LibraryItem extends Model implements HasMedia, Sortable
{
use InteractsWithMedia;
use HasTags;
use SortableTrait;
use HasStatuses;
use HasSlug;
use HasComments;
/**
* The attributes that aren't mass assignable.
* @var array
*/
protected $guarded = [];
/**
* The attributes that should be cast to native types.
* @var array
*/
protected $casts = [
'id' => 'integer',
'lecturer_id' => 'integer',
'library_id' => 'integer',
];
protected static function booted()
{
static::creating(function ($model) {
if (!$model->created_by) {
$model->created_by = auth()->id();
}
});
}
public function getSlugOptions(): SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom(['name'])
->saveSlugsTo('slug')
->usingLanguage(Cookie::get('lang', config('app.locale')));
}
public function registerMediaConversions(Media $media = null): void
{
$this
->addMediaConversion('preview')
->fit(Manipulations::FIT_CROP, 300, 300)
->nonQueued();
$this->addMediaConversion('thumb')
->fit(Manipulations::FIT_CROP, 130, 130)
->width(130)
->height(130);
}
public function registerMediaCollections(): void
{
$this->addMediaCollection('main')
->singleFile()
->useFallbackUrl(asset('img/einundzwanzig.png'));
$this->addMediaCollection('single_file')
->acceptsMimeTypes(['application/pdf', 'application/zip', 'application/octet-stream', 'application/x-zip-compressed', 'multipart/x-zip'])
->singleFile();
$this->addMediaCollection('images')
->useFallbackUrl(asset('img/einundzwanzig.png'));
}
public function createdBy(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
public function lecturer(): BelongsTo
{
return $this->belongsTo(Lecturer::class);
}
public function episode(): BelongsTo
{
return $this->belongsTo(Episode::class);
}
public function libraries(): BelongsToMany
{
return $this->belongsToMany(Library::class);
}
/*
* This string will be used in notifications on what a new comment
* was made.
*/
public function commentableName(): string
{
return __('Library Item');
}
/*
* This URL will be used in notifications to let the user know
* where the comment itself can be read.
*/
public function commentUrl(): string
{
if ($this->type === 'markdown_article') {
return url()->route('article.view', ['libraryItem' => $this]);
} else {
return url()->route('libraryItem.view', ['libraryItem' => $this]);
}
}
}