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

View File

@ -40,17 +40,17 @@ if (isset($id_original)) {
<div class="row"> <div class="row">
<div class="col-md-8"> <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>
<div class="col-md-4"> <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> </div>
<div class="row"> <div class="row">
<div class="col-md-12"> <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>
</div> </div>

View File

@ -18,7 +18,10 @@
*/ */
include_once __DIR__.'/../../core.php'; include_once __DIR__.'/../../core.php';
use Modules\Articoli\Categoria;
if (isset($id_record)) { 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)); $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,21 +18,24 @@
*/ */
include_once __DIR__.'/../../core.php'; 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) { 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 ' echo '
<tr> <tr>
<td>'.$sub['name'].'</td> <td>'.$sub->name.'</td>
<td>'.$sub['colore'].'</td> <td>'.$sub->colore.'</td>
<td>'.$sub['nota'].'</td> <td>'.$sub->nota.'</td>
<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' : '').'"> <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> <i class="fa fa-trash"></i>
</a> </a>
</td> </td>
</tr>'; </tr>';
} }