-
Notifications
You must be signed in to change notification settings - Fork 554
/
Block.php
146 lines (120 loc) · 3.66 KB
/
Block.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
<?php
namespace A17\Twill\Models;
use A17\Twill\Facades\TwillUtil;
use A17\Twill\Models\Behaviors\HasFiles;
use A17\Twill\Models\Behaviors\HasMedias;
use A17\Twill\Models\Behaviors\HasPresenter;
use A17\Twill\Models\Behaviors\HasRelated;
use A17\Twill\Models\Contracts\TwillModelContract;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
class Block extends BaseModel implements TwillModelContract
{
use HasMedias;
use HasFiles;
use HasPresenter;
use HasRelated;
public $timestamps = false;
protected $fillable = [
'blockable_id',
'blockable_type',
'position',
'content',
'type',
'child_key',
'parent_id',
'editor_name',
];
protected $casts = [
'content' => 'array',
];
protected $with = ['medias', 'children'];
public function scopeEditor($query, $name = 'default')
{
return $name === 'default' ?
$query->where('editor_name', $name)->orWhereNull('editor_name') :
$query->where('editor_name', $name);
}
public function blockable(): MorphTo
{
return $this->morphTo();
}
public function children(): HasMany
{
return $this->hasMany(twillModel('block'), 'parent_id')
->orderBy(
$this->getTable() . '.position',
'asc'
);
}
public function wysiwyg(string $name): string
{
return TwillUtil::parseInternalLinks($this->input($name) ?? '');
}
public function translatedWysiwyg(string $name): string
{
return TwillUtil::parseInternalLinks($this->translatedInput($name) ?? '');
}
public function input(string $name): mixed
{
return $this->content[$name] ?? null;
}
public function translatedInput(string $name, string $forceLocale = null): mixed
{
$value = $this->content[$name] ?? null;
$locale = $forceLocale ?? (
config('translatable.use_property_fallback', false) && (! array_key_exists(
app()->getLocale(),
array_filter($value ?? []) ?? []
))
? config('translatable.fallback_locale')
: app()->getLocale()
);
return $value[$locale] ?? null;
}
public function browserIds($name)
{
return isset($this->content['browsers']) ? ($this->content['browsers'][$name] ?? []) : [];
}
public function checkbox($name)
{
return isset($this->content[$name]) && ($this->content[$name][0] ?? $this->content[$name] ?? false);
}
public function getPresenterAttribute()
{
if ($presenter = config('twill.block_editor.block_presenter_path')) {
return $presenter;
}
return null;
}
public function getTable()
{
return config('twill.blocks_table', 'twill_blocks');
}
public function scopePublished(Builder $query): Builder
{
// @todo: These are not used yet by the block editor.
return $query;
}
public function scopeAccessible(Builder $query): Builder
{
// @todo: These are not used yet by the block editor.
return $query;
}
public function scopeOnlyTrashed(Builder $query): Builder
{
// @todo: These are not used yet by the block editor.
return $query;
}
public function scopeDraft(Builder $query): Builder
{
// @todo: These are not used yet by the block editor.
return $query;
}
public function getTranslatedAttributes(): array
{
return [];
}
}