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
2021-01-20 15:08:51 +01:00
* Copyright ( C ) DevCode s . r . l .
2020-09-07 15:04:06 +02:00
*
* 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
namespace HTMLBuilder\Manager ;
2021-03-10 12:20:04 +01:00
use Models\Setting ;
2021-03-15 16:39:32 +01:00
use Models\Upload ;
2019-01-17 16:11:10 +01:00
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-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 >
2020-09-14 10:22:04 +02:00
< div class = " panel-body " > ' ;
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 " > ' ;
2021-03-08 09:48:22 +01:00
if ( ! empty ( $category ) && ! in_array ( $category , [ 'Fattura Elettronica' ])) {
2019-07-31 11:52:13 +02:00
$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 .= '
2020-09-23 17:53:19 +02:00
< a href = " '.base_path().'/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
2021-03-10 12:20:04 +01:00
< small > ( '.$file->extension.' ) '.((!empty($file->size)) ? ' ( '.\Util\FileSystem::formatBytes($file->size).' ) ' : ' ').' '.(((setting(' Logo stampe ') == $r[' filename ']) || (setting(' Filigrana stampe ') == $r[' filename '])) ? ' < 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 " >
2021-03-04 15:24:20 +01:00
< a class = " btn btn-xs btn-primary " href = " '.base_path().'/actions.php?id_module='. $options['id_module'] .'&op=download-allegato&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-09-23 17:53:19 +02: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 = " '.base_path().'/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 .= '
2021-03-10 12:27:54 +01:00
< button type = " button " class = " btn btn-xs btn-warning " data - href = " '.base_path().'/actions.php?op=visualizza-modifica-allegato&id_module='. $options['id_module'] .'&id_allegato='. $r['id'] .' " data - title = " '.tr('Modifica allegato').' " >
2021-03-04 15:24:20 +01:00
< i class = " fa fa-edit " ></ i >
</ button >
< a class = " btn btn-xs btn-danger ask " data - backto = " record-edit " data - msg = " '.tr('Vuoi eliminare questo file?').' " data - op = " rimuovi-allegato " 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 .= '
2018-07-02 15:41:38 +02:00
< div id = " upload-form " class = " row " >
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-09-22 20:28:37 +02:00
$upload_max_filesize = ini_get ( 'upload_max_filesize' );
$upload_max_filesize = substr ( $upload_max_filesize , 0 , - 1 );
2020-06-25 15:00:23 +02:00
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-11-20 16:31:16 +01:00
let dropzone_id = " #'. $attachment_id .' .dropzone " ;
if ( $ ( dropzone_id ) . length == 0 ) {
return ;
}
let dragdrop = new Dropzone ( dropzone_id , {
2020-06-30 13:25:21 +02:00
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 ,
2021-03-04 15:24:20 +01:00
url : " '.base_path().'/actions.php?op=aggiungi-allegato&id_module='. $options['id_module'] .'&id_record='. $options['id_record'] .'&id_plugin='. $options['id_plugin'] .' " ,
2020-06-25 15:00:23 +02:00
init : function ( file , xhr , formData ) {
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'] .' " ,
2021-03-04 15:24:20 +01:00
op : " modifica-categoria-allegato " ,
2019-07-31 11:52:13 +02:00
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-09-14 10:22:04 +02:00
function getFilenameAndExtension ( path ) {
let filename_extension = path . replace ( /^.* [ \\\ / ] / , \ ' \ ' );
let filename = filename_extension . substring ( 0 , filename_extension . lastIndexOf ( \ ' . \ ' ));
let ext = filename_extension . split ( \ ' . \ ' ) . pop ();
2020-06-30 13:25:21 +02:00
2020-03-03 22:59:18 +01:00
return [ filename , ext ];
}
2021-03-04 15:24:20 +01:00
// Auto-completamento categoria
2020-09-23 13:59:50 +02:00
$ ( " #'. $attachment_id .' #categoria_allegato " ) . 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 = {
2021-03-04 15:24:20 +01:00
op : " aggiungi-allegato " ,
2018-07-02 15:41:38 +02:00
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 " );
$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.' () {
2020-09-14 10:22:04 +02:00
localLoading ( $ ( " #'. $attachment_id .' .panel-body " ), true );
2019-05-30 17:26:34 +02:00
}
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-09-14 10:22:04 +02:00
localLoading ( $ ( " #'. $attachment_id .' .panel-body " ), false );
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 " );
2020-09-14 10:22:04 +02:00
if ( id !== undefined ) {
$ ( " # " + id ) . effect ( " highlight " , {}, 1500 );
}
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
}