From 68029751fd7a6809b31f15bf83d5925f4bf36727 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 5 Jul 2011 22:40:35 +0000 Subject: [PATCH] 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. --- htdocs/core/ajaxfileupload.php | 74 ++++++++++++++++------- htdocs/core/class/html.formfile.class.php | 25 ++++---- htdocs/core/tpl/ajaxfileupload.tpl.php | 20 ++---- htdocs/lib/images.lib.php | 12 ++-- htdocs/societe/document.php | 23 ++++--- 5 files changed, 89 insertions(+), 65 deletions(-) diff --git a/htdocs/core/ajaxfileupload.php b/htdocs/core/ajaxfileupload.php index cc51d9c2836..8af04307419 100644 --- a/htdocs/core/ajaxfileupload.php +++ b/htdocs/core/ajaxfileupload.php @@ -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(); + ?> \ No newline at end of file diff --git a/htdocs/core/class/html.formfile.class.php b/htdocs/core/class/html.formfile.class.php index c08006e2935..46d699019fc 100644 --- a/htdocs/core/class/html.formfile.class.php +++ b/htdocs/core/class/html.formfile.class.php @@ -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 ''; - + print '
'; print '
'; print ''; @@ -820,6 +820,7 @@ class FormFile print ''; print '
'; print '
'; + print ''; print ''; print ''; @@ -828,10 +829,12 @@ class FormFile print ''; print ''; print '
'.$langs->trans("Documents2").'
'; + print '
'; + print '
'; print ''; - + // Include template include(DOL_DOCUMENT_ROOT.'/core/tpl/ajaxfileupload.tpl.php'); diff --git a/htdocs/core/tpl/ajaxfileupload.tpl.php b/htdocs/core/tpl/ajaxfileupload.tpl.php index f1d6026c981..cfbfdad12e3 100644 --- a/htdocs/core/tpl/ajaxfileupload.tpl.php +++ b/htdocs/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}} - {{else}} - - - ${name} + + - - {{if thumbnail_url}} - - {{/if}} - - ${sizef} - {{/if}} - - - + +
\ No newline at end of file diff --git a/htdocs/lib/images.lib.php b/htdocs/lib/images.lib.php index 4a91c5af79f..d7841216fdb 100644 --- a/htdocs/lib/images.lib.php +++ b/htdocs/lib/images.lib.php @@ -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; } } diff --git a/htdocs/societe/document.php b/htdocs/societe/document.php index c5af4de609c..9a6495b4c6e 100644 --- a/htdocs/societe/document.php +++ b/htdocs/societe/document.php @@ -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 '
'; } - + $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 "

"; @@ -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 $'); ?>