-
Notifications
You must be signed in to change notification settings - Fork 560
/
Block.php
96 lines (77 loc) · 2.32 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
<?php
namespace A17\Twill\Models;
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 Illuminate\Database\Eloquent\Model as BaseModel;
class Block extends BaseModel
{
use HasMedias, HasFiles, HasPresenter, 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'];
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()
{
return $this->morphTo();
}
public function children()
{
return $this->hasMany('A17\Twill\Models\Block', 'parent_id')
->orderBy(
$this->getTable() . '.position',
'asc'
);
}
public function input($name)
{
return $this->content[$name] ?? null;
}
public function translatedInput($name, $forceLocale = null)
{
$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')) != null) {
return $presenter;
}
return null;
}
public function getTable()
{
return config('twill.blocks_table', 'twill_blocks');
}
}