3
* CMultiFileUpload class file.
5
* @author Qiang Xue <qiang.xue@gmail.com>
6
* @link http://www.yiiframework.com/
7
* @copyright Copyright © 2008-2011 Yii Software LLC
8
* @license http://www.yiiframework.com/license/
12
* CMultiFileUpload generates a file input that can allow uploading multiple files at a time.
14
* This is based on the {@link http://www.fyneworks.com/jquery/multiple-file-upload/ jQuery Multi File Upload plugin}.
15
* The uploaded file information can be accessed via $_FILES[widget-name], which gives an array of the uploaded
16
* files. Note, you have to set the enclosing form's 'enctype' attribute to be 'multipart/form-data'.
21
* $this->widget('CMultiFileUpload', array(
23
* 'attribute'=>'files',
24
* 'accept'=>'jpg|gif',
26
* 'onFileSelect'=>'function(e, v, m){ alert("onFileSelect - "+v) }',
27
* 'afterFileSelect'=>'function(e, v, m){ alert("afterFileSelect - "+v) }',
28
* 'onFileAppend'=>'function(e, v, m){ alert("onFileAppend - "+v) }',
29
* 'afterFileAppend'=>'function(e, v, m){ alert("afterFileAppend - "+v) }',
30
* 'onFileRemove'=>'function(e, v, m){ alert("onFileRemove - "+v) }',
31
* 'afterFileRemove'=>'function(e, v, m){ alert("afterFileRemove - "+v) }',
37
* @author Qiang Xue <qiang.xue@gmail.com>
38
* @version $Id: CMultiFileUpload.php 3221 2011-05-13 12:00:40Z mdomba $
39
* @package system.web.widgets
42
class CMultiFileUpload extends CInputWidget
45
* @var string the file types that are allowed (eg "gif|jpg").
46
* Note, the server side still needs to check if the uploaded files have allowed types.
50
* @var integer the maximum number of files that can be uploaded. If -1, it means no limits. Defaults to -1.
54
* @var string the label for the remove button. Defaults to "Remove".
58
* @var string message that is displayed when a file type is not allowed.
62
* @var string message that is displayed when a file is selected.
66
* @var string message that is displayed when a file appears twice.
70
* @var string the message template for displaying the uploaded file name
75
* @var array additional options that can be passed to the constructor of the multifile js object.
78
public $options=array();
83
* This method registers all needed client scripts and renders
84
* the multiple file uploader.
88
list($name,$id)=$this->resolveNameID();
89
if(substr($name,-2)!=='[]')
91
if(isset($this->htmlOptions['id']))
92
$id=$this->htmlOptions['id'];
94
$this->htmlOptions['id']=$id;
95
$this->registerClientScript();
96
echo CHtml::fileField($name,'',$this->htmlOptions);
100
* Registers the needed CSS and JavaScript.
103
public function registerClientScript()
105
$id=$this->htmlOptions['id'];
107
$options=$this->getClientOptions();
108
$options=$options===array()? '' : CJavaScript::encode($options);
110
$cs=Yii::app()->getClientScript();
111
$cs->registerCoreScript('multifile');
112
$cs->registerScript('Yii.CMultiFileUpload#'.$id,"jQuery(\"#{$id}\").MultiFile({$options});");
116
* @return array the javascript options
118
protected function getClientOptions()
120
$options=$this->options;
121
foreach(array('onFileRemove','afterFileRemove','onFileAppend','afterFileAppend','onFileSelect','afterFileSelect') as $event)
123
if(isset($options[$event]) && strpos($options[$event],'js:')!==0)
124
$options[$event]='js:'.$options[$event];
127
if($this->accept!==null)
128
$options['accept']=$this->accept;
130
$options['max']=$this->max;
133
foreach(array('remove','denied','selected','duplicate','file') as $messageName)
135
if($this->$messageName!==null)
136
$messages[$messageName]=$this->$messageName;
138
if($messages!==array())
139
$options['STRING']=$messages;