Skip to content

Commit

Permalink
Add new jquery upload see BT#10893
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed Feb 29, 2016
1 parent bad4197 commit 3d31150
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 159 deletions.
2 changes: 1 addition & 1 deletion main/document/showinframes.php
Expand Up @@ -86,7 +86,7 @@

$is_allowed_to_edit = api_is_allowed_to_edit();
//fix the screen when you try to access a protected course through the url
$is_allowed_in_course = $_SESSION['is_allowed_in_course'] || $is_allowed_to_edit;
$is_allowed_in_course = api_is_allowed_in_course() || $is_allowed_to_edit;
if ($is_allowed_in_course == false) {
api_not_allowed(true);
}
Expand Down
205 changes: 143 additions & 62 deletions main/document/upload.php
Expand Up @@ -40,47 +40,6 @@

// Adding extra javascript to the form
$htmlHeadXtra[] = api_get_jquery_libraries_js(array('jquery-ui', 'jquery-upload'));
$htmlHeadXtra[] = '<script>
function check_unzip() {
if (document.upload.unzip.checked){
document.upload.if_exists[0].disabled=true;
document.upload.if_exists[1].checked=true;
document.upload.if_exists[2].disabled=true;
} else {
document.upload.if_exists[0].checked=true;
document.upload.if_exists[0].disabled=false;
document.upload.if_exists[2].disabled=false;
}
}
function setFocus(){
$("#title_file").focus();
}
</script>';

$htmlHeadXtra[] = "
<script>
$(function () {
setFocus();
$('#file_upload').fileUploadUI({
uploadTable: $('.files'),
downloadTable: $('.files'),
buildUploadRow: function (files, index) {
$('.files').show();
return $('<tr><td>' + files[index].name + '<\/td>' +
'<td class=\"file_upload_progress\"><div><\/div><\/td>' +
'<td class=\"file_upload_cancel\">' +
'<button class=\"ui-state-default ui-corner-all\" title=\"".get_lang('Cancel')."\">' + '<span class=\"ui-icon ui-icon-cancel\">".get_lang('Cancel')."<\/span>' +'<\/button>'+
'<\/td><\/tr>');
},
buildDownloadRow: function (file) {
return $('<tr><td>' + file.name + '<\/td> <td> ' + file.size + ' <\/td> <td>&nbsp;' + file.result + ' <\/td> <\/tr>');
}
});
});
</script>";

// Variables

Expand Down Expand Up @@ -122,6 +81,131 @@ function setFocus(){
}
$group_properties = array();

$url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath='.$path;

$htmlHeadXtra[] = '<script>
function check_unzip() {
if (document.upload.unzip.checked){
document.upload.if_exists[0].disabled=true;
document.upload.if_exists[1].checked=true;
document.upload.if_exists[2].disabled=true;
} else {
document.upload.if_exists[0].checked=true;
document.upload.if_exists[0].disabled=false;
document.upload.if_exists[2].disabled=false;
}
}
function setFocus(){
$("#title_file").focus();
}
</script>';

$htmlHeadXtra[] = "
<script>
$(function () {
'use strict';
var url = '".$url."';
var uploadButton = $('<button/>')
.addClass('btn btn-primary')
.prop('disabled', true)
.text('".get_lang('Loading')."')
.on('click', function () {
var \$this = $(this),
data = \$this.data();
\$this
.off('click')
.text('".get_lang('Cancel')."')
.on('click', function () {
\$this.remove();
data.abort();
});
data.submit().always(function () {
\$this.remove();
});
});
$('#file_upload').fileupload({
url: url,
dataType: 'json',
autoUpload: false,
// Enable image resizing, except for Android and Opera,
// which actually support image resizing, but fail to
// send Blob objects via XHR requests:
disableImageResize: /Android(?!.*Chrome)|Opera/.test(window.navigator.userAgent),
previewMaxWidth: 100,
previewMaxHeight: 100,
previewCrop: true
}).on('fileuploadadd', function (e, data) {
data.context = $('<div/>').appendTo('#files');
$.each(data.files, function (index, file) {
var node = $('<p/>').append($('<span/>').text(file.name));
if (!index) {
node
.append('<br>')
.append(uploadButton.clone(true).data(data));
}
node.appendTo(data.context);
});
}).on('fileuploadprocessalways', function (e, data) {
var index = data.index,
file = data.files[index],
node = $(data.context.children()[index]);
if (file.preview) {
node
.prepend('<br>')
.prepend(file.preview);
}
if (file.error) {
node
.append('<br>')
.append($('<span class=\"text-danger\"/>').text(file.error));
}
if (index + 1 === data.files.length) {
data.context.find('button')
.text('Upload')
.prop('disabled', !!data.files.error);
}
}).on('fileuploadprogressall', function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .progress-bar').css(
'width',
progress + '%'
);
}).on('fileuploaddone', function (e, data) {
$.each(data.result.files, function (index, file) {
if (file.url) {
var link = $('<a>')
.attr('target', '_blank')
.prop('href', file.url);
$(data.context.children()[index]).wrap(link);
} else if (file.error) {
var error = $('<span class=\"text-danger\"/>').text(file.error);
$(data.context.children()[index])
.append('<br>')
.append(error);
}
});
}).on('fileuploadfail', function (e, data) {
$.each(data.files, function (index) {
var error = $('<span class=\"text-danger\"/>').text('File upload failed.');
$(data.context.children()[index])
.append('<br>')
.append(error);
});
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
});
</script>";


// This needs cleaning!
if (!empty($groupId)) {
// If the group id is set, check if the user has the right to be here
Expand Down Expand Up @@ -174,7 +258,7 @@ function setFocus(){
// Breadcrumbs
if ($is_certificate_mode) {
$interbreadcrumb[] = array(
'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
'url' => '../gradebook/index.php?'.api_get_cidreq(),
'name' => get_lang('Gradebook'),
);
} else {
Expand Down Expand Up @@ -225,10 +309,10 @@ function setFocus(){
// Link back to the documents overview
if ($is_certificate_mode) {
$actions = '<a href="document.php?id='.$document_id.'&selectcat=' . $selectcat.'&'.api_get_cidreq().'">'.
Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('CertificateOverview'),'',ICON_SIZE_MEDIUM).'</a>';
Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('CertificateOverview'),'',ICON_SIZE_MEDIUM).'</a>';
} else {
$actions = '<a href="document.php?id='.$document_id.'&'.api_get_cidreq().'">'.
Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('DocumentsOverview'),'',ICON_SIZE_MEDIUM).'</a>';
Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('DocumentsOverview'),'',ICON_SIZE_MEDIUM).'</a>';
}

// Link to create a folder
Expand Down Expand Up @@ -317,26 +401,23 @@ function setFocus(){

$simple_form = $form->returnForm();

$url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file';
$multiple_form = '<div class="description-upload">'.get_lang('ClickToSelectOrDragAndDropMultipleFilesOnTheUploadField').'</div>';
$multiple_form .= '
<div class="form-ajax">
<form id="file_upload" action="'.$url.'" method="POST" enctype="multipart/form-data">
<input type="hidden" name="curdirpath" value="'.$path.'" />
<input type="file" name="file" multiple>
<button type="submit">Upload</button>
<div class="button-load">
'.get_lang('UploadFiles').'
</div>
</form>
</div>
<table style="display:none; width:50%" class="files data_table">
<tr>
<th>'.get_lang('FileName').'</th>
<th>'.get_lang('Size').'</th>
<th>'.get_lang('Status').'</th>
</tr>
</table>';
<span class="btn btn-success fileinput-button">
<i class="glyphicon glyphicon-plus"></i>
<span>'.get_lang('AddFiles').'</span>
<!-- The file input field used as target for the file upload widget -->
<input id="file_upload" type="file" name="files[]" multiple>
</span>
<br />
<br />
<!-- The global progress bar -->
<div id="progress" class="progress">
<div class="progress-bar progress-bar-success"></div>
</div>
<div id="files" class="files"></div>
';

$nav_info = api_get_navigator();
if ($nav_info ['name'] == 'Internet Explorer') {
Expand Down

0 comments on commit 3d31150

Please sign in to comment.