2017-08-04 16:28:16 +02:00
< ? php
namespace HTMLBuilder\Manager ;
/**
* @ since 2.3
*/
class FileManager implements ManagerInterface
{
public function manage ( $options )
{
2018-05-17 19:30:44 +02:00
$options [ 'readonly' ] = ( $options [ 'readonly' ] == 'true' ) ? true : false ;
2018-05-16 11:03:58 +02:00
$options [ 'ajax' ] = isset ( $options [ 'ajax' ]) ? $options [ 'ajax' ] : false ;
$options [ 'showpanel' ] = isset ( $options [ 'showpanel' ]) ? $options [ 'showpanel' ] : true ;
2017-09-13 13:05:35 +02:00
$options [ 'label' ] = isset ( $options [ 'label' ]) ? $options [ 'label' ] : tr ( 'Nuovo allegato' ) . ':' ;
2017-08-04 16:28:16 +02:00
$dbo = \Database :: getConnection ();
2018-05-16 11:03:58 +02:00
$result .= '
< div id = " attachments_'. $options['id_record'] .((!empty( $options['id_plugin'] )) ? '_'. $options['id_plugin'] : '').' " >
< a name = " attachments_'.rand().' " ></ a > ' ;
2017-08-04 16:28:16 +02:00
if ( ! empty ( $options [ 'showpanel' ])) {
$result .= '
< div class = " panel panel-primary " >
< div class = " panel-heading " >
2017-09-04 12:02:29 +02:00
< h3 class = " panel-title " > '.tr(' Allegati ').' </ h3 >
2017-08-04 16:28:16 +02:00
</ div >
< div class = " panel-body " > ' ;
}
// Visualizzo l'elenco di file già caricati
2018-05-15 19:07:28 +02:00
if ( ! empty ( $options [ 'id_plugin' ]))
$rs = $dbo -> fetchArray ( 'SELECT * FROM zz_files WHERE id_module=' . prepare ( $options [ 'id_module' ]) . ' AND id_record=' . prepare ( $options [ 'id_record' ]) . ' AND id_plugin=' . prepare ( $options [ 'id_plugin' ]));
else
$rs = $dbo -> fetchArray ( 'SELECT * FROM zz_files WHERE id_module=' . prepare ( $options [ 'id_module' ]) . ' AND id_record=' . prepare ( $options [ 'id_record' ]) . ' AND id_plugin = 0' );
2017-08-04 16:28:16 +02:00
if ( ! empty ( $rs )) {
$result .= '
< table class = " table table-condensed table-hover table-bordered " >
< tr >
2017-09-04 12:02:29 +02:00
< th > '.tr(' Nome ').' </ th >
< th > '.tr(' Data ').' </ th >
2018-05-17 19:30:44 +02:00
< th width = " 15% " class = " text-center " > #</th>
2017-08-04 16:28:16 +02:00
</ tr > ' ;
foreach ( $rs as $r ) {
$result .= '
< tr >
< td align = " left " >
2017-09-22 15:19:59 +02:00
< a href = " '.ROOTDIR.'/files/'. \ Modules::get( $options['id_module'] )['directory'].'/'. $r['filename'] .' " target = " _blank " >
2017-08-04 16:28:16 +02:00
< i class = " fa fa-external-link " ></ i > '.$r[' nome '].'
</ a >
</ td >
< td > '.\Translator::timestampToLocale($r[' created_at ']).' </ td >
2017-08-28 09:49:38 +02:00
< td class = " text-center " >
2018-05-17 19:30:44 +02:00
< a class = " btn btn-sm btn-primary " href = " '.ROOTDIR.'/actions.php?id_module='. $options['id_module'] .'&op=download_file&id='. $r['id'] .'&filename='. $r['filename'] .' " target = " _blank " >
2017-08-28 09:49:38 +02:00
< i class = " fa fa-download " ></ i >
2018-05-17 19:30:44 +02:00
</ a > ' ;
if ( ! $options [ 'readonly' ]){
$result .= '
< a class = " btn btn-sm btn-danger ask " data - backto = " record-edit " data - msg = " '.tr('Vuoi eliminare questo file?').' " data - op = " unlink_file " data - id = " '. $r['id'] .' " data - filename = " '. $r['filename'] .' " >
2017-08-04 16:28:16 +02:00
< i class = " fa fa-trash " ></ i >
2018-05-17 19:30:44 +02:00
</ a > ' ;
}
$result .= '
2017-08-04 16:28:16 +02:00
</ td >
</ tr > ' ;
}
$result .= '
</ table >
< div class = " clearfix " ></ div >
< br > ' ;
2018-05-17 19:30:44 +02:00
} else {
//in caso di readonly, se non è stato caricato nessun allegato mostro almeno box informativo
if ( $options [ 'readonly' ]){
$result .= '
< div class = " alert alert-info " style = " margin-bottom:0px; " >
< i class = " fa fa-info-circle " ></ i >
'.tr(' Nessun allegato è stato caricato ', []).' .
</ div > ' ;
}
}
2017-08-04 16:28:16 +02:00
2018-05-17 19:30:44 +02:00
if ( ! $options [ 'readonly' ]){
2017-08-04 16:28:16 +02:00
// Form per l'upload di un nuovo file
$result .= '
< b > '.$options[' label '].' </ b >
< div class = " row " >
2018-05-17 19:30:44 +02:00
2017-08-04 16:28:16 +02:00
< div class = " col-lg-4 " >
2018-05-15 19:07:28 +02:00
{[ " type " : " text " , " placeholder " : " '.tr('Nome').' " , " name " : " nome_allegato " , " id " : " nome_allegato_'. $options['id_record'] .((!empty( $options['id_plugin'] )) ? '_'. $options['id_plugin'] : '').' " ]}
2017-08-04 16:28:16 +02:00
</ div >
< div class = " col-lg-6 " >
2018-05-16 18:14:07 +02:00
{[ " type " : " file " , " placeholder " : " '.tr('File').' " , " name " : " blob " , " id " : " blob_'. $options['id_record'] .((!empty( $options['id_plugin'] )) ? '_'. $options['id_plugin'] : '').' " , " required " : 0 ]}
2018-05-17 19:30:44 +02:00
</ div > ' ;
$result .= '
< div class = " col-lg-2 text-right " >
< button type = " button " class = " btn btn-success " onclick = " saveFile_'. $options['id_record'] .((!empty( $options['id_plugin'] )) ? '_'. $options['id_plugin'] : '').' ( $ (this) ); " >
< i class = " fa fa-upload " ></ i > '.tr(' Carica ').'
</ button >
</ div > ' ;
$result .= '
2017-08-04 16:28:16 +02:00
</ div > ' ;
2018-05-17 19:30:44 +02:00
}
2017-08-04 16:28:16 +02:00
$result .= '
< script >
2018-05-16 00:19:10 +02:00
function saveFile_ '.$options[' id_record '].((!empty($options[' id_plugin '])) ? ' _ '.$options[' id_plugin '] : ' ').' ( btn ){
2018-05-15 19:07:28 +02:00
if ( ! $ ( " #blob_'. $options['id_record'] .((!empty( $options['id_plugin'] )) ? '_'. $options['id_plugin'] : '').' " ) . val ()){
2017-09-13 13:05:35 +02:00
swal ( " '.addslashes(tr('Attenzione!')).' " , " '.addslashes(tr('Devi selezionare un file con il tasto " Sfoglia " ')).'... " , " warning " );
2017-08-04 16:28:16 +02:00
return false ;
}
2018-05-15 19:07:28 +02:00
var file_data = $ ( " #blob_'. $options['id_record'] .((!empty( $options['id_plugin'] )) ? '_'. $options['id_plugin'] : '').' " ) . prop ( " files " )[ 0 ];
2017-08-04 16:28:16 +02:00
var form_data = new FormData ();
form_data . append ( " blob " , file_data );
2018-05-15 19:07:28 +02:00
form_data . append ( " nome_allegato " , $ ( " input[id=nome_allegato_'. $options['id_record'] .((!empty( $options['id_plugin'] )) ? '_'. $options['id_plugin'] : '').'] " ) . val ());
2017-08-04 16:28:16 +02:00
form_data . append ( " op " , " link_file " );
form_data . append ( " id_record " , " '. $options['id_record'] .' " );
form_data . append ( " id_module " , " '. $options['id_module'] .' " );
2018-05-15 19:07:28 +02:00
form_data . append ( " id_plugin " , " '. $options['id_plugin'] .' " );
2017-08-04 16:28:16 +02:00
2018-05-16 00:19:10 +02:00
prev_html = btn . html ();
btn . html ( " <i class= \" fa fa-spinner fa-pulse fa-fw \" ></i>'.tr( " Attendere ... " ).' " );
btn . prop ( " disabled " , true );
2017-08-31 16:23:26 +02:00
2017-08-04 16:28:16 +02:00
$ . ajax ({
url : " '.ROOTDIR.'/actions.php " ,
cache : false ,
type : " post " ,
processData : false ,
contentType : false ,
dataType : " html " ,
data : form_data ,
success : function ( data ) {
2018-05-16 11:03:58 +02:00
2018-05-16 00:19:10 +02:00
btn . html ( prev_html );
2018-05-16 11:03:58 +02:00
btn . prop ( " disabled " , false ); ' ;
if (( $options [ 'ajax' ])) {
2018-05-16 14:55:19 +02:00
$result .= '$("#attachments_' . $options [ 'id_record' ] . (( ! empty ( $options [ 'id_plugin' ])) ? '_' . $options [ 'id_plugin' ] : '' ) . '").load( globals.rootdir + "/ajax.php?op=list_attachments&id_module=' . $options [ 'id_module' ] . '&id_record=' . $options [ 'id_record' ] . (( ! empty ( $options [ 'id_plugin' ])) ? '&id_plugin=' . $options [ 'id_plugin' ] . '#tab_' . $options [ 'id_plugin' ] : '' ) . '" );' ;
2018-05-16 11:03:58 +02:00
} else {
$result .= 'location.href = globals.rootdir + "/editor.php?id_module=' . $options [ 'id_module' ] . '&id_record=' . $options [ 'id_record' ] . (( ! empty ( $options [ 'id_plugin' ])) ? '#tab_' . $options [ 'id_plugin' ] : '' ) . '";' ;
}
$result .= ' },
2017-08-04 16:28:16 +02:00
error : function ( data ) {
alert ( data );
}
})
}
</ script > ' ;
if ( ! empty ( $options [ 'showpanel' ])) {
$result .= '
</ div >
2018-05-16 11:03:58 +02:00
</ div >
2017-08-04 16:28:16 +02:00
</ div > ' ;
}
return $result ;
}
2018-05-16 11:03:58 +02:00
}