static function fileScript($options = array())
{
global $CFG_GLPI;
$randupload = mt_rand();
$p['imagePaste'] = 0;
$p['name'] = 'filename';
$p['multiple'] = false;
$p['onlyimages'] = false;
$p['showfilecontainer'] = '';
$p['pasteZone'] = false;
$p['dropZone'] = 'dropdoc' . $randupload;
$p['rand'] = $randupload;
$p['values'] = array();
if (is_array($options) && count($options)) {
foreach ($options as $key => $val) {
$p[$key] = $val;
}
}
$script = "fileindex" . $p['rand'] . " = 0;\n function uploadFile" . $p['rand'] . "() {\n \$('#fileupload" . $p['rand'] . "').fileupload({\n //forceIframeTransport: true,\n //replaceFileInput: false,\n dataType: 'json',";
if ($p['pasteZone'] != false) {
$script .= "pasteZone : \$('#" . $p['pasteZone'] . "'),";
} else {
if (!$p['imagePaste']) {
$script .= "pasteZone : false,";
}
}
if ($p['dropZone'] != false) {
$script .= "dropZone : \$('#" . $p['dropZone'] . "'),";
} else {
$script .= "dropZone : false,";
}
if ($p['onlyimages']) {
$script .= "acceptFileTypes: '/(\\.|\\/)(gif|jpe?g|png)\$/i',";
}
$script .= " progressall: function (e, data) {\n var progress = parseInt(data.loaded / data.total * 100, 10);\n \$('#progress" . $p['rand'] . "').show();\n \$('#progress" . $p['rand'] . " .uploadbar').css({\n 'width':progress + '%'\n });\n \$('#progress" . $p['rand'] . " .uploadbar').text(progress + '%').show().delay(5000).fadeOut('slow');\n },\n send: function (e, data) {\n if (1==" . ($p['imagePaste'] ? 1 : 0) . "\n && tinyMCE != undefined\n && tinyMCE.imagePaste != undefined\n && tinyMCE.imagePaste.pasteddata == undefined\n && tinyMCE.imagePaste.stockimage == undefined) {\n\n if (!tinyMCE.isIE) {\n var reader = new FileReader();\n reader.readAsDataURL(data.originalFiles[0]);//Convert the blob from clipboard to base64\n reader.onloadend = function(e){\n \$('#desc_paste_image').html(e.target.result);\n tinyMCE.imagePaste.processpaste(\$('#desc_paste_image'),\n '" . _sx('button', 'Paste image') . "',\n data.originalFiles[0]);\n }\n }\n return false\n }\n },\n done: function (e, data) {\n var filedata = data;\n // Load image tag, and display image uploaded\n \$.ajax({\n type: 'POST',\n url: '" . $CFG_GLPI['root_doc'] . "/ajax/getFileTag.php',\n data: {'data':data.result." . $p['name'] . "},\n dataType: 'JSON',\n success: function(tag){\n \$.each(filedata.result." . $p['name'] . ", function (index, file) {\n if (file.error == undefined) {\n\n displayUploadedFile" . $p['rand'] . "(file,tag[index]);\n ";
if ($p['imagePaste']) {
$script .= " // Insert tag in textarea\n if (tinyMCE != undefined) {\n\n tinyMCE.activeEditor.execCommand('mceInsertContent', false, '<p>'+tag[index].tag+'</p>');\n\n if (tinyMCE.imagePaste != undefined) {\n tinyMCE.imagePaste.pasteddata = undefined;\n tinyMCE.imagePaste.stockimage = undefined;\n }\n }\n";
}
$script .= " \$('#progress" . $p['rand'] . " .uploadbar').text('" . __('Upload successful') . "');\n\n \$('#progress" . $p['rand'] . " .uploadbar').css('width', '100%');\n\n } else {\n\n \$('#progress" . $p['rand'] . " .uploadbar').text(file.error);\n\n \$('#progress" . $p['rand'] . " .uploadbar').css('width', '100%');\n\n }\n });\n }\n });\n\n }\n });\n };\n\n function displayUploadedFile" . $p['rand'] . "(file, tag){\n var p = \$('<p/>').attr('id',file.id).html('<b>" . __('File') . " : </b>'+file.display+' <b>" . __('Tag') . " : </b>'+tag.tag+' ').appendTo('#" . $p['showfilecontainer'] . "');\n\n var p2 = \$('<p/>').attr('id',file.id+'2').css({'display':'none'}).appendTo('#" . $p['showfilecontainer'] . "');\n\n\n // File\n \$('<input/>').attr('type', 'hidden').attr('name', '_" . $p['name'] . "['+fileindex" . $p['rand'] . "+']').attr('value',file.name).appendTo(p);\n\n\n // Tag\n \$('<input/>').attr('type', 'hidden').attr('name', '_tag_" . $p['name'] . "['+fileindex" . $p['rand'] . "+']').attr('value', tag.name).appendTo(p);\n\n\n // Coordinates\n if (tinyMCE != undefined\n && tinyMCE.imagePaste != undefined\n && (tinyMCE.imagePaste.imageCoordinates != undefined || tinyMCE.imagePaste.imageCoordinates != null)) {\n \$('<input/>').attr('type', 'hidden').attr('name', '_coordinates['+fileindex" . $p['rand'] . "+']').attr('value', encodeURIComponent(JSON.stringify(tinyMCE.imagePaste.imageCoordinates))).appendTo(p2);\n tinyMCE.imagePaste.imageCoordinates = null;\n }\n\n // Delete button\n var elementsIdToRemove = {0:file.id, 1:file.id+'2'};\n \$('<img src=\"" . $CFG_GLPI['root_doc'] . "/pics/delete.png\" class=\"pointer\">').click(function(){\n\n deleteImagePasted(elementsIdToRemove, tag.tag);\n\n }).appendTo(p);\n\n ";
if ($p['multiple']) {
$script .= " fileindex" . $p['rand'] . " = fileindex" . $p['rand'] . "+1;\n";
}
$script .= "}\n function deleteImagePasted(elementsIdToRemove, tagToRemove){\n\n // Remove file display lines\n \$.each(elementsIdToRemove, function (index, id) {\n\n \$('#'+id).remove();\n\n });\n\n ";
if ($p['imagePaste']) {
$script .= "\n // TINYMCE : Remove tag from textarea\n if (tinyMCE != undefined) {\n tinyMCE.activeEditor.setContent(tinyMCE.activeEditor.getContent().replace('<p>'+tagToRemove+'</p>', ''));\n\n }";
}
$script .= "\n // File counter\n if (fileindex" . $p['rand'] . " > 0) {\n\n fileindex" . $p['rand'] . "--;\n\n }\n };\n";
if (is_array($p['values']) && isset($p['values']['filename']) && is_array($p['values']['filename']) && count($p['values']['filename'])) {
foreach ($p['values']['filename'] as $key => $name) {
if (isset($p['values']['tag'][$key])) {
$file = GLPI_TMP_DIR . '/' . $p['values']['filename'][$key];
if (file_exists($file)) {
$display = sprintf('%1$s %2$s', $p['values']['filename'][$key], Toolbox::getSize(filesize($file)));
$script .= "var tag{$key} = {};\n tag{$key}.tag = '" . $p['values']['tag'][$key] . "';\n tag{$key}.name = '#" . $p['values']['tag'][$key] . "#';\n var file{$key}= {};\n file{$key}.name = '" . addslashes($p['values']['filename'][$key]) . "'\n file{$key}.display = '" . addslashes($display) . "';\n file{$key}.id = 'file{$key}';\n displayUploadedFile" . $p['rand'] . "(file{$key}, tag{$key});\n ";
}
}
}
}
return $script;
}