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