|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\Resources; |
|
4
|
|
|
|
|
5
|
|
|
use BBSLab\NovaTranslation\Models\Locale; |
|
|
|
|
|
|
6
|
|
|
use BBSLab\NovaTranslation\Models\Translation; |
|
7
|
|
|
use Eminiarts\Tabs\Tabs; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Laravel\Nova\Fields\Text; |
|
10
|
|
|
use Laravel\Nova\Http\Requests\NovaRequest; |
|
11
|
|
|
use Laravel\Nova\Resource; |
|
12
|
|
|
|
|
13
|
|
|
abstract class TranslatableResource extends Resource |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Return tabs of translations for current resource. |
|
17
|
|
|
* |
|
18
|
|
|
* @param \Illuminate\Http\Request $request |
|
19
|
|
|
* @param array $fields |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected function translations(Request $request, array $fields) |
|
23
|
|
|
{ |
|
24
|
|
|
$locales = Locale::query()->select('id', 'iso', 'label')->get(); |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
$baseTranslation = Translation::query() |
|
|
|
|
|
|
27
|
|
|
->select('translation_id') |
|
28
|
|
|
->where('translatable_id', '=', $this->resource->getKey()) |
|
29
|
|
|
->where('translatable_type', '=', get_class($this->resource)) |
|
30
|
|
|
->first(); |
|
31
|
|
|
$translationId = ! empty($baseTranslation) ? $baseTranslation->translation_id : $this->resource->freshTranslationId(); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
$tabs = []; |
|
34
|
|
|
$test = [Text::make('key')]; |
|
|
|
|
|
|
35
|
|
|
foreach ($locales as $locale) { |
|
36
|
|
|
$tabs[$locale->label] = [Text::make('key_' . $locale->iso)]; |
|
37
|
|
|
/* |
|
38
|
|
|
$tabs[] = new Tabs($locale->label, array_merge([ |
|
39
|
|
|
// Text::make('Translation ID', 'translation_id', $translationId)->withMeta(['type' => 'hidden']), |
|
40
|
|
|
], $fields)); |
|
41
|
|
|
*/ |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return [ |
|
45
|
|
|
new Tabs('Translations', $tabs), |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function indexQuery(NovaRequest $request, $query) |
|
53
|
|
|
{ |
|
54
|
|
|
return $query->inLocale(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function scoutQuery(NovaRequest $request, $query) |
|
61
|
|
|
{ |
|
62
|
|
|
return $query; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function detailQuery(NovaRequest $request, $query) |
|
69
|
|
|
{ |
|
70
|
|
|
return parent::detailQuery($request, $query); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function relatableQuery(NovaRequest $request, $query) |
|
77
|
|
|
{ |
|
78
|
|
|
return parent::relatableQuery($request, $query); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: