2018-10-18 19:36:19 +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 />.
*/
2018-10-18 19:36:19 +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 ;
2024-03-27 11:07:15 +01:00
use Modules\Impianti\Categoria ;
2023-10-27 15:09:49 +02:00
2024-03-27 16:59:46 +01:00
$modulo_impianti = ( new Module ()) -> getByField ( 'name' , 'Impianti' , Models\Locale :: getPredefined () -> id );
2023-10-27 15:09:49 +02:00
2018-10-18 19:36:19 +02:00
switch ( filter ( 'op' )) {
case 'update' :
$nome = filter ( 'nome' );
$nota = filter ( 'nota' );
$colore = filter ( 'colore' );
2023-11-08 10:58:11 +01:00
$id_original = filter ( 'id_original' ) ? : null ;
2018-10-18 19:36:19 +02:00
if ( isset ( $nome ) && isset ( $nota ) && isset ( $colore )) {
2024-03-27 11:07:15 +01:00
$categoria -> nota = $nota ;
$categoria -> colore = $colore ;
$categoria -> parent = $id_original ? : null ;
$categoria -> setTranslation ( 'name' , $nome );
$categoria -> save ();
2023-11-08 10:58:11 +01:00
2018-10-18 19:36:19 +02:00
flash () -> info ( tr ( 'Salvataggio completato!' ));
} else {
flash () -> error ( tr ( 'Ci sono stati alcuni errori durante il salvataggio!' ));
}
2023-11-08 10:58:11 +01:00
// Redirect alla categoria se si sta modificando una sottocategoria
if ( $id_original != null ) {
$database -> commitTransaction ();
redirect ( base_path () . '/editor.php?id_module=' . $id_module . '&id_record=' . ( $id_original ? : $id_record ));
2024-01-15 15:30:45 +01:00
exit ;
2023-11-08 10:58:11 +01:00
}
2018-10-18 19:36:19 +02:00
break ;
case 'add' :
2023-11-08 10:58:11 +01:00
$nome = filter ( 'nome' );
$nota = filter ( 'nota' );
$colore = filter ( 'colore' );
2018-10-18 19:36:19 +02:00
2023-11-08 10:58:11 +01:00
$id_original = filter ( 'id_original' ) ? : null ;
2018-10-18 19:36:19 +02:00
2024-03-27 11:07:15 +01:00
$categoria_new = Categoria :: where ( 'id' , '=' , ( new Categoria ()) -> getByField ( 'name' , $nome ));
2023-11-08 10:58:11 +01:00
if ( ! empty ( $id_original )) {
2024-03-27 11:07:15 +01:00
$categoria_new = $categoria_new -> where ( 'parent' , '=' , $id_original );
2023-11-08 10:58:11 +01:00
} else {
2024-03-27 11:07:15 +01:00
$categoria_new = $categoria_new -> whereNull ( 'parent' );
2023-11-08 10:58:11 +01:00
}
2024-03-27 11:07:15 +01:00
$categoria_new = $categoria_new -> first ();
if ( ! empty ( $categoria_new )) {
flash () -> error ( tr ( 'Questo nome è già stato utilizzato per un altra categoria.' ));
} else {
$categoria = Categoria :: build ( $nota , $colore );
$id_record = $dbo -> lastInsertedID ();
$categoria -> parent = $id_original ;
$categoria -> setTranslation ( 'name' , $nome );
$categoria -> save ();
2023-11-08 10:58:11 +01:00
flash () -> info ( tr ( 'Aggiunta nuova tipologia di _TYPE_' , [
'_TYPE_' => 'categoria' ,
2018-10-18 19:36:19 +02:00
]));
2023-11-08 10:58:11 +01:00
}
2023-10-30 14:18:26 +01:00
2023-11-08 10:58:11 +01:00
if ( isAjaxRequest ()) {
echo json_encode ([ 'id' => $id_record , 'text' => $nome ]);
} else {
// Redirect alla categoria se si sta aggiungendo una sottocategoria
$database -> commitTransaction ();
redirect ( base_path () . '/editor.php?id_module=' . $id_module . '&id_record=' . ( $id_original ? : $id_record ));
2024-01-15 15:30:45 +01:00
exit ;
2023-10-30 14:18:26 +01:00
}
2023-11-08 10:58:11 +01:00
2018-10-18 19:36:19 +02:00
break ;
case 'delete' :
$id = filter ( 'id' );
if ( empty ( $id )) {
$id = $id_record ;
}
2024-04-04 11:47:36 +02:00
if ( empty ( $dbo -> fetchArray ( 'SELECT * FROM `my_impianti` WHERE (`id_categoria`=' . prepare ( $id ) . ' OR `id_sottocategoria`=' . prepare ( $id ) . ' OR `id_sottocategoria` IN (SELECT `id` FROM `my_impianti_categorie` WHERE `parent`=' . prepare ( $id ) . '))' ))) {
2018-10-18 19:36:19 +02:00
$dbo -> query ( 'DELETE FROM `my_impianti_categorie` WHERE `id`=' . prepare ( $id ));
2023-11-08 10:58:11 +01:00
flash () -> info ( tr ( '_TYPE_ eliminata con successo!' , [
2018-10-18 19:36:19 +02:00
'_TYPE_' => 'categoria' ,
]));
} else {
2023-11-08 10:58:11 +01:00
flash () -> error ( tr ( 'Esistono alcuni impianti collegati a questa categoria. Impossibile eliminarla.' ));
2018-10-18 19:36:19 +02:00
}
2023-10-27 15:09:49 +02:00
break ;
case 'sync_checklist' :
2024-03-25 18:24:38 +01:00
// Azzeramento checklist impianti della categoria
database () -> query ( 'DELETE FROM `zz_checks` WHERE `id_module` = ' . prepare ( $modulo_impianti [ 'id' ]) . ' AND `id_record` IN(SELECT `id` FROM `my_impianti` WHERE `id_categoria` = ' . prepare ( $id_record ) . ')' );
2024-03-27 16:59:46 +01:00
2023-10-27 15:09:49 +02:00
$checks_categoria = $dbo -> fetchArray ( 'SELECT * FROM zz_checks WHERE id_module = ' . prepare ( $id_module ) . ' AND id_record = ' . prepare ( $id_record ));
$impianti = $dbo -> select ( 'my_impianti' , '*' , [], [ 'id_categoria' => $id_record ]);
foreach ( $impianti as $impianto ) {
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' => $modulo_impianti [ 'id' ], 'id_record' => $impianto [ 'id' ]])[ 'id' ];
}
$check = Check :: build ( $user , $structure , $impianto [ 'id' ], $check_categoria [ 'content' ], $id_parent_new , $check_categoria [ 'is_titolo' ], $check_categoria [ 'order' ]);
2023-10-27 15:09:49 +02:00
$check -> id_module = $modulo_impianti [ 'id' ];
$check -> id_plugin = null ;
$check -> note = $check_categoria [ 'note' ];
$check -> save ();
}
}
flash () -> info ( tr ( 'Impianti sincronizzati correttamente!' ));
2018-10-18 19:36:19 +02:00
break ;
}