1
|
|
|
<?php namespace Arcanesoft\Blog\Models; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Localization\Traits\HasTranslations; |
4
|
|
|
use Arcanesoft\Blog\Blog; |
5
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Arcanesoft\Blog\Events\Categories as CategoryEvents; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Category |
12
|
|
|
* |
13
|
|
|
* @package Arcanesoft\Blog\Models |
14
|
|
|
* @author ARCANEDEV <[email protected]> |
15
|
|
|
* |
16
|
|
|
* @property int id |
17
|
|
|
* @property string name |
18
|
|
|
* @property string slug |
19
|
|
|
* @property \Carbon\Carbon created_at |
20
|
|
|
* @property \Carbon\Carbon updated_at |
21
|
|
|
* @property \Carbon\Carbon deleted_at |
22
|
|
|
* |
23
|
|
|
* @property \Illuminate\Database\Eloquent\Collection posts |
24
|
|
|
*/ |
25
|
|
|
class Category extends AbstractModel |
26
|
|
|
{ |
27
|
|
|
/* ----------------------------------------------------------------- |
28
|
|
|
| Constants |
29
|
|
|
| ----------------------------------------------------------------- |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
const SELECT_CACHE_NAME = 'blog::categories.select-data'; |
33
|
|
|
|
34
|
|
|
/* ----------------------------------------------------------------- |
35
|
|
|
| Traits |
36
|
|
|
| ----------------------------------------------------------------- |
37
|
|
|
*/ |
38
|
|
|
|
39
|
|
|
use SoftDeletes, |
40
|
|
|
HasTranslations; |
41
|
|
|
|
42
|
|
|
/* ----------------------------------------------------------------- |
43
|
|
|
| Properties |
44
|
|
|
| ----------------------------------------------------------------- |
45
|
|
|
*/ |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The database table used by the model |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $table = 'categories'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The attributes that are mass assignable |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $fillable = ['name', 'slug']; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The attributes that should be mutated to dates. |
63
|
|
|
* |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected $dates = ['deleted_at']; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The attributes that should be cast to native types. |
70
|
|
|
* |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected $casts = [ |
74
|
|
|
'id' => 'integer', |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* The event map for the model. |
79
|
|
|
* |
80
|
|
|
* @var array |
81
|
|
|
*/ |
82
|
|
|
protected $events = [ |
83
|
|
|
'creating' => CategoryEvents\CategoryCreating::class, |
84
|
|
|
'created' => CategoryEvents\CategoryCreated::class, |
85
|
|
|
'updating' => CategoryEvents\CategoryUpdating::class, |
86
|
|
|
'updated' => CategoryEvents\CategoryUpdated::class, |
87
|
|
|
'saving' => CategoryEvents\CategorySaving::class, |
88
|
|
|
'saved' => CategoryEvents\CategorySaved::class, |
89
|
|
|
'deleting' => CategoryEvents\CategoryDeleting::class, |
90
|
|
|
'deleted' => CategoryEvents\CategoryDeleted::class, |
91
|
|
|
'restoring' => CategoryEvents\CategoryRestoring::class, |
92
|
|
|
'restored' => CategoryEvents\CategoryRestored::class, |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
/* ----------------------------------------------------------------- |
96
|
|
|
| Relationships |
97
|
|
|
| ----------------------------------------------------------------- |
98
|
|
|
*/ |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Relationship with posts. |
102
|
|
|
* |
103
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
104
|
|
|
*/ |
105
|
|
|
public function posts() |
106
|
|
|
{ |
107
|
|
|
return $this->hasMany(Post::class); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/* ----------------------------------------------------------------- |
111
|
|
|
| Getters & Setters |
112
|
|
|
| ----------------------------------------------------------------- |
113
|
|
|
*/ |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set the name attribute. |
117
|
|
|
* |
118
|
|
|
* @param string $name |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function setNameAttribute($name) |
123
|
|
|
{ |
124
|
|
|
return $this->attributes['name'] = $name; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set the slug attribute. |
129
|
|
|
* |
130
|
|
|
* @param string $name |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function setSlugAttribute($name) |
135
|
|
|
{ |
136
|
|
|
return $this->attributes['slug'] = Str::slug($name); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get the translatable attributes. |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
public function getTranslatableAttributes() |
145
|
|
|
{ |
146
|
|
|
return Blog::instance()->isTranslatable() ? ['name', 'slug'] : []; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/* ----------------------------------------------------------------- |
150
|
|
|
| Main Methods |
151
|
|
|
| ----------------------------------------------------------------- |
152
|
|
|
*/ |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Create a new category. |
156
|
|
|
* |
157
|
|
|
* @param array $attributes |
158
|
|
|
* |
159
|
|
|
* @return self |
160
|
|
|
*/ |
161
|
|
|
public static function createOne(array $attributes) |
162
|
|
|
{ |
163
|
|
|
return tap(new self, function (self $category) use ($attributes) { |
164
|
|
|
$category->populate($attributes)->save(); |
165
|
|
|
}); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Update the current category. |
170
|
|
|
* |
171
|
|
|
* @param array $attributes |
172
|
|
|
* |
173
|
|
|
* @return self |
174
|
|
|
*/ |
175
|
|
|
public function updateOne(array $attributes) |
176
|
|
|
{ |
177
|
|
|
$this->populate($attributes)->save(); |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get the categories options for select input. |
184
|
|
|
* |
185
|
|
|
* @param bool $placeholder |
186
|
|
|
* |
187
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
188
|
|
|
*/ |
189
|
|
|
public static function getSelectOptions($placeholder = true) |
190
|
|
|
{ |
191
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection $categories */ |
192
|
|
|
$categories = cache()->remember(self::SELECT_CACHE_NAME, 5, function () { |
193
|
|
|
return self::all()->keyBy('id')->transform(function (Category $category) { |
194
|
|
|
return Blog::instance()->isTranslatable() |
195
|
|
|
? implode(' / ', $category->getTranslations('name')) |
196
|
|
|
: $category->name; |
197
|
|
|
}); |
198
|
|
|
}); |
199
|
|
|
|
200
|
|
|
return $placeholder |
201
|
|
|
? $categories->prepend(trans('blog::categories.select-category'), 0) |
202
|
|
|
: $categories; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/* ----------------------------------------------------------------- |
206
|
|
|
| Check Methods |
207
|
|
|
| ----------------------------------------------------------------- |
208
|
|
|
*/ |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Check if category has posts. |
212
|
|
|
* |
213
|
|
|
* @return bool |
214
|
|
|
*/ |
215
|
|
|
public function hasPosts() |
216
|
|
|
{ |
217
|
|
|
return ! $this->posts->isEmpty(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Check if the category is deletable. |
222
|
|
|
* |
223
|
|
|
* @return bool |
224
|
|
|
*/ |
225
|
|
|
public function isDeletable() |
226
|
|
|
{ |
227
|
|
|
return ! $this->hasPosts(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/* ----------------------------------------------------------------- |
231
|
|
|
| Other Methods |
232
|
|
|
| ----------------------------------------------------------------- |
233
|
|
|
*/ |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Fill the model with an array of attributes. |
237
|
|
|
* |
238
|
|
|
* @param array $attributes |
239
|
|
|
* |
240
|
|
|
* @return self |
241
|
|
|
*/ |
242
|
|
|
protected function populate(array $attributes) |
243
|
|
|
{ |
244
|
|
|
if ( ! Blog::instance()->isTranslatable()) |
245
|
|
|
return $this->fill($attributes); |
246
|
|
|
|
247
|
|
|
$this->setTranslations('name', $attributes['name']) |
248
|
|
|
->setTranslations('slug', $attributes['name']); |
249
|
|
|
|
250
|
|
|
return $this->fill(Arr::except($attributes, ['name', 'slug'])); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|