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-07 16:04:52 +01:00
use Modules\Articoli\Categoria ;
2017-08-04 16:28:16 +02:00
switch ( filter ( 'op' )) {
case 'update' :
$nome = filter ( 'nome' );
$nota = filter ( 'nota' );
$colore = filter ( 'colore' );
2021-04-01 12:02:24 +02:00
$id_original = filter ( 'id_original' ) ? : null ;
2017-08-04 16:28:16 +02:00
if ( isset ( $nome ) && isset ( $nota ) && isset ( $colore )) {
2024-03-07 16:04:52 +01:00
$categoria -> nota = $nota ;
$categoria -> colore = $colore ;
$categoria -> parent = $id_original ? : null ;
2024-03-20 11:13:28 +01:00
$categoria -> setTranslation ( 'name' , $nome );
2024-03-07 16:04:52 +01:00
$categoria -> save ();
2024-03-01 09:08:08 +01:00
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Salvataggio completato!' ));
2017-08-04 16:28:16 +02:00
} else {
2018-07-19 17:29:21 +02:00
flash () -> error ( tr ( 'Ci sono stati alcuni errori durante il salvataggio!' ));
2017-08-04 16:28:16 +02:00
}
2021-04-01 12:02:24 +02:00
// Redirect alla categoria se si sta modificando una sottocategoria
2023-08-04 14:54:28 +02:00
if ( $id_original != null ) {
2023-01-31 17:48:17 +01:00
$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-01-31 17:48:17 +01:00
}
break ;
2017-08-04 16:28:16 +02:00
case 'add' :
$nome = filter ( 'nome' );
2018-07-07 13:56:22 +02:00
$nota = filter ( 'nota' );
2018-07-04 11:01:09 +02:00
$colore = filter ( 'colore' );
2021-03-01 11:16:10 +01:00
$id_original = filter ( 'id_original' ) ? : null ;
2017-08-04 16:28:16 +02:00
2024-03-22 15:52:24 +01:00
$categoria_new = Categoria :: where ( 'id' , '=' , ( new Categoria ()) -> getByField ( 'name' , $nome ));
2021-03-01 11:16:10 +01:00
if ( ! empty ( $id_original )) {
2024-03-07 16:04:52 +01:00
$categoria_new = $categoria_new -> where ( 'parent' , '=' , $id_original );
2023-03-29 10:00:13 +02:00
} else {
2024-03-07 16:04:52 +01:00
$categoria_new = $categoria_new -> whereNull ( 'parent' );
2021-03-01 11:16:10 +01:00
}
2024-03-07 16:04:52 +01:00
$categoria_new = $categoria_new -> first ();
2021-03-01 11:16:10 +01:00
2024-03-22 15:52:24 +01:00
if ( ! empty ( $categoria_new )) {
2024-03-07 16:04:52 +01:00
flash () -> error ( tr ( 'Questo nome è già stato utilizzato per un altra categoria.' ));
} else {
$categoria = Categoria :: build ( $nota , $colore );
2024-03-22 15:52:24 +01:00
$id_record = $dbo -> lastInsertedID ();
2024-03-07 16:04:52 +01:00
$categoria -> parent = $id_original ;
2024-03-20 11:13:28 +01:00
$categoria -> setTranslation ( 'name' , $nome );
2024-03-07 16:04:52 +01:00
$categoria -> save ();
2024-03-01 09:08:08 +01:00
2021-03-01 11:16:10 +01:00
flash () -> info ( tr ( 'Aggiunta nuova tipologia di _TYPE_' , [
'_TYPE_' => 'categoria' ,
]));
}
2024-03-22 15:52:24 +01:00
2021-03-01 11:16:10 +01:00
if ( isAjaxRequest ()) {
echo json_encode ([ 'id' => $id_record , 'text' => $nome ]);
2021-04-01 20:19:27 +02:00
} else {
2021-04-01 12:02:24 +02:00
// 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 ;
2017-08-04 16:28:16 +02:00
}
break ;
case 'delete' :
$id = filter ( 'id' );
if ( empty ( $id )) {
$id = $id_record ;
}
2024-02-29 15:10:55 +01:00
if ( $dbo -> fetchNum ( 'SELECT * FROM `mg_articoli` WHERE (`id_categoria`=' . prepare ( $id ) . ' OR `id_sottocategoria`=' . prepare ( $id ) . ' OR `id_sottocategoria` IN (SELECT `id` FROM `mg_categorie` WHERE `parent`=' . prepare ( $id ) . ')) AND `deleted_at` IS NULL' ) == 0 ) {
2017-08-04 16:28:16 +02:00
$dbo -> query ( 'DELETE FROM `mg_categorie` WHERE `id`=' . prepare ( $id ));
2018-07-07 13:56:22 +02:00
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Tipologia di _TYPE_ eliminata con successo!' , [
2017-09-10 14:35:41 +02:00
'_TYPE_' => 'categoria' ,
2018-07-07 13:56:22 +02:00
]));
2017-08-04 16:28:16 +02:00
} else {
2020-03-18 22:24:18 +01:00
flash () -> error ( tr ( 'Esistono alcuni articoli collegati a questa categoria. Impossibile eliminarla.' ));
2017-08-04 16:28:16 +02:00
}
break ;
}