header('企业模板') ->description('列表') ->body($this->grid()); } /** * Show interface. * * @param mixed $id * @param Content $content * @return Content */ public function show($id, Content $content) { return $content ->header('企业模板') ->description('详情') ->body($this->detail($id)); } /** * Edit interface. * * @param mixed $id * @param Content $content * @return Content */ public function edit($id, Content $content) { return $content ->header('企业模板') ->description('编辑') ->body($this->editForm($id)->edit($id)); } /** * Create interface. * * @param Content $content * @return Content */ public function create(Content $content) { return $content ->header('企业模板') ->description('创建') ->body($this->createForm()); } /** * Make a grid builder. * * @return Grid */ protected function grid() { $grid = new Grid(new Tpl); $grid->model()->where('tpl_type', 1)->orderBy('id', 'desc'); $grid->id('ID'); $grid->images('模板缩略图')->image('', '150', '150'); $grid->name('模板名称')->width(200); $states = [ 'on' => ['value' => 1, 'text' => '是', 'color' => 'primary'], 'off' => ['value' => 0, 'text' => '否', 'color' => 'default'], ]; $grid->display('是否显示')->switch($states); $grid->price('价格')->display(function ($display) { return $display.'积分'; }); $grid->blade_name('压缩包名称'); $grid->created_at('创建时间')->sortable(); $grid->actions(function ($actions) use ($grid) { if (Admin::user()->can('template_company_edit')) { $actions->disableEdit(false); } if (Admin::user()->can('template_company_delete')) { $actions->disableDelete(false); } $actions->disableView(true); }); if (Admin::user()->can('template_company_delete')) { $grid->tools(function ($tools) { $tools->batch(function ($batch) { $batch->disableDelete(false); }); }); $grid->disableRowSelector(false); } if (Admin::user()->can('template_company_create')) { $grid->disableCreateButton(false); } $grid->disableFilter(); return $grid; } /** * Make a show builder. * * @param mixed $id * @return Show */ protected function detail($id) { } /** * Make a form builder. * * @return Form */ protected function editForm($id) { $form = new Form(new Tpl); $form->text('name', '模板名称')->rules([ 'required', ])->setWidth(3)->setMustMark(); $form->radio('display', '是否显示')->options([0=>'否',1=>'是'])->default(1); $form->number('price','价格')->default(0)->min(0)->help('企业会员使用此模板需要支付的积分,0为免费'); $form->image('images','模板缩略图')->setWidth(8)->setMustMark()->options(['layoutTemplates' => ['actionDelete'=>'']]); $form->file('blade_name','模板压缩包')->help('上传相同的ZIP压缩包会覆盖之前的文件!')->uniqueName()->setWidth(8)->setMustMark()->options(['layoutTemplates' => ['actionDelete'=>'']]); $form->saving(function (Form $form) use($id) { $form->old_blade_name = Tpl::find($id)->blade_name; if ($form->display=='on') { $form->display=1; } elseif ($form->display=='off') { $form->display=0; } }); $form->saved(function (Form $form) { if($form->blade_name!=null && $form->blade_name!='_file_del_'){ Storage::disk('public')->deleteDirectory('template/company/'.$form->old_blade_name); $model = $form->model(); $new_blade = $model->blade_name; $zip = new \Chumper\Zipper\Zipper(); $zip->zip(Storage::disk('public')->path($new_blade))->extractTo(Storage::disk('public')->path('template/company/'.$form->old_blade_name)); $zip->close(); $model->blade_name = $form->old_blade_name; $model->save(); Storage::disk('public')->delete($new_blade); } }); return $form; } protected function createForm() { $form = new Form(new Tpl); $form->text('name', '模板名称')->rules([ 'required', ])->setWidth(3)->setMustMark(); $form->radio('display', '是否显示')->options([0=>'否',1=>'是'])->default(1); $form->number('price','价格')->default(0)->min(0)->help('企业会员使用此模板需要支付的积分,0为免费'); $form->image('images','模板缩略图')->setWidth(8)->setMustMark()->rules([ 'required', ]); $form->file('blade_name','模板压缩包')->help('上传相同的ZIP压缩包会覆盖之前的文件!')->rules('mimes:zip')->uniqueName() ->setWidth(8)->setMustMark()->rules([ 'required', ]); $form->saved(function (Form $form) { $model = $form->model(); $new_blade = $model->blade_name; $template_dir=uniqid(); $zip = new \Chumper\Zipper\Zipper(); $zip->zip(Storage::disk('public')->path($new_blade))->extractTo(Storage::disk('public')->path('template/company/'.$template_dir)); $zip->close(); $model->blade_name = $template_dir; $model->save(); Storage::disk('public')->delete($new_blade); }); return $form; } public function store() { return $this->createForm()->store(); } public function update($id) { return $this->editForm($id)->update($id); } /** * Make a form builder. * * @return Form */ protected function form() { $form = new Form(new Tpl); $form->display('ID'); $form->display('Created at'); $form->display('Updated at'); return $form; } public function destroy($id) { if (!$id) { $data = [ 'status' => false, 'message' => '模板不存在!!', ]; return response()->json($data); } $id_arr = explode(',', $id); $blade_name = Tpl::whereIn('id', $id_arr)->pluck('blade_name')->toArray(); \DB::beginTransaction(); try { Tpl::whereIn('id', $id_arr)->delete(); if ($blade_name){ Company::whereIn('tpl', $blade_name)->update(['tpl'=>'default']); } $data = [ 'status' => true, 'message' => '删除成功!', ]; \DB::commit(); return response()->json($data); } catch (\Exception $e) { \DB::rollback(); $data = [ 'status' => false, 'message' => '删除失败!', ]; return response()->json($data); } } }