|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BBS\Nova\Translation\GraphQL\Queries\Translation; |
|
4
|
|
|
|
|
5
|
|
|
use BBS\Nova\Translation\Models\Label; |
|
6
|
|
|
use BBS\Nova\Translation\Models\Locale; |
|
7
|
|
|
use BBS\Nova\Translation\Models\Scopes\TranslatableScope; |
|
8
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
10
|
|
|
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; |
|
11
|
|
|
|
|
12
|
|
|
abstract class ByLocale |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Filter locales Eloquent Builder depending of given arguments. |
|
16
|
|
|
* |
|
17
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
18
|
|
|
* @param array $args |
|
19
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract protected function filterLocales(Builder $query, array $args); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Return a value for the field. |
|
25
|
|
|
* |
|
26
|
|
|
* @param mixed $rootValue |
|
27
|
|
|
* @param array $args |
|
28
|
|
|
* @param \Nuwave\Lighthouse\Support\Contracts\GraphQLContext $context |
|
29
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $resolveInfo |
|
30
|
|
|
* @return array |
|
31
|
|
|
*/ |
|
32
|
|
|
public function resolve($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
$query = Locale::query()->availableInApi()->select('id', 'iso'); |
|
35
|
|
|
$query = $this->filterLocales($query, $args); |
|
36
|
|
|
$locales = $query->get()->pluck('iso', 'id')->toArray(); |
|
37
|
|
|
|
|
38
|
|
|
$labels = Label::query() |
|
|
|
|
|
|
39
|
|
|
->withoutGlobalScope(TranslatableScope::class) |
|
40
|
|
|
->select('labels.key', 'labels.value', 'translations.locale_id') |
|
41
|
|
|
->join('translations', 'labels.id', '=', 'translations.translatable_id') |
|
42
|
|
|
->where('translations.translatable_type', '=', Label::class) |
|
43
|
|
|
->whereIn('translations.locale_id', array_keys($locales)) |
|
44
|
|
|
->get(); |
|
45
|
|
|
|
|
46
|
|
|
$json = []; |
|
47
|
|
|
foreach ($labels as $label) { |
|
48
|
|
|
$iso = $locales[$label->locale_id]; |
|
49
|
|
|
if (! isset($json[$iso])) { |
|
50
|
|
|
$json[$iso] = []; |
|
51
|
|
|
} |
|
52
|
|
|
$json[$iso][$label->key] = $label->value; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return [ |
|
56
|
|
|
'json' => $json, |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.