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
* Copyright ( C ) DevCode s . n . c .
*
* 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
2020-09-09 15:12:02 +02:00
use Modules\Interventi\Intervento ;
use Util\Ini ;
2017-08-04 16:28:16 +02:00
include_once __DIR__ . '/../../../core.php' ;
switch ( filter ( 'op' )) {
2020-09-09 15:12:02 +02:00
case 'modifica_componente' :
2017-08-04 16:28:16 +02:00
$idcomponente = get ( 'id' );
$data = post ( 'data_componente' );
// Ricavo il valore di contenuto leggendolo dal db
$query = 'SELECT * FROM my_impianto_componenti WHERE idimpianto=' . prepare ( $id_record ) . ' AND id=' . prepare ( $idcomponente );
$rs = $dbo -> fetchArray ( $query );
$contenuto = $rs [ 0 ][ 'contenuto' ];
2020-09-09 15:12:02 +02:00
$contenuto = Ini :: write ( $contenuto , $post );
2017-08-04 16:28:16 +02:00
$query = 'UPDATE my_impianto_componenti SET data=' . prepare ( $data ) . ', contenuto=' . prepare ( $contenuto ) . ' WHERE idimpianto=' . prepare ( $id_record ) . ' AND id=' . prepare ( $idcomponente );
$dbo -> query ( $query );
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Informazioni componente aggiornate correttamente!' ));
2017-08-04 16:28:16 +02:00
$_SESSION [ 'idcomponente' ] = $idcomponente ;
break ;
2020-09-09 15:12:02 +02:00
case 'aggiunta_componente' :
2017-08-04 16:28:16 +02:00
$filename = get ( 'filename' );
if ( ! empty ( $filename )) {
2020-09-09 15:12:02 +02:00
$contenuto = file_get_contents ( DOCROOT . '/files/my_impianti/' . $filename );
$nome = Ini :: getValue ( Ini :: readFile ( DOCROOT . '/files/my_impianti/' . $filename ), 'Nome' );
2017-08-04 16:28:16 +02:00
$query = 'INSERT INTO my_impianto_componenti(filename, idimpianto, contenuto, nome, data) VALUES(' . prepare ( $filename ) . ', ' . prepare ( $id_record ) . ', ' . prepare ( $contenuto ) . ', ' . prepare ( $nome ) . ', NOW())' ;
$dbo -> query ( $query );
$idcomponente = $dbo -> lastInsertedID ();
$_SESSION [ 'idcomponente' ] = $idcomponente ;
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( " Aggiunto un nuovo componente all'impianto! " ));
2017-08-04 16:28:16 +02:00
}
break ;
2020-09-09 15:12:02 +02:00
case 'sostituzione_componente' :
2017-08-04 16:28:16 +02:00
$filename = get ( 'filename' );
$id = get ( 'id' );
2020-09-09 15:12:02 +02:00
$nome = Ini :: getValue ( Ini :: readFile ( DOCROOT . '/files/my_impianti/' . $filename ), 'Nome' );
$contenuto = file_get_contents ( DOCROOT . '/files/my_impianti/' . $filename );
2017-08-04 16:28:16 +02:00
// Verifico che questo componente non sia già stato sostituito
$query = 'SELECT * FROM my_impianto_componenti WHERE idsostituto = ' . prepare ( $id );
$rs = $dbo -> fetchArray ( $query );
if ( empty ( $rs )) {
// Inserisco il nuovo componente in sostituzione
$query = 'INSERT INTO my_impianto_componenti(idsostituto, filename, idimpianto, contenuto, nome, data) VALUES(' . prepare ( $id ) . ', ' . prepare ( $filename ) . ', ' . prepare ( $id_record ) . ', ' . prepare ( $contenuto ) . ', ' . prepare ( $nome ) . ', NOW())' ;
$dbo -> query ( $query );
$idcomponente = $dbo -> lastInsertedID ();
$_SESSION [ 'idcomponente' ] = $idcomponente ;
// Aggiorno la data di sostituzione del componente precedente
$query = 'UPDATE my_impianto_componenti SET data_sostituzione = NOW() WHERE idimpianto = ' . prepare ( $id_record ) . ' AND id = ' . prepare ( $id );
$dbo -> query ( $query );
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Aggiunto un nuovo componente in sostituzione al precedente!' ));
2017-08-04 16:28:16 +02:00
} else {
2018-07-19 17:29:21 +02:00
flash () -> error ( tr ( 'Questo componente è già stato sostituito!' ) . ' ' . ( 'Nessuna modifica applicata' ));
2017-08-04 16:28:16 +02:00
}
break ;
2020-09-09 15:12:02 +02:00
case 'unaggiunta_componente' :
2017-08-04 16:28:16 +02:00
$idcomponente = filter ( 'id' );
$query = 'DELETE FROM my_impianto_componenti WHERE id=' . prepare ( $idcomponente ) . ' AND idimpianto=' . prepare ( $id_record );
$dbo -> query ( $query );
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( " Rimosso componente dall'impianto! " ));
2017-08-04 16:28:16 +02:00
break ;
}
// Componenti non ancora collegati
2018-06-23 18:35:08 +02:00
if ( empty ( $id_list )) {
2017-08-04 16:28:16 +02:00
$id_list = '0' ;
}
echo '
< div class = " box " >
< div class = " box-header with-border " >
2017-09-04 12:02:29 +02:00
< h3 class = " box-title " > '.tr(' Componenti installati ').' </ h3 >
2017-08-04 16:28:16 +02:00
</ div >
< div class = " box-body " > ' ;
// Elenca i componenti disponibili
2020-09-09 15:12:02 +02:00
$componenti_disponibili = Ini :: getList ( DOCROOT . '/files/my_impianti/' , $id_list );
2017-08-04 16:28:16 +02:00
echo '
< div class = " row " >
2019-05-16 19:36:57 +02:00
< div class = " col-md-9 " >
2017-08-04 16:28:16 +02:00
< select class = " superselect " id = " filename " name = " filename " > ' ;
2020-09-09 15:12:02 +02:00
if ( ! empty ( $componenti_disponibili )) {
2017-08-04 16:28:16 +02:00
echo '
2017-09-04 12:02:29 +02:00
< option value = " 0 " >- '.tr(' Aggiungi un componente ').' -</ option > ' ;
2020-09-09 15:12:02 +02:00
foreach ( $componenti_disponibili as $componente ) {
2017-08-04 16:28:16 +02:00
echo '
2020-09-09 15:12:02 +02:00
< option value = " '. $componente[0] .' " > '.$componente[1].' </ option > ' ;
2017-08-04 16:28:16 +02:00
}
} else {
echo '
2017-09-04 12:02:29 +02:00
< option value = " 0 " >- '.tr(' Hai già aggiunto tutti i componenti ').' -</ option > ' ;
2017-08-04 16:28:16 +02:00
}
echo '
</ select >
</ div >
2020-09-09 15:12:02 +02:00
< div class = " col-md-3 " >
< button type = " button " class = " btn btn-primary btn-block " onclick = " aggiungiComponente() " >
< i class = " fa fa-plus " ></ i > '.tr(' Aggiungi ').'
</ button >
2017-08-04 16:28:16 +02:00
</ div >
</ div >
< div class = " clearfix " ></ div >
< br > ' ;
// Mostro tutti i componenti utilizzati elencando quelli attualmente installati per primi.
2020-09-09 15:12:02 +02:00
$q2 = 'SELECT * FROM my_impianto_componenti WHERE idimpianto = ' . prepare ( $id_record ) . ' ORDER by nome ASC, idsostituto DESC' ;
$componenti_installati = $dbo -> fetchArray ( $q2 );
2017-08-04 16:28:16 +02:00
2020-09-09 15:12:02 +02:00
if ( ! empty ( $componenti_installati )) {
2017-08-04 16:28:16 +02:00
$prev_componente = '' ;
echo '
< div class = " panel-group " id = " accordion " > ' ;
// Ciclo tra tutti i componenti
2020-09-09 15:12:02 +02:00
foreach ( $componenti_installati as $componente ) {
$contenuto = $componente [ 'contenuto' ];
2017-08-04 16:28:16 +02:00
2020-09-09 15:12:02 +02:00
$nome_componente = $componente [ 'nome' ];
$filename = $componente [ 'filename' ];
2017-08-04 16:28:16 +02:00
2020-09-09 15:12:02 +02:00
if ( empty ( $componente [ 'data_sostituzione' ])) {
$stato_componente = tr ( 'INSTALLATO in data _DATE_' , [
'_DATE_' => dateFormat ( $componente [ 'data' ]),
2017-09-10 14:35:41 +02:00
]);
2017-08-04 16:28:16 +02:00
} else {
2020-09-09 15:12:02 +02:00
$stato_componente = tr ( 'SOSTITUITO in data _DATE_' , [
'_DATE_' => dateFormat ( $componente [ 'data_sostituzione' ]),
2017-09-10 14:35:41 +02:00
]);
2017-08-04 16:28:16 +02:00
}
// Per più "versioni" dello stesso componente mostro un riga meno evidente
// per non confonderlo come componente in uso in questo momento
$same = ( $prev_componente == $nome_componente );
2019-11-22 16:28:03 +01:00
2020-09-09 15:12:02 +02:00
if ( get ( 'id' ) == $componente [ 'id' ]) {
2019-11-21 00:01:08 +01:00
$collapsed = '' ;
$icon = 'minus' ;
2020-09-09 15:12:02 +02:00
} elseif ( $_SESSION [ 'idcomponente' ] == $componente [ 'id' ]) {
2017-08-04 16:28:16 +02:00
unset ( $_SESSION [ 'idcomponente' ]);
2019-11-21 00:01:08 +01:00
$collapsed = '' ;
$icon = 'minus' ;
2017-08-04 16:28:16 +02:00
} else {
2019-11-21 00:01:08 +01:00
$collapsed = 'collapsed-box' ;
$icon = 'plus' ;
2017-08-04 16:28:16 +02:00
}
echo '
2019-11-21 00:01:08 +01:00
< div class = " box '. $collapsed .' box-'.( $same ? 'default' : 'primary').' " >
< div class = " box-header with-border'.( $same ? ' mini' : '').' " >
< h3 class = " box-title'.( $same ? ' mini' : '').' " > ' .
2020-09-09 15:12:02 +02:00
( $same ? '<small>' : '' ) . $nome_componente . ' (' . $stato_componente . ')' . ( $same ? '</small>' : '' ) . '
2019-11-21 00:01:08 +01:00
</ h3 >
< div class = " box-tools pull-right " >
< button type = " button " class = " btn btn-box-tool " data - widget = " collapse " >
< i class = " fa fa-'. $icon .' " ></ i >
</ button >
</ div >
</ div > ' ;
echo '
< div id = " collapse_'. $j .' " class = " box-body " >
2020-09-09 15:12:02 +02:00
< div class = " row " >
< form method = " post " action = " '.ROOTDIR.'/editor.php?id_module='. $id_module .'&id_record='. $id_record .'&op=modifica_componente&id='. $componente['id'] .' " >
2017-08-04 16:28:16 +02:00
< input type = " hidden " name = " backto " value = " record-edit " > ' ;
// Nome
echo '
< div class = " col-md-6 " >
2020-09-09 15:12:02 +02:00
{[ " type " : " span " , " label " : " '.tr('Nome').': " , " name " : " nome " , " value " : " '. $componente['nome'] .' " ]}
2017-08-04 16:28:16 +02:00
</ div > ' ;
// Data
echo '
< div class = " col-md-6 " >
2020-09-09 15:12:02 +02:00
{[ " type " : " date " , " label " : " '.tr('Data').': " , " name " : " data_componente " , " id " : " data_componente'. $j .' " , " value " : " '. $componente['data'] .' " ]}
2017-08-04 16:28:16 +02:00
</ div > ' ;
2020-09-09 15:12:02 +02:00
// Campi previsti dal componente
$fields = Ini :: getFields ( $contenuto );
2017-08-04 16:28:16 +02:00
array_shift ( $fields );
foreach ( $fields as $field ) {
echo '
< div class = " col-md-6 " >
'.$field.'
</ div > ' ;
}
2020-09-09 15:12:02 +02:00
// Interventi collegati al componente
$interventi_collegati = Intervento :: join ( 'my_componenti_interventi' , 'my_componenti_interventi.id_intervento' , '=' , 'in_interventi.id' )
-> where ( 'id_componente' , $componente [ 'id' ])
-> get ();
if ( ! $interventi_collegati -> isEmpty ()) {
2017-08-04 16:28:16 +02:00
echo '
< div class = " col-md-12 " >
2017-09-04 12:02:29 +02:00
< b > '.tr(' Interventi collegati ').' :</ b >
2017-08-04 16:28:16 +02:00
< table class = " table table-condensed " >
< tr >
2017-09-04 12:02:29 +02:00
< th > '.tr(' Codice ').' </ th >
< th > '.tr(' Tipo ').' </ th >
< th > '.tr(' Stato ').' </ th >
< th > '.tr(' Data richiesta ').' </ th >
< th > '.tr(' Dettagli ').' </ th >
2017-08-04 16:28:16 +02:00
</ tr > ' ;
2020-09-09 15:12:02 +02:00
foreach ( $interventi_collegati as $intervento ) {
2017-08-04 16:28:16 +02:00
echo '
2020-09-09 15:12:02 +02:00
< tr bgcolor = " '. $intervento->stato ->colore.' " >
< td > '.$intervento->codice.' </ td >
< td > '.$intervento->tipo->descrizione.' </ td >
< td > '.$intervento->stato->descrizione.' </ td >
< td > '.dateFormat($intervento->data_richiesta).' </ td >
< td > '.Modules::link(' Interventi ', $intervento->id, null, ' - ').' </ td >
2017-08-04 16:28:16 +02:00
</ tr > ' ;
}
echo '
</ table >
</ div > ' ;
} else {
echo '
< div class = " clearfix " ></ div >
< div class = " col-md-12 " >
2020-09-09 15:12:02 +02:00
< div class = " alert alert-info " >
< i class = " fa fa-info-circle " ></ i > '.tr(' Nessuna attività collegato a questo componente ! ').'
</ div >
2017-08-04 16:28:16 +02:00
</ div > ' ;
}
2020-09-09 15:12:02 +02:00
// Intervento di installazione del componente
if ( ! empty ( $componente [ 'idintervento' ])) {
$intervento_origine = Intervento :: find ( $componente [ 'idintervento' ]);
2017-08-04 16:28:16 +02:00
echo '
2020-09-09 15:12:02 +02:00
'.Modules::link(' Interventi ', $componente[' idintervento '], $intervento_origine->getReference()).' < br > ' ;
2017-08-04 16:28:16 +02:00
}
echo '
< div class = " clearfix " ></ div >
< br > ' ;
// Pulsante Salva/Elimina
echo '
2020-09-07 15:04:06 +02:00
< div class = " col-md-12 " >
2020-09-09 15:12:02 +02:00
< a class = " btn btn-danger ask " data - backto = " record-edit " data - op = " unaggiunta_componente " data - id = " '. $componente['id'] .' " >< i class = " fa fa-trash " ></ i > '.tr(' Elimina ').' </ a > ' ;
2019-05-17 05:56:10 +02:00
// Sostituisci componente con un altro dello stesso tipo, posso sostituire solo i componenti installati
2020-09-09 15:12:02 +02:00
if ( empty ( $componente [ 'data_sostituzione' ])) {
echo '
< button type = " button " class = " btn btn-warning " onclick = " sostituisciComponente() " >
< i class = " fa fa-refresh " ></ i > '.tr(' Sostituisci questo componente ').'
</ button > ' ;
2018-09-03 16:49:43 +02:00
} else {
echo '
2020-09-09 15:12:02 +02:00
< button type = " button " class = " btn btn-warning disabled " >
'.tr(' Componente già sostituito ').'
</ button > ' ;
2018-09-03 16:49:43 +02:00
}
2019-05-17 05:56:10 +02:00
echo '
2019-05-16 19:36:57 +02:00
< button type = " submit " class = " btn btn-success pull-right " >< i class = " fa fa-check " ></ i > '.tr(' Salva modifiche ').' </ button > ' ;
2017-08-04 16:28:16 +02:00
2018-09-03 16:49:43 +02:00
echo '
2017-08-04 16:28:16 +02:00
</ div >
</ form >
</ div >
</ div >
</ div > ' ;
$prev_componente = $nome_componente ;
}
echo '
</ div > ' ;
} else {
echo '
2018-08-28 17:10:23 +02:00
< div class = \ 'alert alert-info\' ><i class=\'fa fa-info-circle\'></i> ' . tr ( 'Nessun componente inserito' ) . '.</div>' ;
2017-08-04 16:28:16 +02:00
}
echo '
</ div >
2020-09-09 15:12:02 +02:00
</ div >
< script >
function aggiungiComponente () {
if ( $ ( " #filename " ) . val () != " 0 " ) {
redirect ( " '.ROOTDIR.'/editor.php?id_module='. $id_module .'&id_record='. $id_record .'&op=aggiunta_componente&backto=record-edit&filename= " + $ ( " #filename " ) . val () + " &hash=tab_'. $id_plugin .' " );
} else {
alert ( " '.tr('Seleziona prima un componente').' " );
$ ( " #filename " ) . focus ();
}
}
function sostituisciComponente () {
if ( confirm ( " '.tr('Vuoi sostituire questo componente con un altro dello stesso tipo?').' " )){
redirect ( " '.ROOTDIR.'/editor.php?id_module='. $id_module .'&id_record='. $id_record .'&op=sostituzione_componente&backto=record-edit&filename='. $filename .'&id='. $componente['id'] .' " );
}
}
</ script > ' ;