|
27 | 27 | $nota = filter('nota'); |
28 | 28 | $colore = filter('colore'); |
29 | 29 | $id_original = filter('id_original') ?: null; |
| 30 | + $is_articolo = filter('is_articolo_add') ?: filter('is_articolo') ?: 0; |
| 31 | + $is_impianto = filter('is_impianto_add') ?: filter('is_impianto') ?: 0; |
| 32 | + |
| 33 | + // Verifica che almeno uno dei due flag sia selezionato |
| 34 | + if ($is_articolo == 0 && $is_impianto == 0) { |
| 35 | + flash()->error(tr('È necessario selezionare almeno una delle due opzioni: Articolo o Impianto.')); |
| 36 | + break; |
| 37 | + } |
| 38 | + |
| 39 | + // Verifica se esiste già una categoria con lo stesso nome |
| 40 | + $categoria_esistente = (new Categoria())->getByField('title', $nome); |
| 41 | + |
| 42 | + if (!empty($categoria_esistente) && $categoria_esistente != $id_record) { |
| 43 | + // Mostra un messaggio di errore con link alla categoria esistente |
| 44 | + $message = tr('Esiste già una categoria con il nome _NOME_', [ |
| 45 | + '_NOME_' => '"'.$nome.'"', |
| 46 | + ]); |
| 47 | + |
| 48 | + $link = Modules::link('Categorie', $categoria_esistente->id, $nome); |
| 49 | + flash()->error($message.': '.$link); |
| 50 | + break; |
| 51 | + } |
30 | 52 |
|
31 | 53 | if (isset($nome) && isset($nota) && isset($colore)) { |
32 | 54 | $categoria->colore = $colore; |
33 | 55 | $categoria->parent = $id_original ?: null; |
| 56 | + $categoria->is_articolo = $is_articolo; |
| 57 | + $categoria->is_impianto = $is_impianto; |
34 | 58 | $categoria->setTranslation('title', $nome); |
35 | 59 | $categoria->setTranslation('note', $nota); |
36 | 60 | $categoria->save(); |
37 | 61 |
|
38 | | - flash()->info(tr('Salvataggio completato!')); |
| 62 | + // Aggiorna i flag delle sottocategorie se è un parent |
| 63 | + $subcategorie = Categoria::where('parent', '=', $id_record)->get(); |
| 64 | + if (!empty($subcategorie)) { |
| 65 | + foreach ($subcategorie as $sub) { |
| 66 | + $sub->is_articolo = $is_articolo; |
| 67 | + $sub->is_impianto = $is_impianto; |
| 68 | + $sub->save(); |
| 69 | + } |
| 70 | + |
| 71 | + flash()->info(tr('Salvataggio completato! Aggiornate anche _NUM_ sottocategorie.', [ |
| 72 | + '_NUM_' => count($subcategorie), |
| 73 | + ])); |
| 74 | + } else { |
| 75 | + flash()->info(tr('Salvataggio completato!')); |
| 76 | + } |
39 | 77 | } else { |
40 | 78 | flash()->error(tr('Ci sono stati alcuni errori durante il salvataggio!')); |
41 | 79 | } |
|
54 | 92 | $nota = filter('nota'); |
55 | 93 | $colore = filter('colore'); |
56 | 94 | $id_original = filter('id_original') ?: null; |
| 95 | + $is_articolo = filter('is_articolo_add') ?: filter('is_articolo') ?: 0; |
| 96 | + $is_impianto = filter('is_impianto_add') ?: filter('is_impianto') ?: 0; |
57 | 97 |
|
58 | | - $categoria_new = Categoria::where('id', '=', (new Categoria())->getByField('title', $nome)); |
59 | | - if (!empty($id_original)) { |
60 | | - $categoria_new = $categoria_new->where('parent', '=', $id_original); |
61 | | - } else { |
62 | | - $categoria_new = $categoria_new->whereNull('parent'); |
| 98 | + // Verifica che almeno uno dei due flag sia selezionato |
| 99 | + if ($is_articolo == 0 && $is_impianto == 0) { |
| 100 | + flash()->error(tr('È necessario selezionare almeno una delle due opzioni: Articolo o Impianto.')); |
| 101 | + break; |
63 | 102 | } |
64 | | - $categoria_new = $categoria_new->first(); |
| 103 | + |
| 104 | + // Verifica se esiste già una categoria con lo stesso nome |
| 105 | + $categoria_new = (new Categoria())->getByField('title', $nome); |
65 | 106 |
|
66 | 107 | if (!empty($categoria_new)) { |
67 | | - flash()->error(tr('Questo nome è già stato utilizzato per un altra categoria.')); |
| 108 | + // Mostra un messaggio di errore con link alla categoria esistente |
| 109 | + $message = tr('Esiste già una categoria con il nome _NOME_', [ |
| 110 | + '_NOME_' => '"'.$nome.'"', |
| 111 | + ]); |
| 112 | + |
| 113 | + $link = Modules::link('Categorie', $categoria_new->id, $categoria_new->getTranslation('title')); |
| 114 | + flash()->error($message.': '.$link); |
68 | 115 | } else { |
69 | 116 | $categoria = Categoria::build($colore); |
70 | 117 | $id_record = $dbo->lastInsertedID(); |
71 | 118 | $categoria->parent = $id_original; |
| 119 | + $categoria->is_articolo = $is_articolo; |
| 120 | + $categoria->is_impianto = $is_impianto; |
72 | 121 | $categoria->setTranslation('note', $nota); |
73 | 122 | $categoria->setTranslation('title', $nome); |
74 | 123 | $categoria->save(); |
|
95 | 144 | $id = $id_record; |
96 | 145 | } |
97 | 146 |
|
98 | | - if ($dbo->fetchNum('SELECT * FROM `mg_articoli` WHERE (`id_categoria`='.prepare($id).' OR `id_sottocategoria`='.prepare($id).' OR `id_sottocategoria` IN (SELECT `id` FROM `mg_categorie` WHERE `parent`='.prepare($id).')) AND `deleted_at` IS NULL') == 0) { |
99 | | - $dbo->query('DELETE FROM `mg_categorie` WHERE `id`='.prepare($id)); |
| 147 | + if ($dbo->fetchNum('SELECT * FROM `mg_articoli` WHERE (`id_categoria`='.prepare($id).' OR `id_sottocategoria`='.prepare($id).' OR `id_sottocategoria` IN (SELECT `id` FROM `zz_categorie` WHERE `parent`='.prepare($id).')) AND `deleted_at` IS NULL') == 0) { |
| 148 | + $dbo->query('DELETE FROM `zz_categorie` WHERE `id`='.prepare($id)); |
100 | 149 |
|
101 | 150 | flash()->info(tr('Tipologia di _TYPE_ eliminata con successo!', [ |
102 | 151 | '_TYPE_' => 'categoria', |
|
0 commit comments