| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 | <?phpnamespace App\Admin\Controllers\Content;use App\Http\Controllers\Controller;use App\Models\HelpCategory;use Encore\Admin\Controllers\HasResourceActions;use Encore\Admin\Facades\Admin as SysAdmin;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\Grid;class HelpCategoryController 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 (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);    }}
 |