|
6 | 6 | use Anomaly\Streams\Platform\Entry\EntryObserver; |
7 | 7 | use Visiosoft\AdvsModule\Adv\Event\DeletedAd; |
8 | 8 | use Visiosoft\AdvsModule\Adv\Event\DeletingAd; |
| 9 | +use Illuminate\Support\Facades\DB; |
9 | 10 |
|
10 | 11 | class AdvObserver extends EntryObserver |
11 | 12 | { |
| 13 | + private function translateFixer($entryId) |
| 14 | + { |
| 15 | + //TODO:: Use single query |
| 16 | + $defaultLocale = setting_value('streams::default_locale'); |
| 17 | + $translations = DB::table('advs_advs_translations') |
| 18 | + ->select(['locale', 'name']) |
| 19 | + ->where('entry_id', $entryId) |
| 20 | + ->whereNotNull('locale') |
| 21 | + ->get(); |
| 22 | + |
| 23 | + if ($translations) { |
| 24 | + $name = ""; |
| 25 | + foreach ($translations as $translation) { |
| 26 | + if ($translation->locale == $defaultLocale && !empty($translation->name)) { |
| 27 | + $name = $translation->name; |
| 28 | + break; |
| 29 | + } else if (!empty($translation->name)) { |
| 30 | + $name = $translation->name; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + DB::table('advs_advs_translations') |
| 35 | + ->whereNull('name') |
| 36 | + ->where('entry_id', $entryId) |
| 37 | + ->update(['name' => $name]); |
| 38 | + } |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + public function created(EntryInterface $entry) |
| 43 | + { |
| 44 | + $this->translateFixer($entry->getId()); |
| 45 | + } |
| 46 | + |
| 47 | + public function updated(EntryInterface $entry) |
| 48 | + { |
| 49 | + $this->translateFixer($entry->getId()); |
| 50 | + } |
| 51 | + |
12 | 52 | public function updating(EntryInterface $entry) |
13 | 53 | { |
14 | 54 | $this->dispatchSync(new AddSlug($entry)); |
15 | | - |
16 | 55 | parent::updating($entry); |
17 | 56 | } |
18 | 57 |
|
|
0 commit comments