|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use A17\Twill\Models\Setting; |
|
6
|
|
|
use A17\Twill\Repositories\Behaviors\HandleMedias; |
|
7
|
|
|
use Illuminate\Config\Repository as Config; |
|
8
|
|
|
use Illuminate\Support\Arr; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
|
|
11
|
|
|
class SettingRepository extends ModuleRepository |
|
12
|
|
|
{ |
|
13
|
|
|
use HandleMedias; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var Config |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $config; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param Setting $model |
|
22
|
|
|
* @param Config $config |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(Setting $model, Config $config) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->model = $model; |
|
27
|
|
|
$this->config = $config; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $key |
|
32
|
|
|
* @param string|null $section |
|
33
|
|
|
* @return string|null |
|
34
|
|
|
*/ |
|
35
|
|
|
public function byKey($key, $section = null) |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->model->when($section, function ($query) use ($section) { |
|
38
|
|
|
$query->where('section', $section); |
|
39
|
|
|
})->where('key', $key)->exists() ? $this->model->where('key', $key)->with('translations')->first()->value : null; |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string|null $section |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getFormFields($section = null) |
|
47
|
|
|
{ |
|
48
|
|
|
$settings = $this->model->when($section, function ($query) use ($section) { |
|
49
|
|
|
$query->where('section', $section); |
|
50
|
|
|
})->with('translations', 'medias')->get(); |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
if (config('twill.media_library.translated_form_fields', false)) { |
|
54
|
|
|
$medias = $settings->reduce(function ($carry, $setting) { |
|
55
|
|
|
foreach (getLocales() as $locale) { |
|
56
|
|
|
if (!empty(parent::getFormFields($setting)['medias'][$locale]) && !empty(parent::getFormFields($setting)['medias'][$locale][$setting->key])) |
|
57
|
|
|
{ |
|
58
|
|
|
$carry[$locale][$setting->key] = parent::getFormFields($setting)['medias'][$locale][$setting->key]; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
return $carry; |
|
62
|
|
|
}); |
|
63
|
|
|
} else { |
|
64
|
|
|
$medias = $settings->mapWithKeys(function ($setting) { |
|
65
|
|
|
return [$setting->key => parent::getFormFields($setting)['medias'][$setting->key] ?? null]; |
|
66
|
|
|
})->filter()->toArray(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $settings->mapWithKeys(function ($setting) { |
|
70
|
|
|
$settingValue = []; |
|
71
|
|
|
|
|
72
|
|
|
foreach ($setting->translations as $translation) { |
|
73
|
|
|
$settingValue[$translation->locale] = $translation->value; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return [$setting->key => count(getLocales()) > 1 ? $settingValue : $setting->value]; |
|
77
|
|
|
})->toArray() + ['medias' => $medias]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param array $settingsFields |
|
82
|
|
|
* @param string|null $section |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
|
|
public function saveAll($settingsFields, $section = null) |
|
86
|
|
|
{ |
|
87
|
|
|
$section = $section ? ['section' => $section] : []; |
|
88
|
|
|
|
|
89
|
|
|
foreach (Collection::make($settingsFields)->except('active_languages', 'medias', 'mediaMeta', 'update') as $key => $value) { |
|
90
|
|
|
foreach (getLocales() as $locale) { |
|
91
|
|
|
Arr::set( |
|
92
|
|
|
$settingsTranslated, |
|
93
|
|
|
$key . '.' . $locale, |
|
94
|
|
|
[ |
|
95
|
|
|
'value' => is_array($value) |
|
96
|
|
|
? (array_key_exists($locale, $value) ? $value[$locale] : $value) |
|
97
|
|
|
: $value, |
|
98
|
|
|
] + ['active' => true] |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (isset($settingsTranslated) && !empty($settingsTranslated)) { |
|
104
|
|
|
$settings = []; |
|
105
|
|
|
|
|
106
|
|
|
foreach ($settingsTranslated as $key => $values) { |
|
107
|
|
|
Arr::set($settings, $key, ['key' => $key] + $section + $values); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
foreach ($settings as $key => $setting) { |
|
111
|
|
|
$this->model->updateOrCreate(['key' => $key] + $section, $setting); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
foreach ($settingsFields['medias'] ?? [] as $role => $mediasList) { |
|
116
|
|
|
$medias = []; |
|
117
|
|
|
|
|
118
|
|
|
if (config('twill.media_library.translated_form_fields', false)) { |
|
119
|
|
|
foreach (getLocales() as $locale) { |
|
120
|
|
|
$medias["{$role}[{$locale}]"] = Collection::make($settingsFields['medias'][$role][$locale])->map(function ($media) { |
|
121
|
|
|
return json_decode($media, true); |
|
122
|
|
|
})->filter()->toArray(); |
|
123
|
|
|
} |
|
124
|
|
|
} else { |
|
125
|
|
|
$medias = [ |
|
126
|
|
|
$role => Collection::make($settingsFields['medias'][$role])->mapWithKeys(function ($media, $key) { |
|
|
|
|
|
|
127
|
|
|
return json_decode($media, true); |
|
128
|
|
|
})->values()->filter()->toArray(), |
|
129
|
|
|
]; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
$this->updateOrCreate($section + ['key' => $role], $section + [ |
|
134
|
|
|
'key' => $role, |
|
135
|
|
|
'medias' => $medias, |
|
136
|
|
|
]); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $role |
|
142
|
|
|
* @return array |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getCrops($role) |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->config->get('twill.settings.crops')[$role]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|