1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\Jobs; |
4
|
|
|
|
5
|
|
|
use BBSLab\NovaTranslation\Models\Locale; |
6
|
|
|
use BBSLab\NovaTranslation\Models\Translation; |
7
|
|
|
use BBSLab\NovaTranslation\NovaTranslation; |
8
|
|
|
use Illuminate\Bus\Queueable; |
9
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
10
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
11
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
12
|
|
|
use Illuminate\Queue\SerializesModels; |
13
|
|
|
|
14
|
|
|
class LocaleCreated implements ShouldQueue |
15
|
|
|
{ |
16
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
17
|
|
|
|
18
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Locale */ |
19
|
|
|
protected $locale; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Create a new job instance. |
23
|
|
|
* |
24
|
|
|
* @param \BBSLab\NovaTranslation\Models\Locale $locale |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Locale $locale) |
27
|
|
|
{ |
28
|
|
|
$this->locale = $locale; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Execute the job. |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function handle() |
37
|
|
|
{ |
38
|
|
|
if (empty($models = NovaTranslation::translatableModels())) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
Translation::query() |
43
|
|
|
->selectRaw('translation_id, translatable_type, MIN(translatable_id) as `translatable_id`') |
44
|
|
|
->whereIn('translatable_type', $models) |
45
|
|
|
->groupBy(['translation_id', 'translatable_type']) |
46
|
|
|
->orderBy('translation_id') |
47
|
|
|
->cursor() |
48
|
|
|
->each(function (Translation $translation) { |
49
|
|
|
$this->translateToNewLocale($translation); |
50
|
|
|
}); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function translateToNewLocale(Translation $translation) |
54
|
|
|
{ |
55
|
|
|
$translation->translatable::withoutEvents(function () use ($translation) { |
56
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $model */ |
57
|
|
|
$model = $translation->translatable->query()->create( |
58
|
|
|
$translation->translatable->only( |
59
|
|
|
$translation->translatable->getOnCreateTranslatable() |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
$model->upsertTranslationEntry($this->locale->getKey(), $translation->translation_id); |
63
|
|
|
}); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|