2
0
forked from Wavyzz/dolibarr

Merge pull request #24285 from Hystepik/develop#3

New : uploadfile drag and drop on fourn invoice card
This commit is contained in:
Laurent Destailleur
2023-03-26 15:36:31 +02:00
committed by GitHub
11 changed files with 204 additions and 53 deletions

View File

@@ -3386,3 +3386,86 @@ function getFilesUpdated(&$file_list, SimpleXMLElement $dir, $path = '', $pathre
return $file_list;
}
/**
* Function to manage the drag and drop file.
* We use global variable $object
*
* @param string $htmlname The id of the component where we need to drag and drop
* @return string Js script to display
*/
function dragAndDropFileUpload($htmlname)
{
global $object, $langs;
$out = "";
$out .= '<div id="'.$htmlname.'Message" class="dragDropAreaMessage hidden"><span>'.img_picto("", 'download').'<br>'.$langs->trans("DropFileToAddItToObject").'</span></div>';
$out .= "\n<!-- JS CODE TO ENABLE DRAG AND DROP OF FILE -->\n";
$out .= "<script>";
$out .= '
jQuery(document).ready(function() {
var enterTargetDragDrop = null;
$("#'.$htmlname.'").addClass("cssDragDropArea");
$(".cssDragDropArea").on("dragenter", function(ev) {
// Entering drop area. Highlight area
console.log("We add class highlightDragDropArea")
enterTargetDragDrop = ev.target;
$(this).addClass("highlightDragDropArea");
$("#'.$htmlname.'Message").removeClass("hidden");
ev.preventDefault();
});
$(".cssDragDropArea").on("dragleave", function(ev) {
// Going out of drop area. Remove Highlight
if (enterTargetDragDrop == ev.target){
console.log("We remove class highlightDragDropArea")
$("#'.$htmlname.'Message").addClass("hidden");
$(this).removeClass("highlightDragDropArea");
}
});
$(".cssDragDropArea").on("dragover", function(ev) {
ev.preventDefault();
return false;
});
$(".cssDragDropArea").on("drop", function(e) {
console.log("Trigger event file droped");
e.preventDefault();
fd = new FormData();
fd.append("fk_element","'.dol_escape_json($object->id).'");
fd.append("element","'.dol_escape_json($object->element).'");
fd.append("token","'.newToken().'");
fd.append("action","linkit");
var dataTransfer = e.originalEvent.dataTransfer;
if(dataTransfer.files && dataTransfer.files.length){
var droppedFiles = e.originalEvent.dataTransfer.files;
$.each(droppedFiles, function(index,file){
fd.append("files[]",file,file.name)
});
}
$(".cssDragDropArea").removeClass("highlightDragDropArea");
counterdragdrop = 0;
$.ajax({
url:"'.DOL_URL_ROOT.'/core/ajax/fileupload.php",
type:"POST",
processData:false,
contentType: false,
data:fd,
success:function(){
console.log("Uploaded.",arguments);
window.location.href = "'.$_SERVER["PHP_SELF"].'?id='.dol_escape_json($object->id).'&seteventmessages=UploadFileDragDropSuccess:mesgs";
},
error:function(){
console.log("Error Uploading.",arguments)
if (arguments[0].status == 403){
window.location.href = "'.$_SERVER["PHP_SELF"].'?id='.dol_escape_json($object->id).'&seteventmessages=ErrorUploadPermissionDenied:errors";
}
window.location.href = "'.$_SERVER["PHP_SELF"].'?id='.dol_escape_json($object->id).'&seteventmessages=ErrorUploadFileDragDropPermissionDenied:errors";
},
})
});
});
';
$out .= "</script>\n";
return $out;
}