1
|
|
|
<?php namespace Arcanesoft\Blog\Models; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Localization\Traits\HasTranslations; |
4
|
|
|
use Arcanesoft\Blog\Blog; |
5
|
|
|
use Arcanesoft\Blog\Events\Tags as TagEvents; |
6
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Str; |
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 Tag extends AbstractModel |
26
|
|
|
{ |
27
|
|
|
/* ----------------------------------------------------------------- |
28
|
|
|
| Constants |
29
|
|
|
| ----------------------------------------------------------------- |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
const SELECT_CACHE_NAME = 'blog::tags.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 = 'tags'; |
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 event map for the model. |
70
|
|
|
* |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected $events = [ |
74
|
|
|
'creating' => TagEvents\TagCreating::class, |
75
|
|
|
'created' => TagEvents\TagCreated::class, |
76
|
|
|
'updating' => TagEvents\TagUpdating::class, |
77
|
|
|
'updated' => TagEvents\TagUpdated::class, |
78
|
|
|
'saving' => TagEvents\TagSaving::class, |
79
|
|
|
'saved' => TagEvents\TagSaved::class, |
80
|
|
|
'deleting' => TagEvents\TagDeleting::class, |
81
|
|
|
'deleted' => TagEvents\TagDeleted::class, |
82
|
|
|
'restoring' => TagEvents\TagRestoring::class, |
83
|
|
|
'restored' => TagEvents\TagRestored::class, |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
/* ----------------------------------------------------------------- |
87
|
|
|
| Relationships |
88
|
|
|
| ----------------------------------------------------------------- |
89
|
|
|
*/ |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Posts relationship. |
93
|
|
|
* |
94
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
95
|
|
|
*/ |
96
|
|
|
public function posts() |
97
|
|
|
{ |
98
|
|
|
return $this->belongsToMany(Post::class, $this->getPrefix().'post_tag'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/* ----------------------------------------------------------------- |
102
|
|
|
| Getters & Setters |
103
|
|
|
| ----------------------------------------------------------------- |
104
|
|
|
*/ |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set the name attribute. |
108
|
|
|
* |
109
|
|
|
* @param string $name |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function setNameAttribute($name) |
114
|
|
|
{ |
115
|
|
|
return $this->attributes['name'] = $name; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the slug attribute. |
120
|
|
|
* |
121
|
|
|
* @param string $name |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function setSlugAttribute($name) |
126
|
|
|
{ |
127
|
|
|
return $this->attributes['slug'] = Str::slug($name); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the translatable attributes. |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function getTranslatableAttributes() |
136
|
|
|
{ |
137
|
|
|
return Blog::instance()->isTranslatable() ? ['name', 'slug'] : []; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/* ----------------------------------------------------------------- |
141
|
|
|
| Main Methods |
142
|
|
|
| ----------------------------------------------------------------- |
143
|
|
|
*/ |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Create a new tag. |
147
|
|
|
* |
148
|
|
|
* @param array $attributes |
149
|
|
|
* |
150
|
|
|
* @return self |
151
|
|
|
*/ |
152
|
|
|
public static function createOne(array $attributes) |
153
|
|
|
{ |
154
|
|
|
return tap(new self, function (self $tag) use ($attributes) { |
155
|
|
|
$tag->populate($attributes)->save(); |
156
|
|
|
}); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Update the current tag. |
161
|
|
|
* |
162
|
|
|
* @param array $attributes |
163
|
|
|
* |
164
|
|
|
* @return self |
165
|
|
|
*/ |
166
|
|
|
public function updateOne(array $attributes) |
167
|
|
|
{ |
168
|
|
|
$this->populate($attributes)->save(); |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the categories options for select input. |
175
|
|
|
* |
176
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
177
|
|
|
*/ |
178
|
|
|
public static function getSelectOptions() |
179
|
|
|
{ |
180
|
|
|
return cache()->remember(self::SELECT_CACHE_NAME, 5, function () { |
181
|
|
|
return self::all()->keyBy('id')->transform(function (Tag $tag) { |
182
|
|
|
return Blog::instance()->isTranslatable() |
183
|
|
|
? implode(' / ', $tag->getTranslations('name')) |
184
|
|
|
: $tag->name; |
185
|
|
|
}); |
186
|
|
|
}); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/* ----------------------------------------------------------------- |
190
|
|
|
| Check Methods |
191
|
|
|
| ----------------------------------------------------------------- |
192
|
|
|
*/ |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Check if tag has posts. |
196
|
|
|
* |
197
|
|
|
* @return bool |
198
|
|
|
*/ |
199
|
|
|
public function hasPosts() |
200
|
|
|
{ |
201
|
|
|
return ! $this->posts->isEmpty(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/* ----------------------------------------------------------------- |
205
|
|
|
| Other Methods |
206
|
|
|
| ----------------------------------------------------------------- |
207
|
|
|
*/ |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Fill the model with an array of attributes. |
211
|
|
|
* |
212
|
|
|
* @param array $attributes |
213
|
|
|
* |
214
|
|
|
* @return self |
215
|
|
|
*/ |
216
|
|
|
protected function populate(array $attributes) |
217
|
|
|
{ |
218
|
|
|
if ( ! Blog::instance()->isTranslatable()) |
219
|
|
|
return $this->fill($attributes); |
220
|
|
|
|
221
|
|
|
$this->setTranslations('name', $attributes['name']) |
222
|
|
|
->setTranslations('slug', $attributes['name']); |
223
|
|
|
|
224
|
|
|
return $this->fill(Arr::except($attributes, ['name', 'slug'])); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|