openstamanager/src/HTMLBuilder/Manager/FileManager.php

234 lines
8.2 KiB
PHP
Raw Normal View History

<?php
namespace HTMLBuilder\Manager;
2019-01-17 16:11:10 +01:00
use Models\Upload;
/**
* 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;
$options['label'] = isset($options['label']) ? $options['label'] : tr('Nuovo allegato').':';
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'];
2018-09-20 12:05:22 +02:00
$dbo = database();
// Codice HTML
2018-06-23 15:41:32 +02:00
$result = '
<div id="'.$attachment_id.'" >';
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>
<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-responsive">
<table class="table table-striped table-condensed ">
<thead>
<tr>
<th scope="col" >'.tr('Nome').'</th>
<th scope="col" width="15%" >'.tr('Data').'</th>
<th scope="col" width="15%" class="text-right"></th>
</tr>
</thead>
<tbody>';
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>
<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-04-04 17:30:58 +02:00
</a><small> ('.$file->extension.')'.((!empty($file->size)) ? ' ('.\Util\FileSystem::formatBytes($file->size).')' : '').'</small>'.'
</td>
<td>'.\Translator::timestampToLocale($r['created_at']).'</td>
<td class="text-right">
<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">
<i class="fa fa-download"></i>
</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'].'">
<i class="fa fa-eye"></i>
</button>';
2018-07-04 13:07:30 +02:00
} else {
$result .= '
<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 .= '
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.'">
<i class="fa fa-trash"></i>
</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 .= '
</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>
<br>';
2018-07-04 13:07:30 +02:00
}
2018-06-23 15:41:32 +02:00
}
// Form per l'upload di un nuovo file
2018-06-23 15:41:32 +02:00
if (!$options['readonly']) {
$result .= '
<b>'.$options['label'].'</b>
<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" ]}
</div>
2018-06-23 15:41:32 +02:00
<div class="col-md-3">
2018-07-09 16:12:27 +02:00
{[ "type": "text", "placeholder": "'.tr('Categoria').'", "name": "categoria", "class": "unblockable" ]}
</div>
<div class="col-md-3">
2018-07-09 16:12:27 +02:00
{[ "type": "file", "placeholder": "'.tr('File').'", "name": "blob", "class": "unblockable" ]}
</div>
2018-06-23 15:41:32 +02:00
<div class="col-md-2 text-right">
<button type="button" class="btn btn-success" id="upload">
<i class="fa fa-upload"></i> '.tr('Carica').'
</button>
</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 .= '
<script src="'.ROOTDIR.'/lib/init.js"></script>
<script>
$(document).ready(function(){
$("#'.$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
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({
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");
},
success: function(data){
2018-08-31 18:06:44 +02:00
reload_'.$attachment_id.'();
},
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'].'");
}
</script>';
return $result;
}
2018-06-23 15:41:32 +02:00
}