DistAppearanceController.php 4.8 KB

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