-
Notifications
You must be signed in to change notification settings - Fork 554
/
HandleFiles.php
128 lines (111 loc) · 3.98 KB
/
HandleFiles.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
<?php
namespace A17\Twill\Repositories\Behaviors;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Models\File;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
trait HandleFiles
{
/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
* @return \A17\Twill\Models\Model
*/
public function hydrateHandleFiles($object, $fields)
{
if ($this->shouldIgnoreFieldBeforeSave('files')) {
return $object;
}
$filesCollection = Collection::make();
$filesFromFields = $this->getFiles($fields);
$filesFromFields->each(function ($file) use ($object, $filesCollection) {
$newFile = File::withTrashed()->find($file['id']);
$pivot = $newFile->newPivot($object, Arr::except($file, ['id']), 'fileables', true);
$newFile->setRelation('pivot', $pivot);
$filesCollection->push($newFile);
});
$object->setRelation('files', $filesCollection);
return $object;
}
/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
* @return void
*/
public function afterSaveHandleFiles($object, $fields)
{
if ($this->shouldIgnoreFieldBeforeSave('files')) {
return;
}
$object->files()->sync([]);
$this->getFiles($fields)->each(function ($file) use ($object) {
$object->files()->attach($file['id'], Arr::except($file, ['id']));
});
}
/**
* @param array $fields
* @return \Illuminate\Support\Collection
*/
private function getFiles($fields)
{
$files = Collection::make();
if (isset($fields['medias'])) {
foreach ($fields['medias'] as $role => $filesForRole) {
if (Str::contains($role, ['[', ']'])) {
$start = strpos($role, '[') + 1;
$finish = strpos($role, ']', $start);
$locale = substr($role, $start, $finish - $start);
$role = strtok($role, '[');
}
$locale = $locale ?? config('app.locale');
if (
in_array($role, $this->model->filesParams ?? [])
|| in_array($role, config('twill.block_editor.files', []))
) {
Collection::make($filesForRole)->each(function ($file) use (&$files, $role, $locale) {
$files->push([
'id' => $file['id'],
'role' => $role,
'locale' => $locale,
]);
});
}
}
}
return $files;
}
/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
* @return array
*/
public function getFormFieldsHandleFiles($object, $fields)
{
$fields['files'] = null;
if ($object->has('files')) {
foreach ($object->files->groupBy('pivot.role') as $role => $filesByRole) {
foreach ($filesByRole->groupBy('pivot.locale') as $locale => $filesByLocale) {
$fields['files'][$locale][$role] = $filesByLocale->map(function ($file) {
return $file->toCmsArray();
});
}
}
}
return $fields;
}
public function afterDuplicateHandleFiles(TwillModelContract $object, TwillModelContract $newObject): void
{
$newObject->files()->sync(
$object->files->mapWithKeys(function ($file) use ($object) {
return [
$file->id => Collection::make($object->files()->getPivotColumns())->mapWithKeys(
function ($attribute) use ($file) {
return [$attribute => $file->pivot->$attribute];
}
)->toArray(),
];
})
);
}
}