DistAppearanceController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Actions\Grid\AppearanceImport;
  4. use App\Admin\Repositories\DistAppearance;
  5. use App\Libraries\CommonHelper;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use Dcat\Admin\Http\Controllers\AdminController;
  10. use Dcat\Admin\Layout\Content;
  11. use Dcat\Admin\Admin;
  12. class DistAppearanceController extends AdminController
  13. {
  14. /**
  15. * page index
  16. */
  17. public function index(Content $content)
  18. {
  19. return $content
  20. ->header(admin_trans( 'admin.appearance'))
  21. ->description('')
  22. ->breadcrumb(['text'=>'list','url'=>''])
  23. ->body($this->grid());
  24. }
  25. /**
  26. * Make a grid builder.
  27. *
  28. * @return Grid
  29. */
  30. protected function grid()
  31. {
  32. return Grid::make(new DistAppearance(), function (Grid $grid) {
  33. //默认分页条数
  34. $grid->paginate(config('admin.per_page'));
  35. $grid->column('id')->sortable();
  36. // 标题
  37. $grid->column('title');
  38. // 文件夹
  39. $grid->column('folder')->help('Folder names under the directory:/resources/appearance');
  40. // 封面图
  41. $grid->column('cover_image')->display(function ($image) {
  42. $dataImages = [$image];
  43. return CommonHelper::displayImage($dataImages,100);
  44. });
  45. // 排序
  46. $grid->column('order')->orderable();
  47. //是否导入
  48. $grid->column('imported')->using(config('dictionary.whether'))->label([
  49. 0 => 'default',
  50. 1 => 'success',
  51. ]);
  52. //是否启用
  53. $grid->column('enabled')->switch();
  54. // 时间
  55. $grid->column('created_at');
  56. $grid->column('updated_at')->sortable();
  57. // 过滤器
  58. $grid->filter(function (Grid\Filter $filter) {
  59. $filter->panel();
  60. $filter->expand();
  61. $filter->equal('title')->width(2);
  62. $filter->equal('enabled', )->select(admin_trans_array(config('dictionary.enabled')))->width(2);
  63. });
  64. // 操作
  65. $grid->actions(function (Grid\Displayers\Actions $actions) {
  66. $actions->disableDelete();
  67. $actions->append(new AppearanceImport());
  68. });
  69. });
  70. }
  71. /**
  72. * Make a show builder.
  73. *
  74. * @param mixed $id
  75. *
  76. * @return Show
  77. */
  78. protected function detail($id)
  79. {
  80. return Show::make($id, new DistAppearance(), function (Show $show) {
  81. $show->field('id');
  82. $show->field('title');
  83. $show->field('folder');
  84. $show->field('cover_image')->as(function ($image) {
  85. // 开始生成 HTML
  86. $dataImages = [$image];
  87. return CommonHelper::displayImage($dataImages,100);
  88. })->unescape();
  89. $show->field('describe');
  90. $show->field('enabled')->using(admin_trans_array(config('dictionary.enabled')));
  91. $show->field('created_at');
  92. $show->field('updated_at');
  93. $show->disableDeleteButton();
  94. });
  95. }
  96. /**
  97. * Make a form builder.
  98. *
  99. * @return Form
  100. */
  101. protected function form()
  102. {
  103. return Form::make(new DistAppearance(), function (Form $form) {
  104. $form->display('id');
  105. $form->text('title');
  106. $form->text('folder');
  107. $form->image("cover_image")
  108. ->autoUpload()
  109. ->uniqueName()
  110. ->accept(config('admin.upload.oss_image.accept'))
  111. ->maxSize(config('admin.upload.oss_image.max_size'))
  112. ->dir(config("admin.upload.directory.image").'/appearance/'.date("Ymd"));//
  113. $form->textarea('describe');
  114. $form->switch('enabled')->default(0);
  115. $form->saving(function (Form $form) {
  116. if ($form->isCreating() && $form->input('enabled') == 1) {
  117. return $form->response()->error('Please create the template first, then import it, and finally enable it.');
  118. }
  119. if (!$form->isCreating()) {
  120. if ($form->input('enabled') == 1 && $form->model()->imported == 0) {
  121. return $form->response()->error('Please select the import option first, then enable it.');
  122. }
  123. }
  124. });
  125. $form->disableDeleteButton();
  126. });
  127. }
  128. }