-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathPostBlockList.php
More file actions
86 lines (77 loc) · 2.87 KB
/
PostBlockList.php
File metadata and controls
86 lines (77 loc) · 2.87 KB
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
<?php
namespace App\Sharp\Posts\Blocks;
use App\Models\Media;
use App\Models\PostBlock;
use App\Sharp\Entities\PostBlockTextEntity;
use App\Sharp\Entities\PostBlockVideoEntity;
use App\Sharp\Entities\PostBlockVisualsEntity;
use Code16\Sharp\EntityList\Eloquent\SimpleEloquentReorderHandler;
use Code16\Sharp\EntityList\Fields\EntityListField;
use Code16\Sharp\EntityList\Fields\EntityListFieldsContainer;
use Code16\Sharp\EntityList\Filters\HiddenFilter;
use Code16\Sharp\EntityList\SharpEntityList;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Str;
class PostBlockList extends SharpEntityList
{
protected function buildList(EntityListFieldsContainer $fields): void
{
$fields
->addField(
EntityListField::make('type_label')
->setWidth(.15)
->setLabel('Type')
)
->addField(
EntityListField::make('content')
);
}
public function buildListConfig(): void
{
$this->configureSubEntities('type', [
'text' => PostBlockTextEntity::class,
'video' => PostBlockVideoEntity::class,
'visuals' => PostBlockVisualsEntity::class,
])
->configureReorderable(new SimpleEloquentReorderHandler(PostBlock::class))
->configureQuickCreationForm();
}
protected function getFilters(): ?array
{
return [
HiddenFilter::make('post'),
];
}
public function getListData(): array|Arrayable
{
$postBlocks = PostBlock::orderBy('order')
->where('post_id', $this->queryParams->filterFor('post'))
->get();
return $this
->setCustomTransformer('type_label', fn ($value, PostBlock $instance) => sprintf(
'<span class="badge badge-bloc badge-bloc-%1$s">%1$s</span>',
$instance->type
))
->setCustomTransformer('content', fn ($value, PostBlock $instance) => match ($instance->type) {
'text' => Str::limit($instance->content, 150),
'video' => sprintf(
'<div style="display: flex; gap: .4rem; align-items: center">%s %s</div>',
svg('lucide-youtube')->toHtml(),
Str::match('/\ssrc="(.*)"/mU', $instance->content)
),
'visuals' => $instance->files
->map(function (Media $visual) {
if ($url = $visual->thumbnail(null, 30)) {
return sprintf('<img src="%s" alt="" style="display: inline-block">', $url);
}
return null;
})
->implode(' ')
})
->transform($postBlocks);
}
public function delete($id): void
{
PostBlock::findOrFail($id)->delete();
}
}