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:
Laurent Destailleur 2011-07-05 22:40:35 +00:00
parent 0f483a1654
commit 68029751fd
5 changed files with 89 additions and 65 deletions

View File

@ -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) //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 $res=@include("../main.inc.php"); // For "root" directory
if (! $res) $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 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/files.lib.php");
require_once(DOL_DOCUMENT_ROOT."/lib/images.lib.php");
error_reporting(E_ALL | E_STRICT); error_reporting(E_ALL | E_STRICT);
@ -37,6 +38,10 @@ error_reporting(E_ALL | E_STRICT);
$fk_element = GETPOST('fk_element'); $fk_element = GETPOST('fk_element');
$element = GETPOST('element'); $element = GETPOST('element');
/**
*
*/
class UploadHandler class UploadHandler
{ {
private $options; private $options;
@ -97,8 +102,8 @@ class UploadHandler
$file->url = $this->options['upload_url'].rawurlencode($file->name); $file->url = $this->options['upload_url'].rawurlencode($file->name);
foreach($this->options['image_versions'] as $version => $options) { foreach($this->options['image_versions'] as $version => $options) {
if (is_file($options['upload_dir'].$file_name)) { if (is_file($options['upload_dir'].$file_name)) {
$file->{$version.'_url'} = $options['upload_url'] $tmp=explode('.',$file->name);
.rawurlencode($file->name); $file->{$version.'_url'} = $options['upload_url'].rawurlencode($tmp[0].'_mini.'.$tmp[1]);
} }
} }
$file->delete_url = $this->options['script_url'] $file->delete_url = $this->options['script_url']
@ -116,9 +121,14 @@ class UploadHandler
))); )));
} }
/**
* Create thumbs
* options is array('max_width', 'max_height')
*/
private function create_scaled_image($file_name, $options) { private function create_scaled_image($file_name, $options) {
$file_path = $this->options['upload_dir'].$file_name; $file_path = $this->options['upload_dir'].$file_name;
$new_file_path = $options['upload_dir'].$file_name; $new_file_path = $options['upload_dir'].$file_name;
if (create_exdir($options['upload_dir']) >= 0) if (create_exdir($options['upload_dir']) >= 0)
{ {
list($img_width, $img_height) = @getimagesize($file_path); list($img_width, $img_height) = @getimagesize($file_path);
@ -134,6 +144,11 @@ class UploadHandler
} }
$new_width = $img_width * $scale; $new_width = $img_width * $scale;
$new_height = $img_height * $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); $new_img = @imagecreatetruecolor($new_width, $new_height);
switch (strtolower(substr(strrchr($file_name, '.'), 1))) { switch (strtolower(substr(strrchr($file_name, '.'), 1))) {
case 'jpg': case 'jpg':
@ -164,7 +179,11 @@ class UploadHandler
// Free up memory (imagedestroy does not delete files): // Free up memory (imagedestroy does not delete files):
@imagedestroy($src_img); @imagedestroy($src_img);
@imagedestroy($new_img); @imagedestroy($new_img);
return $success; */
//return $success;
if (preg_match('/error/i',$res)) return false;
return true;
} }
else else
{ {
@ -238,10 +257,12 @@ class UploadHandler
$file_size = filesize($file_path); $file_size = filesize($file_path);
if ($file_size === $file->size) { if ($file_size === $file->size) {
$file->url = $this->options['upload_url'].rawurlencode($file->name); $file->url = $this->options['upload_url'].rawurlencode($file->name);
foreach($this->options['image_versions'] as $version => $options) { foreach($this->options['image_versions'] as $version => $options)
if ($this->create_scaled_image($file->name, $options)) { {
$file->{$version.'_url'} = $options['upload_url'] if ($this->create_scaled_image($file->name, $options))
.rawurlencode($file->name); {
$tmp=explode('.',$file->name);
$file->{$version.'_url'} = $options['upload_url'].rawurlencode($tmp[0].'_mini.'.$tmp[1]);
} }
} }
} else if ($this->options['discard_aborted_uploads']) { } else if ($this->options['discard_aborted_uploads']) {
@ -333,6 +354,12 @@ class UploadHandler
} }
} }
/*
* View
*/
$upload_handler = new UploadHandler(null,$fk_element,$element); $upload_handler = new UploadHandler(null,$fk_element,$element);
header('Pragma: no-cache'); header('Pragma: no-cache');
@ -352,5 +379,10 @@ switch ($_SERVER['REQUEST_METHOD']) {
break; break;
default: default:
header('HTTP/1.0 405 Method Not Allowed'); header('HTTP/1.0 405 Method Not Allowed');
exit;
} }
$db->close();
?> ?>

