| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace App\Module\File\AdminControllers\Helper;
- use App\Module\File\Enums\FILE_STATUS;
- use App\Module\File\Enums\FILE_VISIBILITY;
- use App\Module\File\Enums\STORAGE_STATUS;
- use Dcat\Admin\Form;
- use Dcat\Admin\Form\Field;
- /**
- * 表单辅助特性
- *
- * 提供文件模块后台控制器的表单构建功能的具体实现
- */
- trait FormHelperTrait
- {
- /**
- * 添加文件上传
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\File
- */
- public function fileUpload(string $field = 'file', string $label = '文件上传'): Field\File
- {
- return $this->form->file($field, $label)
- ->required()
- ->autoUpload()
- ->uniqueName();
- }
- /**
- * 添加图片上传
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Image
- */
- public function imageUpload(string $field = 'image', string $label = '图片上传'): Field\Image
- {
- return $this->form->image($field, $label)
- ->required()
- ->autoUpload()
- ->uniqueName();
- }
- /**
- * 添加文件可见性选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Radio
- */
- public function radioFileVisibility(string $field = 'private', string $label = '文件可见性'): Field\Radio
- {
- return $this->form->radio($field, $label)
- ->options([
- FILE_VISIBILITY::PUBLIC->value => FILE_VISIBILITY::getAll()[FILE_VISIBILITY::PUBLIC->value],
- FILE_VISIBILITY::PRIVATE->value => FILE_VISIBILITY::getAll()[FILE_VISIBILITY::PRIVATE->value],
- ])
- ->default(FILE_VISIBILITY::PUBLIC->value);
- }
- /**
- * 添加文件状态选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Radio
- */
- public function radioFileStatus(string $field = 'status', string $label = '文件状态'): Field\Radio
- {
- return $this->form->radio($field, $label)
- ->options(FILE_STATUS::getAll())
- ->default(FILE_STATUS::NORMAL->value);
- }
- /**
- * 添加存储磁盘选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectStorageDisk(string $field = 'storage_disk', string $label = '存储磁盘'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options(function () {
- $disks = config('filesystems.disks');
- $options = [];
- foreach ($disks as $disk => $config) {
- $options[$disk] = $disk;
- }
- return $options;
- })
- ->default('public');
- }
- /**
- * 添加存储状态选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Radio
- */
- public function radioStorageStatus(string $field = 'status', string $label = '存储状态'): Field\Radio
- {
- return $this->form->radio($field, $label)
- ->options(STORAGE_STATUS::getAll())
- ->default(STORAGE_STATUS::ENABLED->value);
- }
- /**
- * 添加关联类型输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Text
- */
- public function textRelationType(string $field = 're_type', string $label = '关联类型'): Field\Text
- {
- return $this->form->text($field, $label)
- ->help('例如:user_avatar, article_image, product_attachment');
- }
- /**
- * 添加关联ID输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Number
- */
- public function numberRelationId(string $field = 're_id', string $label = '关联ID'): Field\Number
- {
- return $this->form->number($field, $label)
- ->min(0)
- ->default(0);
- }
- /**
- * 添加用户ID输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Number
- */
- public function numberUserId(string $field = 'user_id', string $label = '用户ID'): Field\Number
- {
- return $this->form->number($field, $label)
- ->min(0)
- ->default(0);
- }
- }
|