2017-08-04 16:28:16 +02:00
< ? php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager : il software gestionale open source per l ' assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright ( C ) DevCode s . r . l .
2020-09-07 15:04:06 +02:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
2017-08-04 16:28:16 +02:00
include_once __DIR__ . '/../../core.php' ;
2024-03-05 16:01:45 +01:00
use Models\Module ;
2024-03-22 15:52:24 +01:00
use Modules\Checklists\Check ;
2023-10-27 15:09:49 +02:00
2017-08-04 16:28:16 +02:00
$op = post ( 'op' );
2024-06-10 09:47:42 +02:00
$upload_dir = base_dir () . '/files/' . Module :: find (( new Module ()) -> getByField ( 'title' , 'Impianti' , Models\Locale :: getPredefined () -> id )) -> directory ;
2024-04-18 17:44:05 +02:00
$id_modulo_categorie_impianti = ( new Module ()) -> getByField ( 'title' , 'Categorie impianti' , Models\Locale :: getPredefined () -> id );
2017-08-04 16:28:16 +02:00
switch ( $op ) {
// Aggiorno informazioni di base impianto
case 'update' :
$matricola = post ( 'matricola' );
if ( ! empty ( $matricola )) {
2018-07-10 16:39:02 +02:00
$dbo -> update ( 'my_impianti' , [
'idanagrafica' => post ( 'idanagrafica' ),
'nome' => post ( 'nome' ),
'matricola' => $matricola ,
2018-10-19 10:53:47 +02:00
'id_categoria' => post ( 'id_categoria' ) ? : null ,
2023-11-15 13:26:59 +01:00
'id_sottocategoria' => post ( 'id_sottocategoria' ) ? : null ,
2024-05-09 16:57:11 +02:00
'id_marca' => post ( 'id_marca' ) ? : null ,
'id_modello' => post ( 'id_modello' ) ? : null ,
2018-07-10 16:39:02 +02:00
'descrizione' => post ( 'descrizione' ),
'idsede' => post ( 'idsede' ),
2023-10-27 15:35:40 +02:00
'data' => post ( 'data' ) ? : null ,
2018-07-10 16:39:02 +02:00
'proprietario' => post ( 'proprietario' ),
'palazzo' => post ( 'palazzo' ),
'ubicazione' => post ( 'ubicazione' ),
'idtecnico' => post ( 'idtecnico' ),
'scala' => post ( 'scala' ),
'piano' => post ( 'piano' ),
'interno' => post ( 'interno' ),
'occupante' => post ( 'occupante' ),
], [ 'id' => $id_record ]);
2017-08-04 16:28:16 +02:00
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Informazioni salvate correttamente!' ));
2017-08-04 16:28:16 +02:00
// Upload file
if ( ! empty ( $_FILES ) && ! empty ( $_FILES [ 'immagine' ][ 'name' ])) {
2021-03-08 11:20:04 +01:00
$upload = Uploads :: upload ( $_FILES [ 'immagine' ], [
2018-07-10 16:39:02 +02:00
'name' => 'Immagine' ,
'id_module' => $id_module ,
'id_record' => $id_record ,
], [
'thumbnails' => true ,
]);
2021-03-08 11:20:04 +01:00
$filename = $upload -> filename ;
2018-07-10 16:39:02 +02:00
if ( ! empty ( $filename )) {
$dbo -> update ( 'my_impianti' , [
'immagine' => $filename ,
], [
'id' => $id_record ,
]);
2017-08-04 16:28:16 +02:00
} else {
2018-07-19 17:29:21 +02:00
flash () -> warning ( tr ( 'Errore durante il caricamento del file in _DIR_!' , [
2017-09-10 14:35:41 +02:00
'_DIR_' => $upload_dir ,
2018-07-07 13:56:22 +02:00
]));
2017-08-04 16:28:16 +02:00
}
}
// Eliminazione file
2024-06-10 09:47:42 +02:00
if ( ! empty ( post ( 'delete_immagine' ))) {
2018-07-18 15:20:10 +02:00
Uploads :: delete ( $record [ 'immagine' ], [
2018-07-10 16:39:02 +02:00
'id_module' => $id_module ,
'id_record' => $id_record ,
]);
$dbo -> update ( 'my_impianti' , [
'immagine' => null ,
], [
'id' => $id_record ,
]);
2017-08-04 16:28:16 +02:00
}
}
break ;
2024-01-15 15:30:45 +01:00
// Aggiungo impianto
2017-08-04 16:28:16 +02:00
case 'add' :
$matricola = post ( 'matricola' );
$idanagrafica = post ( 'idanagrafica' );
$nome = post ( 'nome' );
$idtecnico = post ( 'idtecnico' );
2019-01-10 18:41:25 +01:00
$idsede = post ( 'idsede' );
2023-10-27 15:09:49 +02:00
$id_categoria = post ( 'id_categoria' );
2023-11-15 13:26:59 +01:00
$id_sottocategoria = post ( 'id_sottocategoria' );
2017-08-04 16:28:16 +02:00
if ( ! empty ( $matricola )) {
2023-10-30 13:12:24 +01:00
$dbo -> insert ( 'my_impianti' , [
'matricola' => $matricola ,
'idanagrafica' => $idanagrafica ,
'nome' => $nome ,
'data' => date ( 'Y-m-d' ),
'idtecnico' => $idtecnico ? : 0 ,
'idsede' => $idsede ? : 0 ,
2023-11-02 17:11:07 +01:00
'id_categoria' => $id_categoria ? : null ,
2023-11-15 13:26:59 +01:00
'id_sottocategoria' => $id_sottocategoria ? : null ,
2023-10-30 13:12:24 +01:00
]);
2017-08-04 16:28:16 +02:00
$id_record = $dbo -> lastInsertedID ();
2018-06-26 09:41:43 +02:00
2024-03-05 16:01:45 +01:00
$checks_categoria = $dbo -> fetchArray ( 'SELECT * FROM zz_checks WHERE id_module = ' . prepare ( $id_modulo_categorie_impianti ) . ' AND id_record = ' . prepare ( $id_categoria ));
2023-10-27 15:09:49 +02:00
foreach ( $checks_categoria as $check_categoria ) {
2023-10-30 13:12:24 +01:00
$id_parent_new = null ;
if ( $check_categoria [ 'id_parent' ]) {
$parent = $dbo -> selectOne ( 'zz_checks' , '*' , [ 'id' => $check_categoria [ 'id_parent' ]]);
$id_parent_new = $dbo -> selectOne ( 'zz_checks' , '*' , [ 'content' => $parent [ 'content' ], 'id_module' => $id_module , 'id_record' => $id_record ])[ 'id' ];
}
$check = Check :: build ( $user , $structure , $id_record , $check_categoria [ 'content' ], $id_parent_new , $check_categoria [ 'is_titolo' ], $check_categoria [ 'order' ]);
2023-10-27 15:09:49 +02:00
$check -> id_plugin = null ;
$check -> note = $check_categoria [ 'note' ];
$check -> save ();
}
2019-11-14 18:37:42 +01:00
if ( isAjaxRequest ()) {
2018-06-26 14:30:26 +02:00
echo json_encode ([ 'id' => $id_record , 'text' => $matricola . ' - ' . $nome ]);
2018-05-14 14:31:18 +02:00
}
2017-08-04 16:28:16 +02:00
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Aggiunto nuovo impianto!' ));
2017-08-04 16:28:16 +02:00
}
break ;
2024-01-15 15:30:45 +01:00
// Carica i campi da compilare del componente
2017-08-04 16:28:16 +02:00
case 'load_componente' :
$filename = post ( 'filename' );
$idarticolo = post ( 'idarticolo' );
// Se è stato specificato un idarticolo, carico il file .ini dal campo `contenuto` di quell'idarticolo
2024-02-29 15:10:55 +01:00
$rs = $dbo -> fetchArray ( 'SELECT `contenuto`, `componente_filename` FROM `mg_articoli` WHERE `id`=' . prepare ( $idarticolo ));
2017-08-04 16:28:16 +02:00
// Se i campi da caricare sono del componente già salvato leggo dal campo `contenuto`...
if ( $rs [ 0 ][ 'componente_filename' ] == $filename ) {
$contenuto = $rs [ 0 ][ 'contenuto' ];
}
// ...altrimenti carico dal file .ini
2020-09-23 13:36:37 +02:00
elseif ( file_exists ( base_dir () . '/files/impianti/' . $filename )) {
$contenuto = file_get_contents ( base_dir () . '/files/impianti/' . $filename );
2017-08-04 16:28:16 +02:00
}
2020-08-25 16:14:10 +02:00
crea_form_componente ( $contenuto );
2017-08-04 16:28:16 +02:00
break ;
2024-01-15 15:30:45 +01:00
// Duplica impianto
2019-05-03 16:31:58 +02:00
case 'copy' :
2024-01-15 15:51:24 +01:00
$database -> beginTransaction ();
2019-05-03 16:31:58 +02:00
$dbo -> query ( 'CREATE TEMPORARY TABLE tmp SELECT * FROM my_impianti WHERE id= ' . prepare ( $id_record ));
$dbo -> query ( 'ALTER TABLE tmp DROP id' );
$dbo -> query ( 'INSERT INTO my_impianti SELECT NULL,tmp. * FROM tmp' );
$id_record = $dbo -> lastInsertedID ();
2019-05-04 03:29:43 +02:00
$dbo -> query ( 'DROP TEMPORARY TABLE tmp' );
2019-05-03 16:31:58 +02:00
2019-05-04 03:29:43 +02:00
$dbo -> query ( 'UPDATE my_impianti SET matricola = CONCAT (matricola, " (copia)") WHERE id = ' . prepare ( $id_record ));
2019-05-03 16:31:58 +02:00
flash () -> info ( tr ( 'Impianto duplicato correttamente!' ));
2019-05-04 03:29:43 +02:00
2019-05-03 16:31:58 +02:00
break ;
2024-01-15 15:30:45 +01:00
// Rimuovo impianto e scollego tutti i suoi componenti
2017-08-04 16:28:16 +02:00
case 'delete' :
$dbo -> query ( 'DELETE FROM my_impianti WHERE id=' . prepare ( $id_record ));
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Impianto e relativi componenti eliminati!' ));
2017-08-04 16:28:16 +02:00
break ;
2023-10-27 15:09:49 +02:00
case 'sync_checklist' :
2023-10-27 15:23:06 +02:00
Check :: deleteLinked ([
'id_module' => $id_module ,
'id_record' => $id_record ,
]);
2023-10-27 15:09:49 +02:00
2024-03-05 16:01:45 +01:00
$checks_categoria = $dbo -> fetchArray ( 'SELECT * FROM zz_checks WHERE id_module = ' . prepare ( $id_modulo_categorie_impianti ) . ' AND id_record = ' . prepare ( post ( 'id_categoria' )));
2023-10-27 15:09:49 +02:00
foreach ( $checks_categoria as $check_categoria ) {
2023-10-30 13:12:24 +01:00
$id_parent_new = null ;
2023-11-02 17:11:07 +01:00
if ( $check_categoria [ 'id_parent' ]) {
$parent = $dbo -> selectOne ( 'zz_checks' , '*' , [ 'id' => $check_categoria [ 'id_parent' ]]);
$id_parent_new = $dbo -> selectOne ( 'zz_checks' , '*' , [ 'content' => $parent [ 'content' ], 'id_module' => $id_module , 'id_record' => $id_record ])[ 'id' ];
}
2023-10-30 13:12:24 +01:00
$check = Check :: build ( $user , $structure , $id_record , $check_categoria [ 'content' ], $id_parent_new , $check_categoria [ 'is_titolo' ], $check_categoria [ 'order' ]);
2023-10-27 15:09:49 +02:00
$check -> id_plugin = null ;
$check -> note = $check_categoria [ 'note' ];
$check -> save ();
}
flash () -> info ( tr ( 'Checklist importate correttamente!' ));
break ;
2017-08-04 16:28:16 +02:00
}
2018-07-10 16:39:02 +02:00
// Operazioni aggiuntive per l'immagine
2021-03-04 15:24:20 +01:00
if ( filter ( 'op' ) == 'rimuovi-allegato' && filter ( 'filename' ) == $record [ 'immagine' ]) {
2018-07-10 16:39:02 +02:00
$dbo -> update ( 'my_impianti' , [
'immagine' => null ,
], [
'id' => $id_record ,
]);
2021-03-04 15:24:20 +01:00
} elseif ( filter ( 'op' ) == 'aggiungi-allegato' && filter ( 'nome_allegato' ) == 'Immagine' ) {
2018-07-10 16:39:02 +02:00
$dbo -> update ( 'my_impianti' , [
2021-03-15 09:50:02 +01:00
'immagine' => $upload -> filename ,
2018-07-10 16:39:02 +02:00
], [
'id' => $id_record ,
]);
}