Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

341 righe
16 KiB

<style>
input::placeholder {
text-align: center;
}
</style>
<?php
/*
parametri che si possono passare:
'prefix' --------> aggiunge un prefisso a tutti gli element html e js per evitare conflitti
'input_name_for_attachments' --------> opzionale override degli input name 'attachments'
'input_name_for_removefiles' --------> opzionale override degli input name 'removefiles'
'filepicker' => true/false --------> inserisce il filepicker oppure no
'georeferenced' => true/false --------> stabilisce se ogni singolo allegato va georeferenziato oppure no. (se è true allore upload_single viene forzato anche a true!!!!!)
'upload_single' => true/false --------> decide se l'upload è singolo o se sono consentiti upload multipli da più paths
'accept_only' => [] --------> se si passa questo array, i tipi di files accettati saranno solo quelli specificati
'viewer' => true/false --------> inserisce o meno il file viewer
'coId' --------> controllable object id, necessario se la proprietà precedente 'viewer' è true
'currentFilesRemovable' => true/false --------> decide se è possibile eliminare i files già presenti o meno
'enableRelated' => true/false --------> visualizza anche gli allegati degli oggetti associati (esempio in un evento visualizza anche gli allegati delle observations)
'allow_private' => true/false --------> stabilisce se devono esserci files pubblici e privati o soltanto pubblci
'public_required' => true/false --------> setta allegato pubblico come obbligatorio oppure no
'required' => true/false --------> setta l'allegato come obbligatorio oppure no
'tags' => [] --------> serve a passare uno o + tags (vedi tabella Tags) per creare diverse sezioni di allegati. ogni elemento key=>value dell'array va inserito come code/description della sezione che si vuole creare. esempio: 'tags' => ['code1' => 'Allegati di tipo 1', 'code2' => 'Allegati di tipo 2', 'code3' => 'Allegati di tipo 3']
'view_in_frame_with_id' --------> se settato, viene effettuato il rendering dell'allegato all'interno deill'iframe con id specificato
*/
$prefix = isset($prefix) && is_string($prefix) ? $prefix : '';
$input_name_for_attachments = isset($input_name_for_attachments) && is_string($input_name_for_attachments) ? $input_name_for_attachments : 'attachments';
$input_name_for_removefiles = isset($input_name_for_removefiles) && is_string($input_name_for_removefiles) ? $input_name_for_removefiles : 'removefiles';
$accept = isset($accept_only) && is_array($accept_only) && count($accept_only) > 0 ? ('accept="' . implode(",", $accept_only) . '"') : '';
$acceptText = isset($accept_only) && is_array($accept_only) && count($accept_only) > 0 ? __('di tipo: "{0}"', implode(",", $accept_only)) : '';
$georeferenced = isset($georeferenced) && $georeferenced;
$multiple = $georeferenced || isset($upload_single) && $upload_single ? '': 'multiple="multiple"';
$oneFilePicker = isset($upload_single) && $upload_single;
$public_required = isset($public_required) && $public_required;
$required = isset($required) && $required;
$coRelated = isset($coRelated) && $coRelated;
if (isset($enableRelated) && $enableRelated) {
echo '<label style="padding-right: 10px;padding-left: 0px;">' . __('Mostra allegati correlati') . '</label>';
echo '<input name="' . $prefix . 'showRelatedAttachments" id="' . $prefix . 'showrelatedattachments" type="checkbox" />';
}
?>
<div class="row">
<div class="col-sm-12">
<div class="row">
<?php
if(!isset($allow_private))
{
$attachment_scope = ['' => ($oneFilePicker ? __('Allega un file ') : __('Allega files ')) . $acceptText];
}
else
{
if($allow_private)
{
$attachment_scope = ['public' => __('Allega file pubblico ') . $acceptText, 'private' => ($oneFilePicker ? __('Allega un file privato ') : __('Allega files privati ')) . $acceptText];
}
else
{
$attachment_scope = ['' => __('Allega files...')];
}
}
if(isset($tags)){
$attachment_scope = [];
foreach ($tags as $tag => $description) {
$attachment_scope[$tag] = __('Allega files {1} {0}',$description,$acceptText);
}
$csv_tags = implode(',',array_keys($tags));
}
if (isset($filepicker) && $filepicker) {
$attachment_index = $georeferenced ? '[0]' : '[]';
$col = (12 / count($attachment_scope)) > 6 ? (12 / count($attachment_scope)) : 6;
foreach($attachment_scope as $scope => $label)
{
echo '<div class="col-sm-' . $col . '">';
echo '<label for="' .$prefix.$scope. 'attachment-input-0">' . $label . '</label>';
echo '<div id="'.$prefix.$scope.'newattachments">';
echo '<div id="'.$prefix.$scope.'attachment-0" class="row">';
echo '<div id="'.$prefix.$scope.'delete-attachment-0" class="col-sm-2"></div><div class="' . (($scope == "public" && $public_required) || $required ? 'input text required ' : '') . 'col-sm-4"><label style="text-align: right;clear: both;float:left;margin-right:15px;" for="' .$prefix.$scope. 'attachment-input-0"></label><input' . ($scope == 'public' ? ($public_required ? ' required="required"' : '') : ($required ? ' required="required"' : '')) . ' name="' . $input_name_for_attachments . ($scope != "" ? '['.$scope.']' : "") . $attachment_index . '" id="'.$prefix.$scope.'attachment-input-0" progressive="0" ' . ($scope == "public" ? '' : $multiple) . ' '. $accept .' type="file"></div>';
echo '<div style="margin-bottom: 50px;" id="'.$prefix.$scope.'geoinputs-attachment-0" class="col-sm-6"></div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
echo $this->Form->hidden($prefix.'removecurrentfiles', ['id' => $prefix.'removecurrentfiles', 'value' => isset($currentFilesRemovable) && $currentFilesRemovable ? 'true' : 'false']);
?>
</div>
</div>
</div>
<div class="form-group text">
<?php
if (isset($filepicker) && $filepicker) {
foreach($attachment_scope as $scope => $label)
{
if ($scope == "public" || $oneFilePicker) {
continue;
}
?>
<script>
function getMarkerCoordinates(obj) {
$(obj).parent()[0].querySelector("[name*=longitude]").value = $('#longitude').val();
$(obj).parent()[0].querySelector("[name*=latitude]").value = $('#latitude').val();
}
var <?= $prefix ?><?= $scope ?>fileInputChange = function (evt) {
console.log("fileInputChange!");
console.log(evt);
console.log();
var current = parseInt($('#' + evt.currentTarget.id).attr('progressive'));
<?php if ($georeferenced): ?>
geoinputs = '<p style="display:inline;margin-right: 5px;">Coordinate georeferenziali</p>';
geoinputs += '<input style="padding-bottom: 7px;padding-top: 3px; text-align: center;" name="geo-<?= $input_name_for_attachments ?><?= $scope != "" ? '['.$scope.']' : ""; ?>['+<?php if ($georeferenced) { ?>current<?php } else { ?> '' <?php } ?>+'][latitude]" placeholder="<?= __('latitudine') ?>..." type="text">';
geoinputs += '<input style="padding-bottom: 7px;padding-top: 3px; text-align: center;" name="geo-<?= $input_name_for_attachments ?><?= $scope != "" ? '['.$scope.']' : ""; ?>['+<?php if ($georeferenced) { ?>current<?php } else { ?> '' <?php } ?>+'][longitude]" placeholder="<?= __('longitudine') ?>..." type="text">';
geoinputs += '<a title="Incolla le coordinate del punto presente in mappa (se disponibile)" onClick="getMarkerCoordinates(this)" class="btn btn-info"><i class="fa fa-map-marker" aria-hidden="true"></i></a>';
<?php endif; ?>
var next = current + 1;
$('#' + evt.currentTarget.id).off('change');
$('#<?= $prefix ?><?= $scope ?>newattachments').append('<div id="<?= $prefix ?><?= $scope ?>attachment-' + next + '" class="row"><div id="<?= $prefix ?><?= $scope ?>delete-attachment-'+ next +'" class="col-sm-2"></div><div class="col-sm-4"><input name="<?= $input_name_for_attachments ?><?= $scope != "" ? '['.$scope.']' : ""; ?>['+<?php if ($georeferenced) { ?>next<?php } else { ?> '' <?php } ?>+']" id="<?= $prefix ?><?= $scope ?>attachment-input-' + next + '" progressive="' + next + '" <?= $multiple ?> <?= $accept ?> type="file"></div><div style="margin-bottom: 5px;" id="<?= $prefix ?><?= $scope ?>geoinputs-attachment-'+ next +'" class="col-sm-6"></div></div>');
$('#<?= $prefix ?><?= $scope ?>delete-attachment-'+current).append('<input type="button" value="' + "<?= __('Rimuovi') ?>" + '" onClick="$(\'#<?= $prefix ?><?= $scope ?>attachment-'+current+'\').remove()"/></div>');
<?php if ($georeferenced): ?> $('#<?= $prefix ?><?= $scope ?>geoinputs-attachment-'+current).append(geoinputs); <?php endif; ?>
$('#<?= $prefix ?><?= $scope ?>attachment-input-' + next).on('change', <?= $prefix ?><?= $scope ?>fileInputChange);
};
window.addEventListener('DOMContentLoaded', function() {
$('#<?= $prefix ?><?= $scope ?>attachment-input-0').on('change', <?= $prefix ?><?= $scope ?>fileInputChange);
});
</script>
<?php
}
}
?>
<script>
var <?= $prefix ?>showRelatedSwitchState = false;
</script>
<?php
if (isset($enableRelated) && $enableRelated) {
?>
<script>
window.addEventListener('DOMContentLoaded', function() {
$('#<?= $prefix ?>showrelatedattachments').bootstrapSwitch({'offColor':'warning','onColor':'success','onText':'SI','offText':'NO'});
$('#<?= $prefix ?>showrelatedattachments').off('switchChange.bootstrapSwitch');
$('#<?= $prefix ?>showrelatedattachments').on('switchChange.bootstrapSwitch', function(event, state) {
console.log(state);
<?= $prefix ?>showRelatedSwitchState = state;
<?= $prefix ?>$globalUrlA = '/controllable_objects/getAttachments/<?= $coId ?>';
<?= $prefix ?>refreshAttachmentsIndex();
});
});
</script>
<?php
}
?>
<?php
if (isset($viewer) && isset($coId) && $viewer) {
?>
</br>
<div id="<?= $prefix ?>attachmentsList"></div>
<?php if (isset($view_in_frame_with_id)): ?>
<hr>
<div class="col-sx-2" hidden="true" id="<?= $view_in_frame_with_id ?>-button" style="padding-bottom:1rem;">
<center><a class='btn btn-info' onclick="closePreview()">Chiudi anteprima</a></center>
</div>
<center><iframe id="<?= $view_in_frame_with_id ?>" hidden="" src="" width="90%" height="600px"></iframe></center>
<?php endif; ?>
<script>
var <?= $prefix ?>$globalUrlA = '/controllable_objects/getAttachments/<?= $coId ?>/<?= $coRelated ?><?= isset($csv_tags)? '?tags='.$csv_tags : ''?>';
function <?= $prefix ?>diffArray(arr1, arr2) {
var newArr = [];
arr1.forEach(function(val){
if(arr2.indexOf(val) < 0) newArr.push(val);
});
arr2.forEach(function(val){
if(arr1.indexOf(val) < 0) newArr.push(val);
});
return newArr;
}
function <?= $prefix ?>handleDiffAttachments(attachmentsObj, prevAttachmentsObj) {
var attachments = [];
var prevAttachments = [];
for (i = 0; i < attachmentsObj.length; i++) {
attachments.push(attachmentsObj[i].file_name);
}
for (j = 0; j < prevAttachmentsObj.length; j++) {
prevAttachments.push(prevAttachmentsObj[j].file_name);
}
console.log("attachments:");
console.log(attachments);
console.log("prevAttachments:");
console.log(prevAttachments);
var diffAttachments = <?= $prefix ?>diffArray(attachments, prevAttachments);
console.log("diff attachments:");
console.log(diffAttachments);
for (k = 0; k < diffAttachments.length; k++) {
$('#attachment-' + diffAttachments[k]).attr("style", "font-weight: bold;background-color:#f8d716");
}
}
var <?= $prefix ?>fileViewerforHistory = function (obj, prevObj, tag_code) {
if (typeof obj != "undefined") {
console.log(obj);
}
if (typeof tag_code != "undefined") {
if (typeof obj != "undefined") obj = obj.filter(attachment => attachment.tags.filter(tag => tag.code == tag_code).length > 0);
if (typeof prevObj != "undefined") prevObj = prevObj.filter(attachment => attachment.tags.filter(tag => tag.code == tag_code).length > 0);
}
$.ajax({
type: "POST",
url: '/controllable_objects/getAttachmentsForSnapshots/<?= $coId ?>',
data: {attachments : obj},
success: function ($data) {
$("#<?= $prefix ?>attachmentsList").html($data);
if (typeof prevObj != "undefined") {
<?= $prefix ?>handleDiffAttachments(obj, prevObj);
}
}
});
};
var <?= $prefix ?>refreshAttachmentsIndex = function () {
console.log("Refresh Attachments");
$.ajax({
type: "GET",
url: <?= $prefix ?>$globalUrlA,
data: '&removecurrentfiles=' + $('#<?= $prefix ?>removecurrentfiles').val() + '&showRelated=' + <?= $prefix ?>showRelatedSwitchState,
success: function ($data, $status) {
console.log("attachments index refreshed with url:", <?= $prefix ?>$globalUrlA);
$("#<?= $prefix ?>attachmentsList").html($data);
}
});
};
window.addEventListener('DOMContentLoaded', function() {
<?= $prefix ?>refreshAttachmentsIndex();
$("#<?= $prefix ?>attachmentsList").undelegate("a", "click");
$("#<?= $prefix ?>attachmentsList").delegate("a", "click", function ( event ) {
if ($(this).attr('href') != undefined) {
if ($(this).attr('href') == "") {
event.preventDefault();
} else {
console.log("captured link:");
console.log($(this).attr('href'));
console.log($(this).attr('data-mimetype'));
if ($(this).attr('href').indexOf("/attachments/view") === 0 || $(this).attr('href').split('nocatch').length > 1) {
<?php if (isset($view_in_frame_with_id)): ?>
if ($(this).attr('data-mimetype') == "application/pdf") {
document.getElementById('<?= $view_in_frame_with_id ?>').hidden = false;
document.getElementById('<?= $view_in_frame_with_id ?>-button').hidden = false;
document.getElementById('<?= $view_in_frame_with_id ?>').src = $(this).attr('href');
event.preventDefault();
} else {
document.getElementById('<?= $view_in_frame_with_id ?>').hidden = true;
document.getElementById('<?= $view_in_frame_with_id ?>-button').hidden = true;
}
<?php endif; ?>
console.log("event propagation allowed!");
} else {
if ($(this).attr('href').indexOf("/attachments/markAsRelevant") === 0) {
console.log("hey relevant!");
event.preventDefault();
$.ajax({
type: "POST",
url: $(this).attr('href') + '.json',
success: function ($data, $status) {
console.log("esito chiamata markAsRelevant:");
console.log($data);
<?= $prefix ?>refreshAttachmentsIndex();
}
});
} else {
// color: red; text-decoration:line-through;
if ($(this).attr('href').indexOf("/attachments/markAsDeleted") === 0) {
co_id = $(this).attr('href').split('markAsDeleted/')[1];
console.log("markAsDeleted!");
if ($('#<?= $prefix ?>removefiles-' + co_id).length == 0) {
console.log($(this));
$(this).text("Ripristina");
$(this).closest('td').append('<input type="hidden" id="<?= $prefix ?>removefiles-' + co_id + '" name="<?= $input_name_for_removefiles ?>[' + co_id + '][id]" value="' + co_id + '"/>');
$(this).closest('tr').attr("style", "color: red; text-decoration:line-through;");
} else {
console.log($(this));
$(this).text("Segna Da Eliminare");
$('#<?= $prefix ?>removefiles-' + co_id).remove();
$(this).closest('tr').attr("style", "");
}
} else {
<?= $prefix ?>$globalUrlA = $(this).attr('href');
<?= $prefix ?>refreshAttachmentsIndex();
}
event.preventDefault();
}
}
}
}
});
});
<?php
if(isset($view_in_frame_with_id)):
?>
function closePreview(){
if(document.getElementById('<?= $view_in_frame_with_id ?>') != null){
document.getElementById('<?= $view_in_frame_with_id ?>').hidden = true;
document.getElementById('<?= $view_in_frame_with_id ?>-button').hidden = true;
}
};
<?php
endif;
?>
</script>
<?php
}
?>
</div>