FormHelperTrait.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Module\File\AdminControllers\Helper;
  3. use App\Module\File\Enums\FILE_STATUS;
  4. use App\Module\File\Enums\FILE_VISIBILITY;
  5. use App\Module\File\Enums\STORAGE_STATUS;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Form\Field;
  8. /**
  9. * 表单辅助特性
  10. *
  11. * 提供文件模块后台控制器的表单构建功能的具体实现
  12. */
  13. trait FormHelperTrait
  14. {
  15. /**
  16. * 添加文件上传
  17. *
  18. * @param string $field 字段名
  19. * @param string $label 标签名
  20. * @return Field\File
  21. */
  22. public function fileUpload(string $field = 'file', string $label = '文件上传'): Field\File
  23. {
  24. return $this->form->file($field, $label)
  25. ->required()
  26. ->autoUpload()
  27. ->uniqueName();
  28. }
  29. /**
  30. * 添加图片上传
  31. *
  32. * @param string $field 字段名
  33. * @param string $label 标签名
  34. * @return Field\Image
  35. */
  36. public function imageUpload(string $field = 'image', string $label = '图片上传'): Field\Image
  37. {
  38. return $this->form->image($field, $label)
  39. ->required()
  40. ->autoUpload()
  41. ->uniqueName();
  42. }
  43. /**
  44. * 添加文件可见性选择
  45. *
  46. * @param string $field 字段名
  47. * @param string $label 标签名
  48. * @return Field\Radio
  49. */
  50. public function radioFileVisibility(string $field = 'private', string $label = '文件可见性'): Field\Radio
  51. {
  52. return $this->form->radio($field, $label)
  53. ->options([
  54. FILE_VISIBILITY::PUBLIC->value => FILE_VISIBILITY::getAll()[FILE_VISIBILITY::PUBLIC->value],
  55. FILE_VISIBILITY::PRIVATE->value => FILE_VISIBILITY::getAll()[FILE_VISIBILITY::PRIVATE->value],
  56. ])
  57. ->default(FILE_VISIBILITY::PUBLIC->value);
  58. }
  59. /**
  60. * 添加文件状态选择
  61. *
  62. * @param string $field 字段名
  63. * @param string $label 标签名
  64. * @return Field\Radio
  65. */
  66. public function radioFileStatus(string $field = 'status', string $label = '文件状态'): Field\Radio
  67. {
  68. return $this->form->radio($field, $label)
  69. ->options(FILE_STATUS::getAll())
  70. ->default(FILE_STATUS::NORMAL->value);
  71. }
  72. /**
  73. * 添加存储磁盘选择
  74. *
  75. * @param string $field 字段名
  76. * @param string $label 标签名
  77. * @return Field\Select
  78. */
  79. public function selectStorageDisk(string $field = 'storage_disk', string $label = '存储磁盘'): Field\Select
  80. {
  81. return $this->form->select($field, $label)
  82. ->options(function () {
  83. $disks = config('filesystems.disks');
  84. $options = [];
  85. foreach ($disks as $disk => $config) {
  86. $options[$disk] = $disk;
  87. }
  88. return $options;
  89. })
  90. ->default('public');
  91. }
  92. /**
  93. * 添加存储状态选择
  94. *
  95. * @param string $field 字段名
  96. * @param string $label 标签名
  97. * @return Field\Radio
  98. */
  99. public function radioStorageStatus(string $field = 'status', string $label = '存储状态'): Field\Radio
  100. {
  101. return $this->form->radio($field, $label)
  102. ->options(STORAGE_STATUS::getAll())
  103. ->default(STORAGE_STATUS::ENABLED->value);
  104. }
  105. /**
  106. * 添加关联类型输入
  107. *
  108. * @param string $field 字段名
  109. * @param string $label 标签名
  110. * @return Field\Text
  111. */
  112. public function textRelationType(string $field = 're_type', string $label = '关联类型'): Field\Text
  113. {
  114. return $this->form->text($field, $label)
  115. ->help('例如:user_avatar, article_image, product_attachment');
  116. }
  117. /**
  118. * 添加关联ID输入
  119. *
  120. * @param string $field 字段名
  121. * @param string $label 标签名
  122. * @return Field\Number
  123. */
  124. public function numberRelationId(string $field = 're_id', string $label = '关联ID'): Field\Number
  125. {
  126. return $this->form->number($field, $label)
  127. ->min(0)
  128. ->default(0);
  129. }
  130. /**
  131. * 添加用户ID输入
  132. *
  133. * @param string $field 字段名
  134. * @param string $label 标签名
  135. * @return Field\Number
  136. */
  137. public function numberUserId(string $field = 'user_id', string $label = '用户ID'): Field\Number
  138. {
  139. return $this->form->number($field, $label)
  140. ->min(0)
  141. ->default(0);
  142. }
  143. }