2017-08-04 16:28:16 +02:00
< ? php
namespace HTMLBuilder\Manager ;
/**
2018-05-22 17:17:08 +02:00
* Gestione allegati .
*
2017-08-04 16:28:16 +02:00
* @ since 2.3
*/
class FileManager implements ManagerInterface
{
2018-05-22 17:17:08 +02:00
/**
* Gestione " filelist_and_upload " .
* Esempio : {( " name " : " filelist_and_upload " , " id_module " : " 2 " , " id_record " : " 1 " , " readonly " : " false " , " ajax " : " true " )} .
*
* @ param array $options
*
* @ return string
*/
2017-08-04 16:28:16 +02:00
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-23 18:05:49 +02:00
$query = 'SELECT * FROM zz_files WHERE id_record = ' . prepare ( $options [ 'id_record' ]) . ' AND id_module' . (( ! empty ( $options [ 'id_module' ])) ? ' = ' . prepare ( $options [ 'id_module' ]) : ' IS NULL' ) . ' AND id_plugin' . (( ! empty ( $options [ 'id_plugin' ])) ? ' = ' . prepare ( $options [ 'id_plugin' ]) : ' IS NULL' ) . '' ;
2018-05-22 18:37:51 +02:00
$rs = $dbo -> fetchArray ( $query );
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-22 17:17:08 +02:00
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 > ' ;
2018-05-22 17:17:08 +02:00
//Anteprime supportate dal browser
2018-05-24 14:32:34 +02:00
$extension = strtolower ( end (( explode ( '.' , $r [ 'original' ]))));
2018-05-22 17:17:08 +02:00
$supported_extensions = [ 'pdf' , 'jpg' , 'png' , 'gif' , 'jpeg' , 'bmp' ];
if ( in_array ( $extension , $supported_extensions )){
2018-05-22 18:54:43 +02:00
$result .= " <div class='hide' id='view- " . $r [ 'id' ] . " ' > " ;
if ( $extension == 'pdf' ){
$result .= "
< iframe src = \ " " . ROOTDIR . '/files/' . \Modules :: get ( $options [ 'id_module' ])[ 'directory' ] . '/' . $r [ 'filename' ] . " \" frameborder= \" 0 \" scrolling= \" no \" width= \" 100% \" height= \" 550 \" ></iframe> " ;
} else {
$result .= "
< img src = \ " " . ROOTDIR . '/files/' . \Modules :: get ( $options [ 'id_module' ])[ 'directory' ] . '/' . $r [ 'filename' ] . " \" width= \" 100% \" ></img> " ;
}
$result .= " </div> " ;
$result .= " <button class= \" btn btn-sm btn-info \" data-target= \" #bs-popup \" type= \" button \" data-title= \" " . htmlentities ( $r [ 'nome' ], ENT_QUOTES , " UTF-8 " ) . " <small><em>( " . $r [ 'filename' ] . " )</em></small> \" data-href= \" #view- " . $r [ 'id' ] . " \" ><i class='fa fa-eye'></i></button> " ;
2018-05-22 17:17:08 +02:00
} else {
2018-05-22 18:54:43 +02:00
$result .= " <button class= \" btn btn-sm btn-default \" title= \" " . tr ( 'Anteprima file non disponibile' ) . " . \" onclick= \" alert(' " . tr ( 'Anteprima file di tipo "' . $extension . '" non supportata.' ) . " '); \" ><i class='fa fa-eye'></i></button> \n " ;
2018-05-22 17:17:08 +02:00
}
2018-05-17 19:30:44 +02:00
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
}