diff --git a/modules/attributi_combinazioni/actions.php b/modules/attributi_combinazioni/actions.php index e18e431d3..3c13622eb 100644 --- a/modules/attributi_combinazioni/actions.php +++ b/modules/attributi_combinazioni/actions.php @@ -24,30 +24,35 @@ use Modules\AttributiCombinazioni\ValoreAttributo; switch (filter('op')) { case 'add': - $nome = post('nome'); - $esistente = (new Attributo())->getByName(post('nome')); + $descrizione = post('nome'); + $title = post('titolo'); + $attributo_new = (new Attributo())->getByName($descrizione)->id_record; - if (!$esistente) { + if ($stato_new) { + flash()->error(tr('Questo nome è già stato utilizzato per un altro attributo.')); + } else { $attributo = Attributo::build(); + $id_record= $dbo->lastInsertedID(); + $attributo->name = $descrizione; + $attributo->title = $title; $attributo->save(); - $id_record = $attributo->id; - - $database->query('INSERT INTO `mg_attributi_lang` (`id_record`, `id_lang`, `name`, `title`) VALUES ('.$id_record.', '.setting('Lingua').', \''.post('nome').'\', \''.post('titolo').'\')'); - flash()->info(tr('Nuovo attributo creato correttamente!')); - } else { - flash()->error(tr('Attributo esistente con lo stesso nome!')); } - break; case 'update': + $title = post('titolo'); + $attributo_new = (new Attributo())->getByName($descrizione)->id_record; - $attributo->save(); - - flash()->info(tr('Attributo aggiornato correttamente!')); + if (!empty($attributo_new) && $attributo_new != $id_record){ + flash()->error(tr('Questo nome è già stato utilizzato per un altro attributo.')); + } else { + $attributo->title = $title; + $attributo->save(); + flash()->info(tr('Attributo aggiornato correttamente!')); + } break; case 'delete': diff --git a/modules/attributi_combinazioni/src/Attributo.php b/modules/attributi_combinazioni/src/Attributo.php index 91b08c3b8..38ec59762 100644 --- a/modules/attributi_combinazioni/src/Attributo.php +++ b/modules/attributi_combinazioni/src/Attributo.php @@ -13,6 +13,14 @@ class Attributo extends Model protected $table = 'mg_attributi'; + public static function build() + { + $model = new static(); + $model->save(); + + return $model; + } + /* Relazioni Eloquent */ public function valori() { @@ -34,6 +42,30 @@ class Attributo extends Model ->first()->name; } + /** + * Imposta l'attributo name dell'attributo. + */ + public function setNameAttribute($value) + { + $table = database()->table($this->table.'_lang'); + + $translated = $table + ->where('id_record', '=', $this->id) + ->where('id_lang', '=', setting('Lingua')); + + if ($translated->count() > 0) { + $translated->update([ + 'name' => $value + ]); + } else { + $table->insert([ + 'id_record' => $this->id, + 'id_lang' => setting('Lingua'), + 'name' => $value + ]); + } + } + /** * Ritorna l'attributo title dell'attributo. * @@ -48,6 +80,30 @@ class Attributo extends Model ->first()->title; } + /** + * Imposta l'attributo title dell'attributo. + */ + public function setTitleAttribute($value) + { + $table = database()->table($this->table.'_lang'); + + $translated = $table + ->where('id_record', '=', $this->id) + ->where('id_lang', '=', setting('Lingua')); + + if ($translated->count() > 0) { + $translated->update([ + 'title' => $value + ]); + } else { + $table->insert([ + 'id_record' => $this->id, + 'id_lang' => setting('Lingua'), + 'title' => $value + ]); + } + } + /** * Ritorna l'id dell'attributo a partire dal nome. *