header(trans('帮助分类')) ->description(trans('删除顶级分类将会自动删除此分类下的子分类。')) ->row(function (Row $row) { if (SysAdmin::user()->can('content_help_category_create')) { $row->column(6, $this->tree()->render()); $row->column(6, function (Column $column) { $form = new \Encore\Admin\Widgets\Form(); $form->action(route('help.category.index')); $cate_option = HelpCategory::where(array('parent_id'=>0))->orderBy('list_order', 'desc')->orderBy('created_at', 'desc')->get()->pluck('category_name', 'id'); $cate_option = $cate_option->toArray(); $cate_option = array('0'=>'顶级分类')+$cate_option; $form->select('parent_id', '所属分类')->options($cate_option)->default(0)->setMustMark(); $form->text('category_name', '分类名称')->attribute('maxlength', '25')->setMustMark(); $form->number('list_order', '排序')->min(0)->default(0)->help('(数字越大越靠前)')->attribute('maxlength', '10')->setMustMark(); $form->hidden('_token')->default(csrf_token()); $form->disableReset(); $column->append((new Box(trans('admin.new'), $form))->style('success')); }); } else { $row->column(12, $this->tree()->render()); } }); } protected function tree() { return HelpCategory::tree(function (Tree $tree) { $tree->branch(function ($branch) { return "{$branch['category_name']} (id:{$branch['id']})"; }); $tree->setView(array( 'tree' => 'admin::tree', 'branch' => 'admin.content.tree_branch' )); $tree->query(function ($model) { return $model->OrderBy('list_order', 'desc')->OrderBy('created_at', 'desc'); }); }); } /** * 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()->edit($id)); } /** * Create interface. * * @param Content $content * @return Content */ public function create(Content $content) { return $content ->header('帮助分类') ->description(' ') ->body($this->form()); } /** * Make a grid builder. * * @return Grid */ protected function grid() { $grid = new Grid(new HelpCategory); $grid->model()->orderBy('parent_id', 'asc')->orderBy('list_order', 'DESC'); $grid->id('ID'); $grid->category_name('分类名称'); $grid->parent()->category_name('上级分类'); $grid->list_order('排序'); $grid->created_at('添加时间'); $grid->updated_at('更新时间'); $grid->disableFilter(); return $grid; } /** * Make a show builder. * * @param mixed $id * @return Show */ protected function detail($id) { $show = new Show(HelpCategory::findOrFail($id)); $show->id('ID'); $show->category_name('分类名称'); $show->parent()->category_name('上级分类'); $show->list_order('排序'); $show->created_at('添加时间'); $show->updated_at('更新时间'); return $show; } /** * Make a form builder. * * @return Form */ protected function form() { $form = new Form(new HelpCategory); $cate_option = HelpCategory::where(array('parent_id'=>0))->get()->pluck('category_name', 'id'); $cate_option = HelpCategory::selectOptions(); $cate_option[0] ='顶级分类'; $form->select('parent_id', '所属分类')->options($cate_option)->rules('required', array('required'=>'请选择所属分类。')); $form->text('category_name', '分类名称')->rules('required', array('required'=>'分类名称不能为空。')); $form->number('list_order', '排序')->min(0)->default(0) ->attribute('maxlength', '10')->rules('required', array('required'=>'排序不能为空。'))->help('(数字越大越靠前)'); return $form; } protected function editForm() { $form = new Form(new HelpCategory); $cate_option = HelpCategory::where(array('parent_id'=>0))->OrderBy('list_order', 'DESC')->orderBy('created_at', 'DESC')->get()->pluck('category_name', 'id'); $cate_option = $cate_option->toArray(); $cate_option = array('0'=>'顶级分类')+$cate_option; $form->select('parent_id', '所属分类')->options($cate_option)->rules('required', array('required'=>'请选择所属分类。'))->setWidth(4)->setMustMark(); $form->text('category_name', '分类名称')->rules('required', array('required'=>'分类名称不能为空。'))->attribute('maxlength', '25')->setWidth(4)->setMustMark(); $form->number('list_order', '排序')->min(0)->default(0)->attribute('maxlength', '10') ->rules('required', array('required'=>'排序不能为空。'))->help('(数字越大越靠前)')->setMustMark(); $form->footer(function ($footer) { $footer->disableViewCheck(); $footer->disableEditingCheck(); $footer->disableCreatingCheck(); $footer->disableReset(); }); $form->tools(function (Form\Tools $tools) { $tools->disableDelete(); $tools->disableView(); }); return $form; } public function update($id) { return $this->editForm()->update($id); } public function destroys($id) { $id_arr = explode(',', $id); $children_ids = HelpCategory::whereIn('parent_id', $id_arr)->select('id')->get()->pluck('id')->toArray(); if ($children_ids) { $id_arr = array_merge($id_arr, $children_ids); } $filter_id = implode(',', $id_arr); if ($this->form()->destroy($filter_id)) { $data = [ 'status' => true, 'message' => trans('admin.delete_succeeded'), ]; } else { $data = [ 'status' => false, 'message' => trans('admin.delete_failed'), ]; } return response()->json($data); } }