MultipleWidget.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace common\modules\attachment\widgets;
  3. use yii\base\Arrayable;
  4. use yii\helpers\ArrayHelper;
  5. use yii\helpers\Html;
  6. use yii\helpers\Json;
  7. use yii\helpers\Url;
  8. use yii\jui\JuiAsset;
  9. use yii\widgets\InputWidget;
  10. class MultipleWidget extends InputWidget
  11. {
  12. public $onlyImage = true;
  13. public $wrapperOptions;
  14. /**
  15. *
  16. * @var array
  17. */
  18. public $clientOptions = [];
  19. /*
  20. * ----------------------------------------------
  21. * 客户端选项,构成$clientOptions
  22. * ----------------------------------------------
  23. */
  24. /**
  25. *
  26. * @var array 上传url地址
  27. */
  28. public $url = [];
  29. /**
  30. * 这里为了配合后台方便处理所有都是设为true,文件上传数目请控制好 $maxNumberOfFiles
  31. * @var bool
  32. */
  33. public $multiple = true;
  34. /**
  35. *
  36. * @var bool
  37. */
  38. public $sortable = false;
  39. /**
  40. *
  41. * @var int 允许上传的最大文件数目
  42. */
  43. public $maxNumberOfFiles = 50;
  44. /**
  45. *
  46. * @var int 允许上传文件最大限制
  47. */
  48. public $maxFileSize;
  49. /**
  50. *
  51. * @var string 允许上传的附件类型
  52. */
  53. public $acceptFileTypes;
  54. /*
  55. * ----------------------------------------------
  56. * 客户端选项,构成$clientOptions
  57. * ----------------------------------------------
  58. */
  59. public $deleteUrl = ["/upload/delete"];
  60. public $fileInputName;
  61. public $onlyUrl = false;
  62. /**
  63. *
  64. * @throws \yii\base\InvalidConfigException
  65. */
  66. public function init()
  67. {
  68. parent::init();
  69. if (empty($this->url)) {
  70. if ($this->onlyImage === false) {
  71. $this->url = $this->multiple ? ['/upload/files-upload'] : ['/upload/file-upload'];
  72. // $this->acceptFileTypes = 'image/png, image/jpg, image/jpeg, image/gif, image/bmp, application/x-zip-compressed';
  73. } else {
  74. $this->url = $this->multiple ? ['/upload/images-upload'] : ['/upload/image-upload'];
  75. // $this->acceptFileTypes = 'image/png, image/jpg, image/jpeg, image/gif, image/bmp';
  76. }
  77. }
  78. if ($this->hasModel()) {
  79. $this->name = $this->name ? : Html::getInputName($this->model, $this->attribute);
  80. $this->attribute = Html::getAttributeName($this->attribute);
  81. $value = $this->model->{$this->attribute};
  82. $attachments = $this->multiple == true ? $value :[$value];
  83. $this->value = [];
  84. if ($attachments) {
  85. foreach ($attachments as $attachment) {
  86. $value = $this->formatAttachment($attachment);
  87. if ($value) {
  88. $this->value[] = $value;
  89. }
  90. }
  91. }
  92. }
  93. $this->fileInputName = md5($this->name);
  94. if (! array_key_exists('fileparam', $this->url)) {
  95. $this->url['fileparam'] = $this->fileInputName;//服务器需要通过这个判断是哪一个input name上传的
  96. }
  97. $this->clientOptions = ArrayHelper::merge($this->clientOptions, [
  98. 'id' => $this->options['id'],
  99. 'name'=> $this->name, //主要用于上传后返回的项目name
  100. 'url' => Url::to($this->url),
  101. 'multiple' => $this->multiple,
  102. 'sortable' => $this->sortable,
  103. 'maxNumberOfFiles' => $this->maxNumberOfFiles,
  104. 'maxFileSize' => $this->maxFileSize,
  105. 'acceptFileTypes' => $this->acceptFileTypes,
  106. 'files' => $this->value?:[]
  107. ]);
  108. }
  109. protected function formatAttachment($attachment)
  110. {
  111. if (!empty($attachment) && is_string($attachment)) {
  112. return [
  113. 'url' => $attachment,
  114. 'path' => $attachment,
  115. ];
  116. } else if (is_array($attachment)) {
  117. return $attachment;
  118. } else if ($attachment instanceof Arrayable)
  119. return $attachment->toArray();
  120. return [];
  121. }
  122. /**
  123. *
  124. * @return string
  125. */
  126. public function run()
  127. {
  128. $this->registerClientScript();
  129. $content = Html::hiddenInput($this->name . ($this->multiple ? '[]' : ''), null, $this->options);
  130. $content .= Html::beginTag('div',$this->wrapperOptions);
  131. $content .= Html::fileInput($this->fileInputName, null, [
  132. 'id' => $this->fileInputName,
  133. 'multiple' => $this->multiple,
  134. 'accept' => $this->acceptFileTypes
  135. ]);
  136. $content .= Html::endTag('div');
  137. return $content;
  138. }
  139. /**
  140. * Registers required script for the plugin to work as jQuery File Uploader
  141. */
  142. public function registerClientScript()
  143. {
  144. Html::addCssClass($this->wrapperOptions, "upload-kit");
  145. AttachmentUploadAsset::register($this->getView());
  146. if ($this->sortable) {
  147. JuiAsset::register($this->getView());
  148. }
  149. $this->clientOptions['onlyUrl'] = $this->onlyUrl;
  150. $options = Json::encode($this->clientOptions);
  151. $this->getView()->registerJs("jQuery('#{$this->fileInputName}').attachmentUpload({$options});");
  152. }
  153. }