2024-04-17 11:21:05 +02:00
< ? php
/*
* OpenSTAManager : il software gestionale open source per l ' assistenza tecnica e la fatturazione
* Copyright ( C ) DevCode s . r . l .
*
* 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 />.
*/
include_once __DIR__ . '/../../core.php' ;
2024-05-09 16:57:11 +02:00
use Models\Module ;
use Modules\Impianti\Marca ;
$id_modulo_marca_impianti = ( new Module ()) -> getByField ( 'title' , 'Marche Impianti' , Models\Locale :: getPredefined () -> id );
switch ( filter ( 'op' )) {
2024-04-17 11:21:05 +02:00
case 'update' :
2024-05-09 16:57:11 +02:00
$title = filter ( 'title' );
$id_original = filter ( 'id_original' ) ? : 0 ;
2024-04-17 11:21:05 +02:00
2024-05-09 16:57:11 +02:00
if ( isset ( $title )) {
$marca -> parent = $id_original ;
$marca -> setTranslation ( 'title' , $title );
$marca -> save ();
2024-04-17 11:21:05 +02:00
2024-05-09 16:57:11 +02:00
flash () -> info ( tr ( 'Salvataggio completato!' ));
} else {
flash () -> error ( tr ( 'Ci sono stati alcuni errori durante il salvataggio!' ));
2024-04-17 11:21:05 +02:00
}
2024-05-09 16:57:11 +02:00
// Redirect alla marca se si sta modificando una sottomarca
if ( ! empty ( $id_original )) {
$database -> commitTransaction ();
redirect ( base_path () . '/editor.php?id_module=' . $id_module . '&id_record=' . ( $id_original ? : $id_record ));
exit ;
2024-04-17 11:21:05 +02:00
}
break ;
case 'add' :
2024-05-09 16:57:11 +02:00
$title = filter ( 'title' );
$id_original = filter ( 'id_original' ) ? : null ;
2024-04-17 11:21:05 +02:00
2024-05-09 16:57:11 +02:00
$marca_new = Marca :: where ( 'id' , '=' , ( new Marca ()) -> getByField ( 'title' , $title ));
if ( ! empty ( $id_original )) {
$marca_new = $marca_new -> where ( 'parent' , '=' , $id_original );
2024-04-17 11:21:05 +02:00
} else {
2024-05-09 16:57:11 +02:00
$marca_new = $marca_new -> whereNull ( 'parent' );
}
$marca_new = $marca_new -> first ();
2024-04-17 11:21:05 +02:00
2024-05-09 16:57:11 +02:00
if ( ! empty ( $marca_new )) {
flash () -> error ( tr ( 'Questo nome è già stato utilizzato per un altra marca.' ));
} else {
$marca = Marca :: build ();
2024-04-17 11:21:05 +02:00
$id_record = $dbo -> lastInsertedID ();
2024-05-09 16:57:11 +02:00
$marca -> parent = $id_original ? : 0 ;
$marca -> setTranslation ( 'title' , $title );
$marca -> save ();
2024-04-17 11:21:05 +02:00
2024-05-09 16:57:11 +02:00
flash () -> info ( tr ( 'Aggiunta nuova tipologia di _TYPE_' , [
'_TYPE_' => 'marca' ,
]));
}
2024-04-17 11:21:05 +02:00
2024-05-09 16:57:11 +02:00
if ( isAjaxRequest ()) {
echo json_encode ([ 'id' => $id_record , 'text' => $title ]);
} else {
// Redirect alla marca se si sta aggiungendo un modello
$database -> commitTransaction ();
redirect ( base_path () . '/editor.php?id_module=' . $id_module . '&id_record=' . ( $id_original ? : $id_record ));
exit ;
2024-04-17 11:21:05 +02:00
}
break ;
case 'delete' :
2024-05-09 16:57:11 +02:00
$id = filter ( 'id' );
if ( empty ( $id )) {
$id = $id_record ;
}
2024-04-17 11:21:05 +02:00
2024-05-09 16:57:11 +02:00
if ( empty ( $dbo -> fetchArray ( 'SELECT * FROM `my_impianti` WHERE (`id_marca`=' . prepare ( $id ) . ' OR `id_modello`=' . prepare ( $id ) . ' OR `id_modello` IN (SELECT `id` FROM `my_impianti_marche` WHERE `parent`=' . prepare ( $id ) . '))' ))) {
$dbo -> query ( 'DELETE FROM `my_impianti_marche` WHERE `id`=' . prepare ( $id ));
flash () -> info ( tr ( '_TYPE_ eliminata con successo!' , [
'_TYPE_' => 'marca' ,
]));
} else {
flash () -> error ( tr ( 'Esistono alcuni impianti collegati a questa marca. Impossibile eliminarla.' ));
}
2024-04-17 11:21:05 +02:00
break ;
}