|
26 | 26 | $operazione = filter('op'); |
27 | 27 | $id_modulo_impianti = Module::where('name', 'Impianti')->first()->id; |
28 | 28 |
|
| 29 | + |
| 30 | + |
29 | 31 | switch ($operazione) { |
30 | 32 | case 'add_impianto': |
31 | 33 | if (post('id_impianto')) { |
|
84 | 86 | break; |
85 | 87 |
|
86 | 88 | case 'delete_impianto': |
87 | | - $dbo->query('DELETE FROM my_impianti_interventi WHERE idintervento='.prepare($id_record).' AND idimpianto = '.prepare(post('id'))); |
88 | | - Check::deleteLinked([ |
89 | | - 'id_module' => $id_module, |
90 | | - 'id_record' => $id_record, |
91 | | - 'id_module_from' => $id_modulo_impianti, |
92 | | - 'id_record_from' => post('id'), |
93 | | - ]); |
| 89 | + try { |
| 90 | + $id_impianto = post('id'); |
94 | 91 |
|
95 | | - $components = $dbo->fetchArray('SELECT * FROM my_componenti WHERE id_impianto = '.prepare($matricola)); |
96 | | - if (!empty($components)) { |
97 | | - foreach ($components as $component) { |
98 | | - $dbo->query('DELETE FROM my_componenti_interventi WHERE id_componente = '.prepare($component['id']).' AND id_intervento = '.prepare($id_record)); |
| 92 | + if (empty($id_impianto)) { |
| 93 | + throw new Exception(tr('ID impianto non specificato')); |
99 | 94 | } |
100 | | - } |
101 | 95 |
|
102 | | - flash()->info(tr('Impianto rimosso correttamente!')); |
| 96 | + // Verifica che l'impianto esista prima di rimuoverlo |
| 97 | + $exists = $dbo->fetchOne('SELECT COUNT(*) as count FROM my_impianti_interventi WHERE idintervento='.prepare($id_record).' AND idimpianto = '.prepare($id_impianto)); |
103 | 98 |
|
104 | | - break; |
| 99 | + if ($exists['count'] == 0) { |
| 100 | + throw new Exception(tr('Impianto non trovato nell\'intervento')); |
| 101 | + } |
| 102 | + |
| 103 | + $result = $dbo->query('DELETE FROM my_impianti_interventi WHERE idintervento='.prepare($id_record).' AND idimpianto = '.prepare($id_impianto)); |
| 104 | + |
| 105 | + // Verifica che l'eliminazione sia avvenuta |
| 106 | + $remaining = $dbo->fetchOne('SELECT COUNT(*) as count FROM my_impianti_interventi WHERE idintervento='.prepare($id_record).' AND idimpianto = '.prepare($id_impianto)); |
| 107 | + if ($remaining['count'] > 0) { |
| 108 | + throw new Exception(tr('Errore durante l\'eliminazione dell\'impianto dal database')); |
| 109 | + } |
| 110 | + Check::deleteLinked([ |
| 111 | + 'id_module' => $id_module, |
| 112 | + 'id_record' => $id_record, |
| 113 | + 'id_module_from' => $id_modulo_impianti, |
| 114 | + 'id_record_from' => $id_impianto, |
| 115 | + ]); |
| 116 | + |
| 117 | + $components = $dbo->fetchArray('SELECT * FROM my_componenti WHERE id_impianto = '.prepare($id_impianto)); |
| 118 | + if (!empty($components)) { |
| 119 | + foreach ($components as $component) { |
| 120 | + $dbo->query('DELETE FROM my_componenti_interventi WHERE id_componente = '.prepare($component['id']).' AND id_intervento = '.prepare($id_record)); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + flash()->info(tr('Impianto rimosso correttamente!')); |
| 125 | + |
| 126 | + // Risposta JSON per il client |
| 127 | + $response = ['status' => 'success', 'message' => tr('Impianto rimosso correttamente!')]; |
| 128 | + } catch (Exception $e) { |
| 129 | + flash()->error(tr('Errore durante la rimozione dell\'impianto: _MSG_', [ |
| 130 | + '_MSG_' => $e->getMessage(), |
| 131 | + ])); |
| 132 | + |
| 133 | + // Risposta JSON di errore per il client |
| 134 | + $response = ['status' => 'error', 'message' => $e->getMessage()]; |
| 135 | + } |
| 136 | + |
| 137 | + // Invia la risposta JSON se è stata impostata |
| 138 | + if (isset($response)) { |
| 139 | + header('Content-Type: application/json'); |
| 140 | + echo json_encode($response); |
| 141 | + } |
105 | 142 |
|
106 | 143 | case 'load_checklist': |
107 | 144 | $checks = Check::where('id_module_from', $id_modulo_impianti)->where('id_record_from', post('id_impianto'))->where('id_module', $id_module)->where('id_record', $id_record)->where('id_parent', null)->get(); |
|
0 commit comments