2017-08-04 16:28:16 +02:00
< ? php
namespace HTMLBuilder\Manager ;
2019-01-17 16:11:10 +01:00
use Models\Upload ;
2017-08-04 16:28:16 +02:00
/**
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-06-23 15:41:32 +02:00
/**
* Gestione " filelist_and_upload " .
2018-07-02 15:41:38 +02:00
* Esempio : {( " name " : " filelist_and_upload " , " id_module " : " 2 " , " id_record " : " 1 " , " readonly " : " false " )} .
2018-05-22 17:17:08 +02:00
*
* @ param array $options
*
* @ return string
*/
2017-08-04 16:28:16 +02:00
public function manage ( $options )
{
2018-06-23 15:41:32 +02:00
$options [ 'readonly' ] = ! empty ( $options [ 'readonly' ]) ? true : 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
2018-07-19 12:47:28 +02:00
$options [ 'id_plugin' ] = ! empty ( $options [ 'id_plugin' ]) ? $options [ 'id_plugin' ] : null ;
2018-07-02 15:41:38 +02:00
// ID del form
2018-08-31 18:06:44 +02:00
$attachment_id = 'attachments_' . $options [ 'id_module' ] . '_' . $options [ 'id_plugin' ];
2018-07-02 15:41:38 +02:00
2018-09-20 12:05:22 +02:00
$dbo = database ();
2017-08-04 16:28:16 +02:00
2018-07-02 15:41:38 +02:00
// Codice HTML
2018-06-23 15:41:32 +02:00
$result = '
2018-07-02 15:41:38 +02:00
< div id = " '. $attachment_id .' " > ' ;
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 " > ' ;
}
2018-07-02 15:41:38 +02:00
$count = 0 ;
2018-08-31 11:39:38 +02:00
$where = '`id_module` ' . ( ! empty ( $options [ 'id_module' ]) && empty ( $options [ 'id_plugin' ]) ? '= ' . prepare ( $options [ 'id_module' ]) : 'IS NULL' ) . ' AND `id_plugin` ' . ( ! empty ( $options [ 'id_plugin' ]) ? '= ' . prepare ( $options [ 'id_plugin' ]) : 'IS NULL' ) . '' ;
2018-05-23 18:05:49 +02:00
2018-07-02 15:41:38 +02:00
// Categorie
$categories = $dbo -> fetchArray ( 'SELECT DISTINCT `category` FROM `zz_files` WHERE ' . $where . ' ORDER BY `category`' );
foreach ( $categories as $category ) {
$category = $category [ 'category' ];
2017-08-04 16:28:16 +02:00
2018-07-04 13:07:30 +02:00
$rs = $dbo -> fetchArray ( 'SELECT * FROM `zz_files` WHERE `category`' . ( ! empty ( $category ) ? '= ' . prepare ( $category ) : 'IS NULL' ) . ' AND `id_record` = ' . prepare ( $options [ 'id_record' ]) . ' AND ' . $where );
if ( ! empty ( $rs )) {
$result .= '
2018-07-02 15:41:38 +02:00
< div class = " box box-success " >
< div class = " box-header with-border " >
< h3 class = " box-title " > '.(!empty($category) ? $category : tr(' Generale ')).' </ h3 >
< div class = " box-tools pull-right " >
< button type = " button " class = " btn btn-box-tool " data - widget = " collapse " >
< i class = " fa fa-minus " ></ i >
</ button >
</ div >
</ div >
< div class = " box-body no-padding " >
< table class = " table " >
2017-08-04 16:28:16 +02:00
< tr >
2017-09-04 12:02:29 +02:00
< th > '.tr(' Nome ').' </ th >
< th > '.tr(' Data ').' </ th >
2018-07-02 15:41:38 +02:00
< th width = " 15% " class = " text-center " > '.tr(' Opzioni ').' </ th >
2017-08-04 16:28:16 +02:00
</ tr > ' ;
2018-07-04 13:07:30 +02:00
foreach ( $rs as $r ) {
2019-01-17 16:11:10 +01:00
$file = Upload :: find ( $r [ 'id' ]);
2018-07-04 13:07:30 +02:00
$result .= '
2017-08-04 16:28:16 +02:00
< tr >
< td align = " left " >
2019-03-07 17:21:13 +01:00
< a href = " '.ROOTDIR.'/view.php?file_id='. $r['id'] .' " target = " _blank " >
2018-08-31 11:39:38 +02:00
< i class = " fa fa-external-link " ></ i > '.$r[' name '].'
2019-01-17 16:11:10 +01:00
</ a > ( '.$file->extension.' ) '.'
2017-08-04 16:28:16 +02:00
</ td >
< td > '.\Translator::timestampToLocale($r[' created_at ']).' </ td >
2017-08-28 09:49:38 +02:00
< td class = " text-center " >
2018-07-09 13:03:33 +02:00
< a class = " btn btn-xs 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-06-23 15:41:32 +02:00
2018-07-04 13:07:30 +02:00
// Anteprime supportate dal browser
2019-01-17 16:11:10 +01:00
if ( $file -> hasPreview ()) {
2018-07-04 13:07:30 +02:00
$result .= '
2019-01-17 16:11:10 +01:00
< button class = " btn btn-xs btn-info " data - target = " #bs-popup2 " type = " button " data - title = " '.prepareToField( $r['name'] ).' <small style= \ 'color:white \ '><i>('. $r['filename'] .')</i></small> " data - href = " '.ROOTDIR.'/view.php?file_id='. $r['id'] .' " >
2018-07-02 15:41:38 +02:00
< i class = " fa fa-eye " ></ i >
</ button > ' ;
2018-07-04 13:07:30 +02:00
} else {
$result .= '
2018-08-31 09:41:43 +02:00
< button class = " btn btn-xs btn-default disabled " title = " '.tr('Anteprima file non disponibile').' " disabled >
2018-07-02 15:41:38 +02:00
< i class = " fa fa-eye " ></ i >
</ button > ' ;
2018-07-04 13:07:30 +02:00
}
2018-06-23 15:41:32 +02:00
2018-07-04 13:07:30 +02:00
if ( ! $options [ 'readonly' ]) {
$result .= '
2018-09-28 18:11:42 +02:00
< a class = " btn btn-xs btn-danger ask " data - backto = " record-edit " data - msg = " '.tr('Vuoi eliminare questo file?').' " data - op = " unlink_file " data - filename = " '. $r['filename'] .' " data - id_record = " '. $r['id_record'] .' " data - id_plugin = " '. $options['id_plugin'] .' " data - callback = " reload_'. $attachment_id .' " >
2017-08-04 16:28:16 +02:00
< i class = " fa fa-trash " ></ i >
2018-05-17 19:30:44 +02:00
</ a > ' ;
2018-07-04 13:07:30 +02:00
}
2018-06-23 15:41:32 +02:00
2018-07-04 13:07:30 +02:00
$result .= '
2017-08-04 16:28:16 +02:00
</ td >
</ tr > ' ;
2018-07-02 15:41:38 +02:00
2018-07-04 13:07:30 +02:00
++ $count ;
}
2017-08-04 16:28:16 +02:00
2018-07-04 13:07:30 +02:00
$result .= '
2017-08-04 16:28:16 +02:00
</ table >
2018-07-02 15:41:38 +02:00
</ div >
</ div >
< div class = " clearfix " ></ div >
< br > ' ;
2018-07-04 13:07:30 +02:00
}
2018-06-23 15:41:32 +02:00
}
2017-08-04 16:28:16 +02:00
2018-07-02 15:41:38 +02:00
// Form per l'upload di un nuovo file
2018-06-23 15:41:32 +02:00
if ( ! $options [ 'readonly' ]) {
$result .= '
2017-08-04 16:28:16 +02:00
< b > '.$options[' label '].' </ b >
2018-07-02 15:41:38 +02:00
< div id = " upload-form " class = " row " >
< div class = " col-md-4 " >
2018-07-09 16:12:27 +02:00
{[ " type " : " text " , " placeholder " : " '.tr('Nome').' " , " name " : " nome_allegato " , " class " : " unblockable " ]}
2018-07-02 15:41:38 +02:00
</ div >
2018-06-23 15:41:32 +02:00
2018-07-02 15:41:38 +02:00
< div class = " col-md-3 " >
2018-07-09 16:12:27 +02:00
{[ " type " : " text " , " placeholder " : " '.tr('Categoria').' " , " name " : " categoria " , " class " : " unblockable " ]}
2017-08-04 16:28:16 +02:00
</ div >
2018-07-02 15:41:38 +02:00
< div class = " col-md-3 " >
2018-07-09 16:12:27 +02:00
{[ " type " : " file " , " placeholder " : " '.tr('File').' " , " name " : " blob " , " class " : " unblockable " ]}
2018-07-02 15:41:38 +02:00
</ div >
2018-06-23 15:41:32 +02:00
2018-07-02 15:41:38 +02:00
< div class = " col-md-2 text-right " >
< button type = " button " class = " btn btn-success " id = " upload " >
2018-05-17 19:30:44 +02:00
< i class = " fa fa-upload " ></ i > '.tr(' Carica ').'
</ button >
2018-07-02 15:41:38 +02:00
</ div >
2017-08-04 16:28:16 +02:00
</ div > ' ;
2018-06-23 15:41:32 +02:00
}
2018-07-02 15:41:38 +02:00
// In caso di readonly, se non è stato caricato nessun allegato mostro almeno box informativo
elseif ( $count == 0 ) {
2018-06-23 15:41:32 +02:00
$result .= '
2018-07-02 15:41:38 +02:00
< 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
}
if ( ! empty ( $options [ 'showpanel' ])) {
$result .= '
</ div >
2018-05-16 11:03:58 +02:00
</ div >
2017-08-04 16:28:16 +02:00
</ div > ' ;
}
2018-10-31 17:46:59 +01:00
$source = array_clean ( array_column ( $categories , 'category' ));
2018-07-02 15:41:38 +02:00
$result .= '
< script src = " '.ROOTDIR.'/lib/init.js " ></ script >
< script >
$ ( document ) . ready ( function (){
2018-10-31 16:22:19 +01:00
$ ( " #'. $attachment_id .' #categoria " ) . autocomplete ({
2018-10-31 17:46:59 +01:00
source : '.json_encode($source).' ,
2018-07-02 17:15:25 +02:00
minLength : 0
}) . focus ( function () {
$ ( this ) . autocomplete ( " search " , $ ( this ) . val ())
2018-09-20 12:05:22 +02:00
});
2018-07-02 17:15:25 +02:00
2018-07-02 15:41:38 +02:00
data = {
op : " link_file " ,
id_module : " '. $options['id_module'] .' " ,
id_plugin : " '. $options['id_plugin'] .' " ,
id_record : " '. $options['id_record'] .' " ,
};
$ ( " #'. $attachment_id .' #upload " ) . click ( function (){
2018-09-27 12:54:57 +02:00
$form = $ ( " #'. $attachment_id .' #upload-form " );
if ( $form . find ( " input[name=nome_allegato] " ) . val () == " " || $form . find ( " input[name=blob] " ) . val () == " " ) {
swal ({
type : " error " ,
title : " '.tr('Errore').' " ,
text : " '.tr('Alcuni campi obbligatori non sono stati compilati correttamente.').' " ,
});
return ;
}
$form . ajaxSubmit ({
2018-07-02 15:41:38 +02:00
url : globals . rootdir + " /actions.php " ,
data : data ,
type : " post " ,
uploadProgress : function ( event , position , total , percentComplete ) {
2018-07-02 15:50:07 +02:00
$ ( " #'. $attachment_id .' #upload " ) . prop ( " disabled " , true ) . html ( percentComplete + " % " ) . removeClass ( " btn-success " ) . addClass ( " btn-info " );
2018-07-02 15:41:38 +02:00
},
success : function ( data ){
2018-08-31 18:06:44 +02:00
reload_ '.$attachment_id.' ();
2018-07-02 15:41:38 +02:00
},
error : function ( data ) {
alert ( " '.tr('Errore').': " + data );
}
});
});
});
2018-08-31 18:06:44 +02:00
function reload_ '.$attachment_id.' () {
$ ( " #'. $attachment_id .' " ) . load ( globals . rootdir + " /ajax.php?op=list_attachments&id_module='. $options['id_module'] .'&id_record='. $options['id_record'] .'&id_plugin='. $options['id_plugin'] .' " );
}
2018-07-02 15:41:38 +02:00
</ script > ' ;
2017-08-04 16:28:16 +02:00
return $result ;
}
2018-06-23 15:41:32 +02:00
}