Allineamento add categorie articoli

This commit is contained in:
Pek5892 2024-03-07 16:04:52 +01:00
parent 2d3465ce4f
commit 61199687d3
5 changed files with 69 additions and 58 deletions

View File

@ -31,11 +31,11 @@ class Categoria extends Model
protected $table = 'mg_categorie';
protected static $parent_identifier = 'parent';
public static function build($nome)
public static function build($nota, $colore)
{
$model = new static();
$model->nome = $nome;
$model->nota = $nota;
$model->colore = $colore;
$model->save();
return $model;
@ -75,4 +75,28 @@ class Categoria extends Model
->where('id_lang', '=', setting('Lingua'))
->first();
}
/**
* Imposta l'attributo name della categoria.
*/
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
]);
}
}
}

View File

@ -18,6 +18,7 @@
*/
include_once __DIR__.'/../../core.php';
use Modules\Articoli\Categoria;
switch (filter('op')) {
case 'update':
@ -27,20 +28,14 @@ switch (filter('op')) {
$id_original = filter('id_original') ?: null;
if (isset($nome) && isset($nota) && isset($colore)) {
$database->table('mg_categorie')
->where('id', '=', $id_record)
->update([
'nota' => $nota,
'colore' => $colore,
]);
$database->table('mg_categorie_lang')
->where('id_record', '=', $id_record)
->update([
'name' => $nome,
]);
$categoria->nota = $nota;
$categoria->colore = $colore;
$categoria->parent = $id_original ?: null;
$categoria->name = $nome;
$categoria->save();
flash()->info(tr('Salvataggio completato!'));
} else {
flash()->error(tr('Ci sono stati alcuni errori durante il salvataggio!'));
}
@ -58,42 +53,28 @@ switch (filter('op')) {
$nome = filter('nome');
$nota = filter('nota');
$colore = filter('colore');
$id_original = filter('id_original') ?: null;
// Ricerca corrispondenze con stesso nome
$corrispondenze = $database->table('mg_categorie')
->join('mg_categorie_lang', 'mg_categorie.id', '=', 'mg_categorie_lang.id_record')
->where('name', '=', $nome);
$categoria_new = Categoria::where('id', "=", (new Categoria())->getByName($nome)->id_record);
if (!empty($id_original)) {
$corrispondenze = $corrispondenze->where('parent', '=', $id_original);
$categoria_new = $categoria_new->where('parent', '=', $id_original);
} else {
$corrispondenze = $corrispondenze->whereNull('parent');
$categoria_new = $categoria_new->whereNull('parent');
}
$corrispondenze = $corrispondenze->get();
$categoria_new = $categoria_new->first();
// Eventuale creazione del nuovo record
if ($corrispondenze->count() == 0) {
$id_record = $database->table('mg_categorie')
->insertGetId([
'nota' => $nota,
'colore' => $colore,
'parent' => $id_original,
]);
$database->table('mg_categorie_lang')
->insertGetId([
'id_record' => $id_record,
'name' => $nome,
'id_lang' => setting('Lingua'),
]);
if (!empty($categoria_new)){
flash()->error(tr('Questo nome è già stato utilizzato per un altra categoria.'));
} else {
$categoria = Categoria::build($nota, $colore);
$id_record= $dbo->lastInsertedID();
$categoria->parent = $id_original;
$categoria->name = $nome;
$categoria->save();
flash()->info(tr('Aggiunta nuova tipologia di _TYPE_', [
'_TYPE_' => 'categoria',
]));
} else {
$id_record = $corrispondenze->first()->id;
flash()->error(tr('Esiste già una categoria con lo stesso nome!'));
}
if (isAjaxRequest()) {

View File

@ -40,17 +40,17 @@ if (isset($id_original)) {
<div class="row">
<div class="col-md-8">
{[ "type": "text", "label": "<?php echo tr('Nome'); ?>", "name": "nome", "required": 1, "value": "$nome$" ]}
{[ "type": "text", "label": "<?php echo tr('Nome'); ?>", "name": "nome", "required": 1, "value": "<?php echo $categoria->name; ?>" ]}
</div>
<div class="col-md-4">
{[ "type": "text", "label": "<?php echo tr('Colore'); ?>", "name": "colore", "id": "colore_", "class": "colorpicker text-center", "value": "$colore$", "extra": "maxlength=\"7\"", "icon-after": "<div class=\"img-circle square\"></div>" ]}
{[ "type": "text", "label": "<?php echo tr('Colore'); ?>", "name": "colore", "id": "colore_", "class": "colorpicker text-center", "value": "<?php echo $categoria->colore; ?>", "extra": "maxlength=\"7\"", "icon-after": "<div class='img-circle square'></div>" ]}
</div>
</div>
<div class="row">
<div class="col-md-12">
{[ "type": "textarea", "label": "<?php echo tr('Nota'); ?>", "name": "nota", "value": "$nota$" ]}
{[ "type": "textarea", "label": "<?php echo tr('Nota'); ?>", "name": "nota", "value": "<?php echo $categoria->nota; ?>" ]}
</div>
</div>

View File

@ -18,7 +18,10 @@
*/
include_once __DIR__.'/../../core.php';
use Modules\Articoli\Categoria;
if (isset($id_record)) {
$record = $dbo->fetchOne('SELECT * FROM `mg_categorie` LEFT JOIN `mg_categorie_lang` ON (`mg_categorie`.`id`=`mg_categorie_lang`.`id_record` AND `mg_categorie_lang`.`id_lang`='.prepare(setting('Lingua')).') WHERE `mg_categorie`.`id`='.prepare($id_record));
$categoria = Categoria::find($id_record);
}

View File

@ -18,18 +18,21 @@
*/
include_once __DIR__.'/../../core.php';
use Modules\Articoli\Categoria;
use Modules\Articoli\Articolo;
$subcategorie = $dbo->fetchArray('SELECT * FROM `mg_categorie` LEFT JOIN `mg_categorie_lang` ON (`mg_categorie`.`id`=`mg_categorie_lang`.`id_record` AND `mg_categorie_lang`.`id_lang`='.prepare(setting('Lingua')).') WHERE `parent`='.prepare($id_record).' ORDER BY `name` ASC ');
$subcategorie = Categoria::where('parent', '=', $id_record)->get();
foreach ($subcategorie as $sub) {
$n_articoli = $dbo->fetchNum('SELECT * FROM `mg_articoli` LEFT JOIN `mg_articoli_lang` ON (`mg_articoli`.`id` = `mg_articoli_lang`.`id_record` AND `mg_articoli_lang`.`id_lang` = '.prepare(setting('Lingua')).') WHERE `id_sottocategoria`='.prepare($sub['id']).' AND deleted_at IS NULL');
$n_articoli = Articolo::where('id_sottocategoria', '=', $sub['id'])->count();
echo '
<tr>
<td>'.$sub['name'].'</td>
<td>'.$sub['colore'].'</td>
<td>'.$sub['nota'].'</td>
<td>'.$sub->name.'</td>
<td>'.$sub->colore.'</td>
<td>'.$sub->nota.'</td>
<td>
<a class="btn btn-warning btn-sm" title="Modifica riga" onclick="launch_modal(\''.tr('Modifica sottocategoria').'\', \''.base_path().'/add.php?id_module='.$id_module.'&id_record='.$sub['id'].'&id_original='.$id_record.'\');"><i class="fa fa-edit"></i></a>
<a class="btn btn-warning btn-sm" title="Modifica riga" onclick="launch_modal(\''.tr('Modifica sottocategoria').'\', \''.base_path().'/add.php?id_module='.$id_module.'&id_record='.$sub->id.'&id_original='.$id_record.'\');"><i class="fa fa-edit"></i></a>
<a class="btn btn-sm btn-danger ask '.(($n_articoli > 0) ? 'disabled tip' : '').'" data-backto="record-edit" data-id="'.$sub['id'].'" title="'.(($n_articoli > 0) ? 'Sottocategoria collegata a '.$n_articoli.' articoli' : '').'">
<i class="fa fa-trash"></i>
</a>