|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Models\Behaviors; |
|
4
|
|
|
|
|
5
|
|
|
use A17\Twill\Models\Behaviors\HasPresenter; |
|
6
|
|
|
use A17\Twill\Services\Capsules\HasCapsules; |
|
7
|
|
|
use A17\Twill\Models\Behaviors\IsTranslatable; |
|
8
|
|
|
use A17\Twill\Models\Tag; |
|
9
|
|
|
use Carbon\Carbon; |
|
10
|
|
|
use Cartalyst\Tags\TaggableTrait; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
|
13
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
14
|
|
|
use Illuminate\Support\Str; |
|
15
|
|
|
|
|
16
|
|
|
trait TwillModel |
|
17
|
|
|
{ |
|
18
|
|
|
use SoftDeletes; |
|
19
|
|
|
use HasPresenter; |
|
20
|
|
|
use SoftDeletes; |
|
21
|
|
|
use TaggableTrait; |
|
22
|
|
|
use IsTranslatable; |
|
23
|
|
|
use HasCapsules; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return bool |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function isTranslationModel(): bool |
|
29
|
|
|
{ |
|
30
|
|
|
return Str::endsWith(get_class($this), 'Translation'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param Builder $query |
|
35
|
|
|
* |
|
36
|
|
|
* @return Builder |
|
37
|
|
|
*/ |
|
38
|
|
|
public function scopePublished(Builder $query): Builder |
|
39
|
|
|
{ |
|
40
|
|
|
return $query->where("{$this->getTable()}.published", true); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Builder $query |
|
45
|
|
|
* |
|
46
|
|
|
* @return Builder |
|
47
|
|
|
*/ |
|
48
|
|
|
public function scopePublishedInListings(Builder $query): Builder |
|
49
|
|
|
{ |
|
50
|
|
|
if ($this->isFillable('public')) { |
|
|
|
|
|
|
51
|
|
|
$query->where("{$this->getTable()}.public", true); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $query->published()->visible(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param Builder $query |
|
59
|
|
|
* |
|
60
|
|
|
* @return Builder |
|
61
|
|
|
*/ |
|
62
|
|
|
public function scopeVisible(Builder $query): Builder |
|
63
|
|
|
{ |
|
64
|
|
|
if ($this->isFillable('publish_start_date')) { |
|
65
|
|
|
$query->where(function ($query) { |
|
66
|
|
|
$query->whereNull("{$this->getTable()}.publish_start_date")->orWhere("{$this->getTable()}.publish_start_date", '<=', Carbon::now()); |
|
67
|
|
|
}); |
|
68
|
|
|
|
|
69
|
|
|
if ($this->isFillable('publish_end_date')) { |
|
70
|
|
|
$query->where(function ($query) { |
|
71
|
|
|
$query->whereNull("{$this->getTable()}.publish_end_date")->orWhere("{$this->getTable()}.publish_end_date", '>=', Carbon::now()); |
|
72
|
|
|
}); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $query; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param Carbon $value |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setPublishStartDateAttribute($value): void |
|
85
|
|
|
{ |
|
86
|
|
|
$this->attributes['publish_start_date'] = $value ?? Carbon::now(); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param Builder $query |
|
91
|
|
|
* |
|
92
|
|
|
* @return Builder |
|
93
|
|
|
*/ |
|
94
|
|
|
public function scopeDraft(Builder $query): Builder |
|
95
|
|
|
{ |
|
96
|
|
|
return $query->where("{$this->getTable()}.published", false); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param Builder $query |
|
101
|
|
|
* |
|
102
|
|
|
* @return Builder |
|
103
|
|
|
*/ |
|
104
|
|
|
public function scopeOnlyTrashed(Builder $query): Builder |
|
105
|
|
|
{ |
|
106
|
|
|
return $query->whereNotNull("{$this->getTable()}.deleted_at"); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getFillable(): array |
|
113
|
|
|
{ |
|
114
|
|
|
// If the fillable attribute is filled, just use it |
|
115
|
|
|
$fillable = $this->fillable; |
|
116
|
|
|
|
|
117
|
|
|
// If fillable is empty |
|
118
|
|
|
// and it's a translation model |
|
119
|
|
|
// and the baseModel was defined |
|
120
|
|
|
// Use the list of translatable attributes on our base model |
|
121
|
|
|
if (blank($fillable) && $this->isTranslationModel() && property_exists($this, 'baseModuleModel')) { |
|
122
|
|
|
$baseModelClass = $this->baseModuleModel; |
|
123
|
|
|
$fillable = (new $baseModelClass())->getTranslatedAttributes(); |
|
124
|
|
|
|
|
125
|
|
|
if (!collect($fillable)->contains('locale')) { |
|
126
|
|
|
$fillable[] = 'locale'; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (!collect($fillable)->contains('active')) { |
|
130
|
|
|
$fillable[] = 'active'; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $fillable; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getTranslatedAttributes(): array |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->translatedAttributes ?? []; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return void |
|
147
|
|
|
*/ |
|
148
|
|
|
protected static function bootTaggableTrait(): void |
|
149
|
|
|
{ |
|
150
|
|
|
static::$tagsModel = Tag::class; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @inheritDoc |
|
155
|
|
|
*/ |
|
156
|
|
|
public function tags(): MorphToMany |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->morphToMany( |
|
|
|
|
|
|
159
|
|
|
static::$tagsModel, |
|
160
|
|
|
'taggable', |
|
161
|
|
|
config('twill.tagged_table', 'tagged'), |
|
162
|
|
|
'taggable_id', |
|
163
|
|
|
'tag_id' |
|
164
|
|
|
); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|