View File

@ -22,7 +22,7 @@
* \file htdocs/core/class/html.formfile.class.php * \file htdocs/core/class/html.formfile.class.php
* \ingroup core * \ingroup core
* \brief File of class to offer components to list and upload files * \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 $
*/ */
@ -820,6 +820,7 @@ class FormFile
print '<button type="reset" class="cancel">'.$langs->trans('CancelUpload').'</button>'; print '<button type="reset" class="cancel">'.$langs->trans('CancelUpload').'</button>';
print '</div></form>'; print '</div></form>';
print '<div class="fileupload-content">'; print '<div class="fileupload-content">';
print '<table width="100%" class="files">'; print '<table width="100%" class="files">';
print '<tr class="liste_titre">'; print '<tr class="liste_titre">';
print '<td>'.$langs->trans("Documents2").'</td>'; print '<td>'.$langs->trans("Documents2").'</td>';
@ -828,7 +829,9 @@ class FormFile
print '<td colspan="3"></td>'; print '<td colspan="3"></td>';
print '</tr>'; print '</tr>';
print '</table>'; print '</table>';
print '<div class="fileupload-progressbar"></div>'; print '<div class="fileupload-progressbar"></div>';
print '</div>'; print '</div>';
print '</div>'; print '</div>';

View File

@ -15,7 +15,7 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 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} {{else}}${error}
{{/if}} {{/if}}
</td> </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>
<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"> <td align="right" class="delete">
<button data-type="${delete_type}" data-url="${delete_url}"><?php echo $langs->trans('Delete'); ?></button> <button data-type="${delete_type}" data-url="${delete_url}"><?php echo $langs->trans('Delete'); ?></button>
</td> </td>
{{/if}}
</tr> </tr>
</script> </script>
<br>
<!-- END PHP TEMPLATE --> <!-- END PHP TEMPLATE -->

View File

@ -21,7 +21,7 @@
/** /**
* \file htdocs/lib/images.lib.php * \file htdocs/lib/images.lib.php
* \brief Set of function for manipulating images * \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 // 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). * 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 * 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 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 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 quality Quality of compression (0=worst, 100=best)
* @param outdir Directory where to store thumb * @param outdir Directory where to store thumb
* @param targetformat New format of target (1,2,3,4 or 0 to keep old format) * @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){ 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) // 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); 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){ 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) // 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); 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 $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)) if (! function_exists($imgfonction))
{ {
// Fonctions de conversion non presente dans ce PHP // 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;
} }
} }

View File

@ -23,7 +23,7 @@
* \file htdocs/societe/document.php * \file htdocs/societe/document.php
* \brief Tab for documents linked to third party * \brief Tab for documents linked to third party
* \ingroup societe * \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"); require("../main.inc.php");
@ -212,20 +212,19 @@ if ($socid > 0)
$formfile=new FormFile($db); $formfile=new FormFile($db);
// Show upload form
if ($conf->global->MAIN_USE_JQUERY_FILEUPLOAD) if ($conf->global->MAIN_USE_JQUERY_FILEUPLOAD)
{ {
$formfile->form_ajaxfileupload($object); $formfile->form_ajaxfileupload($object);
} }
else else
{ {
// Affiche formulaire upload
$formfile->form_attach_new_file($_SERVER["PHP_SELF"].'?socid='.$socid,'',0,0,$user->rights->societe->creer); $formfile->form_attach_new_file($_SERVER["PHP_SELF"].'?socid='.$socid,'',0,0,$user->rights->societe->creer);
}
// List of document // List of document
$param='&socid='.$object->id; $param='&socid='.$object->id;
$formfile->list_of_documents($filearray,$object,'societe',$param); $formfile->list_of_documents($filearray,$object,'societe',$param);
}
print "<br><br>"; print "<br><br>";
@ -289,6 +288,6 @@ else
$db->close(); $db->close();
llxFooter('$Date: 2011/07/05 17:21:19 $ - $Revision: 1.33 $'); llxFooter('$Date: 2011/07/05 22:40:35 $ - $Revision: 1.34 $');
?> ?>