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