From a5a28ad70844e735aa235ad6b9ce2659790aa44f Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Thu, 26 Feb 2009 13:51:25 +0000 Subject: [PATCH] Enhanced file upload to allow user to upload multiple files by appending [] to the name of the widget, eg. "upload[]". In that case attaching a file adds an other file upload via javascript direct under the current upload and etemplate returns an array of files (each with keys 'tmp_name', 'name', etc.). --- etemplate/inc/class.etemplate.inc.php | 57 +++++++++++++++++++++------ etemplate/js/etemplate.js | 20 ++++++++-- 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/etemplate/inc/class.etemplate.inc.php b/etemplate/inc/class.etemplate.inc.php index 6234708b6f..39f0375c51 100644 --- a/etemplate/inc/class.etemplate.inc.php +++ b/etemplate/inc/class.etemplate.inc.php @@ -1525,10 +1525,25 @@ class etemplate extends boetemplate if (!$readonly) { if ((int) $cell_options) $options .= ' size="'.(int)$cell_options.'"'; - $html .= html::input_hidden($path_name = str_replace($name,$name.'_path',$form_name),'.'); + if (substr($name,-2) == '[]') + { + $GLOBALS['egw_info']['etemplate']['form_options'] .= ' enctype="multipart/form-data"'; + if (strpos($options,'onChange="') !== false) + { + $options = preg_replace('/onChange="([^"]+)"/i','onChange="\\1; add_upload(this);"',$options); + } + else + { + $options .= ' onChange="add_upload(this);"'; + } + } + else + { + $html .= html::input_hidden($path_name = str_replace($name,$name.'_path',$form_name),'.'); + $GLOBALS['egw_info']['etemplate']['form_options'] .= + " enctype=\"multipart/form-data\" onsubmit=\"set_element2(this,'$path_name','$form_name')\""; + } $html .= html::input($form_name,'','file',$options); - $GLOBALS['egw_info']['etemplate']['form_options'] = - "enctype=\"multipart/form-data\" onsubmit=\"set_element2(this,'$path_name','$form_name')\""; $GLOBALS['egw_info']['etemplate']['to_process'][$form_name] = $cell['type']; } break; @@ -2071,21 +2086,39 @@ class etemplate extends boetemplate } break; case 'file': + if (($multiple = substr($form_name,-2) == '[]')) + { + $form_name = substr($form_name,0,-2); + } $parts = explode('[',str_replace(']','',$form_name)); $name = array_shift($parts); $index = count($parts) ? '['.implode('][',$parts).']' : ''; $value = array(); - foreach(array('tmp_name','type','size','name') as $part) + for($i=0; $i < 100; ++$i) { - $value[$part] = is_array($_FILES[$name]) ? $this->get_array($_FILES[$name],$part.$index) : False; + $file = array(); + foreach(array('tmp_name','type','size','name','error') as $part) + { + $file[$part] = $this->get_array($_FILES[$name],$part.$index.($multiple ? "[$i]" : '')); + } + if (!$multiple) $file['path'] = $this->get_array($content_in,substr($form_name,0,-1).'_path]'); + $file['ip'] = get_var('REMOTE_ADDR',Array('SERVER')); + if ((string)$file['tmp_name'] === '' || function_exists('is_uploaded_file') && !is_uploaded_file($file['tmp_name'])) + { + if ($multiple && ($file['tmp_name'] === '' || $file['error'])) + { + continue; // ignore empty upload box + } + break; + } + if (!$multiple) + { + $value = $file; + break; + } + $value[] = $file; } - $value['path'] = $this->get_array($content_in,substr($form_name,0,-1).'_path]'); - $value['ip'] = get_var('REMOTE_ADDR',Array('SERVER')); - if (function_exists('is_uploaded_file') && !is_uploaded_file($value['tmp_name'])) - { - $value = array(); // to be on the save side - } - //_debug_array($value); + //echo $form_name; _debug_array($value); // fall-throught default: $this->set_array($content,$form_name,$value); diff --git a/etemplate/js/etemplate.js b/etemplate/js/etemplate.js index 8fc6c171a2..8d55d8f353 100644 --- a/etemplate/js/etemplate.js +++ b/etemplate/js/etemplate.js @@ -6,7 +6,7 @@ * @subpackage api * @link http://www.egroupware.org * @author Ralf Becker - * @version $Id$ + * @version $Id$ */ function submitit(form,name) @@ -57,12 +57,12 @@ function activate_tab(tab,all_tabs,name) var tabs = all_tabs.split('|'); var parts = tab.split('.'); var last_part = parts.length-1; - + for (n = 0; n < tabs.length; n++) { var t = tabs[n]; - if (t.indexOf('.') < 0 && parts.length > 1) + if (t.indexOf('.') < 0 && parts.length > 1) { parts[last_part] = t; t = parts.join('.'); @@ -116,7 +116,7 @@ function selectbox_add_option(id,label,value,do_onchange) function toggle_all(form,name) { var all_set = true; - + /* this is for use with a sub-grid. To use it pass "true" as third parameter */ if(toggle_all.arguments.length > 2 && toggle_all.arguments[2] == true) { @@ -277,3 +277,15 @@ function set_multiselection(name,values,reset) } } } + +// add an other upload +function add_upload(upload) +{ + var parent = upload.parentNode; + var newUpload = upload.cloneNode(true); + parent.insertBefore(newUpload,upload); + var br = document.createElement('br'); + parent.insertBefore(br,upload); + upload.id += parent.childNodes.length; + upload.value = ''; +}