|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use BBSLab\NovaTranslation\Models\Locale; |
|
6
|
|
|
use BBSLab\NovaTranslation\Models\Translation as TranslationModel; |
|
7
|
|
|
use BBSLab\NovaTranslation\NovaTranslationServiceProvider; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
9
|
|
|
use Laravel\Nova\Fields\Field; |
|
10
|
|
|
|
|
11
|
|
|
class Translation extends Field |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The field's component. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
public $component = 'nova-translation-field'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(string $name = '', ?string $attribute = null, ?mixed $resolveCallback = null) |
|
24
|
|
|
{ |
|
25
|
|
|
$name = trans(NovaTranslationServiceProvider::PACKAGE_ID.'::lang.field'); |
|
26
|
|
|
$attribute = 'translation'; |
|
27
|
|
|
|
|
28
|
|
|
parent::__construct($name, $attribute, $resolveCallback); |
|
29
|
|
|
|
|
30
|
|
|
$this->withMeta([ |
|
31
|
|
|
'locales' => $this->locales(), |
|
32
|
|
|
]); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function resolve($resource, $attribute = null) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->withMeta([ |
|
41
|
|
|
'translations' => $this->translations($resource), |
|
42
|
|
|
]); |
|
43
|
|
|
|
|
44
|
|
|
return parent::resolve($resource, $attribute); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Return all indexed locales. |
|
49
|
|
|
* |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function locales() |
|
53
|
|
|
{ |
|
54
|
|
|
$locales = []; |
|
55
|
|
|
|
|
56
|
|
|
$query = Locale::query(); |
|
57
|
|
|
foreach ($query->cursor() as $locale) { |
|
58
|
|
|
$locales[$locale->id] = $locale->toArray(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $locales; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Return translations entries for given translatable model. |
|
66
|
|
|
* |
|
67
|
|
|
* @param \Illuminate\Database\Eloquent\Model $resource |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function translations(Model $resource) |
|
71
|
|
|
{ |
|
72
|
|
|
$translations = []; |
|
73
|
|
|
|
|
74
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Translation $resourceTranslation */ |
|
75
|
|
|
$resourceTranslation = $resource->translation; |
|
76
|
|
|
|
|
77
|
|
|
if (! empty($resourceTranslation)) { |
|
78
|
|
|
$query = TranslationModel::query() |
|
79
|
|
|
->where('translation_id', '=', $resourceTranslation->translation_id) |
|
80
|
|
|
->where('translatable_type', '=', $resourceTranslation->translatable_type); |
|
81
|
|
|
|
|
82
|
|
|
foreach ($query->cursor() as $translation) { |
|
83
|
|
|
$translations[$translation->locale_id] = $translation->toArray(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $translations; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|