-
Notifications
You must be signed in to change notification settings - Fork 554
/
Model.php
209 lines (175 loc) · 6.16 KB
/
Model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
namespace A17\Twill\Models;
use A17\Twill\Facades\TwillPermissions;
use A17\Twill\Models\Behaviors\HasPresenter;
use A17\Twill\Models\Behaviors\IsTranslatable;
use A17\Twill\Models\Contracts\TwillLinkableModel;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Models\Contracts\TwillSchedulableModel;
use Carbon\Carbon;
use Cartalyst\Tags\TaggableInterface;
use Cartalyst\Tags\TaggableTrait;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
abstract class Model extends BaseModel implements TaggableInterface, TwillModelContract, TwillSchedulableModel, TwillLinkableModel
{
use HasPresenter;
use SoftDeletes;
use TaggableTrait;
use IsTranslatable;
public $timestamps = true;
protected function isTranslationModel(): bool
{
return Str::endsWith(get_class($this), 'Translation');
}
public function scopePublished($query): Builder
{
return $query->where("{$this->getTable()}.published", true);
}
public function scopeAccessible($query): Builder
{
if (! TwillPermissions::enabled()) {
return $query;
}
$model = get_class($query->getModel());
$moduleName = TwillPermissions::getPermissionModule(getModuleNameByModel($model));
if ($moduleName && ! Auth::user()->isSuperAdmin()) {
// Get all permissions the logged in user has regards to the model.
$allPermissions = Auth::user()->allPermissions();
$allModelPermissions = (clone $allPermissions)->ofModel($model);
// If the user has any module permissions, or global manage all modules permissions, all items will be return
if (
(clone $allModelPermissions)->module()
->whereIn('name', Permission::available(Permission::SCOPE_MODULE))
->exists()
|| (clone $allPermissions)->global()->where('name', 'manage-modules')->exists()
) {
return $query;
}
// If the module is submodule, skip the scope.
if (strpos($moduleName, '.')) {
return $query;
}
$authorizedItemsIds = $allModelPermissions->moduleItem()->pluck('permissionable_id');
return $query->whereIn($this->getTable() . '.id', $authorizedItemsIds);
}
return $query;
}
public function scopePublishedInListings($query): Builder
{
// @todo: Remove? Seems unused.
if ($this->isFillable('public')) {
$query->where("{$this->getTable()}.public", true);
}
return $query->published()->visible();
}
/**
* @todo: Document
*/
public function scopeVisible($query): Builder
{
if ($this->isFillable('publish_start_date')) {
$query->where(function ($query) {
$query->whereNull("{$this->getTable()}.publish_start_date")->orWhere(
"{$this->getTable()}.publish_start_date",
'<=',
Carbon::now()
);
});
if ($this->isFillable('publish_end_date')) {
$query->where(function ($query) {
$query->whereNull("{$this->getTable()}.publish_end_date")->orWhere(
"{$this->getTable()}.publish_end_date",
'>=',
Carbon::now()
);
});
}
}
return $query;
}
public function setPublishStartDateAttribute($value): void
{
$this->attributes['publish_start_date'] = $value ?? Carbon::now();
}
public function scopeDraft($query): Builder
{
return $query->where("{$this->getTable()}.published", false);
}
public function scopeOnlyTrashed($query): Builder
{
return $query->onlyTrashed();
}
public function getFillable(): array
{
// If the fillable attribute is filled, just use it
$fillable = $this->fillable;
// If fillable is empty
// and it's a translation model
// and the baseModel was defined
// Use the list of translatable attributes on our base model
if (
blank($fillable) &&
$this->isTranslationModel() &&
property_exists($this, 'baseModuleModel')
) {
$fillable = (new $this->baseModuleModel())->getTranslatedAttributes();
if (! collect($fillable)->contains('locale')) {
$fillable[] = 'locale';
}
if (! collect($fillable)->contains('active')) {
$fillable[] = 'active';
}
}
return $fillable;
}
public function getTranslatedAttributes(): array
{
return $this->translatedAttributes
? [...$this->translatedAttributes, 'active']
: [];
}
protected static function bootTaggableTrait(): void
{
static::$tagsModel = Tag::class;
}
/**
* @inheritdoc
*/
public function tags(): MorphToMany
{
return $this->morphToMany(
static::$tagsModel,
'taggable',
config('twill.tagged_table', 'tagged'),
'taggable_id',
'tag_id'
);
}
public function getFullUrl(): string
{
if (! method_exists($this, 'getSlug')) {
return '#';
}
// @phpstan-ignore-next-line
if (method_exists($this, 'getUrlWithoutSlug') && $this->urlWithoutSlug) {
return rtrim($this->urlWithoutSlug, '/') . '/' . $this->getSlug();
}
try {
$controller = getModelController($this);
} catch (Exception) {
// Fallback to never crash on production.
return '#';
}
return Str::replace(
['/{preview}', '{language}'],
['', app()->getLocale()],
rtrim($controller->getPermalinkBaseUrl(), '/') . '/' . $this->getSlug()
);
}
}