|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fomvasss\LaravelTranslatable\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
|
|
9
|
|
|
trait HasTranslations |
|
10
|
|
|
{ |
|
11
|
|
|
use HasLanguage; |
|
12
|
|
|
|
|
13
|
|
|
protected static function bootHasTranslations() |
|
14
|
|
|
{ |
|
15
|
|
|
static::saved(function ($model) { |
|
16
|
|
|
if ($model->{$model->getLangcodeColumn()} && empty($model->{$model->getTranslationUuidColumn()})) { |
|
17
|
|
|
$model->{$model->getTranslationUuidColumn()} = Str::uuid(); |
|
18
|
|
|
$model->save(); |
|
19
|
|
|
} |
|
20
|
|
|
}); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getTranslationUuidColumn(): string |
|
27
|
|
|
{ |
|
28
|
|
|
return config('translatable.db.columns.translation_uuid'); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
33
|
|
|
*/ |
|
34
|
|
|
public function translations(): HasMany |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->hasMany(static::class, $this->getTranslationUuidColumn(), $this->getTranslationUuidColumn()); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function saveTranslatable(string $langcode, ?string $uuid = null) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->setAttribute($this->getLangcodeColumn(), $langcode); |
|
|
|
|
|
|
42
|
|
|
if ($uuid) { |
|
43
|
|
|
$this->setAttribute($this->getTranslationUuidColumn(), $uuid); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $this->save(); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return Collection |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getTranslationList(): Collection |
|
53
|
|
|
{ |
|
54
|
|
|
$languages = config('translatable.languages', []); |
|
|
|
|
|
|
55
|
|
|
$translations = $this->translations; |
|
56
|
|
|
$result = new Collection(); |
|
57
|
|
|
|
|
58
|
|
|
foreach ($languages as $langcode => $language) { |
|
59
|
|
|
$model = $translations->where($this->getLangcodeColumn(), $langcode)->first(); |
|
60
|
|
|
$result->push([ |
|
61
|
|
|
'model_key' => optional($model)->{$this->getKeyName()}, |
|
|
|
|
|
|
62
|
|
|
'model_type' => Str::snake(class_basename(static::class)), |
|
63
|
|
|
'translation_uuid' => optional($model)->{$this->getTranslationUuidColumn()}, |
|
64
|
|
|
'langcode' => $langcode, |
|
65
|
|
|
'language' => $language, |
|
66
|
|
|
'status' => $this->getTranslationStatus($langcode, $model) |
|
67
|
|
|
]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $result; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $langcode |
|
75
|
|
|
* @param null $model |
|
|
|
|
|
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function getTranslationStatus(string $langcode, $model = null): string |
|
79
|
|
|
{ |
|
80
|
|
|
if (empty($model)) { |
|
81
|
|
|
return 'empty'; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($this->{$this->getLangcodeColumn()} === $langcode) { |
|
85
|
|
|
return 'current'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return 'translation'; |
|
89
|
|
|
} |
|
90
|
|
|
} |