header(trans('新闻分类')) ->description(trans('系统默认分类不能删除!删除顶级级分类将会自动删除此分类下的子分类。')) ->row(function (Row $row) { if (userAdmin::user()->can('content_article_category_create')) { $row->column(6, $this->treeView()->render()); $row->column(6, function (Column $column) { $form = new \Encore\Admin\Widgets\Form(); $form->action(route('article.category.index')); $form->text('category_name', '名称')->setMustMark(); $cate_option = ArticleCategory::where(array('parent_id'=>0))->OrderBy('list_order', 'DESC')->orderBy('created_at', 'DESC')->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->number('list_order', '排序')->min(0)->default(0)->help('(数字越大越靠前)')->setMustMark(); $form->multipleSelect('roles', trans('admin.roles'))->options(Role::all()->pluck('name', 'id')); $form->text('title', 'Title')->help('(不填则为分类名称)'); $form->text('keywords', 'Keywords')->placeholder('合理设置Keywords有利于搜索引擎排名'); $form->textarea('description', 'Description')->placeholder('合理设置Description有利于搜索引擎排名'); $form->hidden('admin_set')->value(0); $form->hidden('_token')->default(csrf_token()); $form->disableReset(); $column->append((new Box(trans('admin.new'), $form))->style('success')); }); } else { $row->column(12, $this->treeView()->render()); } }); } /** * 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->form()->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 ArticleCategory); $grid->id('ID'); $grid->created_at('添加时间'); $grid->updated_at('更新时间'); return $grid; } /** * Make a show builder. * * @param mixed $id * @return Show */ protected function detail($id) { $show = new Show(ArticleCategory::findOrFail($id)); $show->id('ID'); $show->category_name('名称'); $show->parent_id('所属分类')->as(function ($parent_id) { $category_name = '顶级分类'; if ($parent_id) { $cate_info = ArticleCategory::where('id', $parent_id)->first(); if ($cate_info) { $category_name = $cate_info->category_name; } else { $category_name = ''; } } return $category_name; }); $show->list_order('排序'); $show->roles(trans('admin.roles'))->as(function ($role) { return $role->pluck('name'); })->label(); $show->title('Title'); $show->keywords('Keywords'); $show->description('Description'); $show->admin_set('系统分类')->as(function ($admin_set) { return $admin_set?'是':'否'; }); $show->created_at('添加时间'); $show->updated_at('更新时间'); return $show; } /** * Make a form builder. * * @return Form */ protected function form() { $form = new ValidateForm(new ArticleCategory); $form->text('category_name', '名称')->rules('required|max:15', array('required'=>'名称不能为空。', 'max'=>'名称长度不能超过15。'))->setWidth(4)->setMustMark(); $cate_option = ArticleCategory::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)->rules('required', array('required'=>'请选择所属分类。'))->setWidth(4)->setMustMark(); $form->number('list_order', '排序') ->min(0)->default(0) ->rules('required', array('required'=>'排序不能为空。')) ->help('(数字越大越靠前)') ->setMustMark(); $form->multipleSelect('roles', trans('admin.roles'))->options(Role::all()->pluck('name', 'id')); $form->text('title', 'Title')->help('(不填则为分类名称)')->rules('max:25', array('max'=>'Title长度不能大于25。'))->setWidth(4); $form->text('keywords', 'Keywords') ->placeholder('合理设置Keywords有利于搜索引擎排名') ->rules('max:25', array('max'=>'Keywords长度不能大于25。')); $form->textarea('description', 'Description') ->rules('max:25', array('max'=>'Description长度不能大于25。')) ->placeholder('合理设置Description有利于搜索引擎排名'); $form->hidden('admin_set'); $form->saving(function (Form $form) { $form->admin_set = $form->admin_set?$form->admin_set:0; }); $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 stores() { $data=$this->form()->getValidateInput(); $cate = new ArticleCategory(); $cate->category_name = $data['category_name']; $cate->parent_id = $data['parent_id']; $cate->list_order = $data['list_order']; $cate->title = $data['title']; $cate->keywords = $data['keywords']; $cate->description = $data['description']; $cate->admin_set = $data['admin_set']?$data['admin_set']:'0'; if ($cate->save()) { $cate_id = $cate->id; $roles = $data['roles']; $time = date('Y-m-d H:i:s', time()); $role_data = array(); if ($roles) { foreach ($roles as $k => $v) { $role_data[] = array( 'role_id'=> $v, 'article_category_id' => $cate_id, 'created_at' => $time, 'updated_at' => $time ); } ArticleCategoryRole::insert($role_data); } admin_toastr(trans('admin.save_succeeded')); return redirect(admin_base_path('/content/article/category')); } else { throw new \Exception(trans('admin.save_failed')); } } /** * Make a grid builder. * * @return Tree */ protected function treeView() { return ArticleCategory::tree(function (Tree $tree) { $tree->disableRefresh(); $tree->disableSave(); $tree->branch(function ($branch) { return "{$branch['category_name']} (id:{$branch['id']})"; }); $tree->setView(array( 'tree' => 'admin::tree', 'branch' => 'admin.content.article_branch' )); $tree->query(function ($model) { $not_ids = ArticleCategory::categoryIds(); if ($not_ids) { return $model->whereNotIn('id', $not_ids)->OrderBy('list_order', 'DESC')->orderBy('created_at', 'DESC'); } else { return $model->OrderBy('list_order', 'DESC')->orderBy('created_at', 'DESC'); } }); }); } public function destroy($id) { $id_arr = explode(',', $id); $children_ids = ArticleCategory::whereIn('parent_id', $id_arr)->select('id')->get()->pluck('id')->toArray(); if ($children_ids) { $children_ids = implode(',', $children_ids); } else { $children_ids = ''; } if ($this->form()->destroy($id)) { $data = [ 'status' => true, 'message' => trans('admin.delete_succeeded'), ]; //删除分类与角色的对应关系 if ($children_ids) { $where = array(); $ids = explode(',', $children_ids); ArticleCategoryRole::whereIn('article_category_id', $ids)->delete(); } } else { $data = [ 'status' => false, 'message' => trans('admin.delete_failed'), ]; } return response()->json($data); } }