|
37 | 37 | } |
38 | 38 |
|
39 | 39 | // Verifica se esiste già una categoria con lo stesso nome |
40 | | - $categoria_esistente = (new Categoria())->getByField('title', $nome); |
| 40 | + $categoria_esistente = Categoria::where('name', $nome)->where('id', '!=', $id_record)->where('parent', '=', $id_original)->first(); |
41 | 41 |
|
42 | 42 | if (!empty($categoria_esistente) && $categoria_esistente != $id_record) { |
43 | 43 | // Mostra un messaggio di errore con link alla categoria esistente |
|
47 | 47 |
|
48 | 48 | $link = Modules::link('Categorie', $categoria_esistente->id, $nome); |
49 | 49 | flash()->error($message.': '.$link); |
50 | | - break; |
51 | | - } |
52 | | - |
53 | | - if (isset($nome) && isset($nota) && isset($colore)) { |
54 | | - $categoria->colore = $colore; |
55 | | - $categoria->parent = $id_original ?: null; |
56 | | - $categoria->is_articolo = $is_articolo; |
57 | | - $categoria->is_impianto = $is_impianto; |
58 | | - $categoria->save(); |
| 50 | + } else { |
59 | 51 |
|
60 | | - $categoria->setTranslation('title', $nome); |
61 | | - $categoria->setTranslation('note', $nota); |
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(); |
| 52 | + if (isset($nome) && isset($nota) && isset($colore)) { |
| 53 | + $categoria->colore = $colore; |
| 54 | + $categoria->parent = $id_original ?: null; |
| 55 | + $categoria->is_articolo = $is_articolo; |
| 56 | + $categoria->is_impianto = $is_impianto; |
| 57 | + $categoria->name = $nome; |
| 58 | + $categoria->save(); |
| 59 | + |
| 60 | + $categoria->setTranslation('title', $nome); |
| 61 | + $categoria->setTranslation('note', $nota); |
| 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!')); |
69 | 76 | } |
70 | | - |
71 | | - flash()->info(tr('Salvataggio completato! Aggiornate anche _NUM_ sottocategorie.', [ |
72 | | - '_NUM_' => count($subcategorie), |
73 | | - ])); |
74 | 77 | } else { |
75 | | - flash()->info(tr('Salvataggio completato!')); |
| 78 | + flash()->error(tr('Ci sono stati alcuni errori durante il salvataggio!')); |
76 | 79 | } |
77 | | - } else { |
78 | | - flash()->error(tr('Ci sono stati alcuni errori durante il salvataggio!')); |
79 | 80 | } |
80 | 81 |
|
81 | 82 | // Redirect alla categoria se si sta modificando una sottocategoria |
|
126 | 127 | ])); |
127 | 128 | } |
128 | 129 |
|
129 | | - if (isAjaxRequest()) { |
130 | | - echo json_encode(['id' => $id_record, 'text' => $nome]); |
131 | | - } else { |
132 | | - // Redirect alla categoria se si sta aggiungendo una sottocategoria |
| 130 | + if (!empty($id_original)) { |
133 | 131 | $database->commitTransaction(); |
134 | 132 | redirect_url(base_path_osm().'/editor.php?id_module='.$id_module.'&id_record='.($id_original ?: $id_record)); |
135 | 133 | exit; |
| 134 | + } else { |
| 135 | + echo json_encode(['id' => $id_record, 'text' => $nome]); |
136 | 136 | } |
137 | 137 |
|
138 | 138 | break; |
|
0 commit comments