|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Back\Updaters; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Spatie\Regex\MatchResult; |
|
9
|
|
|
use Spatie\Regex\Regex; |
|
10
|
|
|
|
|
11
|
|
|
trait UpdateSeoValues |
|
12
|
|
|
{ |
|
13
|
|
|
protected function updateSeoValues(Model $model, FormRequest $request) |
|
14
|
|
|
{ |
|
15
|
|
|
collect($request->all()) |
|
16
|
|
|
->filter(function ($value, $fieldName) { |
|
17
|
|
|
// Filter out everything that starts with 'translated_<locale>_seo_' |
|
18
|
|
|
return Regex::match('/^translated_([a-z][a-z])_seo_/', $fieldName)->hasMatch(); |
|
19
|
|
|
}) |
|
20
|
|
|
->map(function ($value, $fieldName) { |
|
21
|
|
|
|
|
22
|
|
|
// Replace 'translated_<locale>_seo_<attribute>' with '<locale>_<attribute>' |
|
23
|
|
|
$localeAndAttribute = Regex::replace('/translated_([a-z][a-z])_seo_/', function (MatchResult $matchResult) { |
|
24
|
|
|
return $matchResult->group(1).'_'; |
|
25
|
|
|
}, $fieldName)->result(); |
|
26
|
|
|
|
|
27
|
|
|
$localeAndAttribute = explode('_', $localeAndAttribute, 2); |
|
28
|
|
|
|
|
29
|
|
|
return [ |
|
30
|
|
|
'locale' => $localeAndAttribute[0], |
|
31
|
|
|
'attribute' => $localeAndAttribute[1], |
|
32
|
|
|
'value' => $value, |
|
33
|
|
|
]; |
|
34
|
|
|
}) |
|
35
|
|
|
->groupBy('locale') |
|
36
|
|
|
->map(function (Collection $valuesInLocale) { |
|
37
|
|
|
return $valuesInLocale->mapToAssoc(function ($values) { |
|
38
|
|
|
return [$values['attribute'], $values['value']]; |
|
39
|
|
|
}); |
|
40
|
|
|
}) |
|
41
|
|
|
->each(function ($values, $locale) use ($model) { |
|
42
|
|
|
$model->setTranslation('seo_values', $locale, $values); |
|
43
|
|
|
}); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|