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-15 16:39:32 +01:00
use Models\Upload ;
2021-07-20 15:23:39 +02:00
use Util\FileSystem ;
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
2023-08-04 14:54:28 +02:00
if ( ini_get ( 'upload_max_filesize' ) < ini_get ( 'post_max_size' )) {
2023-02-21 18:16:21 +01:00
$upload_max_filesize = ini_get ( 'upload_max_filesize' );
2023-08-04 14:54:28 +02:00
} elseif ( ini_get ( 'upload_max_filesize' ) > ini_get ( 'post_max_size' )) {
2023-02-21 18:16:21 +01:00
$upload_max_filesize = ini_get ( 'post_max_size' );
2023-08-04 14:54:28 +02:00
} else {
2023-02-21 18:16:21 +01:00
$upload_max_filesize = ini_get ( 'upload_max_filesize' );
}
2021-07-20 15:23:39 +02:00
$upload_max_filesize = substr ( $upload_max_filesize , 0 , - 1 );
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 = '
2021-07-20 15:23:39 +02:00
< div class = " gestione-allegati " id = " '. $attachment_id .' " data - id_module = " '. $options['id_module'] .' " data - id_plugin = " '. $options['id_plugin'] .' " data - id_record = " '. $options['id_record'] .' " data - max_filesize = " '. $upload_max_filesize .' " > ' ;
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
2021-07-20 15:23:39 +02:00
{[ " type " : " text " , " class " : " hidden 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 .= '
2021-07-20 15:23:39 +02:00
< button type = " button " class = " btn btn-box-tool category-save hidden " >
2019-07-31 11:52:13 +02:00
< i class = " fa fa-check " ></ i >
</ button >
2020-06-30 13:25:21 +02:00
2021-07-20 15:23:39 +02:00
< button type = " button " class = " btn btn-box-tool category-cancel hidden " >
< i class = " fa fa-close " ></ i >
</ button >
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 >
2023-03-30 14:29:51 +02:00
< th scope = " col " width = " 5% " class = " text-center " ></ th >
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 >
2023-03-30 14:29:51 +02:00
< tbody class = " files " > ' ;
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 .= '
2021-08-03 14:21:29 +02:00
< tr id = " row_'. $file->id .' " data - id = " '. $file->id .' " data - filename = " '. $file->filename .' " data - nome = " '. $file->name .' " >
2023-03-30 16:55:40 +02:00
< td class = " text-center " >
< input class = " check_files unblockable " type = " checkbox " />
2023-03-30 14:29:51 +02:00
</ td >
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 .= '
2021-08-03 14:21:29 +02:00
< a href = " '.base_path().'/view.php?file_id='. $file->id .' " target = " _blank " >
< i class = " fa fa-external-link " ></ i > '.$file->name.'
2019-07-31 11:52:13 +02:00
</ a >
2020-06-30 13:25:21 +02:00
2024-02-13 15:50:26 +01:00
< small > ( '.$file->extension.' ) '.((!empty($file->size)) ? ' ( '.FileSystem::formatBytes($file->size).' ) ' : ' ').' '.(((setting(' Logo stampe ') == $file->filename) || (setting(' Filigrana stampe ') == $file->filename)) ? ' < span class = " tip " title = " '.tr('Logo caricato correttamente').'. " > ✔️ </ span > ' : ' ').' </ small > '.'
2017-08-04 16:28:16 +02:00
</ td >
2020-06-30 13:25:21 +02:00
2021-08-03 14:21:29 +02:00
< td > '.timestampFormat($file[' created_at ']).' </ td >
2020-06-30 13:25:21 +02:00
2019-07-31 11:52:13 +02:00
< td class = " text-center " >
2021-08-06 12:29:51 +02:00
< button type = " button " class = " btn btn-xs btn-primary " onclick = " scaricaAllegato(this) " >
2017-08-28 09:49:38 +02:00
< i class = " fa fa-download " ></ i >
2021-07-20 15:23:39 +02:00
</ button > ' ;
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 .= '
2021-07-20 15:23:39 +02:00
< button type = " button " class = " btn btn-xs btn-info " onclick = " visualizzaAllegato(this) " >
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 .= '
2021-07-20 15:23:39 +02:00
< button type = " 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 .= '
2023-04-17 17:28:17 +02:00
< button type = " button " class = " btn btn-xs btn-warning " onclick = " modificaAllegato(this,[ $ (this).closest( \ 'tr \ ').data( \ 'id \ ')]) " >
2021-03-04 15:24:20 +01:00
< i class = " fa fa-edit " ></ i >
2022-01-13 11:56:12 +01:00
</ button > ' ;
if ( ! $file -> isFatturaElettronica () || $options [ 'abilita_genera' ]) {
$result .= '
2021-07-20 15:23:39 +02:00
< button type = " button " class = " btn btn-xs btn-danger " onclick = " rimuoviAllegato(this) " >
2017-08-04 16:28:16 +02:00
< i class = " fa fa-trash " ></ i >
2021-07-20 15:23:39 +02:00
</ button > ' ;
2022-01-13 11:56:12 +01:00
}
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 >
2023-03-30 14:29:51 +02:00
' ;
2018-07-04 13:07:30 +02:00
}
2018-06-23 15:41:32 +02:00
}
2017-08-04 16:28:16 +02:00
2023-03-30 16:55:40 +02:00
if ( ! empty ( $file )) {
2023-03-30 14:29:51 +02:00
$result .= '
2023-03-30 16:55:40 +02:00
< div class = " btn-group " >
2023-03-30 14:29:51 +02:00
< button type = " button " class = " btn btn-xs btn-default " >
2023-03-30 16:55:40 +02:00
< input class = " pull-left unblockable " id = " check_all_files " type = " checkbox " />
2023-03-30 14:29:51 +02:00
</ button > ' ;
2023-08-04 14:54:28 +02:00
if ( ! $options [ 'readonly' ]) {
$result .= '
2023-04-17 17:28:17 +02:00
< button type = " button " class = " btn btn-xs btn-default disabled " id = " modifica_files " onclick = " modificaAllegato(this,JSON.stringify(getSelectFiles())); " >
2023-03-30 14:29:51 +02:00
< i class = " fa fa-edit " ></ i >
2023-03-30 16:55:40 +02:00
</ button > ' ;
2023-08-04 14:54:28 +02:00
}
$result .= '
2023-04-17 17:28:17 +02:00
< button type = " button " class = " btn btn-xs btn-default disabled " id = " zip_files " onclick = " scaricaZipAllegati(this,JSON.stringify(getSelectFiles())); " >
2023-03-30 14:29:51 +02:00
< i class = " fa fa-file-archive-o " ></ i >
</ button >
</ div > ' ;
}
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' ));
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-25 16:48:50 +02:00
$ ( document ) . ready ( function () {
2021-07-20 15:23:39 +02:00
const container = $ ( " #'. $attachment_id .' " );
2020-11-20 16:31:16 +01:00
2021-07-20 15:23:39 +02:00
initGestioneAllegati ( container );
impostaCategorieAllegatiDisponibili ( container , '.json_encode($source).' );
});
2020-06-30 13:25:21 +02:00
2021-07-20 15:23:39 +02:00
// Modifica categoria
$ ( " #'. $attachment_id .' .category-edit " ) . click ( function () {
const container = $ ( this ) . closest ( " .gestione-allegati " );
2020-06-30 13:25:21 +02:00
2021-07-20 15:23:39 +02:00
modificaCategoriaAllegati ( container , this );
});
2020-03-03 22:59:18 +01:00
2021-07-20 15:23:39 +02:00
$ ( " #'. $attachment_id .' .category-save " ) . click ( function () {
const container = $ ( this ) . closest ( " .gestione-allegati " );
salvaCategoriaAllegati ( container , this );
2018-07-02 15:41:38 +02:00
});
2018-08-31 18:06:44 +02:00
2021-07-20 15:23:39 +02:00
$ ( " #'. $attachment_id .' .category-cancel " ) . click ( function () {
const container = $ ( this ) . closest ( " .gestione-allegati " );
2019-05-30 17:26:34 +02:00
2021-07-20 15:23:39 +02:00
ricaricaAllegati ( gestione );
});
2020-06-30 13:25:21 +02:00
2021-07-20 15:23:39 +02:00
// Upload
$ ( " #'. $attachment_id .' #upload " ) . click ( function (){
const container = $ ( this ) . closest ( " .gestione-allegati " );
aggiungiAllegato ( container );
});
2023-03-30 14:29:51 +02:00
// Estraggo le righe spuntate
2023-04-17 17:28:17 +02:00
function getSelectFiles () {
2023-03-30 14:29:51 +02:00
let data = new Array ();
2023-04-17 17:28:17 +02:00
$ ( " .files " ) . find ( " .check_files:checked " ) . each ( function (){
data . push ( $ ( this ) . closest ( " tr " ) . data ( " id " ));
2023-03-30 14:29:51 +02:00
});
return data ;
}
$ ( " .check_files " ) . on ( " change " , function () {
let checked = 0 ;
$ ( " .check_files " ) . each ( function () {
if ( $ ( this ) . is ( " :checked " )) {
checked = 1 ;
}
});
if ( checked ) {
$ ( " #zip_files " ) . removeClass ( " disabled " );
$ ( " #modifica_files " ) . removeClass ( " disabled " );
} else {
$ ( " #zip_files " ) . addClass ( " disabled " );
$ ( " #modifica_files " ) . addClass ( " disabled " );
}
});
$ ( " #check_all_files " ) . click ( function (){
if ( $ ( this ) . is ( " :checked " ) ){
$ ( " .check_files " ) . each ( function (){
if ( ! $ ( this ) . is ( " :checked " ) ){
$ ( this ) . trigger ( " click " );
}
});
} else {
$ ( " .check_files " ) . each ( function (){
if ( $ ( this ) . is ( " :checked " ) ){
$ ( this ) . trigger ( " click " );
}
});
}
});
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
}