|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use A17\Twill\Models\Behaviors\HasPresenter; |
|
7
|
|
|
use Carbon\Carbon; |
|
8
|
|
|
use Cartalyst\Tags\TaggableInterface; |
|
9
|
|
|
use Cartalyst\Tags\TaggableTrait; |
|
10
|
|
|
use A17\Twill\Models\Behaviors\IsTranslatable; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Model as BaseModel; |
|
12
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
13
|
|
|
|
|
14
|
|
|
abstract class Model extends BaseModel implements TaggableInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use HasPresenter, SoftDeletes, TaggableTrait, IsTranslatable; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
public $timestamps = true; |
|
19
|
|
|
|
|
20
|
5 |
|
public function scopePublished($query) |
|
21
|
|
|
{ |
|
22
|
5 |
|
return $query->wherePublished(true); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function scopePublishedInListings($query) |
|
26
|
|
|
{ |
|
27
|
|
|
if ($this->isFillable('public')) { |
|
28
|
|
|
$query->wherePublic(true); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return $query->published()->visible(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function scopeVisible($query) |
|
35
|
|
|
{ |
|
36
|
|
|
if ($this->isFillable('publish_start_date')) { |
|
37
|
|
|
$query->where(function ($query) { |
|
38
|
|
|
$query->whereNull('publish_start_date')->orWhere('publish_start_date', '<=', Carbon::now()); |
|
39
|
|
|
}); |
|
40
|
|
|
|
|
41
|
|
|
if ($this->isFillable('publish_end_date')) { |
|
42
|
|
|
$query->where(function ($query) { |
|
43
|
|
|
$query->whereNull('publish_end_date')->orWhere('publish_end_date', '>=', Carbon::now()); |
|
44
|
|
|
}); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $query; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
6 |
|
public function setPublishStartDateAttribute($value) |
|
52
|
|
|
{ |
|
53
|
6 |
|
$this->attributes['publish_start_date'] = $value ?? Carbon::now(); |
|
54
|
6 |
|
} |
|
55
|
|
|
|
|
56
|
5 |
|
public function scopeDraft($query) |
|
57
|
|
|
{ |
|
58
|
5 |
|
return $query->wherePublished(false); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function scopeOnlyTrashed($query) |
|
62
|
|
|
{ |
|
63
|
|
|
return $query->whereNotNull('deleted_at'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
47 |
|
public function getFillable() |
|
67
|
|
|
{ |
|
68
|
|
|
// If the fillable attribute is filled, just use it |
|
69
|
47 |
|
$fillable = $this->fillable; |
|
70
|
|
|
|
|
71
|
|
|
// If fillable is empty |
|
72
|
|
|
// and it's a translation model |
|
73
|
|
|
// and the baseModel was defined |
|
74
|
|
|
// Use the list of translatable attributes on our base model |
|
75
|
|
|
if ( |
|
76
|
47 |
|
blank($fillable) && |
|
77
|
47 |
|
Str::contains($class= get_class($this), 'Models\Translations') && |
|
78
|
47 |
|
property_exists($class, 'baseModuleModel') |
|
79
|
|
|
) { |
|
80
|
|
|
$fillable = (new $this->baseModuleModel)->getTranslatedAttributes(); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
if (!collect($fillable)->contains('locale')) { |
|
83
|
|
|
$fillable[] = 'locale'; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
47 |
|
return $fillable; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getTranslatedAttributes() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->translatedAttributes ?? []; |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|