-
Notifications
You must be signed in to change notification settings - Fork 588
Expand file tree
/
Copy pathHandleFiles.php
More file actions
131 lines (115 loc) · 4.21 KB
/
Copy pathHandleFiles.php
File metadata and controls
131 lines (115 loc) · 4.21 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
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
<?php
namespace A17\Twill\Repositories\Behaviors;
use A17\Twill\Facades\TwillUtil;
use A17\Twill\Models\Behaviors\HasFiles;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Models\File;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
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['file_id']);
$pivot = $newFile->newPivot($object, $file, 'fileables', true);
$newFile->setRelation('pivot', $pivot);
$filesCollection->push($newFile);
});
$object->setRelation('files', $filesCollection);
return $object;
}
/**
* @param \A17\Twill\Models\Model|HasFiles $object
* @param array $fields
* @return void
*/
public function afterSaveHandleFiles($object, $fields)
{
if ($this->shouldIgnoreFieldBeforeSave('files')) {
return;
}
TwillUtil::syncUsingPrimaryKey($object->files(), $this->getFiles($fields));
}
/**
* @param array $fields
* @return 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, $index) use (&$files, $role, $locale) {
$files[$file['pivot_id'] ?? uniqid('file')] = [
'file_id' => $file['id'],
'role' => $role,
'locale' => $locale,
'position' => $index + 1,
];
});
}
}
}
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 $file) {
return $file->toCmsArray() + ['pivot_id' => $file->pivot->id];
});
}
}
}
return $fields;
}
/**
* @param HasFiles|TwillModelContract $object
* @param HasFiles|TwillModelContract $newObject
*/
public function afterDuplicateHandleFiles(TwillModelContract $object, TwillModelContract $newObject): void
{
$newObject->files()->attach(
$object->files->mapWithKeys(function ($file) use ($object) {
return [
$file->id => Collection::make($object->files()->getPivotColumns())->mapWithKeys(
function ($attribute) use ($file) {
$value = $attribute == 'id' ? null : $file->pivot->$attribute;
return [$attribute => $value];
}
)->toArray(),
];
})
);
}
}