| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage com_vikbooking |
| 5 |
* @author Alessio Gaggii - E4J srl |
| 6 |
* @copyright Copyright (C) 2024 E4J srl. All rights reserved. |
| 7 |
* @license GNU General Public License version 2 or later; see LICENSE |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 12 |
|
| 13 |
/** |
| 14 |
* Obtain vars from arguments received in the layout file. |
| 15 |
* |
| 16 |
* @var string $caller the name of the page invoking the layout file (i.e. "view" or "widget"). |
| 17 |
* @var array $customer the customer record involved. |
| 18 |
*/ |
| 19 |
extract($displayData); |
| 20 |
|
| 21 |
if (!is_array($customer) || !$customer) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
$caller = strtolower($caller ?? 'view'); |
| 26 |
|
| 27 |
$files = VikBooking::getCustomerDocuments($customer['id']); |
| 28 |
|
| 29 |
if ($caller === 'view') { |
| 30 |
?> |
| 31 |
<fieldset class="adminform"> |
| 32 |
<div class="vbo-params-wrap"> |
| 33 |
<legend class="adminlegend"><?php echo JText::translate('VBOCUSTOMERDOCUMENTS'); ?></legend> |
| 34 |
<div class="vbo-params-container"> |
| 35 |
<?php |
| 36 |
} |
| 37 |
?> |
| 38 |
<div class="vbo-dropfiles-target"> |
| 39 |
<div class="vbo-uploaded-files" id="vbo-uploaded-files"> |
| 40 |
|
| 41 |
<?php |
| 42 |
foreach ($files as $file) |
| 43 |
{ |
| 44 |
?> |
| 45 |
<div class="file-elem" data-file="<?php echo $file->basename; ?>"> |
| 46 |
<div class="file-elem-inner"> |
| 47 |
<a href="<?php echo $file->url; ?>" target="_blank" class="file-link"> |
| 48 |
<?php VikBookingIcons::e('file'); ?> |
| 49 |
<span class="file-extension"><?php echo $file->ext; ?></span> |
| 50 |
</a> |
| 51 |
|
| 52 |
<div class="file-summary"> |
| 53 |
<div class="filename"><?php echo $file->name; ?></div> |
| 54 |
<div class="filesize"><?php echo JHtml::fetch('number.bytes', $file->size, 'auto', 0); ?></div> |
| 55 |
</div> |
| 56 |
|
| 57 |
<a href="javascript:void(0);" class="delete-file"><?php VikBookingIcons::e('times-circle'); ?></a> |
| 58 |
</div> |
| 59 |
</div> |
| 60 |
<?php |
| 61 |
} |
| 62 |
?> |
| 63 |
|
| 64 |
</div> |
| 65 |
|
| 66 |
<p class="icon"> |
| 67 |
<i class="<?php echo VikBookingIcons::i('upload'); ?>" style="font-size: 48px;"></i> |
| 68 |
</p> |
| 69 |
|
| 70 |
<div class="lead"> |
| 71 |
<a href="javascript: void(0);" id="upload-file"><?php echo JText::translate('VBOMANUALUPLOAD'); ?></a> <?php echo JText::translate('VBODROPFILES'); ?> |
| 72 |
</div> |
| 73 |
|
| 74 |
<p class="maxsize"> |
| 75 |
<?php echo JText::sprintf('JGLOBAL_MAXIMUM_UPLOAD_SIZE_LIMIT', JHtml::fetch('vikbooking.maxuploadsize')); ?> |
| 76 |
</p> |
| 77 |
|
| 78 |
<input type="file" id="legacy-upload" style="display: none;" multiple="multiple"> |
| 79 |
</div> |
| 80 |
|
| 81 |
<?php |
| 82 |
if ($caller === 'view'): |
| 83 |
?> |
| 84 |
<div class="drop-files-hint"> |
| 85 |
<?php |
| 86 |
echo VikBooking::getVboApplication()->createPopover(array( |
| 87 |
'title' => 'Drop Files', |
| 88 |
'content' => JText::translate('VBODROPFILESHINT'), |
| 89 |
'placement' => 'left', |
| 90 |
)); |
| 91 |
?> |
| 92 |
</div> |
| 93 |
<?php |
| 94 |
endif; |
| 95 |
?> |
| 96 |
<?php |
| 97 |
if ($caller === 'view') { |
| 98 |
?> |
| 99 |
</div> |
| 100 |
</div> |
| 101 |
</fieldset> |
| 102 |
<?php |
| 103 |
} |
| 104 |
?> |
| 105 |
|
| 106 |
<div class="stop-managing-files-hint"><?php echo JText::translate('VBODROPFILESSTOPREMOVING'); ?></div> |
| 107 |
|
| 108 |
<script> |
| 109 |
|
| 110 |
jQuery(function() { |
| 111 |
|
| 112 |
var dragCounter = 0; |
| 113 |
|
| 114 |
// drag&drop actions on target div |
| 115 |
|
| 116 |
jQuery('.vbo-dropfiles-target').on('drag dragstart dragend dragover dragenter dragleave drop', function(e) { |
| 117 |
e.preventDefault(); |
| 118 |
e.stopPropagation(); |
| 119 |
}); |
| 120 |
|
| 121 |
jQuery('.vbo-dropfiles-target').on('dragenter', function(e) { |
| 122 |
// increase the drag counter because we may |
| 123 |
// enter into a child element |
| 124 |
dragCounter++; |
| 125 |
|
| 126 |
jQuery(this).addClass('drag-enter'); |
| 127 |
}); |
| 128 |
|
| 129 |
jQuery('.vbo-dropfiles-target').on('dragleave', function(e) { |
| 130 |
// decrease the drag counter to check if we |
| 131 |
// left the main container |
| 132 |
dragCounter--; |
| 133 |
|
| 134 |
if (dragCounter <= 0) { |
| 135 |
jQuery(this).removeClass('drag-enter'); |
| 136 |
} |
| 137 |
}); |
| 138 |
|
| 139 |
jQuery('.vbo-dropfiles-target').on('drop', function(e) { |
| 140 |
|
| 141 |
jQuery(this).removeClass('drag-enter'); |
| 142 |
|
| 143 |
var files = e.originalEvent.dataTransfer.files; |
| 144 |
|
| 145 |
execUploads(files); |
| 146 |
|
| 147 |
}); |
| 148 |
|
| 149 |
jQuery('.vbo-dropfiles-target #upload-file').on('click', function() { |
| 150 |
|
| 151 |
jQuery('input#legacy-upload').trigger('click'); |
| 152 |
|
| 153 |
}); |
| 154 |
|
| 155 |
jQuery('input#legacy-upload').on('change', function() { |
| 156 |
|
| 157 |
execUploads(jQuery(this)[0].files); |
| 158 |
|
| 159 |
}); |
| 160 |
|
| 161 |
// make all current files removable by pressing them |
| 162 |
makeFileRemovable(); |
| 163 |
|
| 164 |
jQuery(window).keyup(function(event) { |
| 165 |
if (event.keyCode == 27) { |
| 166 |
VBO_DROP_FILES_CAN_REMOVE = false; |
| 167 |
jQuery('#vbo-uploaded-files .file-elem').removeClass('do-shake'); |
| 168 |
|
| 169 |
jQuery('.stop-managing-files-hint').hide(); |
| 170 |
} |
| 171 |
}); |
| 172 |
|
| 173 |
jQuery('#vbo-uploaded-files .file-elem a.delete-file').on('click', fileRemoveThread); |
| 174 |
|
| 175 |
}); |
| 176 |
|
| 177 |
// upload |
| 178 |
|
| 179 |
function execUploads(files) { |
| 180 |
if (VBO_DROP_FILES_CAN_REMOVE) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
for (var i = 0; i < files.length; i++) { |
| 185 |
if (isFileSupported(files[i].name)) { |
| 186 |
var status = new UploadingFile(); |
| 187 |
status.setFileNameSize(files[i].name, files[i].size); |
| 188 |
status.setProgress(0); |
| 189 |
|
| 190 |
jQuery('#vbo-uploaded-files').prepend(status.getHtml()); |
| 191 |
|
| 192 |
makeFileRemovable(jQuery('#vbo-uploaded-files .file-elem:first-child a')); |
| 193 |
|
| 194 |
fileUploadThread(status, files[i]); |
| 195 |
} else { |
| 196 |
alert('File ' + files[i].name + ' not supported'); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
function UploadingFile() { |
| 202 |
// create parent |
| 203 |
this.fileBlock = jQuery('<div class="file-elem uploading"></div>'); |
| 204 |
|
| 205 |
// create file link and append it to parent block |
| 206 |
this.fileUrl = jQuery('<a href="javascript:void(0);" target="_blank"><?php VikBookingIcons::e('file'); ?></a>').appendTo(this.fileBlock); |
| 207 |
// create file extension |
| 208 |
this.fileExt = jQuery('<span class="file-extension"></span>').appendTo(this.fileUrl); |
| 209 |
|
| 210 |
// create file summary |
| 211 |
this.fileSummary = jQuery('<div class="file-summary"></div>').appendTo(this.fileBlock); |
| 212 |
|
| 213 |
// create file name |
| 214 |
this.fileName = jQuery('<div class="filename"></div>').appendTo(this.fileSummary); |
| 215 |
// create file size |
| 216 |
this.fileSize = jQuery('<div class="filesize"></div>').appendTo(this.fileSummary); |
| 217 |
|
| 218 |
// create remove link |
| 219 |
this.removeLink = jQuery('<a href="javascript:void(0);" class="delete-file"><?php VikBookingIcons::e('times'); ?></a>').appendTo(this.fileBlock); |
| 220 |
|
| 221 |
this.removeLink.on('click', fileRemoveThread); |
| 222 |
|
| 223 |
this.setFileNameSize = function(name, size) { |
| 224 |
// fetch name |
| 225 |
var match = name.match(/(.*?)\.([a-z0-9]{2,})$/i); |
| 226 |
|
| 227 |
if (match && match.length) { |
| 228 |
this.fileName.html(match[1]); |
| 229 |
this.fileExt.html(match[2]); |
| 230 |
} else { |
| 231 |
this.fileName.html(name); |
| 232 |
} |
| 233 |
|
| 234 |
// fetch size |
| 235 |
var sizeStr = ""; |
| 236 |
|
| 237 |
if (size > 1024*1024) { |
| 238 |
var sizeMB = size/(1024*1024); |
| 239 |
sizeStr = sizeMB.toFixed(2)+" MB"; |
| 240 |
} else if (size > 1024) { |
| 241 |
var sizeKB = size/1024; |
| 242 |
sizeStr = sizeKB.toFixed(2)+" kB"; |
| 243 |
} else { |
| 244 |
sizeStr = size.toFixed(2)+" B"; |
| 245 |
} |
| 246 |
|
| 247 |
this.fileSize.html(sizeStr); |
| 248 |
} |
| 249 |
|
| 250 |
this.setProgress = function(progress) { |
| 251 |
var opacity = parseFloat(progress / 100); |
| 252 |
|
| 253 |
this.fileBlock.css('opacity', opacity); |
| 254 |
} |
| 255 |
|
| 256 |
this.complete = function(file) { |
| 257 |
this.setProgress(100); |
| 258 |
|
| 259 |
this.fileUrl.attr('href', file.url); |
| 260 |
this.fileName.html(file.name); |
| 261 |
this.fileExt.html(file.ext); |
| 262 |
this.fileSize.html(file.size); |
| 263 |
this.fileBlock.removeClass('uploading'); |
| 264 |
this.fileBlock.attr('data-file', file.filename); |
| 265 |
} |
| 266 |
|
| 267 |
this.getHtml = function() { |
| 268 |
return this.fileBlock; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
var vboDropFilesFormData = null; |
| 273 |
|
| 274 |
function fileUploadThread(status, file) { |
| 275 |
jQuery.noConflict(); |
| 276 |
|
| 277 |
vboDropFilesFormData = new FormData(); |
| 278 |
vboDropFilesFormData.append('file', file); |
| 279 |
vboDropFilesFormData.append('customer', <?php echo (int) $customer['id']; ?>); |
| 280 |
|
| 281 |
var jqxhr = jQuery.ajax({ |
| 282 |
xhr: function() { |
| 283 |
var xhrobj = jQuery.ajaxSettings.xhr(); |
| 284 |
if (xhrobj.upload) { |
| 285 |
xhrobj.upload.addEventListener('progress', function(event) { |
| 286 |
var percent = 0; |
| 287 |
var position = event.loaded || event.position; |
| 288 |
var total = event.total; |
| 289 |
if (event.lengthComputable) { |
| 290 |
percent = Math.ceil(position / total * 100); |
| 291 |
} |
| 292 |
// update progress |
| 293 |
status.setProgress(percent); |
| 294 |
}, false); |
| 295 |
} |
| 296 |
return xhrobj; |
| 297 |
}, |
| 298 |
url: '<?php echo VikBooking::ajaxUrl('index.php?option=com_vikbooking&task=upload_customer_document'); ?>', |
| 299 |
type: 'POST', |
| 300 |
contentType: false, |
| 301 |
processData: false, |
| 302 |
cache: false, |
| 303 |
data: vboDropFilesFormData, |
| 304 |
success: function(resp) { |
| 305 |
try { |
| 306 |
resp = typeof resp === 'string' ? JSON.parse(resp) : resp; |
| 307 |
|
| 308 |
if (resp.status == 1) { |
| 309 |
status.complete(resp); |
| 310 |
} else { |
| 311 |
throw resp.error ? resp.error : 'An error occurred! Please try again.'; |
| 312 |
} |
| 313 |
} catch (err) { |
| 314 |
console.warn(err, resp); |
| 315 |
|
| 316 |
alert(err); |
| 317 |
|
| 318 |
status.fileBlock.remove(); |
| 319 |
} |
| 320 |
}, |
| 321 |
error: function(err) { |
| 322 |
console.error(err.responseText); |
| 323 |
|
| 324 |
status.fileBlock.remove(); |
| 325 |
|
| 326 |
alert('An error occurred! Please try again.'); |
| 327 |
}, |
| 328 |
}); |
| 329 |
} |
| 330 |
|
| 331 |
function isFileSupported(name) { |
| 332 |
return name.match(/\.(jpe?g|png|bmp|heic|pdf|zip|rar|txt|md|docx?|odt|rtf|pages|xlsx?|ods|numbers|csv)$/i); |
| 333 |
} |
| 334 |
|
| 335 |
var VBO_DROP_FILES_CAN_REMOVE = false; |
| 336 |
|
| 337 |
function makeFileRemovable(selector) { |
| 338 |
if (!selector) { |
| 339 |
selector = '#vbo-uploaded-files .file-elem a'; |
| 340 |
} |
| 341 |
|
| 342 |
jQuery(selector).each(function() { |
| 343 |
var timeout = null; |
| 344 |
|
| 345 |
jQuery(this).on('mousedown', function(event) { |
| 346 |
timeout = setTimeout(function() { |
| 347 |
VBO_DROP_FILES_CAN_REMOVE = true; |
| 348 |
|
| 349 |
jQuery('#vbo-uploaded-files .file-elem').addClass('do-shake'); |
| 350 |
|
| 351 |
jQuery('.stop-managing-files-hint').show(); |
| 352 |
}, 1000); |
| 353 |
}).on('mouseup mouseleave', function(event) { |
| 354 |
clearTimeout(timeout); |
| 355 |
}).on('click', function(event) { |
| 356 |
if (VBO_DROP_FILES_CAN_REMOVE) { |
| 357 |
event.preventDefault(); |
| 358 |
event.stopPropagation(); |
| 359 |
return false; |
| 360 |
} |
| 361 |
}); |
| 362 |
}); |
| 363 |
} |
| 364 |
|
| 365 |
function fileRemoveThread() { |
| 366 |
var elem = jQuery(this).closest('.file-elem'); |
| 367 |
var file = jQuery(elem).attr('data-file'); |
| 368 |
|
| 369 |
if (!file.length) { |
| 370 |
return false; |
| 371 |
} |
| 372 |
|
| 373 |
elem.addClass('removing'); |
| 374 |
|
| 375 |
jQuery.ajax({ |
| 376 |
url: '<?php echo VikBooking::ajaxUrl('index.php?option=com_vikbooking&task=delete_customer_document'); ?>', |
| 377 |
type: 'post', |
| 378 |
data: { |
| 379 |
file: file, |
| 380 |
customer: <?php echo (int) $customer['id']; ?>, |
| 381 |
}, |
| 382 |
}).done(function(resp) { |
| 383 |
|
| 384 |
elem.remove(); |
| 385 |
|
| 386 |
if (jQuery('#vbo-uploaded-files .file-elem').length == 0) { |
| 387 |
var esc = jQuery.Event('keyup', { keyCode: 27 }); |
| 388 |
jQuery(window).trigger(esc); |
| 389 |
} |
| 390 |
|
| 391 |
}).fail(function(resp) { |
| 392 |
|
| 393 |
console.error(err.responseText); |
| 394 |
|
| 395 |
elem.removeClass('removing'); |
| 396 |
|
| 397 |
alert('An error occurred! Please try again.'); |
| 398 |
|
| 399 |
}); |
| 400 |
} |
| 401 |
|
| 402 |
</script> |
| 403 |
|