|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Models\Behaviors; |
|
4
|
|
|
|
|
5
|
|
|
use Astrotomic\Translatable\Translatable; |
|
6
|
|
|
use Illuminate\Database\Query\JoinClause; |
|
7
|
|
|
|
|
8
|
|
|
trait HasTranslation |
|
9
|
|
|
{ |
|
10
|
|
|
use Translatable; |
|
11
|
|
|
|
|
12
|
19 |
|
public function getTranslationModelNameDefault() |
|
13
|
|
|
{ |
|
14
|
19 |
|
return config('twill.namespace') . "\Models\Translations\\" . class_basename($this) . 'Translation'; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function scopeWithActiveTranslations($query, $locale = null) |
|
18
|
|
|
{ |
|
19
|
|
|
if (method_exists($query->getModel(), 'translations')) { |
|
20
|
|
|
$locale = $locale == null ? app()->getLocale() : $locale; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
$query->whereHas('translations', function ($query) use ($locale) { |
|
23
|
|
|
$query->whereActive(true); |
|
24
|
|
|
$query->whereLocale($locale); |
|
25
|
|
|
|
|
26
|
|
|
if (config('translatable.use_property_fallback', false)) { |
|
27
|
|
|
$query->orWhere('locale', config('translatable.fallback_locale')); |
|
28
|
|
|
} |
|
29
|
|
|
}); |
|
30
|
|
|
|
|
31
|
|
|
return $query->with(['translations' => function ($query) use ($locale) { |
|
32
|
|
|
$query->whereActive(true); |
|
33
|
|
|
$query->whereLocale($locale); |
|
34
|
|
|
|
|
35
|
|
|
if (config('translatable.use_property_fallback', false)) { |
|
36
|
|
|
$query->orWhere('locale', config('translatable.fallback_locale')); |
|
37
|
|
|
} |
|
38
|
|
|
}]); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function scopeOrderByTranslation($query, $orderField, $orderType = 'ASC', $locale = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$translationTable = $this->getTranslationsTable(); |
|
45
|
|
|
$localeKey = $this->getLocaleKey(); |
|
46
|
|
|
$table = $this->getTable(); |
|
|
|
|
|
|
47
|
|
|
$keyName = $this->getKeyName(); |
|
|
|
|
|
|
48
|
|
|
$locale = $locale == null ? app()->getLocale() : $locale; |
|
49
|
|
|
|
|
50
|
|
|
return $query |
|
51
|
|
|
->join($translationTable, function (JoinClause $join) use ($translationTable, $localeKey, $table, $keyName) { |
|
52
|
|
|
$join |
|
53
|
|
|
->on($translationTable.'.'.$this->getRelationKey(), '=', $table.'.'.$keyName) |
|
|
|
|
|
|
54
|
|
|
->where($translationTable.'.'.$localeKey, $this->locale()); |
|
55
|
|
|
}) |
|
56
|
|
|
->where($translationTable.'.'.$this->getLocaleKey(), $locale) |
|
57
|
|
|
->orderBy($translationTable.'.'.$orderField, $orderType) |
|
58
|
|
|
->select($table.'.*') |
|
59
|
|
|
->with('translations'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function scopeOrderByRawByTranslation($query, $orderRawString, $groupByField, $locale = null) |
|
63
|
|
|
{ |
|
64
|
|
|
$translationTable = $this->getTranslationsTable(); |
|
65
|
|
|
$table = $this->getTable(); |
|
66
|
|
|
$locale = $locale == null ? app()->getLocale() : $locale; |
|
67
|
|
|
|
|
68
|
|
|
return $query->join("{$translationTable} as t", "t.{$this->getRelationKey()}", "=", "{$table}.id") |
|
|
|
|
|
|
69
|
|
|
->where($this->getLocaleKey(), $locale) |
|
70
|
|
|
->groupBy("{$table}.id") |
|
71
|
|
|
->groupBy("t.{$groupByField}") |
|
72
|
|
|
->select("{$table}.*") |
|
73
|
|
|
->orderByRaw($orderRawString) |
|
74
|
|
|
->with('translations'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function hasActiveTranslation($locale = null) |
|
78
|
|
|
{ |
|
79
|
|
|
$locale = $locale ?: $this->locale(); |
|
80
|
|
|
|
|
81
|
|
|
$translations = $this->memoizedTranslations ?? ($this->memoizedTranslations = $this->translations()->get()); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
foreach ($translations as $translation) { |
|
84
|
|
|
if ($translation->getAttribute($this->getLocaleKey()) == $locale && $translation->getAttribute('active')) { |
|
85
|
|
|
return true; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
4 |
|
public function getActiveLanguages() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->translations->map(function ($translation) { |
|
95
|
|
|
return [ |
|
96
|
4 |
|
'shortlabel' => strtoupper($translation->locale), |
|
97
|
4 |
|
'label' => getLanguageLabelFromLocaleCode($translation->locale), |
|
98
|
4 |
|
'value' => $translation->locale, |
|
99
|
4 |
|
'published' => $translation->active ?? false, |
|
100
|
|
|
]; |
|
101
|
|
|
})->sortBy(function ($translation) { |
|
102
|
4 |
|
$localesOrdered = config('translatable.locales'); |
|
103
|
4 |
|
return array_search($translation['value'], $localesOrdered); |
|
104
|
4 |
|
})->values(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function translatedAttribute($key) |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->translations->mapWithKeys(function ($translation) use ($key) { |
|
110
|
|
|
return [$translation->locale => $this->translate($translation->locale)->$key]; |
|
111
|
|
|
}); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|