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 ;
2020-06-25 15:00:23 +02:00
$options [ 'label' ] = isset ( $options [ 'label' ]) ? $options [ 'label' ] : tr ( '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 >
2019-07-15 09:34:14 +02:00
< div class = " panel-body " >< div id = " loading_'. $attachment_id .' " class = " text-center hide " style = " position:relative;top:100px;z-index:2;opacity:0.5; " >< i class = " fa fa-refresh fa-spin fa-3x fa-fw " ></ i >< span class = " sr-only " > '.tr(' Caricamento ... ').' </ span ></ div > ' ;
2017-08-04 16:28:16 +02:00
}
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
2019-03-19 18:07:02 +01:00
$categories = $dbo -> fetchArray ( 'SELECT DISTINCT(BINARY `category`) AS `category` FROM `zz_files` WHERE ' . $where . ' ORDER BY `category`' );
2018-07-02 15:41:38 +02:00
foreach ( $categories as $category ) {
$category = $category [ 'category' ];
2017-08-04 16:28:16 +02:00
2019-03-19 18:10:10 +01:00
$rs = $dbo -> fetchArray ( 'SELECT * FROM `zz_files` WHERE BINARY `category`' . ( ! empty ( $category ) ? '= ' . prepare ( $category ) : 'IS NULL' ) . ' AND `id_record` = ' . prepare ( $options [ 'id_record' ]) . ' AND ' . $where );
2018-07-04 13:07:30 +02:00
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 >
2020-06-30 13:25:21 +02:00
2019-07-31 11:52:13 +02:00
{[ " type " : " text " , " class " : " hide category-name " , " value " : " '. $category .' " ]}
2020-06-30 13:25:21 +02:00
2019-07-31 11:52:13 +02:00
< div class = " box-tools pull-right " > ' ;
if ( ! empty ( $category )) {
$result .= '
< button type = " button " class = " btn btn-box-tool category-save hide " >
< i class = " fa fa-check " ></ i >
</ button >
2020-06-30 13:25:21 +02:00
2019-07-31 11:52:13 +02:00
< button type = " button " class = " btn btn-box-tool category-edit " >
< i class = " fa fa-edit " ></ i >
</ button > ' ;
}
$result .= '
2018-07-02 15:41:38 +02:00
< button type = " button " class = " btn btn-box-tool " data - widget = " collapse " >
< i class = " fa fa-minus " ></ i >
</ button >
</ div >
</ div >
2019-03-19 19:23:07 +01:00
< div class = " box-body no-padding table-responsive " >
< table class = " table table-striped table-condensed " >
< thead >
2017-08-04 16:28:16 +02:00
< tr >
2019-03-19 19:23:07 +01:00
< th scope = " col " > '.tr(' Nome ').' </ th >
< th scope = " col " width = " 15% " > '.tr(' Data ').' </ th >
2019-07-31 11:52:13 +02:00
< th scope = " col " width = " 10% " class = " text-center " > #</th>
2019-03-19 19:23:07 +01:00
</ tr >
</ thead >
< tbody > ' ;
2017-08-04 16:28:16 +02:00
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 .= '
2020-06-25 16:48:50 +02:00
< tr id = " row_'. $r['id'] .' " >
2019-07-31 11:52:13 +02:00
< td align = " left " > ' ;
if ( $file -> user && $file -> user -> photo ) {
$result .= '
< img class = " attachment-img tip " src = " '. $file->user ->photo.' " title = " '. $file->user ->nome_completo.' " > ' ;
} else {
$result .= '
2020-06-30 13:25:21 +02:00
2019-07-31 11:52:13 +02:00
< i class = " fa fa-user-circle-o attachment-img tip " title = " '.tr('OpenSTAManager').' " ></ i > ' ;
}
$result .= '
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-07-31 11:52:13 +02:00
</ a >
2020-06-30 13:25:21 +02:00
2020-02-05 14:51:39 +01:00
< small > ( '.$file->extension.' ) '.((!empty($file->size)) ? ' ( '.\Util\FileSystem::formatBytes($file->size).' ) ' : ' ').' '.(($r[' name '] == ' Logo stampe ' or $r[' name '] == ' Filigrana stampe ') ? ' < i class = " fa fa-file-text-o " ></ i > ' : ' ').' </ small > '.'
2017-08-04 16:28:16 +02:00
</ td >
2020-06-30 13:25:21 +02:00
2017-08-04 16:28:16 +02:00
< td > '.\Translator::timestampToLocale($r[' created_at ']).' </ td >
2020-06-30 13:25:21 +02:00
2019-07-31 11:52:13 +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 .= '
2020-02-05 12:40:44 +01:00
< button class = " btn btn-xs btn-info " 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 .= '
2019-05-30 17:26:34 +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 - before = " show_'. $attachment_id .' " 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 .= '
2019-03-19 19:23:07 +01:00
</ tbody >
</ 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 " >
2020-06-25 15:00:23 +02:00
< div class = " col-md-6 " >
{[ " type " : " text " , " placeholder " : " '.tr('Nome file').' " , " name " : " nome_allegato " , " class " : " unblockable " ]}
</ div >
< div class = " col-md-6 " >
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 >
2020-06-17 17:44:16 +02:00
< div class = " col-md-12 " >
< div class = " dropzone dz-clickable " id = " dragdrop " >
2020-06-30 13:25:21 +02:00
2020-06-17 17:44:16 +02:00
</ div >
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' ));
2020-06-25 15:00:23 +02:00
$upload_max_filesize = \Util\FileSystem :: formatBytes ( ini_get ( 'upload_max_filesize' ), 0 );
//remove unit
$upload_max_filesize = substr ( $upload_max_filesize , 0 , strrpos ( $upload_max_filesize , ' ' ));
2018-07-02 15:41:38 +02:00
$result .= '
2019-07-26 17:40:52 +02:00
< script > $ ( document ) . ready ( init ) </ script >
2018-07-02 15:41:38 +02:00
< script >
2020-06-17 17:44:16 +02:00
2020-06-25 16:48:50 +02:00
// Disabling autoDiscover, otherwise Dropzone will try to attach twice.
2020-06-17 17:44:16 +02:00
Dropzone . autoDiscover = false ;
2020-06-25 16:48:50 +02:00
$ ( document ) . ready ( function () {
2020-06-30 13:25:21 +02:00
var dragdrop = new Dropzone ( " #'. $attachment_id .' .dropzone " , {
dictDefaultMessage : " '.tr('Clicca o trascina qui per caricare uno o più file').'.<br>('.tr('Max upload: _SIZE_', [
2020-06-30 13:26:15 +02:00
'_SIZE_' => $upload_max_filesize . ' MB' ,
2020-06-30 13:25:21 +02:00
]) . ' ) " ,
2020-06-25 15:00:23 +02:00
paramName : " file " ,
maxFilesize : '.$upload_max_filesize.' , // MB
2020-06-25 16:48:50 +02:00
uploadMultiple : false ,
parallelUploads : 2 ,
2020-06-25 15:00:23 +02:00
addRemoveLinks : false ,
2020-06-25 16:48:50 +02:00
autoProcessQueue : true ,
autoQueue : true ,
2020-06-25 15:00:23 +02:00
url : " '.ROOTDIR.'/actions.php?op=link_file&id_module='. $options['id_module'] .'&id_record='. $options['id_record'] .'&id_plugin='. $options['id_plugin'] .' " ,
init : function ( file , xhr , formData ) {
this . on ( " sending " , function ( file , xhr , formData ) {
2020-06-30 13:25:21 +02:00
formData . append ( " categoria " , $ ( " #categoria " ) . val ());
2020-06-25 16:48:50 +02:00
formData . append ( " nome_allegato " , $ ( " #nome_allegato " ) . val ());
2020-06-30 13:25:21 +02:00
});
2020-06-25 15:00:23 +02:00
this . on ( " success " , function ( file ) {
dragdrop . removeFile ( file );
2020-06-30 13:25:21 +02:00
});
2020-06-25 15:00:23 +02:00
this . on ( " complete " , function ( file ) {
2020-06-30 13:25:21 +02:00
// Ricarico solo quando ho finito
2020-06-25 16:48:50 +02:00
if ( this . getUploadingFiles () . length === 0 && this . getQueuedFiles () . length === 0 ) {
reload_ '.$attachment_id.' ();
}
2020-06-25 15:00:23 +02:00
});
}
2020-06-30 13:25:21 +02:00
});
2020-06-25 16:48:50 +02:00
2019-07-31 11:52:13 +02:00
// Modifica categoria
$ ( " #'. $attachment_id .' .category-edit " ) . click ( function () {
var nome = $ ( this ) . parent () . parent () . find ( " .box-title " );
var save_button = $ ( this ) . parent () . find ( " .category-save " );
var input = $ ( this ) . parent () . parent () . find ( " .category-name " );
2020-06-30 13:25:21 +02:00
2019-07-31 11:52:13 +02:00
nome . hide ();
$ ( this ) . hide ();
2020-06-30 13:25:21 +02:00
2019-07-31 11:52:13 +02:00
input . removeClass ( " hide " );
save_button . removeClass ( " hide " );
});
2020-06-30 13:25:21 +02:00
2019-07-31 11:52:13 +02:00
$ ( " #'. $attachment_id .' .category-save " ) . click ( function () {
var nome = $ ( this ) . parent () . parent () . find ( " .box-title " );
var input = $ ( this ) . parent () . parent () . find ( " .category-name " );
2020-06-30 13:25:21 +02:00
2019-07-31 11:52:13 +02:00
show_ '.$attachment_id.' ();
$ . ajax ({
url : globals . rootdir + " /actions.php " ,
cache : false ,
type : " POST " ,
data : {
id_module : " '. $options['id_module'] .' " ,
id_plugin : " '. $options['id_plugin'] .' " ,
id_record : " '. $options['id_record'] .' " ,
op : " upload_category " ,
category : nome . text (),
name : input . val (),
},
success : function ( data ) {
reload_ '.$attachment_id.' ();
},
error : function ( data ) {
reload_ '.$attachment_id.' ();
}
});
});
2020-06-30 13:25:21 +02:00
2020-03-03 22:59:18 +01:00
function getFilenameAndExtension ( pathfilename ){
var filenameextension = pathfilename . replace ( /^.* [ \\\ / ] / , \ ' \ ' );
var filename = filenameextension . substring ( 0 , filenameextension . lastIndexOf ( \ ' . \ ' ));
var ext = filenameextension . split ( \ ' . \ ' ) . pop ();
2020-06-30 13:25:21 +02:00
2020-03-03 22:59:18 +01:00
return [ filename , ext ];
2020-06-30 13:25:21 +02:00
2020-03-03 22:59:18 +01:00
}
2019-07-31 11:52:13 +02:00
// Autocompletamento nome
2019-07-10 17:36:27 +02:00
$ ( " #'. $attachment_id .' #blob " ) . change ( function (){
var nome = $ ( " #'. $attachment_id .' #nome_allegato " );
2020-06-30 13:25:21 +02:00
2019-07-10 17:36:27 +02:00
if ( ! nome . val ()) {
var fullPath = $ ( this ) . val ();
2020-06-30 13:25:21 +02:00
2020-03-03 22:59:18 +01:00
var filename = getFilenameAndExtension ( fullPath );
2020-06-30 13:25:21 +02:00
2020-03-03 22:59:18 +01:00
nome . val ( filename [ 0 ]);
2019-07-10 17:36:27 +02:00
}
2019-07-31 11:52:13 +02:00
});
2020-06-30 13:25:21 +02:00
2019-07-31 11:52:13 +02:00
// Autocompletamento categoria
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
2019-07-31 11:52:13 +02:00
var data = {
2018-07-02 15:41:38 +02:00
op : " link_file " ,
id_module : " '. $options['id_module'] .' " ,
id_plugin : " '. $options['id_plugin'] .' " ,
id_record : " '. $options['id_record'] .' " ,
};
2019-07-31 11:52:13 +02:00
// Upload
2018-07-02 15:41:38 +02:00
$ ( " #'. $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
2019-05-30 17:26:34 +02:00
function show_ '.$attachment_id.' () {
$ ( " #loading_'. $attachment_id .' " ) . removeClass ( " hide " );
}
2018-08-31 18:06:44 +02:00
function reload_ '.$attachment_id.' () {
2019-05-30 17:26:34 +02:00
$ ( " #'. $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'] .' " , function () {
2020-06-25 16:48:50 +02:00
2019-05-30 17:26:34 +02:00
$ ( " #loading_'. $attachment_id .' " ) . addClass ( " hide " );
2020-06-30 13:25:21 +02:00
2020-06-25 16:48:50 +02:00
var id = $ ( " #'. $attachment_id .' table tr " ) . eq ( - 1 ) . attr ( " id " );
if ( id !== undefined )
$ ( " # " + id ) . effect ( " highlight " , {}, 1500 );
2020-06-30 13:25:21 +02:00
2019-05-30 17:26:34 +02:00
});
2018-08-31 18:06:44 +02:00
}
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
}