|
1
|
|
|
<?php namespace Arcanesoft\Blog\Models; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelSeo\Traits\Seoable; |
|
4
|
|
|
use Arcanesoft\Blog\Entities\PostStatus; |
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
8
|
|
|
use Illuminate\Support\Arr; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
use Illuminate\Support\Facades\DB; |
|
11
|
|
|
use Illuminate\Support\Str; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Post |
|
15
|
|
|
* |
|
16
|
|
|
* @package Arcanesoft\Blog\Models |
|
17
|
|
|
* @author ARCANEDEV <[email protected]> |
|
18
|
|
|
* |
|
19
|
|
|
* @property int id |
|
20
|
|
|
* @property int author_id |
|
21
|
|
|
* @property int category_id |
|
22
|
|
|
* @property string title |
|
23
|
|
|
* @property string slug |
|
24
|
|
|
* @property string excerpt |
|
25
|
|
|
* @property string content_raw |
|
26
|
|
|
* @property string content_html |
|
27
|
|
|
* @property bool is_draft |
|
28
|
|
|
* @property \Carbon\Carbon published_at |
|
29
|
|
|
* @property \Carbon\Carbon created_at |
|
30
|
|
|
* @property \Carbon\Carbon updated_at |
|
31
|
|
|
* @property \Carbon\Carbon deleted_at |
|
32
|
|
|
* |
|
33
|
|
|
* @property \Arcanesoft\Contracts\Auth\Models\User user |
|
34
|
|
|
* @property \Arcanesoft\Blog\Models\Category category |
|
35
|
|
|
* |
|
36
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder published() |
|
37
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder publishedAt(int $year) |
|
38
|
|
|
*/ |
|
39
|
|
|
class Post extends AbstractModel |
|
40
|
|
|
{ |
|
41
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
42
|
|
|
| Constants |
|
43
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
44
|
|
|
*/ |
|
45
|
|
|
const STATUS_DRAFT = 'draft'; |
|
46
|
|
|
const STATUS_PUBLISHED = 'published'; |
|
47
|
|
|
|
|
48
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
49
|
|
|
| Traits |
|
50
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
51
|
|
|
*/ |
|
52
|
|
|
use Seoable, SoftDeletes; |
|
53
|
|
|
|
|
54
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
55
|
|
|
| Properties |
|
56
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
57
|
|
|
*/ |
|
58
|
|
|
/** |
|
59
|
|
|
* The database table used by the model |
|
60
|
|
|
* |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $table = 'posts'; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* The attributes that are mass assignable |
|
67
|
|
|
* |
|
68
|
|
|
* @var array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $fillable = [ |
|
71
|
|
|
'author_id', 'category_id', 'title', 'excerpt', 'content', 'published_at' |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* The attributes that should be mutated to dates. |
|
76
|
|
|
* |
|
77
|
|
|
* @var array |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $dates = ['published_at', 'deleted_at']; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* The attributes that should be casted to native types. |
|
83
|
|
|
* |
|
84
|
|
|
* @var array |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $casts = [ |
|
87
|
|
|
'author_id' => 'integer', |
|
88
|
|
|
'category_id' => 'integer', |
|
89
|
|
|
'is_draft' => 'boolean', |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
93
|
|
|
| Scopes |
|
94
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
95
|
|
|
*/ |
|
96
|
|
|
/** |
|
97
|
|
|
* Scope only published posts. |
|
98
|
|
|
* |
|
99
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
100
|
|
|
*/ |
|
101
|
|
|
public function scopePublished(Builder $builder) |
|
102
|
|
|
{ |
|
103
|
|
|
$builder->where('is_draft', false) |
|
104
|
|
|
->where('published_at', '<=', Carbon::now()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Scope only published posts. |
|
109
|
|
|
* |
|
110
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
111
|
|
|
* @param int $year |
|
112
|
|
|
*/ |
|
113
|
|
|
public function scopePublishedAt(Builder $builder, $year) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->scopePublished($builder); |
|
116
|
|
|
$builder->where(DB::raw('YEAR(published_at)'), $year); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
120
|
|
|
| Relationships |
|
121
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
122
|
|
|
*/ |
|
123
|
|
|
/** |
|
124
|
|
|
* Author relationship. |
|
125
|
|
|
* |
|
126
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
127
|
|
|
*/ |
|
128
|
|
|
public function author() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->belongsTo( |
|
131
|
|
|
config('auth.providers.users.model', \App\Models\User::class), |
|
132
|
|
|
'author_id' |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Category relationship. |
|
138
|
|
|
* |
|
139
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
140
|
|
|
*/ |
|
141
|
|
|
public function category() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->belongsTo(Category::class); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Tags relationship. |
|
148
|
|
|
* |
|
149
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
150
|
|
|
*/ |
|
151
|
|
|
public function tags() |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->belongsToMany(Tag::class, "{$this->prefix}post_tag"); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
157
|
|
|
| Getters & Setters |
|
158
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
159
|
|
|
*/ |
|
160
|
|
|
/** |
|
161
|
|
|
* Set the title attribute. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $title |
|
164
|
|
|
*/ |
|
165
|
|
|
public function setTitleAttribute($title) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->attributes['title'] = $title; |
|
168
|
|
|
$this->attributes['slug'] = Str::slug($title); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Set the content attribute. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $content |
|
175
|
|
|
*/ |
|
176
|
|
|
public function setContentAttribute($content) |
|
177
|
|
|
{ |
|
178
|
|
|
$this->attributes['content_raw'] = $content; |
|
179
|
|
|
$this->attributes['content_html'] = markdown($content); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Get the content attribute. |
|
184
|
|
|
* |
|
185
|
|
|
* @return \Illuminate\Support\HtmlString |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getContentAttribute() |
|
188
|
|
|
{ |
|
189
|
|
|
return new \Illuminate\Support\HtmlString($this->content_html); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Set the status attribute. |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $content |
|
|
|
|
|
|
196
|
|
|
*/ |
|
197
|
|
|
public function setStatusAttribute($status) |
|
198
|
|
|
{ |
|
199
|
|
|
$this->attributes['is_draft'] = ($status === self::STATUS_DRAFT); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Get the status name attribute. |
|
204
|
|
|
* |
|
205
|
|
|
* @return string|null |
|
206
|
|
|
*/ |
|
207
|
|
|
public function getStatusNameAttribute() |
|
208
|
|
|
{ |
|
209
|
|
|
return self::getStatuses()->get( |
|
210
|
|
|
$this->isDraft() ? self::STATUS_DRAFT : self::STATUS_PUBLISHED |
|
211
|
|
|
); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
215
|
|
|
| Main Functions |
|
216
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
217
|
|
|
*/ |
|
218
|
|
|
/** |
|
219
|
|
|
* Create a post. |
|
220
|
|
|
* |
|
221
|
|
|
* @param array $inputs |
|
222
|
|
|
* |
|
223
|
|
|
* @return bool |
|
224
|
|
|
*/ |
|
225
|
|
|
public function createOne(array $inputs) |
|
226
|
|
|
{ |
|
227
|
|
|
$attributes = [ |
|
228
|
|
|
'author_id' => auth()->user()->getAuthIdentifier(), |
|
229
|
|
|
'category_id' => $inputs['category'], |
|
230
|
|
|
] + Arr::only($inputs, [ |
|
231
|
|
|
'title', 'excerpt', 'content', 'published_at', 'status' |
|
232
|
|
|
]); |
|
233
|
|
|
|
|
234
|
|
|
$this->fill($attributes); |
|
235
|
|
|
|
|
236
|
|
|
$saved = $this->save(); |
|
237
|
|
|
$this->tags()->sync($inputs['tags']); |
|
238
|
|
|
|
|
239
|
|
|
$this->createSeo( |
|
240
|
|
|
$this->extractSeoAttributes($inputs) |
|
241
|
|
|
); |
|
242
|
|
|
|
|
243
|
|
|
return $saved; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Create a post. |
|
248
|
|
|
* |
|
249
|
|
|
* @param array $inputs |
|
250
|
|
|
* |
|
251
|
|
|
* @return bool|int |
|
252
|
|
|
*/ |
|
253
|
|
|
public function updateOne(array $inputs) |
|
254
|
|
|
{ |
|
255
|
|
|
$attributes = ['category_id' => $inputs['category']] + Arr::only($inputs, [ |
|
256
|
|
|
'title', 'excerpt', 'content', 'published_at', 'status' |
|
257
|
|
|
]); |
|
258
|
|
|
|
|
259
|
|
|
$updated = $this->update($attributes); |
|
260
|
|
|
$this->tags()->sync($inputs['tags']); |
|
261
|
|
|
|
|
262
|
|
|
$seoAttributes = $this->extractSeoAttributes($inputs); |
|
263
|
|
|
$this->hasSeo() ? $this->updateSeo($seoAttributes) : $this->createSeo($seoAttributes); |
|
264
|
|
|
|
|
265
|
|
|
return $updated; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
269
|
|
|
| Check Functions |
|
270
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
271
|
|
|
*/ |
|
272
|
|
|
/** |
|
273
|
|
|
* Check if the post's status is "draft". |
|
274
|
|
|
* |
|
275
|
|
|
* @return bool |
|
276
|
|
|
*/ |
|
277
|
|
|
public function isDraft() |
|
278
|
|
|
{ |
|
279
|
|
|
return $this->is_draft; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Check if the post's status is "published". |
|
284
|
|
|
* |
|
285
|
|
|
* @return bool |
|
286
|
|
|
*/ |
|
287
|
|
|
public function isPublished() |
|
288
|
|
|
{ |
|
289
|
|
|
return ! $this->isDraft(); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
293
|
|
|
| Other Functions |
|
294
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
295
|
|
|
*/ |
|
296
|
|
|
/** |
|
297
|
|
|
* Get the post statuses. |
|
298
|
|
|
* |
|
299
|
|
|
* @return \Illuminate\Support\Collection |
|
300
|
|
|
*/ |
|
301
|
|
|
public static function getStatuses() |
|
302
|
|
|
{ |
|
303
|
|
|
return Collection::make( |
|
304
|
|
|
trans('blog::posts.statuses', []) |
|
305
|
|
|
); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Extract the seo attributes. |
|
310
|
|
|
* |
|
311
|
|
|
* @param array $inputs |
|
312
|
|
|
* |
|
313
|
|
|
* @return array |
|
314
|
|
|
*/ |
|
315
|
|
|
protected function extractSeoAttributes(array $inputs) |
|
316
|
|
|
{ |
|
317
|
|
|
return [ |
|
318
|
|
|
'title' => Arr::get($inputs, 'seo_title'), |
|
319
|
|
|
'description' => Arr::get($inputs, 'seo_description'), |
|
320
|
|
|
'keywords' => Arr::get($inputs, 'seo_keywords'), |
|
321
|
|
|
'metas' => Arr::get($inputs, 'seo_metas'), |
|
322
|
|
|
]; |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.