123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace App\Admin\Controllers\Content;
- use App\Http\Controllers\Controller;
- use App\Models\NoticeCategory;
- use Encore\Admin\Controllers\HasResourceActions;
- use Encore\Admin\Facades\Admin;
- use Encore\Admin\Form;
- use Encore\Admin\Grid;
- use Encore\Admin\Layout\Content;
- use Encore\Admin\Show;
- class NoticeCategoryController extends Controller
- {
- use HasResourceActions;
- /**
- * Index interface.
- *
- * @param Content $content
- * @return Content
- */
- public function index(Content $content)
- {
- return $content
- ->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->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 NoticeCategory);
- $grid->model()->orderBy('listorder', 'ASC');
- $grid->id('ID');
- $grid->category_name('分类名字');
- $grid->admin_set('系统分类')->display(function ($admin_set) {
- return $admin_set?"是":"否";
- });
- $grid->listorder('排序');
- $grid->created_at('添加时间');
- if (Admin::user()->can('content_noticemanager_noticlecategory_create')) {
- $grid->disableCreateButton(false);
- }
- if (Admin::user()->can('content_noticemanager_noticlecategory_delete')) {
- $grid->tools(function ($tools) {
- $tools->batch(function ($batch) {
- $batch->disableDelete(false);
- });
- });
- } else {
- $grid->disableRowSelector();
- }
- $grid->actions(function ($actions) {
- if (Admin::user()->can('content_noticemanager_noticlecategory_edit')) {
- $actions->disableEdit(false);
- }
- if (Admin::user()->can('content_noticemanager_noticlecategory_delete') && $actions->row['admin_set']!=1) {
- $actions->disableDelete(false);
- }
- });
- $grid->disableFilter();
- return $grid;
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(NoticeCategory::findOrFail($id));
- $show->id('ID');
- $show->category_name('分类名字');
- $show->admin_set('是否系统默认分类')->display(function ($admin_set) {
- return $admin_set?"是":"否";
- });
- $show->listorder('排序');
- $show->created_at('Created at');
- $show->updated_at('Updated at');
- /* $show->panel()
- ->tools(function ($tools) {
- $tools->disableEdit();
- $tools->disableDelete();
- });*/
- return $show;
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- protected function form()
- {
- $form = new Form(new NoticeCategory);
- $form->text('category_name', '分类名字')->rules('required|max:10', array('required'=>'分类名称不能为空。','max'=>'分类名称长度不能大于25个字符。'))->setWidth(4)->setMustMark();
- $form->number('listorder', '排序')->min(1)->max(255)->default(255)->rules('required', array('required'=>'排序不能为空。'));
- $form->hidden('admin_set')->value(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 destroy($id)
- {
- $id_arr = explode(',', $id);
- $admin_set_cates = NoticeCategory::where(array('admin_set'=>'1'))->select('id')->get()->pluck('id')->toArray();
- foreach ($id_arr as $k => $v) {
- if (in_array($v, $admin_set_cates)) {
- unset($id_arr[$k]);
- }
- }
- $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);
- }
- }
|