shaark/app/Post.php

129 lines
3.2 KiB
PHP
Raw Normal View History

<?php
namespace App;
use App\Concerns\Models\HasTags;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
2019-10-10 14:02:10 +01:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2019-10-22 11:19:54 +01:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Http\Request;
2019-10-22 19:29:59 +01:00
use Illuminate\Support\Collection;
use Laravel\Scout\Searchable;
/**
2019-10-22 19:29:59 +01:00
* @method Builder|Post pinnedFirst()
* @method Builder|Post withPrivate(bool|User|Request $private)
* @method Builder|Post withoutChests()
*/
class Post extends Model
{
use HasTags,
Searchable;
protected $fillable = [
'postable_type',
'postable_id',
'is_private',
2019-10-22 19:29:59 +01:00
'is_pinned',
2019-10-10 14:02:10 +01:00
'user_id',
'created_at',
];
protected $casts = [
2019-10-22 19:29:59 +01:00
'is_pinned' => 'bool',
'is_private' => 'bool',
];
public function postable(): MorphTo
{
return $this->morphTo();
}
2019-10-10 14:02:10 +01:00
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
2019-10-22 11:19:54 +01:00
public function shares(): HasMany
{
return $this->hasMany(Share::class);
}
public function getCreatedAtFormatedAttribute(): string
{
return $this->created_at->diffForHumans();
}
2019-10-22 19:29:59 +01:00
public function setIsPinnedAttribute($value): void
{
if ($value instanceof Collection) {
$value = (bool)$value->get('is_pinned', false);
}
$this->attributes['is_pinned'] = $value;
}
public function setIsPrivateAttribute($value): void
{
if ($value instanceof Collection) {
$value = (bool)$value->get('is_private', false);
}
$this->attributes['is_private'] = $value;
}
public function scopeWithPrivate(Builder $query, $user = null): Builder
{
if ($user instanceof Request) {
$user = $user->user();
}
if (empty($user)) {
2019-10-10 14:02:10 +01:00
return $query->where('is_private', 0);
}
2019-10-10 14:02:10 +01:00
if ($user->is_admin === false) {
return $query
->where('is_private', 0)
->orWhere(function ($query) use ($user) {
return $query
->where('is_private', 1)
->where('user_id', $user->id);
});
}
return $query;
}
2019-10-22 19:29:59 +01:00
public function scopePinnedFirst(Builder $query): Builder
{
return $query->orderByDesc('is_pinned');
}
public function scopeWithoutChests(Builder $query): Builder
{
return $query->where('postable_type', '!=', Chest::class);
}
public function scopeOnlyLinks(Builder $query): Builder
{
return $query->where('postable_type', '=', Link::class);
}
public function scopeLinksWithArchive(Builder $query): Builder
{
return $query->whereHasMorph('postable', Link::class, function (Builder $query) {
return $query->whereNotNull('archive');
});
}
public function toSearchableArray()
{
2019-08-27 14:43:44 +01:00
return array_merge([
'id' => $this->id,
2019-08-27 14:43:44 +01:00
'date' => $this->created_at->toDateTimeString(),
], $this->postable->toSearchableArray());
}
}