1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\Models\Observers; |
4
|
|
|
|
5
|
|
|
use BBSLab\NovaTranslation\Models\Contracts\IsTranslatable; |
6
|
|
|
use BBSLab\NovaTranslation\Models\Locale; |
7
|
|
|
use BBSLab\NovaTranslation\Models\Translation; |
8
|
|
|
use BBSLab\NovaTranslation\NovaTranslation; |
9
|
|
|
|
10
|
|
|
class TranslatableObserver |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Handle the Translatable "created" event. |
14
|
|
|
* |
15
|
|
|
* @param \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $translatable |
16
|
|
|
* @return void |
17
|
|
|
* @throws \Exception |
18
|
|
|
*/ |
19
|
|
|
public function created(IsTranslatable $translatable) |
20
|
|
|
{ |
21
|
|
|
$translation = $translatable->upsertTranslationEntry( |
22
|
|
|
($currentLocale = NovaTranslation::currentLocale())->getKey(), |
23
|
|
|
$translatable->getKey() |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
if (! in_array(get_class($translatable), NovaTranslation::translatableModels())) { |
27
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$attributes = $translatable->only( |
31
|
|
|
$translatable->getOnCreateTranslatable() |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
$translatable::withoutEvents(function () use ($translatable, $translation, $currentLocale, $attributes) { |
35
|
|
|
NovaTranslation::otherLocales($currentLocale)->each(function (Locale $locale) use ( |
36
|
|
|
$translatable, $translation, $attributes |
37
|
|
|
) { |
38
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $model */ |
39
|
|
|
$model = $translatable->query()->create($attributes); |
40
|
|
|
$model->upsertTranslationEntry( |
41
|
|
|
$locale->getkey(), $translatable->getKey(), $translation->translation_id |
42
|
|
|
); |
43
|
|
|
}); |
44
|
|
|
}); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Handle the Translatable "updated" event. |
49
|
|
|
* |
50
|
|
|
* @param \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $translatable |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function updated(IsTranslatable $translatable) |
54
|
|
|
{ |
55
|
|
|
$attributes = $translatable->only( |
56
|
|
|
$translatable->getNonTranslatable() |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$translatable::withoutEvents(function () use ($translatable, $attributes) { |
60
|
|
|
$translatable->translations->each(function (Translation $translation) use ($attributes) { |
|
|
|
|
61
|
|
|
$translation->translatable->update($attributes); |
62
|
|
|
}); |
63
|
|
|
}); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Handle the Translatable "deleted" event. |
68
|
|
|
* |
69
|
|
|
* @param \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $translatable |
70
|
|
|
* @return void |
71
|
|
|
* @throws \Exception |
72
|
|
|
*/ |
73
|
|
|
public function deleted(IsTranslatable $translatable) |
74
|
|
|
{ |
75
|
|
|
$translatable->load('translations'); |
76
|
|
|
$translatable->translation->delete(); |
|
|
|
|
77
|
|
|
|
78
|
|
|
if (! in_array(get_class($translatable), NovaTranslation::translatableModels())) { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Prevent deleted translation to delete other translations again. |
83
|
|
|
if ($translatable->isDeletingTranslation()) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$translatable->translations->each(function (Translation $translation) { |
|
|
|
|
88
|
|
|
$translatable = $translation->translatable; |
89
|
|
|
$translatable->deletingTranslation(); |
90
|
|
|
$translatable->delete(); |
91
|
|
|
}); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: