1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Repositories\Behaviors; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
|
8
|
|
|
trait HandleTranslations |
9
|
|
|
{ |
10
|
|
|
protected $nullableFields = []; |
11
|
|
|
|
12
|
|
|
public function prepareFieldsBeforeCreateHandleTranslations($fields) |
13
|
|
|
{ |
14
|
|
|
return $this->prepareFieldsBeforeSaveHandleTranslations(null, $fields); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function prepareFieldsBeforeSaveHandleTranslations($object, $fields) |
18
|
|
|
{ |
19
|
|
|
if ($this->model->isTranslatable()) { |
20
|
|
|
$locales = getLocales(); |
21
|
|
|
$localesCount = count($locales); |
22
|
|
|
$attributes = Collection::make($this->model->translatedAttributes); |
23
|
|
|
|
24
|
|
|
$submittedLanguages = Collection::make($fields['languages'] ?? []); |
25
|
|
|
|
26
|
|
|
$atLeastOneLanguageIsPublished = $submittedLanguages->contains(function ($language) { |
27
|
|
|
return $language['published']; |
28
|
|
|
}); |
29
|
|
|
|
30
|
|
|
foreach ($locales as $index => $locale) { |
31
|
|
|
$submittedLanguage = Arr::first($submittedLanguages->filter(function ($lang) use ($locale) { |
32
|
|
|
return $lang['value'] == $locale; |
33
|
|
|
})); |
34
|
|
|
|
35
|
|
|
$shouldPublishFirstLanguage = ($index === 0 && !$atLeastOneLanguageIsPublished); |
36
|
|
|
|
37
|
|
|
$activeField = $shouldPublishFirstLanguage || (isset($submittedLanguage) ? $submittedLanguage['published'] : false); |
38
|
|
|
|
39
|
|
|
$fields[$locale] = [ |
40
|
|
|
'active' => $activeField, |
41
|
|
|
] + $attributes->mapWithKeys(function ($attribute) use (&$fields, $locale, $localesCount, $index) { |
42
|
|
|
$attributeValue = $fields[$attribute] ?? null; |
43
|
|
|
|
44
|
|
|
// if we are at the last locale, |
45
|
|
|
// let's unset this field as it is now managed by this trait |
46
|
|
|
if ($index + 1 === $localesCount) { |
47
|
|
|
unset($fields[$attribute]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return [ |
51
|
|
|
$attribute => ($attributeValue[$locale] ?? null), |
52
|
|
|
]; |
53
|
|
|
})->toArray(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
unset($fields['languages']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $fields; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getFormFieldsHandleTranslations($object, $fields) |
63
|
|
|
{ |
64
|
|
|
unset($fields['translations']); |
65
|
|
|
|
66
|
|
|
if ($object->translations != null && $object->translatedAttributes != null) { |
67
|
|
|
foreach ($object->translations as $translation) { |
68
|
|
|
foreach ($object->translatedAttributes as $attribute) { |
69
|
|
|
unset($fields[$attribute]); |
70
|
|
|
$fields['translations'][$attribute][$translation->locale] = $translation->{$attribute}; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $fields; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function filterHandleTranslations($query, &$scopes) |
79
|
|
|
{ |
80
|
|
|
if ($this->model->isTranslatable()) { |
81
|
|
|
$attributes = $this->model->translatedAttributes; |
82
|
|
|
$query->whereHas('translations', function ($q) use ($scopes, $attributes) { |
83
|
|
|
foreach ($attributes as $attribute) { |
84
|
|
|
if (isset($scopes[$attribute]) && is_string($scopes[$attribute])) { |
85
|
|
|
$q->where($attribute, 'like', '%' . $scopes[$attribute] . '%'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
}); |
89
|
|
|
|
90
|
|
|
foreach ($attributes as $attribute) { |
91
|
|
|
if (isset($scopes[$attribute])) { |
92
|
|
|
unset($scopes[$attribute]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function orderHandleTranslations($query, &$orders) |
99
|
|
|
{ |
100
|
|
|
if ($this->model->isTranslatable()) { |
101
|
|
|
$attributes = $this->model->translatedAttributes; |
102
|
|
|
$table = $this->model->getTable(); |
103
|
|
|
$tableTranslation = $this->model->translations()->getRelated()->getTable(); |
104
|
|
|
$foreignKeyMethod = method_exists($this->model->translations(), 'getQualifiedForeignKeyName') ? 'getQualifiedForeignKeyName' : 'getForeignKey'; |
105
|
|
|
$foreignKey = $this->model->translations()->$foreignKeyMethod(); |
106
|
|
|
|
107
|
|
|
$isOrdered = false; |
108
|
|
|
foreach ($attributes as $attribute) { |
109
|
|
|
if (isset($orders[$attribute])) { |
110
|
|
|
$query->orderBy($tableTranslation . '.' . $attribute, $orders[$attribute]); |
111
|
|
|
$isOrdered = true; |
112
|
|
|
unset($orders[$attribute]); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($isOrdered) { |
117
|
|
|
$query |
118
|
|
|
->join($tableTranslation, $foreignKey, '=', $table . '.id') |
119
|
|
|
->where($tableTranslation . '.locale', '=', $orders['locale'] ?? app()->getLocale()) |
|
|
|
|
120
|
|
|
->select($table . '.*') |
121
|
|
|
; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getPublishedScopesHandleTranslations() |
127
|
|
|
{ |
128
|
|
|
return ['withActiveTranslations']; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|