forked from area17/twill-capsule-base
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleRepository.php
More file actions
120 lines (93 loc) · 3.19 KB
/
ModuleRepository.php
File metadata and controls
120 lines (93 loc) · 3.19 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
<?php
namespace App\Twill\Capsules\Base;
use A17\CDN\Behaviours\HasCDNTags;
use A17\Twill\Models\Behaviors\HasMedias;
use App\Twill\Capsules\Base\Behaviors\Finder;
use A17\TwillTransformers\RepositoryTrait;
use A17\Twill\Repositories\ModuleRepository as TwillModuleRepository;
abstract class ModuleRepository extends TwillModuleRepository
{
use RepositoryTrait, Finder, HasCDNTags;
/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
* @return void
*/
public function afterSave($object, $fields)
{
parent::afterSave($object, $fields);
$this->invalidateCDNTags($object);
}
protected function makeBrowserData($object, $module, $prefix = null): array
{
$titleColumnKey = repository($module)->getModel()->titleColumnKey;
return [
'id' => $object->id,
'name' => $object->$titleColumnKey,
'edit' => moduleRoute($module, $prefix, 'edit', $object->id),
] +
(classHasTrait($object, HasMedias::class)
? [
'thumbnail' => $object->defaultCmsImage([
'w' => 100,
'h' => 100,
]),
]
: []);
}
protected function getManyToManyBrowserField($model, array $fields, $relation, $prefix): array
{
if (blank($models = $model->{$relation})) {
return $fields;
}
$fields['browsers'][$relation] = $models
->map(fn($model) => $this->makeBrowserData($model, $relation, $prefix))
->toArray();
return $fields;
}
public function afterSaveUpdateManyToOneBrowser($page, $fields, $key)
{
$object = $fields['browsers'][$key][0] ?? null;
$page->{$key} = filled($object) ? $object['id'] : null;
$page->save();
}
protected function getManyToOneBrowserField($object, $fields, $key, $module, $relation, $prefix = null): array
{
if (filled($object = $object->{$relation})) {
$fields['browsers'][$key] = collect([$object])
->map(fn($country) => $this->makeBrowserData($object, $module, $prefix))
->toArray();
}
return $fields;
}
public function findByCode($code)
{
return $this->model->where('code', $code)->first();
}
public function updateHasManyBrowser($object, $fields, $formField, $field, $hasManyClass)
{
$items = $fields['browsers'][$formField] ?? null;
if (blank($items)) {
$object->$formField()->delete();
return;
}
foreach ($items as $position => $record) {
$hasMany = app($hasManyClass)->find($record['id']);
if (filled($hasMany)) {
$hasMany->$field = $object->id;
$hasMany->position = $position;
$hasMany->save();
}
}
foreach ($object->$formField as $city) {
if (
collect($items)
->where('id', $city->id)
->isEmpty()
) {
$city->$field = null;
$city->save();
}
}
}
}