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
|
|
|
0 |
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($locale->getkey(), $translation->translation_id); |
41
|
|
|
}); |
42
|
|
|
}); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Handle the Translatable "updated" event. |
47
|
|
|
* |
48
|
|
|
* @param \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $translatable |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function updated(IsTranslatable $translatable) |
52
|
|
|
{ |
53
|
|
|
$attributes = $translatable->only( |
54
|
|
|
$translatable->getNonTranslatable() |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$translatable::withoutEvents(function () use ($translatable, $attributes) { |
58
|
|
|
$translatable->translations()->each(function ($model) use ($translatable, $attributes) { |
59
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $model */ |
60
|
|
|
$model->update($attributes); |
61
|
|
|
}); |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Handle the Translatable "deleted" event. |
67
|
|
|
* |
68
|
|
|
* @param \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $translatable |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function deleted(IsTranslatable $translatable) |
72
|
|
|
{ |
73
|
|
|
Translation::query() |
74
|
|
|
->where('translatable_id', '=', $translatable->translation->translatable_id) |
|
|
|
|
75
|
|
|
->where('translatable_type', '=', $translatable->translation->translatable_type) |
|
|
|
|
76
|
|
|
->where('translation_id', '=', $translatable->translation->translation_id) |
|
|
|
|
77
|
|
|
->where('locale_id', '=', $translatable->translation->locale_id) |
|
|
|
|
78
|
|
|
->delete(); |
79
|
|
|
|
80
|
|
|
if (! in_array(get_class($translatable), NovaTranslation::translatableModels())) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Prevent deleted translation to delete other translations again. |
85
|
|
|
if ($translatable->deleting_translation) { |
|
|
|
|
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$translatable->translations()->each(function (IsTranslatable $translatable) { |
90
|
|
|
$translatable->deleting_translation = true; |
|
|
|
|
91
|
|
|
$translatable->delete(); |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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: