From 8c2a3023a430fed8355774e827c51dab4c129897 Mon Sep 17 00:00:00 2001 From: FabioL <1647128+loviuz@users.noreply.github.com> Date: Tue, 19 Mar 2024 15:50:41 +0100 Subject: [PATCH] Ulteriore standardizzazione della gestione lingue --- core.php | 4 +++ modules/articoli/src/Articolo.php | 9 +++++++ src/App.php | 13 ++++++++- src/Traits/RecordTrait.php | 45 +++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) diff --git a/core.php b/core.php index 2ec5169a9..b20f0ef70 100755 --- a/core.php +++ b/core.php @@ -305,6 +305,10 @@ foreach ($list as $file) { include_once $file; } +// Inizializzazione traduzioni if (database()->tableExists('zz_settings')) { + $available_langs = database()->table('zz_langs')->select('id')->pluck('id')->toArray(); + App::setAvailableLangs($available_langs); + App::setLang(setting('Lingua')); } diff --git a/modules/articoli/src/Articolo.php b/modules/articoli/src/Articolo.php index e7d03e196..9e69b9344 100755 --- a/modules/articoli/src/Articolo.php +++ b/modules/articoli/src/Articolo.php @@ -42,6 +42,10 @@ class Articolo extends Model protected $table = 'mg_articoli'; + protected static $translated_fields = [ + 'name', + ]; + public static function build($codice, ?Categoria $categoria = null, ?Categoria $sottocategoria = null) { $model = new static(); @@ -57,6 +61,7 @@ class Articolo extends Model return $model; } + /** * Funzione per registrare un movimento del magazzino in relazione all'articolo corrente, modificando di conseguenza la quantità dell'articolo stesso. * @@ -377,4 +382,8 @@ class Articolo extends Model ]); } } + + public static function getTranslatedFields(){ + return self::$translated_fields; + } } diff --git a/src/App.php b/src/App.php index ed47b5fe9..f8452cd58 100755 --- a/src/App.php +++ b/src/App.php @@ -30,7 +30,8 @@ class App public static $rootdir; public static $baseurl; - public static $lang; + protected static $available_langs = []; + protected static $lang; /** @var array Identificativo del modulo corrente */ protected static $current_module; @@ -100,6 +101,16 @@ class App return self::$config; } + public static function getAvailableLangs() + { + return self::$available_langs; + } + + public static function setAvailableLangs($values) + { + self::$available_langs = (array)$values; + } + public static function getLang() { return self::$lang; diff --git a/src/Traits/RecordTrait.php b/src/Traits/RecordTrait.php index 83478572d..6ec4dab8f 100755 --- a/src/Traits/RecordTrait.php +++ b/src/Traits/RecordTrait.php @@ -66,4 +66,49 @@ trait RecordTrait return collect(); } + + + /** + * Estensione del salvataggio oggetto per popolare le lingue aggiuntive + */ + public function save() + { + if ($this->id) { + // Lingue aggiuntive disponibili + $langs = \App::getAvailableLangs(); + $other_langs = array_diff($langs, [\App::getLang()]); + + // Popolo inizialmente i campi traducibili o allineo quelli uguali + foreach ($this->getTranslatedFields() as $field) { + foreach ($other_langs as $id_lang) { + $translation = database()->table($this->table.'_lang') + ->select($field) + ->where('id_record', '=', $this->id) + ->where('id_lang', '=', $id_lang); + + // Se la traduzione non è presente la creo... + if ($translation->count() == 0) { + $translation->insert([ + 'id_record' => $this->id, + 'id_lang' => $id_lang, + $field => $this->{$field}, + ]); + } + + // ...altrimenti la aggiorno se è uguale (quindi probabilmente non ancora tradotta) + /* + else{ + if ($translation->first()->{$field} == $this->{$field}) { + $translation->update([ + $field => $this->{$field}, + ]); + } + } + */ + } + } + } + + parent::save(); + } }