FileWidget.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace common\modules\attachment\widgets;
  3. use common\modules\attachment\models\Attachment;
  4. use yii\helpers\Html;
  5. use yii\helpers\Json;
  6. use yii\jui\JuiAsset;
  7. class FileWidget extends MultipleWidget
  8. {
  9. public $multiple = false;
  10. public $url = [
  11. '/upload/file-upload'
  12. ];
  13. public $maxFileSize = 0;
  14. /**
  15. * Registers required script for the plugin to work as jQuery File Uploader
  16. */
  17. public function registerClientScript()
  18. {
  19. Html::addCssClass($this->wrapperOptions, " upload-kit-input");
  20. FileUploadAsset::register($this->getView());
  21. if ($this->sortable) {
  22. JuiAsset::register($this->getView());
  23. }
  24. $options = Json::encode($this->clientOptions);
  25. $this->getView()->registerJs("jQuery('#{$this->options['id']}').attachmentFileUpload({$options});");
  26. }
  27. protected function formartAttachment($attachment)
  28. {
  29. if (is_string($attachment) && !empty($attachment)) {
  30. $model = Attachment::find()->where(['url' => $attachment])->one();
  31. return [
  32. "url"=>$attachment,
  33. "path"=>$attachment,
  34. 'filename' => $model ? $model->name : $attachment
  35. ];
  36. } else if (is_array($attachment)) {
  37. return $attachment;
  38. }
  39. return null;
  40. }
  41. }