252 lines
6.2 KiB
PHP
252 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Image;
|
|
use App\Models\Heading;
|
|
use App\Models\Question;
|
|
use App\Models\CommonWord;
|
|
use App\Models\BlogSetObject;
|
|
use App\Models\CommonWordBlog;
|
|
use App\Models\Blog as ModelBlog;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Blog extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $table = 'blogs';
|
|
protected $fillable = [
|
|
'content',
|
|
'preview',
|
|
'changefreq',
|
|
'priority',
|
|
'image',
|
|
'status',
|
|
'subject',
|
|
'description',
|
|
'reed_time',
|
|
'views',
|
|
'category_array',
|
|
'category_id',
|
|
'chosen',
|
|
'slug',
|
|
'user_id', 'imageCheck', 'headingCheck', 'notBlog', 'content_id', 'published_at', 'grade', 'content_errors'];
|
|
|
|
protected $casts = [
|
|
'category_array' => 'array',
|
|
'published_at' => 'datetime',
|
|
];
|
|
|
|
protected $appends = ['computed_published_at'];
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::addGlobalScope('filtered', function ($query) {
|
|
$query->where(function ($query) {
|
|
$query
|
|
->whereNotNull('content_id')
|
|
->where('blogs.id', function ($subQuery) {
|
|
$subQuery->selectRaw('MAX(id)')->from('blogs as blog_edits')->whereColumn('blog_edits.content_id', 'blogs.content_id');
|
|
})
|
|
->orWhere(function ($query) {
|
|
$query->whereNull('content_id')->whereNotIn('blogs.id', function ($subQuery) {
|
|
$subQuery->select('content_id')->from('blogs')->whereNotNull('content_id');
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
public function getComputedPublishedAtAttribute()
|
|
{
|
|
if ($this->published_at) {
|
|
return $this->published_at;
|
|
}
|
|
|
|
if (!is_null($this->content_id)) {
|
|
$blog = DB::table('blogs')->where('id', $this->content_id)->first();
|
|
if ($blog->published_at == null) {
|
|
$blog->published_at = $blog->created_at;
|
|
}
|
|
return $blog->published_at ?? '2021-08-02 12:05:01';
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function getPublishAtAttribute()
|
|
{
|
|
if ($this->published_at) {
|
|
return $this->published_at;
|
|
}
|
|
|
|
if (!is_null($this->content_id)) {
|
|
$blog = DB::table('blogs')->where('id', $this->content_id)->first();
|
|
if ($blog->published_at == null) {
|
|
$blog->published_at = $blog->created_at;
|
|
}
|
|
return $blog->published_at ?? '2021-08-02 12:05:01';
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
public function parent()
|
|
{
|
|
return $this->belongsTo(ModelBlog::class, 'content_id', 'id');
|
|
}
|
|
|
|
public function children()
|
|
{
|
|
return $this->hasMany(ModelBlog::class, 'content_id', 'id');
|
|
}
|
|
|
|
public function headers()
|
|
{
|
|
return $this->hasMany(Heading::class);
|
|
}
|
|
|
|
public function headings()
|
|
{
|
|
return $this->hasMany(Heading::class);
|
|
}
|
|
|
|
public function images()
|
|
{
|
|
return $this->hasMany(Image::class);
|
|
}
|
|
|
|
public function latestChild()
|
|
{
|
|
return $this->children()->orderBy('created_at', 'desc')->first();
|
|
}
|
|
|
|
public function editsAsContent()
|
|
{
|
|
return $this->hasMany(Blog::class, 'content_id', 'id');
|
|
}
|
|
|
|
public function latestVersion()
|
|
{
|
|
return $this->hasMany(ModelBlog::class, 'content_id', 'id')->orderBy('created_at', 'desc')->limit(1);
|
|
}
|
|
|
|
public function categories()
|
|
{
|
|
return Category::whereIn('id', $this->category_array)->get();
|
|
}
|
|
|
|
|
|
public function commonWords()
|
|
{
|
|
return $this->belongsToMany(CommonWord::class, 'common_word_blog')
|
|
->using(CommonWordBlog::class)
|
|
->withPivot('count')
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function category()
|
|
{
|
|
return $this->belongsTo(Category::class);
|
|
}
|
|
|
|
public function opinions()
|
|
{
|
|
return $this->hasMany(UserOpinion::class, 'blog_id', 'id');
|
|
}
|
|
|
|
public function comments()
|
|
{
|
|
return $this->hasMany(Comment::class, 'blog_id', 'id');
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function getStatusClassAttribute()
|
|
{
|
|
return $this->status == 0 ? 'bg-orange-500' : 'bg-ahrom';
|
|
}
|
|
|
|
public function getStatusTextAttribute()
|
|
{
|
|
return $this->status == 0 ? 'پیشنویس' : 'انتشار';
|
|
}
|
|
|
|
public function getStatusImageAttribute()
|
|
{
|
|
if ($this->imageCheck == 1) {
|
|
return 'bg-[#FFA500]';
|
|
} elseif ($this->imageCheck == 2) {
|
|
return 'bg-green-500';
|
|
} else {
|
|
return 'bg-slate-500';
|
|
}
|
|
}
|
|
|
|
public function getStatusHeadAttribute()
|
|
{
|
|
if ($this->headingCheck == 1) {
|
|
return 'bg-[#FFA500]';
|
|
} elseif ($this->headingCheck == 2) {
|
|
return 'bg-green-500';
|
|
} else {
|
|
return 'bg-slate-500';
|
|
}
|
|
}
|
|
|
|
public function questions()
|
|
{
|
|
return $this->hasMany(Question::class);
|
|
}
|
|
|
|
public function getContentAttribute($value)
|
|
{
|
|
return str_replace('***', '‌', $value);
|
|
}
|
|
|
|
public function getPreviewAttribute($value)
|
|
{
|
|
return str_replace('***', '‌', $value);
|
|
}
|
|
|
|
public function setContentAttribute($value)
|
|
{
|
|
$this->attributes['content'] = str_replace('‌', '***', $value);
|
|
}
|
|
|
|
public function setPreviewAttribute($value)
|
|
{
|
|
$this->attributes['preview'] = str_replace('‌', '***', $value);
|
|
}
|
|
|
|
public function scopeOrderByPublished($query, $direction = 'desc')
|
|
{
|
|
return $query->orderBy('computed_published_at', $direction);
|
|
}
|
|
|
|
public function setObjects()
|
|
{
|
|
return $this->hasMany(BlogSetObject::class, 'blog_id', 'id');
|
|
}
|
|
|
|
public function sectionImages()
|
|
{
|
|
return $this->hasMany(BlogSectionImage::class);
|
|
}
|
|
}
|
|
|
|
|
|
|