Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix update of already saved db entries #18

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions src/Models/TranslationLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Backpack\TranslationManager\Models;

use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Backpack\TranslationManager\Models\TranslationLineOriginal;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use ReflectionClass;
Expand All @@ -16,7 +15,7 @@
* @property int $id_database
* @property array $text
* @property string $search
* @property boolean $database
* @property bool $database
* @property string $group
* @property string $key
* @property string $created_at
Expand All @@ -41,7 +40,7 @@ class TranslationLine extends TranslationLineOriginal
* The attributes that should be cast.
*/
protected $casts = [
'text' => 'array',
'text' => 'array',
'database' => 'boolean',
];

Expand All @@ -50,13 +49,13 @@ class TranslationLine extends TranslationLineOriginal
* Used to handle empty datasets
*/
protected $schema = [
'id' => 'string',
'id' => 'string',
'id_database' => 'integer',
'database' => 'boolean',
'group' => 'string',
'key' => 'string',
'text' => 'string',
'created_at' => 'date',
'database' => 'boolean',
'group' => 'string',
'key' => 'string',
'text' => 'string',
'created_at' => 'date',
];

/**
Expand All @@ -74,14 +73,14 @@ public function getRows(): array
{
// database entries
$entries = TranslationLineOriginal::all()
->mapWithKeys(fn(TranslationLineOriginal $item) => ["$item->group.$item->key" => [
'id' => "$item->group.$item->key",
->mapWithKeys(fn (TranslationLineOriginal $item) => ["$item->group.$item->key" => [
'id' => "$item->group.$item->key",
'id_database' => $item->id,
'database' => true,
'group' => $item->group,
'key' => $item->key,
'text' => array_filter($item->text ?? []),
'created_at' => $item->created_at,
'database' => true,
'group' => $item->group,
'key' => $item->key,
'text' => array_filter($item->text ?? []),
'created_at' => $item->created_at,
]])
->toArray();

Expand All @@ -95,24 +94,24 @@ public function getRows(): array

// file entries
collect($filePaths)
->flatMap(fn(string $path) => File::allFiles($path))
->filter(fn(SplFileInfo $file) => $file->getExtension() === 'php')
->flatMap(fn (string $path) => File::allFiles($path))
->filter(fn (SplFileInfo $file) => $file->getExtension() === 'php')
->each(function (SplFileInfo $file) use (&$entries) {
$group = Str::beforeLast($file->getFilename(), '.php');
$locale = Str::of($file->getPath())->afterLast('/')->afterLast('\\')->value();

collect(include $file)
->dot()
->filter(fn($text): bool => is_string($text))
->filter(fn ($text): bool => is_string($text))
->each(function (string $text, string $key) use ($group, $file, $locale, &$entries) {
$entries["$group.$key"] ??= [
'id' => "$group.$key",
'id' => "$group.$key",
'id_database' => null,
'database' => false,
'group' => $group,
'key' => $key,
'text' => [],
'created_at' => $file->getMTime(),
'database' => false,
'group' => $group,
'key' => $key,
'text' => [],
'created_at' => $file->getMTime(),
];
$entries["$group.$key"]['text'][$locale] ??= $text;
});
Expand All @@ -136,9 +135,13 @@ public static function boot()

static::saved(function (TranslationLine $entry): void {
if (! $entry->database) {
$entry = TranslationLineOriginal::create([
TranslationLineOriginal::create([
'group' => $entry->group,
'key' => $entry->key,
'key' => $entry->key,
'text' => $entry->text,
]);
} else {
TranslationLineOriginal::where('id', $entry->id_database)->update([
'text' => $entry->text,
]);
}
Expand Down
Loading