|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Modules\Blog\Entities; |
|
4
|
|
|
|
|
5
|
|
|
use Dimsav\Translatable\Translatable; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Laracasts\Presenter\PresentableTrait; |
|
9
|
|
|
use Modules\Blog\Presenters\PostPresenter; |
|
10
|
|
|
use Modules\Media\Entities\File; |
|
11
|
|
|
use Modules\Media\Support\Traits\MediaRelation; |
|
12
|
|
|
|
|
13
|
|
|
class Post extends Model |
|
14
|
|
|
{ |
|
15
|
|
|
use Translatable, MediaRelation, PresentableTrait; |
|
16
|
|
|
|
|
17
|
|
|
public $translatedAttributes = ['title', 'slug', 'content']; |
|
18
|
|
|
protected $fillable = ['category_id', 'status', 'title', 'slug', 'content']; |
|
19
|
|
|
protected $table = 'blog__posts'; |
|
20
|
|
|
protected $presenter = PostPresenter::class; |
|
21
|
|
|
protected $casts = [ |
|
22
|
|
|
'status' => 'int', |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
public function category() |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->belongsTo(Category::class); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function tags() |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->belongsToMany(Tag::class, 'blog__post_tag')->withTimestamps(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Get the thumbnail image for the current blog post |
|
37
|
|
|
* @return File|string |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getThumbnailAttribute() |
|
40
|
|
|
{ |
|
41
|
|
|
$thumbnail = $this->files()->where('zone', 'thumbnail')->first(); |
|
42
|
|
|
|
|
43
|
|
|
if ($thumbnail === null) { |
|
44
|
|
|
return ''; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $thumbnail; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Check if the post is in draft |
|
52
|
|
|
* @param Builder $query |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
|
|
public function scopeDraft(Builder $query) |
|
56
|
|
|
{ |
|
57
|
|
|
return (bool) $query->whereStatus(0); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Check if the post is pending review |
|
62
|
|
|
* @param Builder $query |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
|
|
public function scopePending(Builder $query) |
|
66
|
|
|
{ |
|
67
|
|
|
return (bool) $query->whereStatus(1); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Check if the post is published |
|
72
|
|
|
* @param Builder $query |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
public function scopePublished(Builder $query) |
|
76
|
|
|
{ |
|
77
|
|
|
return (bool) $query->whereStatus(2); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Check if the post is unpublish |
|
82
|
|
|
* @param Builder $query |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public function scopeUnpublished(Builder $query) |
|
86
|
|
|
{ |
|
87
|
|
|
return (bool) $query->whereStatus(3); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param $method |
|
92
|
|
|
* @param $parameters |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
*/ |
|
95
|
|
|
public function __call($method, $parameters) |
|
96
|
|
|
{ |
|
97
|
|
|
#i: Convert array to dot notation |
|
98
|
|
|
$config = implode('.', ['asgard.blog.config.post.relations', $method]); |
|
99
|
|
|
|
|
100
|
|
|
#i: Relation method resolver |
|
101
|
|
|
if (config()->has($config)) { |
|
102
|
|
|
$function = config()->get($config); |
|
103
|
|
|
|
|
104
|
|
|
return $function($this); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
#i: No relation found, return the call to parent (Eloquent) to handle it. |
|
108
|
|
|
return parent::__call($method, $parameters); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|