TplController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace App\Admin\Controllers\Tool;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Tpl;
  5. use Encore\Admin\Controllers\HasResourceActions;
  6. use Encore\Admin\Form;
  7. use Encore\Admin\Grid;
  8. use Encore\Admin\Layout\Content;
  9. use Encore\Admin\Show;
  10. class TplController extends Controller
  11. {
  12. use HasResourceActions;
  13. /**
  14. * Index interface.
  15. *
  16. * @param Content $content
  17. * @return Content
  18. */
  19. public function index(Content $content)
  20. {
  21. return $content
  22. ->header('风格模板')
  23. ->description('')
  24. ->body($this->grid());
  25. }
  26. /**
  27. * Show interface.
  28. *
  29. * @param mixed $id
  30. * @param Content $content
  31. * @return Content
  32. */
  33. public function show($id, Content $content)
  34. {
  35. return $content
  36. ->header('风格模板')
  37. ->description('详细')
  38. ->body($this->detail($id));
  39. }
  40. /**
  41. * Edit interface.
  42. *
  43. * @param mixed $id
  44. * @param Content $content
  45. * @return Content
  46. */
  47. public function edit($id, Content $content)
  48. {
  49. return $content
  50. ->header('风格模板')
  51. ->description('编辑')
  52. ->body($this->editForm()->edit($id));
  53. }
  54. /**
  55. * Create interface.
  56. *
  57. * @param Content $content
  58. * @return Content
  59. */
  60. public function create(Content $content)
  61. {
  62. return $content
  63. ->header('风格模板')
  64. ->description('创建')
  65. ->body($this->createForm());
  66. }
  67. /**
  68. * Make a grid builder.
  69. *
  70. * @return Grid
  71. */
  72. protected function grid()
  73. {
  74. $grid = new Grid(new Tpl);
  75. $grid->id('ID');
  76. $grid->name('模板名称');
  77. $grid->images('图片')->image('', 100, 100);
  78. $grid->display('是否显示')->display(function ($display) {
  79. return $display ? "<i class='fa fa-check' style='color:green'></i>" : "<i class='fa fa-close' style='color:red'></i>";
  80. });
  81. $grid->tpl_type('类型')->display(function ($tpl_type) {
  82. switch ($tpl_type) {
  83. case 1:
  84. $tpl_type = '企业';
  85. break;
  86. case 2:
  87. $tpl_type = '个人';
  88. break;
  89. default:
  90. $tpl_type = '未定义';
  91. break;
  92. }
  93. return $tpl_type;
  94. });
  95. $grid->price('积分');
  96. $grid->created_at('添加时间');
  97. $grid->updated_at('更新时间');
  98. $grid->actions(function ($actions) {
  99. $actions->disableView();
  100. if ($actions->row['default']==1) {
  101. $actions->disableDelete();
  102. }
  103. });
  104. return $grid;
  105. }
  106. /**
  107. * Make a show builder.
  108. *
  109. * @param mixed $id
  110. * @return Show
  111. */
  112. protected function detail($id)
  113. {
  114. $show = new Show(Tpl::findOrFail($id));
  115. $show->id('ID');
  116. $show->name('模板名称');
  117. $show->tpl_type('类型')->as(function ($tpl_type) {
  118. switch ($tpl_type) {
  119. case 1:
  120. $tpl_type = '企业';
  121. break;
  122. case 2:
  123. $tpl_type = '个人';
  124. break;
  125. default:
  126. $tpl_type = '未定义';
  127. break;
  128. }
  129. return $tpl_type;
  130. });
  131. $show->display('是否显示')->as(function ($display) {
  132. return $display ? "显示" : "隐藏";
  133. });
  134. $show->images()->image();
  135. $show->dir('路径');
  136. $show->created_at('添加时间');
  137. $show->updated_at('更新时间');
  138. return $show;
  139. }
  140. /**
  141. * Make a form builder.
  142. *
  143. * @return Form
  144. */
  145. protected function form()
  146. {
  147. $form = new Form(new Tpl);
  148. $form->display('ID');
  149. $form->display('添加时间');
  150. $form->display('更新时间');
  151. return $form;
  152. }
  153. /**
  154. * Make a form builder.
  155. *
  156. * @return Form
  157. */
  158. protected function editForm()
  159. {
  160. $form = new Form(new Tpl);
  161. $form->display('name', '模板名称')->rules('required');
  162. $form->display('default', '默认');
  163. $form->number('price', '售价')->min(0);
  164. $form->radio('tpl_type', '类型')->options(['1'=>'企业','2'=>'个人'])->default(1);
  165. $form->radio('display', '是否显示')->options(['1'=>'显示','0'=>'不显示'])->default(0);
  166. $form->image('images', '图片')->uniqueName();
  167. $form->text('dir', '路径');
  168. $form->footer(function ($footer) {
  169. // 去掉`重置`按钮
  170. $footer->disableReset();
  171. // 去掉`查看`checkbox
  172. $footer->disableViewCheck();
  173. // 去掉`继续编辑`checkbox
  174. $footer->disableEditingCheck();
  175. // 去掉`继续创建`checkbox
  176. $footer->disableCreatingCheck();
  177. });
  178. $form->tools(function (Form\Tools $tools) {
  179. $tools->disableDelete();
  180. $tools->disableView();
  181. });
  182. return $form;
  183. }
  184. protected function createForm()
  185. {
  186. $form = new Form(new Tpl);
  187. $form->text('name', '模板名称')->rules('required');
  188. $form->radio('default', '默认')->options([0=>'否'])->default(0);
  189. $form->number('price', '售价')->min(0);
  190. $form->radio('tpl_type', '类型')->options(['1'=>'企业','2'=>'个人'])->default(1);
  191. $form->radio('display', '是否显示')->options(['1'=>'显示','0'=>'不显示'])->default(0);
  192. $form->image('images', '图片')->uniqueName();
  193. $form->text('dir', '路径');
  194. $form->footer(function ($footer) {
  195. // 去掉`重置`按钮
  196. $footer->disableReset();
  197. // 去掉`查看`checkbox
  198. $footer->disableViewCheck();
  199. // 去掉`继续编辑`checkbox
  200. $footer->disableEditingCheck();
  201. // 去掉`继续创建`checkbox
  202. $footer->disableCreatingCheck();
  203. });
  204. return $form;
  205. }
  206. /**
  207. * Store a newly created resource in storage.
  208. *
  209. * @return mixed
  210. */
  211. public function store()
  212. {
  213. return $this->createForm()->store();
  214. }
  215. /**
  216. * Update the specified resource in storage.
  217. *
  218. * @param int $id
  219. *
  220. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  221. */
  222. public function update($id)
  223. {
  224. return $this->editForm()->update($id);
  225. }
  226. }