openstamanager/src/HTMLBuilder/Manager/FileManager.php

340 lines
12 KiB
PHP
Raw Normal View History

<?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/>.
*/
namespace HTMLBuilder\Manager;
use Models\Upload;
use Util\FileSystem;
2019-01-17 16:11:10 +01:00
/**
* Gestione allegati.
*
* @since 2.3
*/
class FileManager implements ManagerInterface
{
2018-06-23 15:41:32 +02:00
/**
* Gestione "filelist_and_upload".
* Esempio: {( "name": "filelist_and_upload", "id_module": "2", "id_record": "1", "readonly": "false" )}.
*
* @param array $options
*
* @return string
*/
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;
2018-07-19 12:47:28 +02:00
$options['id_plugin'] = !empty($options['id_plugin']) ? $options['id_plugin'] : null;
// ID del form
2018-08-31 18:06:44 +02:00
$attachment_id = 'attachments_'.$options['id_module'].'_'.$options['id_plugin'];
2023-08-04 14:54:28 +02:00
if (ini_get('upload_max_filesize') < ini_get('post_max_size')) {
$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')) {
$upload_max_filesize = ini_get('post_max_size');
2023-08-04 14:54:28 +02:00
} else {
$upload_max_filesize = ini_get('upload_max_filesize');
}
$upload_max_filesize = substr($upload_max_filesize, 0, -1);
2018-09-20 12:05:22 +02:00
$dbo = database();
// Codice HTML
2018-06-23 15:41:32 +02:00
$result = '
<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.'">';
if (!empty($options['showpanel'])) {
$result .= '
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">'.tr('Allegati').'</h3>
</div>
<div class="panel-body">';
}
$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
// 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`');
foreach ($categories as $category) {
$category = $category['category'];
$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 .= '
<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
{[ "type": "text", "class": "hidden category-name", "value": "'.$category.'" ]}
2020-06-30 13:25:21 +02:00
<div class="box-tools pull-right">';
if (!empty($category) && !in_array($category, ['Fattura Elettronica'])) {
$result .= '
<button type="button" class="btn btn-box-tool category-save hidden">
<i class="fa fa-check"></i>
</button>
2020-06-30 13:25:21 +02:00
<button type="button" class="btn btn-box-tool category-cancel hidden">
<i class="fa fa-close"></i>
</button>
<button type="button" class="btn btn-box-tool category-edit">
<i class="fa fa-edit"></i>
</button>';
}
$result .= '
<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-responsive">
<table class="table table-striped table-condensed ">
<thead>
<tr>
2023-03-30 14:29:51 +02:00
<th scope="col" width="5%" class="text-center"></th>
<th scope="col" >'.tr('Nome').'</th>
<th scope="col" width="15%" >'.tr('Data').'</th>
<th scope="col" width="10%" class="text-center">#</th>
</tr>
</thead>
2023-03-30 14:29:51 +02:00
<tbody class="files">';
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 .= '
<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>
<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
<i class="fa fa-user-circle-o attachment-img tip" title="'.tr('OpenSTAManager').'"></i>';
}
$result .= '
<a href="'.base_path().'/view.php?file_id='.$file->id.'" target="_blank">
<i class="fa fa-external-link"></i> '.$file->name.'
</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>'.'
</td>
2020-06-30 13:25:21 +02:00
<td>'.timestampFormat($file['created_at']).'</td>
2020-06-30 13:25:21 +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)">
<i class="fa fa-download"></i>
</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 .= '
<button type="button" class="btn btn-xs btn-info" onclick="visualizzaAllegato(this)">
<i class="fa fa-eye"></i>
</button>';
2018-07-04 13:07:30 +02:00
} else {
$result .= '
<button type="button" class="btn btn-xs btn-default disabled" title="'.tr('Anteprima file non disponibile').'" disabled>
<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\')])">
<i class="fa fa-edit"></i>
</button>';
if (!$file->isFatturaElettronica() || $options['abilita_genera']) {
$result .= '
<button type="button" class="btn btn-xs btn-danger" onclick="rimuoviAllegato(this)">
<i class="fa fa-trash"></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
$result .= '
</td>
</tr>';
2018-07-04 13:07:30 +02:00
++$count;
}
2018-07-04 13:07:30 +02:00
$result .= '
</tbody>
</table>
</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
}
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>';
}
// Form per l'upload di un nuovo file
2018-06-23 15:41:32 +02:00
if (!$options['readonly']) {
$result .= '
<div id="upload-form" class="row">
<div class="col-md-12">
<div class="dropzone dz-clickable" id="dragdrop">
2020-06-30 13:25:21 +02:00
</div>
</div>
</div>';
2018-06-23 15:41:32 +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 .= '
<div class="alert alert-info" style="margin-bottom:0px;" >
<i class="fa fa-info-circle"></i>
'.tr('Nessun allegato è stato caricato').'.
</div>';
}
if (!empty($options['showpanel'])) {
$result .= '
</div>
2018-05-16 11:03:58 +02:00
</div>
</div>';
}
2018-10-31 17:46:59 +01:00
$source = array_clean(array_column($categories, 'category'));
$result .= '
2019-07-26 17:40:52 +02:00
<script>$(document).ready(init)</script>
<script>
2020-06-25 16:48:50 +02:00
$(document).ready(function() {
const container = $("#'.$attachment_id.'");
2020-11-20 16:31:16 +01:00
initGestioneAllegati(container);
impostaCategorieAllegatiDisponibili(container, '.json_encode($source).');
});
2020-06-30 13:25:21 +02:00
// Modifica categoria
$("#'.$attachment_id.' .category-edit").click(function() {
const container = $(this).closest(".gestione-allegati");
2020-06-30 13:25:21 +02:00
modificaCategoriaAllegati(container, this);
});
2020-03-03 22:59:18 +01:00
$("#'.$attachment_id.' .category-save").click(function() {
const container = $(this).closest(".gestione-allegati");
salvaCategoriaAllegati(container, this);
});
2018-08-31 18:06:44 +02:00
$("#'.$attachment_id.' .category-cancel").click(function() {
const container = $(this).closest(".gestione-allegati");
ricaricaAllegati(gestione);
});
2020-06-30 13:25:21 +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");
}
});
}
});
</script>';
return $result;
}
2018-06-23 15:41:32 +02:00
}