CMultiFileUpload.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * CMultiFileUpload class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CMultiFileUpload generates a file input that can allow uploading multiple files at a time.
  12. *
  13. * This is based on the {@link http://www.fyneworks.com/jquery/multiple-file-upload/ jQuery Multi File Upload plugin}.
  14. * The uploaded file information can be accessed via $_FILES[widget-name], which gives an array of the uploaded
  15. * files. Note, you have to set the enclosing form's 'enctype' attribute to be 'multipart/form-data'.
  16. *
  17. * Example:
  18. * <pre>
  19. * <?php
  20. * $this->widget('CMultiFileUpload', array(
  21. * 'model'=>$model,
  22. * 'attribute'=>'files',
  23. * 'accept'=>'jpg|gif',
  24. * 'options'=>array(
  25. * 'onFileSelect'=>'function(e, v, m){ alert("onFileSelect - "+v) }',
  26. * 'afterFileSelect'=>'function(e, v, m){ alert("afterFileSelect - "+v) }',
  27. * 'onFileAppend'=>'function(e, v, m){ alert("onFileAppend - "+v) }',
  28. * 'afterFileAppend'=>'function(e, v, m){ alert("afterFileAppend - "+v) }',
  29. * 'onFileRemove'=>'function(e, v, m){ alert("onFileRemove - "+v) }',
  30. * 'afterFileRemove'=>'function(e, v, m){ alert("afterFileRemove - "+v) }',
  31. * ),
  32. * ));
  33. * ?>
  34. * </pre>
  35. *
  36. * @author Qiang Xue <qiang.xue@gmail.com>
  37. * @package system.web.widgets
  38. * @since 1.0
  39. */
  40. class CMultiFileUpload extends CInputWidget
  41. {
  42. /**
  43. * @var string the file types that are allowed (eg "gif|jpg").
  44. * Note, the server side still needs to check if the uploaded files have allowed types.
  45. */
  46. public $accept;
  47. /**
  48. * @var integer the maximum number of files that can be uploaded. If -1, it means no limits. Defaults to -1.
  49. */
  50. public $max=-1;
  51. /**
  52. * @var string the label for the remove button. Defaults to "Remove".
  53. */
  54. public $remove;
  55. /**
  56. * @var string message that is displayed when a file type is not allowed.
  57. */
  58. public $denied;
  59. /**
  60. * @var string message that is displayed when a file is selected.
  61. */
  62. public $selected;
  63. /**
  64. * @var string message that is displayed when a file appears twice.
  65. */
  66. public $duplicate;
  67. /**
  68. * @var string the message template for displaying the uploaded file name
  69. * @since 1.1.3
  70. */
  71. public $file;
  72. /**
  73. * @var array additional options that can be passed to the constructor of the multifile js object.
  74. * @since 1.1.7
  75. */
  76. public $options=array();
  77. /**
  78. * Runs the widget.
  79. * This method registers all needed client scripts and renders
  80. * the multiple file uploader.
  81. */
  82. public function run()
  83. {
  84. list($name,$id)=$this->resolveNameID();
  85. if(substr($name,-2)!=='[]')
  86. $name.='[]';
  87. if(isset($this->htmlOptions['id']))
  88. $id=$this->htmlOptions['id'];
  89. else
  90. $this->htmlOptions['id']=$id;
  91. $this->registerClientScript();
  92. echo CHtml::fileField($name,'',$this->htmlOptions);
  93. }
  94. /**
  95. * Registers the needed CSS and JavaScript.
  96. */
  97. public function registerClientScript()
  98. {
  99. $id=$this->htmlOptions['id'];
  100. $options=$this->getClientOptions();
  101. $options=$options===array()? '' : CJavaScript::encode($options);
  102. $cs=Yii::app()->getClientScript();
  103. $cs->registerCoreScript('multifile');
  104. $cs->registerScript('Yii.CMultiFileUpload#'.$id,"jQuery(\"#{$id}\").MultiFile({$options});");
  105. }
  106. /**
  107. * @return array the javascript options
  108. */
  109. protected function getClientOptions()
  110. {
  111. $options=$this->options;
  112. foreach(array('onFileRemove','afterFileRemove','onFileAppend','afterFileAppend','onFileSelect','afterFileSelect') as $event)
  113. {
  114. if(isset($options[$event]) && !($options[$event] instanceof CJavaScriptExpression))
  115. $options[$event]=new CJavaScriptExpression($options[$event]);
  116. }
  117. if($this->accept!==null)
  118. $options['accept']=$this->accept;
  119. if($this->max>0)
  120. $options['max']=$this->max;
  121. $messages=array();
  122. foreach(array('remove','denied','selected','duplicate','file') as $messageName)
  123. {
  124. if($this->$messageName!==null)
  125. $messages[$messageName]=$this->$messageName;
  126. }
  127. if($messages!==array())
  128. $options['STRING']=$messages;
  129. return $options;
  130. }
  131. }