|
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\Database\Eloquent\Collection; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Laravel\Nova\Fields\Text; |
|
11
|
|
|
use Laravel\Nova\Http\Requests\NovaRequest; |
|
12
|
|
|
use Laravel\Nova\Http\Requests\ResourceIndexRequest; |
|
13
|
|
|
use Laravel\Nova\Panel; |
|
14
|
|
|
use Laravel\Nova\Resource; |
|
15
|
|
|
|
|
16
|
|
|
abstract class TranslatableResource extends Resource |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Return tabs of translations for current resource. |
|
20
|
|
|
* |
|
21
|
|
|
* @param \Illuminate\Http\Request $request |
|
22
|
|
|
* @param array $fields |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function translations(Request $request, array $fields) |
|
26
|
|
|
{ |
|
27
|
|
|
if ($request instanceof ResourceIndexRequest) { |
|
|
|
|
|
|
28
|
|
|
return $fields; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$locales = Locale::query()->select('id', 'iso', 'label')->get(); |
|
|
|
|
|
|
32
|
|
|
$translations = $this->getResourceTranslations($locales); |
|
33
|
|
|
|
|
34
|
|
|
$tabs = []; |
|
35
|
|
|
foreach ($locales as $locale) { |
|
36
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Locale $locale */ |
|
37
|
|
|
$localeResource = $translations->where('locale_id', '=', $locale->id)->first(); |
|
38
|
|
|
if (! empty($localeResource)) { |
|
39
|
|
|
$this->resource = clone $localeResource; |
|
40
|
|
|
$tabs[] = new Panel($locale->iso, (new \ArrayObject($fields))->getArrayCopy()); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return [ |
|
45
|
|
|
new Tabs('Translations', $tabs), |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Return resource translations. |
|
51
|
|
|
* |
|
52
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $locales |
|
53
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getResourceTranslations(Collection $locales) |
|
56
|
|
|
{ |
|
57
|
|
|
$baseTranslation = Translation::query() |
|
|
|
|
|
|
58
|
|
|
->select('translation_id') |
|
59
|
|
|
->where('translatable_id', '=', $this->resource->getKey()) |
|
60
|
|
|
->where('translatable_type', '=', get_class($this->resource)) |
|
61
|
|
|
->first(); |
|
62
|
|
|
|
|
63
|
|
|
if (empty($baseTranslation)) { |
|
64
|
|
|
$translationId = $this->resource->freshTranslationId(); |
|
|
|
|
|
|
65
|
|
|
$translations = new Collection(); |
|
66
|
|
|
foreach ($locales as $locale) { |
|
67
|
|
|
// @TODO... Instantiate new model (maybe with nonTranslatable() already filled in + "locale_id" and "translation_id") |
|
68
|
|
|
} |
|
69
|
|
|
} else { |
|
70
|
|
|
$translations = $this->resource->translations(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $translations; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function indexQuery(NovaRequest $request, $query) |
|
80
|
|
|
{ |
|
81
|
|
|
return $query->locale(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function scoutQuery(NovaRequest $request, $query) |
|
88
|
|
|
{ |
|
89
|
|
|
return $query; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function detailQuery(NovaRequest $request, $query) |
|
96
|
|
|
{ |
|
97
|
|
|
return parent::detailQuery($request, $query); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function relatableQuery(NovaRequest $request, $query) |
|
104
|
|
|
{ |
|
105
|
|
|
return parent::relatableQuery($request, $query); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
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: