Skip to content

Commit

Permalink
Fixed issue #08028: File upload dialogue window can't be styled
Browse files Browse the repository at this point in the history
Dev: except the table in "question" part all is done by css
Dev: fix some HTML and javascript
  • Loading branch information
Shnoulle committed Apr 10, 2015
1 parent ea4b821 commit de95f90
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 183 deletions.
33 changes: 14 additions & 19 deletions application/controllers/UploaderController.php
Expand Up @@ -15,23 +15,14 @@
class UploaderController extends SurveyController {
function run($actionID)
{
if(isset($_SESSION['LEMsid']) && $oSurvey=Survey::model()->findByPk($_SESSION['LEMsid'])){
$surveyid= $_SESSION['LEMsid'];
}else{
$surveyid=Yii::app()->session['LEMsid'];
$oSurvey=Survey::model()->findByPk($surveyid);
if(!$oSurvey)
throw new CHttpException(400);// See for debug > 1
}
if (isset($_SESSION['survey_'.$surveyid]['s_lang']))
{
$sLanguage = $_SESSION['survey_'.$surveyid]['s_lang'];
}
else
{
$sLanguage='';
}

$sLanguage=isset(Yii::app()->session['survey_'.$surveyid]['s_lang']) ? Yii::app()->session['survey_'.$surveyid]['s_lang']: "";
$uploaddir = Yii::app()->getConfig("uploaddir");
$tempdir = Yii::app()->getConfig("tempdir");

Yii::app()->loadHelper("database");

// Fill needed var
Expand All @@ -45,8 +36,8 @@ function run($actionID)

// Validate and filter and throw error if problems
// Using 'futmp_'.randomChars(15).'_'.$pathinfo['extension'] for filename, then remove all other characters
$sFileGetContentFiltered=preg_replace('/[^a-z0-9_]/', '', $sFileGetContent);
$sFileNameFiltered = preg_replace('/[^a-z0-9_]/', '',$sFileName);
$sFileGetContentFiltered=preg_replace('/[^a-zA-Z0-9_]/', '', $sFileGetContent);
$sFileNameFiltered = preg_replace('/[^a-zA-Z0-9_]/', '',$sFileName);
$sFieldNameFiltered=preg_replace('/[^X0-9]/', '', $sFieldName);
if($sFileGetContent!=$sFileGetContentFiltered || $sFileName!=$sFileNameFiltered || $sFieldName!=$sFieldNameFiltered)
{// If one seems to be a hack: Bad request
Expand Down Expand Up @@ -149,13 +140,13 @@ function run($actionID)
$valid_extensions_array = array_map('trim',$valid_extensions_array);

$pathinfo = pathinfo($_FILES['uploadfile']['name']);
$ext = $pathinfo['extension'];
$ext = strtolower($pathinfo['extension']);
$randfilename = 'futmp_'.randomChars(15).'_'.$pathinfo['extension'];
$randfileloc = $sTempUploadDir . $randfilename;

// check to see that this file type is allowed
// it is also checked at the client side, but jst double checking
if (!in_array(strtolower($ext), $valid_extensions_array))
if (!in_array($ext, $valid_extensions_array))
{
$return = array(
"success" => false,
Expand Down Expand Up @@ -291,7 +282,7 @@ function run($actionID)
showpopups="'.Yii::app()->getConfig("showpopups").'";
';
$sLangScriptVar="
translt = {
uploadLang = {
titleFld: '" . gT('Title','js') . "',
commentFld: '" . gT('Comment','js') . "',
errorNoMoreFiles: '" . gT('Sorry, no more files can be uploaded!','js') . "',
Expand All @@ -302,7 +293,9 @@ function run($actionID)
errorMoreAllowed: '" . gT('If you wish, you may upload %s more file(s); else you may return back to survey.','js') . "',
errorMaxReached: '" . gT('The maximum number of files has been uploaded. You may return back to survey.','js') . "',
errorTooMuch: '" . gT('The maximum number of files has been uploaded. You may return back to survey.','js') . "',
errorNeedMoreConfirm: '" . gT("You need to upload %s more files for this question.\nAre you sure you want to exit?",'js') . "'
errorNeedMoreConfirm: '" . gT("You need to upload %s more files for this question.\nAre you sure you want to exit?",'js') . "',
deleteFile : '".gt('Delete','js') . "',
editFile : '".gt('Edit','js') . "',
};
";
$aSurveyInfo=getSurveyInfo($surveyid, $sLanguage);
Expand All @@ -321,6 +314,8 @@ function run($actionID)
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig("generalscripts").'uploader.js');
App()->getClientScript()->registerScriptFile("{$sTemplateUrl}template.js");
App()->clientScript->registerCssFile(Yii::app()->getConfig("publicstyleurl")."uploader.css");
App()->getClientScript()->registerCssFile(Yii::app()->getConfig('publicstyleurl') . "uploader-files.css");

if (file_exists($sTemplateDir .DIRECTORY_SEPARATOR.'jquery-ui-custom.css'))
{
Yii::app()->getClientScript()->registerCssFile("{$sTemplateUrl}jquery-ui-custom.css");
Expand Down
42 changes: 20 additions & 22 deletions application/helpers/qanda_helper.php
Expand Up @@ -2734,61 +2734,60 @@ function do_file_upload($ia)
{
global $thissurvey;



$checkconditionFunction = "checkconditions";

$aQuestionAttributes=getQuestionAttributeValues($ia[0]);

// Fetch question attributes
$_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['fieldname'] = $ia[1];

$currentdir = getcwd();
$pos = stripos($currentdir, "admin");
$scriptloc = Yii::app()->getController()->createUrl('uploader/index');
$bPreview=Yii::app()->request->getParam('action')=="previewgroup" || Yii::app()->request->getParam('action')=="previewquestion" || $thissurvey['active'] != "Y";

if ($pos)
if ($bPreview)
{
$_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['preview'] = 1 ;
$questgrppreview = 1; // Preview is launched from Question or group level

}
else if ($thissurvey['active'] != "Y")
{
$_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['preview'] = 1;
$questgrppreview = 0;
}
else
{
$_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['preview'] = 0;
$questgrppreview = 0;
elseif ($thissurvey['active'] != "Y")
{
$_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['preview'] = 1;
$questgrppreview = 0;
}
else
{
$_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['preview'] = 0;
$questgrppreview = 0;
}

$uploadbutton = "<h2><a id='upload_".$ia[1]."' class='upload' ";
$uploadbutton = "<div class='upload-button'><a id='upload_".$ia[1]."' class='upload' ";
$uploadbutton .= " href='#' onclick='javascript:upload_$ia[1]();'";
$uploadbutton .=">" .gT('Upload files'). "</a></h2>";
$uploadbutton .=">" .gT('Upload files'). "</a></div>";

$answer = "<script type='text/javascript'>
function upload_$ia[1]() {
var uploadurl = '{$scriptloc}?sid=".Yii::app()->getConfig('surveyID')."&fieldname={$ia[1]}&qid={$ia[0]}';
uploadurl += '&preview={$questgrppreview}&show_title={$aQuestionAttributes['show_title']}';
uploadurl += '&show_comment={$aQuestionAttributes['show_comment']}&pos=".($pos?1:0)."';
uploadurl += '&show_comment={$aQuestionAttributes['show_comment']}';
uploadurl += '&minfiles=' + LEMval('{$aQuestionAttributes['min_num_of_files']}');
uploadurl += '&maxfiles=' + LEMval('{$aQuestionAttributes['max_num_of_files']}');
$('#upload_$ia[1]').attr('href',uploadurl);
}
var translt = {
var uploadLang = {
title: '" . gT('Upload your files','js') . "',
returnTxt: '" . gT('Return to survey','js') . "',
headTitle: '" . gT('Title','js') . "',
headComment: '" . gT('Comment','js') . "',
headFileName: '" . gT('File name','js') . "'
headFileName: '" . gT('File name','js') . "',
deleteFile : '".gt('Delete')."',
editFile : '".gt('Edit')."'
};
var imageurl = '".Yii::app()->getConfig('imageurl')."';
var uploadurl = '".$scriptloc."';
</script>\n";
Yii::app()->getClientScript()->registerScriptFile(Yii::app()->getConfig('generalscripts')."modaldialog.js");

Yii::app()->getClientScript()->registerCssFile(Yii::app()->getConfig('publicstyleurl') . "uploader-files.css");
// Modal dialog
$answer .= $uploadbutton;

Expand Down Expand Up @@ -2821,8 +2820,7 @@ function upload_$ia[1]() {
var json = $("#"+fieldname).val();
var show_title = "'.$aQuestionAttributes["show_title"].'";
var show_comment = "'.$aQuestionAttributes["show_comment"].'";
var pos = "'.($pos ? 1 : 0).'";
displayUploadedFiles(json, filecount, fieldname, show_title, show_comment, pos);
displayUploadedFiles(json, filecount, fieldname, show_title, show_comment);
});
</script>';

Expand Down
77 changes: 43 additions & 34 deletions scripts/modaldialog.js
@@ -1,35 +1,38 @@
$(function() {
openUploadModalDialog();
});

function openUploadModalDialog(){
$(document).on('click','.upload-edit',function(){
var fieldname=$(this).data('fieldname');
$('#upload_'+fieldname).click();
});

$('.upload').click(function(e) {
e.preventDefault();
var $this = $(this);

var show_title = getQueryVariable('show_title', this.href);
var show_comment = getQueryVariable('show_comment', this.href);
var pos = getQueryVariable('pos', this.href);
var fieldname = getQueryVariable('fieldname', this.href);
var buttonsOpts = {};
buttonsOpts[translt.returnTxt] = function() {
buttonsOpts[uploadLang.returnTxt] = function() {
$(this).dialog("close");
};
// Get the dialogheigth and width from windows (with some maximum: same than old behaviour (why 440 ?)
var windowwidth = $(window).width()-30;
var windowheight = $(window).height()-30;
var dialogwidth= Math.min(windowwidth, 940);
var dialogheight= Math.min(windowheight, 440);

$('#uploader').dialog('destroy').remove(); // destroy the old modal dialog
$('<iframe id=\"uploader\" name=\"uploader\" class=\"externalSite\" src=\"' + this.href + '\" />').dialog({
title: translt.title,
title: uploadLang.title,
autoOpen: true,
width: dialogwidth,
height: dialogheight,
height: 'auto',
open: function( event, ui ) {
setWidthUploader();
},
modal: true,
resizable: true,
autoResize: true,
resizable: false,
autoResize: false,
draggable: true,
closeOnEscape: false,
beforeClose: function() {
Expand All @@ -55,12 +58,27 @@ function openUploadModalDialog(){
checkconditions();
}
});
// Fix the heigth and width of the iframe
var horizontalPadding = 20;
var verticalPadding = 20 + $("#uploader").closest(".ui-dialog").children(".ui-dialog-buttonpane").outerHeight() + $("#uploader").closest(".ui-dialog").children(".ui-dialog-titlebar").outerHeight();
$("#uploader").width(dialogwidth - horizontalPadding).height(dialogheight - verticalPadding);
});
}
$(window).resize(function() {
setWidthUploader();


if(typeof $("iframe#uploader")[0]!=="undefined" && jQuery.isFunction($("iframe#uploader")[0].contentWindow.fixParentHeigth))
$("iframe#uploader")[0].contentWindow.fixParentHeigth();
});
/* Reset the position of the dialog (recenter) */
function resetUploaderPosition(){
$( "#uploader" ).dialog( "option", "position", $( "#uploader" ).dialog( "option", "position" ) );
}
/* Set the with of upload madal and uploader frame according to windows width */
function setWidthUploader(){
var maxwidth=Math.min($("body").innerWidth()-4, 974);
if(maxwidth!=$( "#uploader" ).dialog( "option", "width" )){
$("#uploader").dialog( "option", "width", maxwidth).width(maxwidth-18) // Leave 20px for overflow
}
resetUploaderPosition();
}

function getQueryVariable(variable, url) {
var vars = url.split("/");
Expand Down Expand Up @@ -91,7 +109,7 @@ function isValueInArray(arr, val) {
return inArray;
}

function displayUploadedFiles(jsonstring, filecount, fieldname, show_title, show_comment, pos) {
function displayUploadedFiles(jsonstring, filecount, fieldname, show_title, show_comment) {
var jsonobj;
var i;
var display = '';
Expand All @@ -102,37 +120,28 @@ function displayUploadedFiles(jsonstring, filecount, fieldname, show_title, show
}

if (jsonstring !== '')
{
{
jsonobj = eval('(' + jsonstring + ')');
display = '<table width="100%" class="question uploadedfiles"><thead><tr><td width="20%">&nbsp;</td>';
if (show_title != 0)
display += '<th>'+translt.headTitle+'</th>';
display += '<th>'+uploadLang.headTitle+'</th>';
if (show_comment != 0)
display += '<th>'+translt.headComment+'</th>';
display += '<th>'+translt.headFileName+'</th><th class="edit"></th></tr></thead><tbody>';
display += '<th>'+uploadLang.headComment+'</th>';
display += '<th>'+uploadLang.headFileName+'</th><th class="edit"></th></tr></thead><tbody>';
var image_extensions = new Array('gif', 'jpeg', 'jpg', 'png', 'swf', 'psd', 'bmp', 'tiff', 'jp2', 'iff', 'bmp', 'xbm', 'ico');

for (i = 0; i < filecount; i++)
{
if (pos)
{
if (isValueInArray(image_extensions, jsonobj[i].ext))
display += '<tr><td class="upload image"><img src="' + uploadurl + '/sid/'+surveyid+'/filegetcontents/'+decodeURIComponent(jsonobj[i].filename)+'" height=100px /></td>';
else
display += '<tr><td class="upload placeholder"><img src="'+imageurl+'/placeholder.png" height=100px /></td>';
}
{
if (isValueInArray(image_extensions, jsonobj[i].ext))
display += '<tr><td class="upload image"><img src="' + uploadurl + '/filegetcontents/'+decodeURIComponent(jsonobj[i].filename)+'" class="uploaded" /></td>';
else
{
if (isValueInArray(image_extensions, jsonobj[i].ext))
display += '<tr><td class="upload image"><img src="' + uploadurl + 'filegetcontents/'+decodeURIComponent(jsonobj[i].filename)+'" height=100px /></td>';
else
display += '<tr><td class="upload placeholder"><img src="'+imageurl+'/placeholder.png" height=100px /></td>';
}
display += '<tr><td class="upload placeholder"><div class="upload-placeholder" /></td>';

if (show_title != 0)
display += '<td class="upload title">'+jsonobj[i].title+'</td>';
if (show_comment != 0)
display += '<td class="upload comment">'+jsonobj[i].comment+'</td>';
display +='<td class="upload edit">'+decodeURIComponent(jsonobj[i].name)+'</td><td>'+'<img src="'+imageurl+'/edit.png" onclick="javascript:upload_'+fieldname+'();$(\'#upload_'+fieldname+'\').click();" style="cursor:pointer"></td></tr>';
display +='<td class="upload edit">'+decodeURIComponent(jsonobj[i].name)+'</td><td>'+'<a class="upload-edit" data-fieldname="'+fieldname+'">'+uploadLang.editFile+'</a></td></tr>';
}
display += '</tbody></table>';

Expand Down

0 comments on commit de95f90

Please sign in to comment.