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.).
This commit is contained in:
Ralf Becker 2009-02-26 13:51:25 +00:00
parent 684ad891b8
commit a5a28ad708
2 changed files with 61 additions and 16 deletions

View File

@ -1525,10 +1525,25 @@ class etemplate extends boetemplate
if (!$readonly) if (!$readonly)
{ {
if ((int) $cell_options) $options .= ' size="'.(int)$cell_options.'"'; 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); $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']; $GLOBALS['egw_info']['etemplate']['to_process'][$form_name] = $cell['type'];
} }
break; break;
@ -2071,21 +2086,39 @@ class etemplate extends boetemplate
} }
break; break;
case 'file': case 'file':
if (($multiple = substr($form_name,-2) == '[]'))
{
$form_name = substr($form_name,0,-2);
}
$parts = explode('[',str_replace(']','',$form_name)); $parts = explode('[',str_replace(']','',$form_name));
$name = array_shift($parts); $name = array_shift($parts);
$index = count($parts) ? '['.implode('][',$parts).']' : ''; $index = count($parts) ? '['.implode('][',$parts).']' : '';
$value = array(); $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]'); //echo $form_name; _debug_array($value);
$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);
// fall-throught // fall-throught
default: default:
$this->set_array($content,$form_name,$value); $this->set_array($content,$form_name,$value);

View File

@ -6,7 +6,7 @@
* @subpackage api * @subpackage api
* @link http://www.egroupware.org * @link http://www.egroupware.org
* @author Ralf Becker <RalfBecker@outdoor-training.de> * @author Ralf Becker <RalfBecker@outdoor-training.de>
* @version $Id$ * @version $Id$
*/ */
function submitit(form,name) function submitit(form,name)
@ -57,12 +57,12 @@ function activate_tab(tab,all_tabs,name)
var tabs = all_tabs.split('|'); var tabs = all_tabs.split('|');
var parts = tab.split('.'); var parts = tab.split('.');
var last_part = parts.length-1; var last_part = parts.length-1;
for (n = 0; n < tabs.length; n++) for (n = 0; n < tabs.length; n++)
{ {
var t = tabs[n]; var t = tabs[n];
if (t.indexOf('.') < 0 && parts.length > 1) if (t.indexOf('.') < 0 && parts.length > 1)
{ {
parts[last_part] = t; parts[last_part] = t;
t = parts.join('.'); t = parts.join('.');
@ -116,7 +116,7 @@ function selectbox_add_option(id,label,value,do_onchange)
function toggle_all(form,name) function toggle_all(form,name)
{ {
var all_set = true; var all_set = true;
/* this is for use with a sub-grid. To use it pass "true" as third parameter */ /* 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) 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 = '';
}