-
Notifications
You must be signed in to change notification settings - Fork 554
/
HandleMedias.php
142 lines (121 loc) · 5.48 KB
/
HandleMedias.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
<?php
namespace A17\Twill\Repositories\Behaviors;
use A17\Twill\Models\Media;
trait HandleMedias
{
public function hydrateHandleMedias($object, $fields)
{
if ($this->shouldIgnoreFieldBeforeSave('medias')) {
return $object;
}
$mediasCollection = collect();
$mediasFromFields = $this->getMedias($fields);
$mediasFromFields->each(function ($media) use ($object, $mediasCollection) {
$newMedia = Media::withTrashed()->find(is_array($media['id']) ? array_first($media['id']) : $media['id']);
$pivot = $newMedia->newPivot($object, array_except($media, ['id']), 'mediables', true);
$newMedia->setRelation('pivot', $pivot);
$mediasCollection->push($newMedia);
});
$object->setRelation('medias', $mediasCollection);
return $object;
}
public function afterSaveHandleMedias($object, $fields)
{
if ($this->shouldIgnoreFieldBeforeSave('medias')) {
return;
}
$object->medias()->sync([]);
$this->getMedias($fields)->each(function ($media) use ($object) {
$object->medias()->attach($media['id'], array_except($media, ['id']));
});
}
private function getMedias($fields)
{
$medias = collect();
if (isset($fields['medias'])) {
foreach ($fields['medias'] as $role => $mediasForRole) {
if (in_array($role, array_keys($this->model->mediasParams ?? []))
|| in_array($role, array_keys(config('twill.block_editor.crops')))) {
collect($mediasForRole)->each(function ($media) use (&$medias, $role) {
$customMetadatas = $media['metadatas']['custom'] ?? [];
if (isset($media['crops'])) {
foreach ($media['crops'] as $cropName => $cropData) {
$medias->push([
'id' => $media['id'],
'crop' => $cropName,
'role' => $role,
'ratio' => $cropData['name'],
'crop_w' => $cropData['width'],
'crop_h' => $cropData['height'],
'crop_x' => $cropData['x'],
'crop_y' => $cropData['y'],
'metadatas' => json_encode($customMetadatas),
]);
}
} else {
foreach ($this->getCrops($role) as $cropName => $cropDefinitions) {
$medias->push([
'id' => $media['id'],
'crop' => $cropName,
'role' => $role,
'ratio' => array_first($cropDefinitions)['name'],
'crop_w' => null,
'crop_h' => null,
'crop_x' => null,
'crop_y' => null,
'metadatas' => json_encode($customMetadatas),
]);
}
}
});
}
}
}
return $medias;
}
public function getFormFieldsHandleMedias($object, $fields)
{
$fields['medias'] = null;
if ($object->has('medias')) {
foreach ($object->medias->groupBy('pivot.role') as $role => $mediasByRole) {
foreach ($mediasByRole->groupBy('id') as $id => $mediasById) {
$item = $mediasById->first();
$metadatas = json_decode($item->pivot->metadatas, true);
$caption = $metadatas['caption'] ?? '';
$altText = $metadatas['altText'] ?? '';
$video = $metadatas['video'] ?? '';
$itemForForm = $item->toCmsArray() + [
'metadatas' => [
'default' => [
'caption' => $item->caption,
'altText' => $item->alt_text,
'video' => '',
],
'custom' => [
'caption' => $caption, // TODO: add caption and alttext to mediables table
'altText' => $altText,
'video' => $video,
],
],
];
foreach ($mediasById->groupBy('pivot.crop') as $crop => $mediaByCrop) {
$media = $mediaByCrop->first();
$itemForForm['crops'][$crop] = [
'name' => $media->pivot->ratio,
'width' => $media->pivot->crop_w,
'height' => $media->pivot->crop_h,
'x' => $media->pivot->crop_x,
'y' => $media->pivot->crop_y,
];
}
$fields['medias'][$role][] = $itemForForm;
}
}
}
return $fields;
}
public function getCrops($role)
{
return $this->model->mediasParams[$role];
}
}