v1.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace zin;
  3. /**
  4. * @deprecated Use `fileSelector` instead.
  5. */
  6. class upload extends wg
  7. {
  8. /**
  9. * @var mixed[]
  10. */
  11. protected static $defineProps = array(
  12. 'name: string="files[]"', // 字段名
  13. 'id?: string', // 元素 ID
  14. 'icon?: string', // 文件图标
  15. 'showIcon?: bool=true', // 是否展示文件图标
  16. 'showSize?: bool=true', // 是否展示文件大小
  17. 'multiple?: bool=true', // 是否启用多文件上传
  18. 'listPosition?: string="bottom"', // 文件列表位置
  19. 'uploadText?: string', // 上传按钮文本
  20. 'uploadIcon?: string', // 上传按钮图标
  21. 'renameBtn?: bool=true', // 是否启用重命名按钮
  22. 'renameIcon?: string', // 重命名图标
  23. 'renameText?: string', // 重命名文本
  24. 'renameClass?: string', // 重命名按钮类
  25. 'deleteBtn?: bool=true', // 是否启用删除按钮
  26. 'deleteIcon?: string', // 删除图标
  27. 'deleteText?: string', // 删除文本
  28. 'deleteClass?: string', // 删除按钮类
  29. 'confirmText?: string', // 确认按钮文本
  30. 'cancelText?: string', // 取消按钮文本
  31. 'useIconBtn?: string', // 是否启用图标按钮
  32. 'tip?: string', // 提示文本
  33. 'btnClass?: string', // 上传按钮类
  34. 'onAdd?: callable', // 添加文件回调
  35. 'onDelete?: callable', // 删除文件回调
  36. 'onRename?: callable', // 重命名文件回调
  37. 'onSizeChange?: callable', // 文件大小变更回调
  38. 'draggable?: bool=true', // 是否启用拖拽上传
  39. 'limitCount?: int', // 上传文件数量限制
  40. 'accept?: string', // input accept 属性
  41. 'defaultFileList?: object[]', // 默认文件列表
  42. 'limitSize?: false|string=false', // 上传尺寸限制
  43. 'duplicatedHint?: string', // 文件名重复提示
  44. 'exceededSizeHint?: string', // 上传超出大小限制提示
  45. 'exceededCountHint?: string' // 上传超出个数限制提示
  46. );
  47. protected function created()
  48. {
  49. global $lang, $app;
  50. $app->loadLang('file');
  51. /* Check file type. */
  52. $checkFiles = jsCallback('file')
  53. ->const('dangerFileTypes', ",{$app->config->file->dangers},")
  54. ->const('dangerFile', $lang->file->dangerFile)
  55. ->do(<<<'JS'
  56. const typeIndex = file.name.lastIndexOf(".");
  57. const fileType = file.name.slice(typeIndex + 1);
  58. if(dangerFileTypes.indexOf(fileType) > -1)
  59. {
  60. zui.Modal.alert(dangerFile);
  61. return false;
  62. }
  63. JS
  64. );
  65. /* Get onAdd function.*/
  66. $onAdd = $this->prop('onAdd');
  67. if($onAdd)
  68. {
  69. if(is_object($onAdd))
  70. {
  71. /*
  72. * 获取在 ui 界面上通过 jsCallback 和 js 定义的 onAdd 函数。
  73. * eg: 1. $onAdd = jsCallbakc()..;
  74. * fileSelector(set::onAdd($onAdd));
  75. * 2. $onAdd = js()..;
  76. * fileSelector(set::onAdd($onAdd));
  77. */
  78. $objectClass = get_class($onAdd);
  79. if($objectClass == 'zin\js') $onAdd = $onAdd->toJS();
  80. if($objectClass == 'zin\jsCallback') $onAdd = $onAdd->buildBody();
  81. if(!is_object($onAdd)) $checkFiles = $checkFiles->do($onAdd);
  82. }
  83. else
  84. {
  85. /* 获取在 ui 界面上通过 jsRaw 定义的 onAdd 函数。 eg: fileSelector(set::onAdd(jsRaw('window.onAdd'))); */
  86. $onAdd = js::value($onAdd);
  87. $checkFiles = $checkFiles->call($onAdd, jsRaw('file'));
  88. }
  89. }
  90. $checkFiles = $checkFiles->do('return file');
  91. $this->setProp('onAdd', $checkFiles);
  92. }
  93. protected function build()
  94. {
  95. global $lang, $app;
  96. if(!$this->prop('class')) $this->setProp('class', 'w-full');
  97. if(!$this->prop('icon')) $this->setProp('icon', 'paper-clip');
  98. if(!$this->prop('tip')) $this->setProp('tip', sprintf($lang->noticeDrag, strtoupper(ini_get('upload_max_filesize'))));
  99. if($this->prop('limitCount') && !$this->prop('exceededCountHint'))
  100. {
  101. $app->loadLang('file');
  102. $this->setProp('exceededCountHint', sprintf($lang->file->errorFileCount, $this->prop('limitCount')));
  103. }
  104. $name = $this->prop('name');
  105. if($this->prop('multiple') && strpos($name, '[]') === false) $this->setProp('name', $name . '[]');
  106. if($app->getClientLang() == 'en') $this->setProp('uploadText', $lang->uploadFiles);
  107. $otherProps = $this->getRestProps();
  108. return zui::upload(inherit($this), set('_props', $otherProps));
  109. }
  110. }