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
|
|
|
$settingQuery = $this->model->when($section, function ($query) use ($section) { |
38
|
|
|
$query->where('section', $section); |
39
|
|
|
})->where('key', $key); |
40
|
|
|
|
41
|
|
|
if ($settingQuery->exists()) { |
42
|
|
|
return $settingQuery->with('translations')->first()->value; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string|null $section |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getFormFields($section = null) |
53
|
|
|
{ |
54
|
|
|
$settings = $this->model->when($section, function ($query) use ($section) { |
55
|
|
|
$query->where('section', $section); |
56
|
|
|
})->with('translations', 'medias')->get(); |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
if (config('twill.media_library.translated_form_fields', false)) { |
60
|
|
|
$medias = $settings->reduce(function ($carry, $setting) { |
61
|
|
|
foreach (getLocales() as $locale) { |
62
|
|
|
if (!empty(parent::getFormFields($setting)['medias'][$locale]) && !empty(parent::getFormFields($setting)['medias'][$locale][$setting->key])) |
63
|
|
|
{ |
64
|
|
|
$carry[$locale][$setting->key] = parent::getFormFields($setting)['medias'][$locale][$setting->key]; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
return $carry; |
68
|
|
|
}); |
69
|
|
|
} else { |
70
|
|
|
$medias = $settings->mapWithKeys(function ($setting) { |
71
|
|
|
return [$setting->key => parent::getFormFields($setting)['medias'][$setting->key] ?? null]; |
72
|
|
|
})->filter()->toArray(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $settings->mapWithKeys(function ($setting) { |
76
|
|
|
$settingValue = []; |
77
|
|
|
|
78
|
|
|
foreach ($setting->translations as $translation) { |
79
|
|
|
$settingValue[$translation->locale] = $translation->value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return [$setting->key => count(getLocales()) > 1 ? $settingValue : $setting->value]; |
83
|
|
|
})->toArray() + ['medias' => $medias]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array $settingsFields |
88
|
|
|
* @param string|null $section |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function saveAll($settingsFields, $section = null) |
92
|
|
|
{ |
93
|
|
|
$section = $section ? ['section' => $section] : []; |
94
|
|
|
|
95
|
|
|
foreach (Collection::make($settingsFields)->except('active_languages', 'medias', 'mediaMeta', 'update') as $key => $value) { |
96
|
|
|
foreach (getLocales() as $locale) { |
97
|
|
|
Arr::set( |
98
|
|
|
$settingsTranslated, |
99
|
|
|
$key . '.' . $locale, |
100
|
|
|
[ |
101
|
|
|
'value' => is_array($value) |
102
|
|
|
? (array_key_exists($locale, $value) ? $value[$locale] : $value) |
103
|
|
|
: $value, |
104
|
|
|
] + ['active' => true] |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (isset($settingsTranslated) && !empty($settingsTranslated)) { |
110
|
|
|
$settings = []; |
111
|
|
|
|
112
|
|
|
foreach ($settingsTranslated as $key => $values) { |
113
|
|
|
Arr::set($settings, $key, ['key' => $key] + $section + $values); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
foreach ($settings as $key => $setting) { |
117
|
|
|
$this->model->updateOrCreate(['key' => $key] + $section, $setting); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
foreach ($settingsFields['medias'] ?? [] as $role => $mediasList) { |
122
|
|
|
$medias = []; |
123
|
|
|
|
124
|
|
|
if (config('twill.media_library.translated_form_fields', false)) { |
125
|
|
|
foreach (getLocales() as $locale) { |
126
|
|
|
$medias["{$role}[{$locale}]"] = Collection::make($settingsFields['medias'][$role][$locale])->map(function ($media) { |
127
|
|
|
return json_decode($media, true); |
128
|
|
|
})->filter()->toArray(); |
129
|
|
|
} |
130
|
|
|
} else { |
131
|
|
|
$medias = [ |
132
|
|
|
$role => Collection::make($settingsFields['medias'][$role])->map(function ($media) { |
133
|
|
|
return json_decode($media, true); |
134
|
|
|
})->values()->filter()->toArray(), |
135
|
|
|
]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
$this->updateOrCreate($section + ['key' => $role], $section + [ |
140
|
|
|
'key' => $role, |
141
|
|
|
'medias' => $medias, |
142
|
|
|
]); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $role |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
public function getCrops($role) |
151
|
|
|
{ |
152
|
|
|
return $this->config->get('twill.settings.crops')[$role]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|