TrainCategoryController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use Encore\Admin\Controllers\AdminController;
  4. use Encore\Admin\Form;
  5. use Encore\Admin\Grid;
  6. use Encore\Admin\Show;
  7. use App\Models\MentalityDrillCategory;
  8. use Illuminate\Http\Request;
  9. class TrainCategoryController extends AdminController
  10. {
  11. /**
  12. * Title for current resource.
  13. *
  14. * @var string
  15. */
  16. protected $title = '心理训练 类别管理';
  17. /**
  18. * Make a grid builder.
  19. *
  20. * @return Grid
  21. */
  22. protected function grid()
  23. {
  24. $oneLevelData = MentalityDrillCategory::query()
  25. ->where(array("pid"=>0))
  26. ->select(array("id","name"))
  27. ->get()
  28. ->toArray();
  29. if (!empty($oneLevelData)){
  30. $oneLevelData = array_column($oneLevelData,"name","id");
  31. $oneLevelData[0] = "一级分类";
  32. ksort($oneLevelData);
  33. }
  34. $grid = new Grid(new MentalityDrillCategory);
  35. $grid->filter(function(\Encore\Admin\Grid\Filter $filter) use ($oneLevelData){
  36. // 在这里添加字段过滤器
  37. $filter->like('name', '分类名称');
  38. $filter->expand();
  39. });
  40. $grid->column('id', __('ID'))->sortable();
  41. $grid->column('name', __('分类名称'));
  42. $grid->column('rank', __('排序(越大越前)'));
  43. $grid->column('is_index', __('是否首页'))->display(function ($isIndex){
  44. return isset(MentalityDrillCategory::isIndexMap[$isIndex])
  45. ?
  46. MentalityDrillCategory::isIndexMap[$isIndex]
  47. :
  48. ""
  49. ;
  50. });
  51. $grid->column('pid', __('父级分类'))->display(function ($id) use ($grid,$oneLevelData){
  52. if ($id > 0){
  53. if (isset($oneLevelData[$id])){
  54. return $oneLevelData[$id];
  55. }else {
  56. $data = MentalityDrillCategory::query()
  57. ->newQuery()
  58. ->where(array("id"=>$id))
  59. ->select(array("id","name"))
  60. ->first();
  61. if (!empty($data)){
  62. return $data->name;
  63. }
  64. }
  65. return "";
  66. }else{
  67. return $oneLevelData[0];
  68. }
  69. });
  70. $grid->column('is_subject', __('是否开启课程'))->display(function ($isSubject){
  71. return isset(MentalityDrillCategory::isSubjectMap[$isSubject])
  72. ?
  73. MentalityDrillCategory::isSubjectMap[$isSubject]
  74. :
  75. ""
  76. ;
  77. });
  78. $grid->column('created_at', __('创建时间'));
  79. $grid->column('updated_at', __('更新时间'));
  80. $grid->actions(function (Grid\Displayers\Actions $actions){
  81. // 去掉删除
  82. $actions->disableDelete();
  83. });
  84. return $grid;
  85. }
  86. /**
  87. * Make a show builder.
  88. *
  89. * @param mixed $id
  90. * @return Show
  91. */
  92. protected function detail($id)
  93. {
  94. $show = new Show(MentalityDrillCategory::findOrFail($id));
  95. $show->field('id', __('ID'));
  96. $show->field('name', __('分类名称'));
  97. $show->field('created_at', __('Created at'));
  98. $show->field('updated_at', __('Updated at'));
  99. return $show;
  100. }
  101. /**
  102. * Make a form builder.
  103. *
  104. * @return Form
  105. */
  106. protected function form()
  107. {
  108. $form = new Form(new MentalityDrillCategory);
  109. $oneLevelData = MentalityDrillCategory::query()
  110. ->where(array("pid"=>0))
  111. ->select(array("id","name"))
  112. ->get()
  113. ->toArray();
  114. if (!empty($oneLevelData)){
  115. $oneLevelData = array_column($oneLevelData,"name","id");
  116. }
  117. $form->display('id', __('ID'));
  118. $oneLevelData[0] = "一级分类";
  119. ksort($oneLevelData);
  120. $form->select('pid', __('父级分类'))->options($oneLevelData)->default(0);
  121. $form->text('name', __('分类名称'))->rules('required');
  122. $form->text('index_info', __('分类首页简介'));
  123. $form->image('index_pic', __('分类首页图片'));
  124. $form->image('pic', __('分类图片'))->uniqueName()->move('pics/'.date('Y').'/'.date('m'))->rules('required');
  125. $form->text('rank', __('排序(越大越前)'))->default(0);
  126. $form->radio("is_index","是否首页")
  127. ->options(MentalityDrillCategory::isIndexMap)
  128. ->default(MentalityDrillCategory::isIndexNo)
  129. ;
  130. $form->radio("is_subject","是否开启课程")
  131. ->options(MentalityDrillCategory::isSubjectMap)
  132. ->default(MentalityDrillCategory::isSubjectNo)
  133. ;
  134. $form->display('created_at', __('创建时间'));
  135. $form->display('updated_at', __('更新时间'));
  136. $form->tools(function (Form\Tools $tools){
  137. $tools->disableDelete();
  138. });
  139. return $form;
  140. }
  141. /**
  142. * 下级分类
  143. * @param Request $request
  144. * @return array
  145. */
  146. public function lowerLevel(Request $request)
  147. {
  148. $pid = $request->get("q",0);
  149. $oneLevelData = MentalityDrillCategory::query()
  150. ->where(array("pid"=>$pid))
  151. ->select(array("id","name as text"))
  152. ->get()
  153. ->toArray();
  154. return $oneLevelData;
  155. }
  156. }