123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace App\Admin\Controllers\Content;
- use App\Admin\Extensions\Form\ValidateForm;
- use App\Http\Controllers\Controller;
- use App\Models\ArticleCategory;
- use App\Models\ArticleCategoryRole;
- use App\Models\Role;
- use Encore\Admin\Controllers\HasResourceActions;
- use Encore\Admin\Facades\Admin as userAdmin;
- use Encore\Admin\Form;
- use Encore\Admin\Grid;
- use Encore\Admin\Layout\Column;
- use Encore\Admin\Layout\Content;
- use Encore\Admin\Layout\Row;
- use Encore\Admin\Show;
- use Encore\Admin\Tree;
- use Encore\Admin\Widgets\Box;
- //use Encore\Admin\Facades\Admin;
- class ArticleCategoryController extends Controller
- {
- use HasResourceActions;
- /**
- * Index interface.
- *
- * @param Content $content
- * @return Content
- */
- public function index(Content $content)
- {
- return $content
- ->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);
- }
- }
|