Skip to content

Commit

Permalink
Merge pull request #2918 from EnesSacid-Buker/category-type
Browse files Browse the repository at this point in the history
Category types saved incorrectly in database issue
  • Loading branch information
cuneytsenturk committed Mar 3, 2023
2 parents a71ecb7 + a9d8d09 commit fdf5526
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
71 changes: 71 additions & 0 deletions app/Listeners/Update/V30/Version3013.php
@@ -0,0 +1,71 @@
<?php

namespace App\Listeners\Update\V30;

use App\Abstracts\Listeners\Update as Listener;
use App\Events\Install\UpdateFinished as Event;
use App\Traits\Categories;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Version3013 extends Listener
{
use Categories;

const ALIAS = 'core';

const VERSION = '3.0.13';

/**
* Handle the event.
*
* @param $event
* @return void
*/
public function handle(Event $event)
{
if ($this->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;
}
}
1 change: 1 addition & 0 deletions app/Providers/Event.php
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions app/Traits/Categories.php
Expand Up @@ -7,7 +7,7 @@

trait Categories
{
public function getCategoryTypes(): array
public function getCategoryTypes(bool $translate = true): array
{
$types = [];
$configs = config('type.category');
Expand All @@ -21,7 +21,7 @@ public function getCategoryTypes(): array
$name = $attr['alias'] . '::' . $name;
}

$types[$type] = trans_choice($name, 1);
$types[$type] = $translate ? trans_choice($name, 1) : $name;
}

return $types;
Expand Down

0 comments on commit fdf5526

Please sign in to comment.