海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.6 KiB

4 years ago
  1. <?php
  2. namespace App\AdminSupplier\Controllers;
  3. use App\AdminSupplier\Repositories\DiyForm;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use Dcat\Admin\Http\Controllers\AdminController;
  9. use Dcat\Admin\Widgets\Table;
  10. class DiyFormController extends AdminController
  11. {
  12. private $field_types = [
  13. 'text' => '单行文本框',
  14. 'textarea' => '多行文本框',
  15. 'select' => '选择框',
  16. 'checkbox' => '多选框',
  17. 'image' => '图片上传',
  18. ];
  19. /**
  20. * Make a grid builder.
  21. *
  22. * @return Grid
  23. */
  24. protected function grid()
  25. {
  26. return Grid::make(new DiyForm(['fields']), function (Grid $grid) {
  27. $grid->disableViewButton();
  28. $grid->model()->where('supplier_id', Admin::user()->id);
  29. $grid->column('id')->sortable();
  30. $grid->column('name');
  31. $field_types = $this->field_types;
  32. $grid->column('fields')
  33. ->display('查看')
  34. ->modal('字段列表', function ($modal) use ($field_types) {
  35. $fields = array_map(function ($v) use ($field_types) {
  36. return [
  37. $v['field'],
  38. $field_types[$v['type']] ?? '',
  39. $v['required'] ? '必填' : '选填',
  40. $v['sort'],
  41. ];
  42. }, $this->fields->toArray());
  43. return Table::make(['字段名', '字段类型', '是否必填', '排序'], $fields);
  44. });
  45. $grid->column('created_at');
  46. $grid->filter(function (Grid\Filter $filter) {
  47. $filter->equal('id')->width(2);
  48. $filter->like('name')->width(3);
  49. });
  50. });
  51. }
  52. /**
  53. * Make a show builder.
  54. *
  55. * @param mixed $id
  56. *
  57. * @return Show
  58. */
  59. protected function detail($id)
  60. {
  61. return Show::make($id, new DiyForm(['fields']), function (Show $show) {
  62. //不允许查看非自己的数据
  63. if ($show->model()->supplier_id != Admin::user()->id) {
  64. Admin::exit('数据不存在');
  65. }
  66. $show->field('id');
  67. $show->field('name');
  68. $show->field('fields');
  69. $show->field('created_at');
  70. $show->field('updated_at');
  71. });
  72. }
  73. /**
  74. * Make a form builder.
  75. *
  76. * @return Form
  77. */
  78. protected function form()
  79. {
  80. return Form::make(new DiyForm(['fields']), function (Form $form) {
  81. $form->disableViewButton();
  82. //不允许编辑非自己数据
  83. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  84. return $form->response()->error('数据不存在');
  85. }
  86. $form->display('id');
  87. $form->text('name')->required();
  88. $form->hasMany('fields', function (Form\NestedForm $form) {
  89. $form->text('field', '字段名称')->required();
  90. $form->switch('required', '是否必填')->value(1)->default(1)->required();
  91. $form->radio('type', '字段类型')
  92. ->required()->default('text')->value('text')
  93. ->options($this->field_types)->when(['select', 'checkbox'], function (Form\NestedForm $form) {
  94. $form->list('options', '选项列表');
  95. });
  96. $form->number('sort', '排序')
  97. ->help('数字越小越靠前')->value(255)
  98. ->default(255)->required();
  99. });
  100. })->saving(function (Form $form) {
  101. //不允许编辑非自己数据
  102. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  103. return $form->response()->error('数据不存在');
  104. }
  105. $form->hidden(['supplier_id']);
  106. $form->supplier_id = Admin::user()->id;
  107. })->deleting(function (Form $form) {
  108. //不允许删除非自己的数据
  109. if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) {
  110. return $form->response()->error('数据不存在');
  111. }
  112. });
  113. }
  114. }