PersonTemplateController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace App\Admin\Controllers\Company;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Tpl;
  5. use Encore\Admin\Controllers\HasResourceActions;
  6. use Encore\Admin\Facades\Admin;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Layout\Content;
  10. use Encore\Admin\Show;
  11. use Illuminate\Support\Facades\Storage;
  12. class PersonTemplateController extends Controller
  13. {
  14. use HasResourceActions;
  15. /**
  16. * Index interface.
  17. *
  18. * @param Content $content
  19. * @return Content
  20. */
  21. public function index(Content $content)
  22. {
  23. return $content
  24. ->header('个人模板')
  25. ->description('列表')
  26. ->body($this->grid());
  27. }
  28. /**
  29. * Show interface.
  30. *
  31. * @param mixed $id
  32. * @param Content $content
  33. * @return Content
  34. */
  35. public function show($id, Content $content)
  36. {
  37. return $content
  38. ->header('个人模板')
  39. ->description('详情')
  40. ->body($this->detail($id));
  41. }
  42. /**
  43. * Edit interface.
  44. *
  45. * @param mixed $id
  46. * @param Content $content
  47. * @return Content
  48. */
  49. public function edit($id, Content $content)
  50. {
  51. return $content
  52. ->header('个人模板')
  53. ->description('编辑')
  54. ->body($this->editForm($id)->edit($id));
  55. }
  56. /**
  57. * Create interface.
  58. *
  59. * @param Content $content
  60. * @return Content
  61. */
  62. public function create(Content $content)
  63. {
  64. return $content
  65. ->header('个人模板')
  66. ->description('创建')
  67. ->body($this->createForm());
  68. }
  69. /**
  70. * Make a grid builder.
  71. *
  72. * @return Grid
  73. */
  74. protected function grid()
  75. {
  76. $grid = new Grid(new Tpl);
  77. $grid->model()->where('tpl_type', 2)->orderBy('id', 'desc');
  78. $grid->id('ID');
  79. $grid->images('模板缩略图')->image('', '150', '150');
  80. $grid->name('模板名称')->width(200);
  81. $states = [
  82. 'on' => ['value' => 1, 'text' => '是', 'color' => 'primary'],
  83. 'off' => ['value' => 0, 'text' => '否', 'color' => 'default'],
  84. ];
  85. $grid->display('是否显示')->switch($states);
  86. $grid->price('价格')->display(function ($display) {
  87. return $display.'积分';
  88. });
  89. $grid->blade_name('压缩包名称');
  90. $grid->created_at('创建时间')->sortable();
  91. $grid->actions(function ($actions) use ($grid) {
  92. if (Admin::user()->can('template_person_edit')) {
  93. $actions->disableEdit(false);
  94. }
  95. if (Admin::user()->can('template_person_delete')) {
  96. $actions->disableDelete(false);
  97. }
  98. $actions->disableView(true);
  99. });
  100. if (Admin::user()->can('template_person_delete')) {
  101. $grid->tools(function ($tools) {
  102. $tools->batch(function ($batch) {
  103. $batch->disableDelete(false);
  104. });
  105. });
  106. $grid->disableRowSelector(false);
  107. }
  108. if (Admin::user()->can('template_person_create')) {
  109. $grid->disableCreateButton(false);
  110. }
  111. $grid->disableFilter();
  112. return $grid;
  113. }
  114. /**
  115. * Make a show builder.
  116. *
  117. * @param mixed $id
  118. * @return Show
  119. */
  120. protected function detail($id)
  121. {
  122. }
  123. /**
  124. * Make a form builder.
  125. *
  126. * @return Form
  127. */
  128. protected function editForm($id)
  129. {
  130. $form = new Form(new Tpl);
  131. $form->text('name', '模板名称')->rules([
  132. 'required',
  133. ])->setWidth(3)->setMustMark();
  134. $form->radio('display', '是否显示')->options([0=>'否',1=>'是'])->default(1);
  135. $form->number('price','价格')->default(0)->min(0)->help('企业会员使用此模板需要支付的积分,0为免费');
  136. $form->image('images','模板缩略图')->setWidth(8)->setMustMark()->options(['layoutTemplates' => ['actionDelete'=>'']]);
  137. $form->file('blade_name','模板压缩包')->help('上传相同的ZIP压缩包会覆盖之前的文件!')->uniqueName()->setWidth(8)->setMustMark()->options(['layoutTemplates' => ['actionDelete'=>'']]);
  138. $form->saving(function (Form $form) use($id) {
  139. $form->old_blade_name = Tpl::find($id)->blade_name;
  140. if ($form->display=='on') {
  141. $form->display=1;
  142. } elseif ($form->display=='off') {
  143. $form->display=0;
  144. }
  145. });
  146. $form->saved(function (Form $form) {
  147. if($form->blade_name!=null && $form->blade_name!='_file_del_'){
  148. Storage::disk('public')->deleteDirectory('template/person/'.$form->old_blade_name);
  149. $model = $form->model();
  150. $new_blade = $model->blade_name;
  151. $zip = new \Chumper\Zipper\Zipper();
  152. $zip->zip(Storage::disk('public')->path($new_blade))->extractTo(Storage::disk('public')->path('template/person/'.$form->old_blade_name));
  153. $zip->close();
  154. $model->blade_name = $form->old_blade_name;
  155. $model->save();
  156. Storage::disk('public')->delete($new_blade);
  157. }
  158. });
  159. return $form;
  160. }
  161. protected function createForm()
  162. {
  163. $form = new Form(new Tpl);
  164. $form->text('name', '模板名称')->rules([
  165. 'required',
  166. ])->setWidth(3)->setMustMark();
  167. $form->radio('display', '是否显示')->options([0=>'否',1=>'是'])->default(1);
  168. $form->number('price','价格')->default(0)->min(0)->help('企业会员使用此模板需要支付的积分,0为免费');
  169. $form->image('images','模板缩略图')->setWidth(8)->setMustMark()->rules([
  170. 'required',
  171. ]);
  172. $form->file('blade_name','模板压缩包')->help('上传相同的ZIP压缩包会覆盖之前的文件!')->rules('mimes:zip')->uniqueName()
  173. ->setWidth(8)->setMustMark()->rules([
  174. 'required',
  175. ]);
  176. $form->hidden('tpl_type')->default(2);
  177. $form->saved(function (Form $form) {
  178. $model = $form->model();
  179. $new_blade = $model->blade_name;
  180. $template_dir=uniqid();
  181. $zip = new \Chumper\Zipper\Zipper();
  182. $zip->zip(Storage::disk('public')->path($new_blade))->extractTo(Storage::disk('public')->path('template/person/'.$template_dir));
  183. $zip->close();
  184. $model->blade_name = $template_dir;
  185. $model->save();
  186. Storage::disk('public')->delete($new_blade);
  187. });
  188. return $form;
  189. }
  190. public function store()
  191. {
  192. return $this->createForm()->store();
  193. }
  194. public function update($id)
  195. {
  196. return $this->editForm($id)->update($id);
  197. }
  198. /**
  199. * Make a form builder.
  200. *
  201. * @return Form
  202. */
  203. protected function form()
  204. {
  205. $form = new Form(new Tpl);
  206. $form->display('ID');
  207. $form->display('Created at');
  208. $form->display('Updated at');
  209. return $form;
  210. }
  211. }