123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace App\Admin\Controllers;
- use Encore\Admin\Controllers\AdminController;
- use Encore\Admin\Form;
- use Encore\Admin\Grid;
- use Encore\Admin\Show;
- use App\Models\MentalityDrillCategory;
- use Illuminate\Http\Request;
- class TrainCategoryController extends AdminController
- {
- /**
- * Title for current resource.
- *
- * @var string
- */
- protected $title = '心理训练 类别管理';
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- $oneLevelData = MentalityDrillCategory::query()
- ->where(array("pid"=>0))
- ->select(array("id","name"))
- ->get()
- ->toArray();
- if (!empty($oneLevelData)){
- $oneLevelData = array_column($oneLevelData,"name","id");
- $oneLevelData[0] = "一级分类";
- ksort($oneLevelData);
- }
- $grid = new Grid(new MentalityDrillCategory);
- $grid->filter(function(\Encore\Admin\Grid\Filter $filter) use ($oneLevelData){
- // 在这里添加字段过滤器
- $filter->like('name', '分类名称');
- $filter->expand();
- });
- $grid->column('id', __('ID'))->sortable();
- $grid->column('name', __('分类名称'));
- $grid->column('rank', __('排序(越大越前)'));
- $grid->column('is_index', __('是否首页'))->display(function ($isIndex){
- return isset(MentalityDrillCategory::isIndexMap[$isIndex])
- ?
- MentalityDrillCategory::isIndexMap[$isIndex]
- :
- ""
- ;
- });
- $grid->column('pid', __('父级分类'))->display(function ($id) use ($grid,$oneLevelData){
- if ($id > 0){
- if (isset($oneLevelData[$id])){
- return $oneLevelData[$id];
- }else {
- $data = MentalityDrillCategory::query()
- ->newQuery()
- ->where(array("id"=>$id))
- ->select(array("id","name"))
- ->first();
- if (!empty($data)){
- return $data->name;
- }
- }
- return "";
- }else{
- return $oneLevelData[0];
- }
- });
- $grid->column('is_subject', __('是否开启课程'))->display(function ($isSubject){
- return isset(MentalityDrillCategory::isSubjectMap[$isSubject])
- ?
- MentalityDrillCategory::isSubjectMap[$isSubject]
- :
- ""
- ;
- });
- $grid->column('created_at', __('创建时间'));
- $grid->column('updated_at', __('更新时间'));
- $grid->actions(function (Grid\Displayers\Actions $actions){
- // 去掉删除
- $actions->disableDelete();
- });
- return $grid;
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(MentalityDrillCategory::findOrFail($id));
- $show->field('id', __('ID'));
- $show->field('name', __('分类名称'));
- $show->field('created_at', __('Created at'));
- $show->field('updated_at', __('Updated at'));
- return $show;
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- protected function form()
- {
- $form = new Form(new MentalityDrillCategory);
- $oneLevelData = MentalityDrillCategory::query()
- ->where(array("pid"=>0))
- ->select(array("id","name"))
- ->get()
- ->toArray();
- if (!empty($oneLevelData)){
- $oneLevelData = array_column($oneLevelData,"name","id");
- }
- $form->display('id', __('ID'));
- $oneLevelData[0] = "一级分类";
- ksort($oneLevelData);
- $form->select('pid', __('父级分类'))->options($oneLevelData)->default(0);
- $form->text('name', __('分类名称'))->rules('required');
- $form->text('index_info', __('分类首页简介'));
- $form->image('index_pic', __('分类首页图片'));
- $form->image('pic', __('分类图片'))->uniqueName()->move('pics/'.date('Y').'/'.date('m'))->rules('required');
- $form->text('rank', __('排序(越大越前)'))->default(0);
- $form->radio("is_index","是否首页")
- ->options(MentalityDrillCategory::isIndexMap)
- ->default(MentalityDrillCategory::isIndexNo)
- ;
- $form->radio("is_subject","是否开启课程")
- ->options(MentalityDrillCategory::isSubjectMap)
- ->default(MentalityDrillCategory::isSubjectNo)
- ;
- $form->display('created_at', __('创建时间'));
- $form->display('updated_at', __('更新时间'));
- $form->tools(function (Form\Tools $tools){
- $tools->disableDelete();
- });
- return $form;
- }
- /**
- * 下级分类
- * @param Request $request
- * @return array
- */
- public function lowerLevel(Request $request)
- {
- $pid = $request->get("q",0);
- $oneLevelData = MentalityDrillCategory::query()
- ->where(array("pid"=>$pid))
- ->select(array("id","name as text"))
- ->get()
- ->toArray();
- return $oneLevelData;
- }
- }
|