1 | <?php namespace Arcanesoft\Blog\Models; |
||
41 | class Post extends AbstractModel |
||
42 | { |
||
43 | /* ----------------------------------------------------------------- |
||
44 | | Constants |
||
45 | | ----------------------------------------------------------------- |
||
46 | */ |
||
47 | |||
48 | const STATUS_DRAFT = 'draft'; |
||
49 | const STATUS_PUBLISHED = 'published'; |
||
50 | |||
51 | /* ----------------------------------------------------------------- |
||
52 | | Traits |
||
53 | | ----------------------------------------------------------------- |
||
54 | */ |
||
55 | |||
56 | use Presenters\PostPresenter, |
||
57 | Seoable, |
||
58 | SoftDeletes; |
||
59 | |||
60 | /* ----------------------------------------------------------------- |
||
61 | | Properties |
||
62 | | ----------------------------------------------------------------- |
||
63 | */ |
||
64 | |||
65 | /** |
||
66 | * The database table used by the model |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $table = 'posts'; |
||
71 | |||
72 | /** |
||
73 | * The attributes that are mass assignable |
||
74 | * |
||
75 | * @var array |
||
76 | */ |
||
77 | protected $fillable = [ |
||
78 | 'author_id', 'category_id', 'locale', 'title', 'excerpt', 'thumbnail', 'content', 'published_at', |
||
79 | ]; |
||
80 | |||
81 | /** |
||
82 | * The attributes that should be mutated to dates. |
||
83 | * |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $dates = ['published_at', 'deleted_at']; |
||
87 | |||
88 | /** |
||
89 | * The attributes that should be casted to native types. |
||
90 | * |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $casts = [ |
||
94 | 'author_id' => 'integer', |
||
95 | 'category_id' => 'integer', |
||
96 | 'is_draft' => 'boolean', |
||
97 | ]; |
||
98 | |||
99 | /** |
||
100 | * The event map for the model. |
||
101 | * |
||
102 | * @var array |
||
103 | */ |
||
104 | protected $dispatchesEvents = [ |
||
105 | 'creating' => PostEvents\PostCreating::class, |
||
106 | 'created' => PostEvents\PostCreated::class, |
||
107 | 'updating' => PostEvents\PostUpdating::class, |
||
108 | 'updated' => PostEvents\PostUpdated::class, |
||
109 | 'saving' => PostEvents\PostSaving::class, |
||
110 | 'saved' => PostEvents\PostSaved::class, |
||
111 | 'deleting' => PostEvents\PostDeleting::class, |
||
112 | 'deleted' => PostEvents\PostDeleted::class, |
||
113 | 'restoring' => PostEvents\PostRestoring::class, |
||
114 | 'restored' => PostEvents\PostRestored::class, |
||
115 | ]; |
||
116 | |||
117 | /* ----------------------------------------------------------------- |
||
118 | | Scopes |
||
119 | | ----------------------------------------------------------------- |
||
120 | */ |
||
121 | |||
122 | /** |
||
123 | * Scope only published posts. |
||
124 | * |
||
125 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
126 | * |
||
127 | * @return \Illuminate\Database\Eloquent\Builder |
||
128 | */ |
||
129 | public function scopePublished(Builder $query) |
||
134 | |||
135 | /** |
||
136 | * Scope only published posts. |
||
137 | * |
||
138 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
139 | * @param int $year |
||
140 | * |
||
141 | * @return \Illuminate\Database\Eloquent\Builder |
||
142 | */ |
||
143 | public function scopePublishedAt(Builder $query, $year) |
||
148 | |||
149 | /** |
||
150 | * Scope by post's locale. |
||
151 | * |
||
152 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
153 | * @param string|null $locale |
||
154 | * |
||
155 | * @return \Illuminate\Database\Eloquent\Builder |
||
156 | */ |
||
157 | public function scopeLocalized(Builder $query, $locale = null) |
||
161 | |||
162 | /* ----------------------------------------------------------------- |
||
163 | | Relationships |
||
164 | | ----------------------------------------------------------------- |
||
165 | */ |
||
166 | |||
167 | /** |
||
168 | * Author relationship. |
||
169 | * |
||
170 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
171 | */ |
||
172 | public function author() |
||
173 | { |
||
174 | return $this->belongsTo( |
||
175 | config('auth.providers.users.model', 'App\Models\User'), |
||
176 | 'author_id' |
||
177 | ); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Category relationship. |
||
182 | * |
||
183 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
184 | */ |
||
185 | 2 | public function category() |
|
186 | { |
||
187 | 2 | return $this->belongsTo(Category::class); |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * Tags relationship. |
||
192 | * |
||
193 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
194 | */ |
||
195 | public function tags() |
||
199 | |||
200 | /* ----------------------------------------------------------------- |
||
201 | | Getters & Setters |
||
202 | | ----------------------------------------------------------------- |
||
203 | */ |
||
204 | |||
205 | /** |
||
206 | * Set the title attribute. |
||
207 | * |
||
208 | * @param string $title |
||
209 | */ |
||
210 | 6 | public function setTitleAttribute($title) |
|
211 | { |
||
212 | 6 | $this->attributes['title'] = $title; |
|
213 | 6 | $this->attributes['slug'] = Str::slug($title); |
|
214 | 6 | } |
|
215 | |||
216 | /** |
||
217 | * Set the content attribute. |
||
218 | * |
||
219 | * @param string $content |
||
220 | */ |
||
221 | 6 | public function setContentAttribute($content) |
|
222 | { |
||
223 | 6 | $this->attributes['content_raw'] = $content; |
|
224 | 6 | $this->attributes['content_html'] = markdown($content); |
|
225 | 6 | } |
|
226 | |||
227 | /* ----------------------------------------------------------------- |
||
228 | | Main Functions |
||
229 | | ----------------------------------------------------------------- |
||
230 | */ |
||
231 | |||
232 | /** |
||
233 | * Create a post. |
||
234 | * |
||
235 | * @param array $attributes |
||
236 | * |
||
237 | * @return self |
||
238 | */ |
||
239 | public static function createOne(array $attributes) |
||
240 | { |
||
241 | return tap(new self($attributes), function (self $post) use ($attributes) { |
||
242 | $post->save(); |
||
243 | |||
244 | $post->tags()->sync($attributes['tags']); |
||
245 | |||
246 | $post->createSeo( |
||
247 | static::extractSeoAttributes($attributes) |
||
248 | ); |
||
249 | }); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Create a post. |
||
254 | * |
||
255 | * @param array $inputs |
||
256 | * |
||
257 | * @return bool|int |
||
258 | */ |
||
259 | public function updateOne(array $inputs) |
||
272 | |||
273 | /* ----------------------------------------------------------------- |
||
274 | | Check Functions |
||
275 | | ----------------------------------------------------------------- |
||
276 | */ |
||
277 | |||
278 | /** |
||
279 | * Check if the post's status is "draft". |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | public function isDraft() |
||
287 | |||
288 | /** |
||
289 | * Check if the post's status is "published". |
||
290 | * |
||
291 | * @return bool |
||
292 | */ |
||
293 | public function isPublished() |
||
297 | |||
298 | /** |
||
299 | * Check if the post has thumbnail. |
||
300 | * |
||
301 | * @return bool |
||
302 | */ |
||
303 | public function hasThumbnail() |
||
307 | |||
308 | /* ----------------------------------------------------------------- |
||
309 | | Other Methods |
||
310 | | ----------------------------------------------------------------- |
||
311 | */ |
||
312 | |||
313 | /** |
||
314 | * Extract the seo attributes. |
||
315 | * |
||
316 | * @param array $inputs |
||
317 | * |
||
318 | * @return array |
||
319 | */ |
||
320 | protected static function extractSeoAttributes(array $inputs) |
||
321 | { |
||
322 | return [ |
||
323 | 'title' => Arr::get($inputs, 'seo_title'), |
||
324 | 'description' => Arr::get($inputs, 'seo_description'), |
||
325 | 'keywords' => Arr::get($inputs, 'seo_keywords'), |
||
326 | 'metas' => Arr::get($inputs, 'seo_metas', []), |
||
327 | ]; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Get the show url. |
||
332 | * |
||
333 | * @return string |
||
334 | */ |
||
335 | public function getShowUrl() |
||
339 | |||
340 | /** |
||
341 | * Get the edit url. |
||
342 | * |
||
343 | * @return string |
||
344 | */ |
||
345 | public function getEditUrl() |
||
349 | } |
||
350 |