From 43c4a7b096e6dca9ebe6b9ef335a16f44a21fd7b Mon Sep 17 00:00:00 2001 From: EnesSacid-Buker Date: Fri, 3 Mar 2023 10:11:26 +0300 Subject: [PATCH 1/2] Fixed issue with category type --- app/Listeners/Update/V30/Version3013.php | 72 ++++++++++++++++++++++++ app/Traits/Categories.php | 4 +- 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 app/Listeners/Update/V30/Version3013.php diff --git a/app/Listeners/Update/V30/Version3013.php b/app/Listeners/Update/V30/Version3013.php new file mode 100644 index 000000000..5f4ef6934 --- /dev/null +++ b/app/Listeners/Update/V30/Version3013.php @@ -0,0 +1,72 @@ +skipThisUpdate($event)) { + return; + } + + Log::channel('stdout')->info('Updating to 3.0.13 version...'); + + DB::transaction(function () { + $types = $this->getTypesByAllowedTranslations(); + + foreach ($types as $type => $translations) { + DB::table('categories')->whereIn('type', $translations)->update(['type' => $type]); + } + + }); + + Log::channel('stdout')->info('Done!'); + } + + protected function getTypesByAllowedTranslations(): array + { + $types = $this->getCategoryTypes(false); + $lang_codes = array_keys(language()->allowed()); + + foreach ($types as $type => $trans_name) { + $translations_for_type = []; + + foreach ($lang_codes as $lang_code) { + $translation = trans_choice($trans_name, 1, locale: $lang_code); + + if ($translation === $trans_name) { + continue; + } + + $translations_for_type[] = $translation; + } + + $types[$type] = $translations_for_type; + } + + /** + * Example: en-GB es-ES + * 'income' => ['Income', 'Ingresos'] + */ + return $types; + } +} diff --git a/app/Traits/Categories.php b/app/Traits/Categories.php index 14789fdd8..c2ee8f5fb 100644 --- a/app/Traits/Categories.php +++ b/app/Traits/Categories.php @@ -7,7 +7,7 @@ use Illuminate\Support\Str; trait Categories { - public function getCategoryTypes(): array + public function getCategoryTypes(bool $translate = true): array { $types = []; $configs = config('type.category'); @@ -21,7 +21,7 @@ trait Categories $name = $attr['alias'] . '::' . $name; } - $types[$type] = trans_choice($name, 1); + $types[$type] = $translate ? trans_choice($name, 1) : $name; } return $types; From a9d8d09f3df82c6240fedc6183746de9a1482842 Mon Sep 17 00:00:00 2001 From: EnesSacid-Buker Date: Fri, 3 Mar 2023 10:27:39 +0300 Subject: [PATCH 2/2] Update listener added to event --- app/Listeners/Update/V30/Version3013.php | 1 - app/Providers/Event.php | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Listeners/Update/V30/Version3013.php b/app/Listeners/Update/V30/Version3013.php index 5f4ef6934..4bda06ab4 100644 --- a/app/Listeners/Update/V30/Version3013.php +++ b/app/Listeners/Update/V30/Version3013.php @@ -36,7 +36,6 @@ class Version3013 extends Listener foreach ($types as $type => $translations) { DB::table('categories')->whereIn('type', $translations)->update(['type' => $type]); } - }); Log::channel('stdout')->info('Done!'); diff --git a/app/Providers/Event.php b/app/Providers/Event.php index 498fe7a85..9593392f5 100644 --- a/app/Providers/Event.php +++ b/app/Providers/Event.php @@ -21,6 +21,7 @@ class Event extends Provider 'App\Listeners\Update\V30\Version305', 'App\Listeners\Update\V30\Version307', 'App\Listeners\Update\V30\Version309', + 'App\Listeners\Update\V30\Version3013', ], 'Illuminate\Auth\Events\Login' => [ 'App\Listeners\Auth\Login',