|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chriscreates\Blog; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Chriscreates\Blog\Builders\PostBuilder; |
|
7
|
|
|
use Chriscreates\Blog\Traits\IsAuthorable; |
|
8
|
|
|
use Chriscreates\Blog\Traits\Post\PostAttributes; |
|
9
|
|
|
use Chriscreates\Blog\Traits\Post\PostsHaveACategory; |
|
10
|
|
|
use Chriscreates\Blog\Traits\Post\PostsHaveComments; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
12
|
|
|
use Illuminate\Support\Str; |
|
13
|
|
|
|
|
14
|
|
|
class Post extends Model |
|
15
|
|
|
{ |
|
16
|
|
|
use PostAttributes, |
|
17
|
|
|
IsAuthorable, |
|
18
|
|
|
PostsHaveComments, |
|
19
|
|
|
PostsHaveACategory; |
|
20
|
|
|
|
|
21
|
|
|
public const PUBLISHED = 'published'; |
|
22
|
|
|
public const DRAFT = 'draft'; |
|
23
|
|
|
public const SCHEDULED = 'scheduled'; |
|
24
|
|
|
|
|
25
|
|
|
protected $primaryKey = 'id'; |
|
26
|
|
|
|
|
27
|
|
|
public $guarded = ['id']; |
|
28
|
|
|
|
|
29
|
|
|
public $timestamps = true; |
|
30
|
|
|
|
|
31
|
|
|
protected $appends = [ |
|
32
|
|
|
'tags_count', |
|
33
|
|
|
'statuses', |
|
34
|
|
|
'friendly_status', |
|
35
|
|
|
'view_path', |
|
36
|
|
|
'delete_path', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
protected $dates = ['published_at']; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct(array $attributes = []) |
|
42
|
|
|
{ |
|
43
|
|
|
if ( ! isset($this->table)) { |
|
44
|
|
|
$this->setTable(config('blog.table_prefix', 'blog').'_posts'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
parent::__construct($attributes); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function category() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->hasOne(Category::class, 'id', 'category_id'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function comments() |
|
56
|
|
|
{ |
|
57
|
|
|
if ( ! $this->allow_comments) { |
|
58
|
|
|
return null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ( ! $this->allow_guest_comments) { |
|
62
|
|
|
return $this->morphMany(Comment::class, 'commentable') |
|
63
|
|
|
->whereNull('user_id'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this->morphMany(Comment::class, 'commentable'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function approvedComments() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->comments()->where('is_approved', true); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function disapprovedComments() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->comments()->where('is_approved', false); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function guestComments() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->comments()->whereNull('user_id'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function userComments() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->comments()->whereNotNull('user_id'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function tags() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->morphToMany(Tag::class, 'taggable'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function newEloquentBuilder($query) : PostBuilder |
|
95
|
|
|
{ |
|
96
|
|
|
return new PostBuilder($query); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function path() |
|
100
|
|
|
{ |
|
101
|
|
|
return "/posts/{$this->slug}"; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function setPublished() |
|
105
|
|
|
{ |
|
106
|
|
|
$this->status = self::PUBLISHED; |
|
107
|
|
|
$this->published_at = now(); |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function setDrafted() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->status = self::DRAFT; |
|
115
|
|
|
$this->published_at = null; |
|
116
|
|
|
|
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function setScheduled($publish_date = null) |
|
121
|
|
|
{ |
|
122
|
|
|
if (is_null($publish_date)) { |
|
123
|
|
|
$publish_date = request('published_at'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$this->status = self::SCHEDULED; |
|
127
|
|
|
$this->published_at = new Carbon($publish_date); |
|
128
|
|
|
|
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function setStatus(string $status = null) |
|
133
|
|
|
{ |
|
134
|
|
|
if (is_null($status)) { |
|
135
|
|
|
$status = request('status'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
if ($status == self::PUBLISHED) { |
|
139
|
|
|
$this->setPublished(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if ($status == self::DRAFT) { |
|
143
|
|
|
$this->setDrafted(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
if ($status == self::SCHEDULED) { |
|
147
|
|
|
$this->setScheduled(request('published_at')); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function setSlug(string $title = null) |
|
154
|
|
|
{ |
|
155
|
|
|
$set_slug = ! is_null($title) ? $title : $this->title; |
|
156
|
|
|
|
|
157
|
|
|
$this->slug = Str::slug($set_slug); |
|
158
|
|
|
|
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function statuses() |
|
163
|
|
|
{ |
|
164
|
|
|
return collect([ |
|
165
|
|
|
self::PUBLISHED, |
|
166
|
|
|
self::DRAFT, |
|
167
|
|
|
self::SCHEDULED, |
|
168
|
|
|
]); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function getStatusesAttribute() |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->statuses()->toArray(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function getFriendlyStatusAttribute() |
|
177
|
|
|
{ |
|
178
|
|
|
if ($this->isDraft()) { |
|
179
|
|
|
return ucfirst(self::DRAFT); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
if ($this->isScheduled()) { |
|
183
|
|
|
return ucfirst(self::SCHEDULED)." for: {$this->published_at->format('d-m-Y')}"; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return ucfirst(self::PUBLISHED); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function getViewPathAttribute() |
|
190
|
|
|
{ |
|
191
|
|
|
if ( ! $this->id) { |
|
192
|
|
|
return ''; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return route('posts.show', ['post' => $this->id]); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function getDeletePathAttribute() |
|
199
|
|
|
{ |
|
200
|
|
|
if ( ! $this->id) { |
|
201
|
|
|
return ''; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return route('posts.destroy', ['post' => $this->id]); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|