Work on ajax file upload component. Component must be used only to make "upload" or multiupload. Output list of files must be independant of upload component because will be enhanced differently. Also, usage of upload component is more clear like that.
Still TODO: Once last files was uploaded (when there is several files), we must launch the refresh of page.
This commit is contained in:
parent
0f483a1654
commit
68029751fd
@ -22,11 +22,12 @@ if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); // If we don't nee
|
||||
//if (! defined("NOLOGIN")) define("NOLOGIN",'1'); // If this page is public (can be called outside logged session)
|
||||
|
||||
|
||||
$res=@include("../../main.inc.php"); // For "custom" directory
|
||||
if (! $res) $res=@include("../main.inc.php"); // For root directory
|
||||
$res=@include("../main.inc.php"); // For "root" directory
|
||||
if (! $res) $res=@include("../../main.inc.php"); // For "custom" directory
|
||||
if (! $res) @include("../../../../dolibarr/htdocs/main.inc.php"); // Used on dev env only
|
||||
|
||||
require_once(DOL_DOCUMENT_ROOT."/lib/files.lib.php");
|
||||
require_once(DOL_DOCUMENT_ROOT."/lib/images.lib.php");
|
||||
|
||||
error_reporting(E_ALL | E_STRICT);
|
||||
|
||||
@ -37,19 +38,23 @@ error_reporting(E_ALL | E_STRICT);
|
||||
$fk_element = GETPOST('fk_element');
|
||||
$element = GETPOST('element');
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class UploadHandler
|
||||
{
|
||||
private $options;
|
||||
private $fk_elment;
|
||||
private $element;
|
||||
|
||||
|
||||
function __construct($options=null,$fk_element=null,$element=null) {
|
||||
|
||||
|
||||
global $conf;
|
||||
|
||||
|
||||
$this->fk_element=$fk_element;
|
||||
$this->element=$element;
|
||||
|
||||
|
||||
$this->options = array(
|
||||
'script_url' => $_SERVER['PHP_SELF'],
|
||||
'upload_dir' => $conf->$element->dir_output . '/' . $fk_element . '/',
|
||||
@ -86,7 +91,7 @@ class UploadHandler
|
||||
$this->options = array_merge_recursive($this->options, $options);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function get_file_object($file_name) {
|
||||
$file_path = $this->options['upload_dir'].$file_name;
|
||||
if (is_file($file_path) && $file_name[0] !== '.') {
|
||||
@ -97,8 +102,8 @@ class UploadHandler
|
||||
$file->url = $this->options['upload_url'].rawurlencode($file->name);
|
||||
foreach($this->options['image_versions'] as $version => $options) {
|
||||
if (is_file($options['upload_dir'].$file_name)) {
|
||||
$file->{$version.'_url'} = $options['upload_url']
|
||||
.rawurlencode($file->name);
|
||||
$tmp=explode('.',$file->name);
|
||||
$file->{$version.'_url'} = $options['upload_url'].rawurlencode($tmp[0].'_mini.'.$tmp[1]);
|
||||
}
|
||||
}
|
||||
$file->delete_url = $this->options['script_url']
|
||||
@ -108,7 +113,7 @@ class UploadHandler
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private function get_file_objects() {
|
||||
return array_values(array_filter(array_map(
|
||||
array($this, 'get_file_object'),
|
||||
@ -116,9 +121,14 @@ class UploadHandler
|
||||
)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create thumbs
|
||||
* options is array('max_width', 'max_height')
|
||||
*/
|
||||
private function create_scaled_image($file_name, $options) {
|
||||
$file_path = $this->options['upload_dir'].$file_name;
|
||||
$new_file_path = $options['upload_dir'].$file_name;
|
||||
|
||||
if (create_exdir($options['upload_dir']) >= 0)
|
||||
{
|
||||
list($img_width, $img_height) = @getimagesize($file_path);
|
||||
@ -134,6 +144,11 @@ class UploadHandler
|
||||
}
|
||||
$new_width = $img_width * $scale;
|
||||
$new_height = $img_height * $scale;
|
||||
|
||||
|
||||
$res=true;
|
||||
$res=vignette($file_path,$options['max_width'],$options['max_height'],'_mini');
|
||||
/* Replaced with more efficient function vignette
|
||||
$new_img = @imagecreatetruecolor($new_width, $new_height);
|
||||
switch (strtolower(substr(strrchr($file_name, '.'), 1))) {
|
||||
case 'jpg':
|
||||
@ -164,14 +179,18 @@ class UploadHandler
|
||||
// Free up memory (imagedestroy does not delete files):
|
||||
@imagedestroy($src_img);
|
||||
@imagedestroy($new_img);
|
||||
return $success;
|
||||
*/
|
||||
|
||||
//return $success;
|
||||
if (preg_match('/error/i',$res)) return false;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function has_error($uploaded_file, $file, $error) {
|
||||
if ($error) {
|
||||
return $error;
|
||||
@ -201,7 +220,7 @@ class UploadHandler
|
||||
}
|
||||
return $error;
|
||||
}
|
||||
|
||||
|
||||
private function handle_file_upload($uploaded_file, $name, $size, $type, $error) {
|
||||
$file = new stdClass();
|
||||
$file->name = basename(stripslashes($name));
|
||||
@ -238,10 +257,12 @@ class UploadHandler
|
||||
$file_size = filesize($file_path);
|
||||
if ($file_size === $file->size) {
|
||||
$file->url = $this->options['upload_url'].rawurlencode($file->name);
|
||||
foreach($this->options['image_versions'] as $version => $options) {
|
||||
if ($this->create_scaled_image($file->name, $options)) {
|
||||
$file->{$version.'_url'} = $options['upload_url']
|
||||
.rawurlencode($file->name);
|
||||
foreach($this->options['image_versions'] as $version => $options)
|
||||
{
|
||||
if ($this->create_scaled_image($file->name, $options))
|
||||
{
|
||||
$tmp=explode('.',$file->name);
|
||||
$file->{$version.'_url'} = $options['upload_url'].rawurlencode($tmp[0].'_mini.'.$tmp[1]);
|
||||
}
|
||||
}
|
||||
} else if ($this->options['discard_aborted_uploads']) {
|
||||
@ -257,10 +278,10 @@ class UploadHandler
|
||||
}
|
||||
return $file;
|
||||
}
|
||||
|
||||
|
||||
public function get() {
|
||||
$file_name = isset($_REQUEST['file']) ?
|
||||
basename(stripslashes($_REQUEST['file'])) : null;
|
||||
basename(stripslashes($_REQUEST['file'])) : null;
|
||||
if ($file_name) {
|
||||
$info = $this->get_file_object($file_name);
|
||||
} else {
|
||||
@ -269,7 +290,7 @@ class UploadHandler
|
||||
header('Content-type: application/json');
|
||||
echo json_encode($info);
|
||||
}
|
||||
|
||||
|
||||
public function post() {
|
||||
$upload = isset($_FILES[$this->options['param_name']]) ?
|
||||
$_FILES[$this->options['param_name']] : array(
|
||||
@ -314,7 +335,7 @@ class UploadHandler
|
||||
}
|
||||
echo json_encode($info);
|
||||
}
|
||||
|
||||
|
||||
public function delete() {
|
||||
$file_name = isset($_REQUEST['file']) ?
|
||||
basename(stripslashes($_REQUEST['file'])) : null;
|
||||
@ -333,6 +354,12 @@ class UploadHandler
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* View
|
||||
*/
|
||||
|
||||
$upload_handler = new UploadHandler(null,$fk_element,$element);
|
||||
|
||||
header('Pragma: no-cache');
|
||||
@ -352,5 +379,10 @@ switch ($_SERVER['REQUEST_METHOD']) {
|
||||
break;
|
||||
default:
|
||||
header('HTTP/1.0 405 Method Not Allowed');
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
$db->close();
|
||||
|
||||
?>
|
||||
@ -22,7 +22,7 @@
|
||||
* \file htdocs/core/class/html.formfile.class.php
|
||||
* \ingroup core
|
||||
* \brief File of class to offer components to list and upload files
|
||||
* \version $Id: html.formfile.class.php,v 1.38 2011/07/05 09:14:27 hregis Exp $
|
||||
* \version $Id: html.formfile.class.php,v 1.39 2011/07/05 22:40:36 eldy Exp $
|
||||
*/
|
||||
|
||||
|
||||
@ -738,7 +738,7 @@ class FormFile
|
||||
function form_ajaxfileupload($object)
|
||||
{
|
||||
global $langs;
|
||||
|
||||
|
||||
// PHP post_max_size
|
||||
$post_max_size = ini_get('post_max_size');
|
||||
$mul_post_max_size = substr($post_max_size, -1);
|
||||
@ -751,16 +751,16 @@ class FormFile
|
||||
$upload_max_filesize = $mul_upload_max_filesize*(int)$upload_max_filesize;
|
||||
// Max file size
|
||||
$max_file_size = (($post_max_size < $upload_max_filesize) ? $post_max_size : $upload_max_filesize);
|
||||
|
||||
|
||||
print '<script type="text/javascript">
|
||||
$(function () {
|
||||
\'use strict\';
|
||||
|
||||
|
||||
var max_file_size = \''.$max_file_size.'\';
|
||||
|
||||
|
||||
// Initialize the jQuery File Upload widget:
|
||||
$("#fileupload").fileupload( { maxFileSize: max_file_size} );
|
||||
|
||||
|
||||
// Load existing files:
|
||||
$.getJSON($("#fileupload form").prop("action"), { fk_element: "'.$object->id.'", element: "'.$object->element.'"}, function (files) {
|
||||
var fu = $("#fileupload").data("fileupload");
|
||||
@ -772,7 +772,7 @@ class FormFile
|
||||
$(this).show();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// Open download dialogs via iframes,
|
||||
// to prevent aborting current uploads:
|
||||
$("#fileupload .files a:not([target^=_blank])").live("click", function (e) {
|
||||
@ -781,11 +781,11 @@ class FormFile
|
||||
.prop("src", this.href)
|
||||
.appendTo("body");
|
||||
});
|
||||
|
||||
|
||||
// Confirm delete file
|
||||
$("#fileupload").fileupload({
|
||||
destroy: function (e, data) {
|
||||
var that = $(this).data("fileupload");
|
||||
var that = $(this).data("fileupload");
|
||||
if ( confirm("Delete this file ?") == true ) {
|
||||
if (data.url) {
|
||||
$.ajax(data)
|
||||
@ -805,7 +805,7 @@ class FormFile
|
||||
});
|
||||
});
|
||||
</script>';
|
||||
|
||||
|
||||
print '<div id="fileupload">';
|
||||
print '<form action="'.DOL_URL_ROOT.'/core/ajaxfileupload.php" method="POST" enctype="multipart/form-data">';
|
||||
print '<input type="hidden" name="fk_element" value="'.$object->id.'">';
|
||||
@ -820,6 +820,7 @@ class FormFile
|
||||
print '<button type="reset" class="cancel">'.$langs->trans('CancelUpload').'</button>';
|
||||
print '</div></form>';
|
||||
print '<div class="fileupload-content">';
|
||||
|
||||
print '<table width="100%" class="files">';
|
||||
print '<tr class="liste_titre">';
|
||||
print '<td>'.$langs->trans("Documents2").'</td>';
|
||||
@ -828,10 +829,12 @@ class FormFile
|
||||
print '<td colspan="3"></td>';
|
||||
print '</tr>';
|
||||
print '</table>';
|
||||
|
||||
print '<div class="fileupload-progressbar"></div>';
|
||||
|
||||
print '</div>';
|
||||
print '</div>';
|
||||
|
||||
|
||||
// Include template
|
||||
include(DOL_DOCUMENT_ROOT.'/core/tpl/ajaxfileupload.tpl.php');
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* $Id: ajaxfileupload.tpl.php,v 1.6 2011/07/05 09:30:11 hregis Exp $
|
||||
* $Id: ajaxfileupload.tpl.php,v 1.7 2011/07/05 22:40:35 eldy Exp $
|
||||
*/
|
||||
?>
|
||||
|
||||
@ -65,22 +65,12 @@
|
||||
{{else}}${error}
|
||||
{{/if}}
|
||||
</td>
|
||||
{{else}}
|
||||
<td class="name">
|
||||
<img src="<?php echo DOL_URL_ROOT; ?>/theme/common/mime/${mime}" border="0">
|
||||
<a href="${url}"{{if thumbnail_url}} target="_blank"{{/if}}>${name}</a>
|
||||
<td align="right" class="delete">
|
||||
<button data-type="${delete_type}" data-url="${delete_url}"><?php echo $langs->trans('Delete'); ?></button>
|
||||
</td>
|
||||
<td class="preview">
|
||||
{{if thumbnail_url}}
|
||||
<a href="${url}" target="_blank"><img src="${thumbnail_url}"></a>
|
||||
{{/if}}
|
||||
</td>
|
||||
<td class="size">${sizef}</td>
|
||||
<td colspan="2"></td>
|
||||
{{/if}}
|
||||
<td align="right" class="delete">
|
||||
<button data-type="${delete_type}" data-url="${delete_url}"><?php echo $langs->trans('Delete'); ?></button>
|
||||
</td>
|
||||
</tr>
|
||||
</script>
|
||||
|
||||
<br>
|
||||
<!-- END PHP TEMPLATE -->
|
||||
@ -21,7 +21,7 @@
|
||||
/**
|
||||
* \file htdocs/lib/images.lib.php
|
||||
* \brief Set of function for manipulating images
|
||||
* \version $Id$
|
||||
* \version $Id: images.lib.php,v 1.22 2011/07/05 22:40:36 eldy Exp $
|
||||
*/
|
||||
|
||||
// Define size of logo small and mini
|
||||
@ -287,10 +287,10 @@ function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x=0, $s
|
||||
/**
|
||||
* Create a thumbnail from an image file (Supported extensions are gif, jpg, png and bmp).
|
||||
* If file is myfile.jpg, new file may be myfile_small.jpg
|
||||
* @param file PAth of file to resize
|
||||
* @param file Path of source file to resize
|
||||
* @param maxWidth Largeur maximum que dois faire la miniature (-1=unchanged, 160 by default)
|
||||
* @param maxHeight Hauteur maximum que dois faire l'image (-1=unchanged, 120 by default)
|
||||
* @param extName Extension to differenciate thumb file name
|
||||
* @param extName Extension to differenciate thumb file name ('_small', '_mini')
|
||||
* @param quality Quality of compression (0=worst, 100=best)
|
||||
* @param outdir Directory where to store thumb
|
||||
* @param targetformat New format of target (1,2,3,4 or 0 to keep old format)
|
||||
@ -327,12 +327,12 @@ function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $
|
||||
elseif(!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1){
|
||||
// Si la largeur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0)
|
||||
dol_syslog('Wrong value for parameter maxWidth',LOG_ERR);
|
||||
return 'Wrong value for parameter maxWidth';
|
||||
return 'Error: Wrong value for parameter maxWidth';
|
||||
}
|
||||
elseif(!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1){
|
||||
// Si la hauteur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0)
|
||||
dol_syslog('Wrong value for parameter maxHeight',LOG_ERR);
|
||||
return 'Wrong value for parameter maxHeight';
|
||||
return 'Error: Wrong value for parameter maxHeight';
|
||||
}
|
||||
|
||||
$fichier = realpath($file); // Chemin canonique absolu de l'image
|
||||
@ -374,7 +374,7 @@ function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $
|
||||
if (! function_exists($imgfonction))
|
||||
{
|
||||
// Fonctions de conversion non presente dans ce PHP
|
||||
return 'Creation de vignette impossible. Ce PHP ne supporte pas les fonctions du module GD '.$imgfonction;
|
||||
return 'Error: Creation of thumbs not possible. This PHP does not support GD function '.$imgfonction;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -23,7 +23,7 @@
|
||||
* \file htdocs/societe/document.php
|
||||
* \brief Tab for documents linked to third party
|
||||
* \ingroup societe
|
||||
* \version $Id: document.php,v 1.33 2011/07/05 17:21:19 hregis Exp $
|
||||
* \version $Id: document.php,v 1.34 2011/07/05 22:40:35 eldy Exp $
|
||||
*/
|
||||
|
||||
require("../main.inc.php");
|
||||
@ -85,7 +85,7 @@ if ( $_POST["sendit"] && ! empty($conf->global->MAIN_UPLOAD_DOC))
|
||||
// Create small thumbs for company (Ratio is near 16/9)
|
||||
// Used on logon for example
|
||||
$imgThumbSmall = vignette($upload_dir . "/" . $_FILES['userfile']['name'], $maxwidthsmall, $maxheightsmall, '_small', $quality, "thumbs");
|
||||
|
||||
|
||||
// Create mini thumbs for company (Ratio is near 16/9)
|
||||
// Used on menu or for setup page for example
|
||||
$imgThumbMini = vignette($upload_dir . "/" . $_FILES['userfile']['name'], $maxwidthmini, $maxheightmini, '_mini', $quality, "thumbs");
|
||||
@ -209,23 +209,22 @@ if ($socid > 0)
|
||||
$ret=$html->form_confirm($_SERVER["PHP_SELF"].'?socid='.$socid.'&urlfile='.urldecode($_GET["urlfile"]), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', 0, 1);
|
||||
if ($ret == 'html') print '<br>';
|
||||
}
|
||||
|
||||
|
||||
$formfile=new FormFile($db);
|
||||
|
||||
|
||||
// Show upload form
|
||||
if ($conf->global->MAIN_USE_JQUERY_FILEUPLOAD)
|
||||
{
|
||||
$formfile->form_ajaxfileupload($object);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Affiche formulaire upload
|
||||
$formfile->form_attach_new_file($_SERVER["PHP_SELF"].'?socid='.$socid,'',0,0,$user->rights->societe->creer);
|
||||
|
||||
|
||||
// List of document
|
||||
$param='&socid='.$object->id;
|
||||
$formfile->list_of_documents($filearray,$object,'societe',$param);
|
||||
}
|
||||
}
|
||||
|
||||
// List of document
|
||||
$param='&socid='.$object->id;
|
||||
$formfile->list_of_documents($filearray,$object,'societe',$param);
|
||||
|
||||
print "<br><br>";
|
||||
|
||||
@ -289,6 +288,6 @@ else
|
||||
$db->close();
|
||||
|
||||
|
||||
llxFooter('$Date: 2011/07/05 17:21:19 $ - $Revision: 1.33 $');
|
||||
llxFooter('$Date: 2011/07/05 22:40:35 $ - $Revision: 1.34 $');
|
||||
|
||||
?>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user