TrainDrillController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Controllers\RowAction\CustomAction;
  4. use App\Models\MentalityDrill;
  5. use App\Models\MentalityDrillCategory;
  6. use Encore\Admin\Admin;
  7. use Encore\Admin\Controllers\AdminController;
  8. use Encore\Admin\Form;
  9. use Encore\Admin\Grid;
  10. use Encore\Admin\Show;
  11. use Illuminate\Support\Facades\Log;
  12. class TrainDrillController extends AdminController
  13. {
  14. /**
  15. * Title for current resource.
  16. *
  17. * @var string
  18. */
  19. protected $title = '心理训练 内容管理';
  20. /**
  21. * Make a grid builder.
  22. *
  23. * @return Grid
  24. */
  25. protected function grid()
  26. {
  27. $grid = new Grid(new MentalityDrill());
  28. $oneLevelData = MentalityDrillCategory::query()
  29. ->where(array("pid"=>0))
  30. ->select(array("id","name"))
  31. ->get()
  32. ->toArray();
  33. if (!empty($oneLevelData)){
  34. $oneLevelData = array_column($oneLevelData,"name","id");
  35. }
  36. $grid->column('id', __('Id'));
  37. $subjectData = MentalityDrillCategory::query()
  38. ->where(array("is_subject"=>MentalityDrillCategory::isSubjectYes))
  39. ->select(array("id","name"))
  40. ->get()
  41. ->toArray();
  42. if (!empty($subjectData)){
  43. $subjectData = array_column($subjectData,"name","id");
  44. }
  45. $grid->filter(function(\Encore\Admin\Grid\Filter $filter) use ($oneLevelData){
  46. // 在这里添加字段过滤器
  47. $filter->like('title', '标题');
  48. $filter->equal('category_id', '训练')->select(
  49. $oneLevelData
  50. );
  51. $filter->expand();
  52. });
  53. $grid->column('category_id', __('一级分类'))->display(function ($categoryId) use ($oneLevelData,$subjectData){
  54. if ($categoryId > 0 && isset($oneLevelData[$categoryId])){
  55. return isset($subjectData[$categoryId]) ? $oneLevelData[$categoryId]." [课程]" : $oneLevelData[$categoryId];
  56. }
  57. return '';
  58. });
  59. $grid->column('two_category_id', __('二级分类'))->display(function ($twoCategoryId) use ($oneLevelData){
  60. if ($twoCategoryId>0){
  61. $twoLevelData = MentalityDrillCategory::query()
  62. ->where(array("id"=>$twoCategoryId))
  63. ->select(array("id","name"))
  64. ->first();
  65. if ($twoLevelData){
  66. return $twoLevelData->name;
  67. }
  68. }
  69. return '';
  70. });
  71. $grid->column('title', __('标题'));
  72. $grid->column('number_of_studies', __('学习次数'));
  73. $grid->column('rank', __('排序 越大越前'));
  74. $grid->column('rank', __('排序 越大越前'));
  75. $grid->column('created_at', __('创建时间'));
  76. $grid->column('updated_at', __('更新时间'));
  77. $grid->actions(function (Grid\Displayers\Actions $actions) use ($subjectData) {
  78. $categoryId = $actions->row->category_id;
  79. if (isset($subjectData[$categoryId])){
  80. $actions->add(new CustomAction(
  81. "课程列表",
  82. admin_url("/mentality_drill_subject")."?".
  83. http_build_query(array("mentality_drills_id"=>$actions->row->id))
  84. ));
  85. }
  86. // 去掉删除
  87. $actions->disableDelete();
  88. });
  89. return $grid;
  90. }
  91. /**
  92. * Make a show builder.
  93. *
  94. * @param mixed $id
  95. * @return Show
  96. */
  97. protected function detail($id)
  98. {
  99. $show = new Show(MentalityDrill::findOrFail($id));
  100. $show->field('id', __('Id'));
  101. $show->field('category_id', __('Category id'));
  102. $show->field('title', __('Title'));
  103. $show->field('subtitle', __('Subtitle'));
  104. $show->field('pic', __('Pic'));
  105. $show->field('content', __('Content'));
  106. $show->field('number_of_studies', __('Number of studies'));
  107. $show->field('rank', __('Rank'));
  108. $show->field('created_at', __('Created at'));
  109. $show->field('updated_at', __('Updated at'));
  110. $show->field('deleted_at', __('Deleted at'));
  111. return $show;
  112. }
  113. /**
  114. * Make a form builder.
  115. *
  116. * @return Form
  117. */
  118. protected function form()
  119. {
  120. $form = new Form(new MentalityDrill());
  121. Admin::script("
  122. $(document).ready(function() {
  123. // 页面加载完毕后执行的代码
  124. console.log('页面已加载完毕');
  125. $('.category_id').trigger('change');
  126. });
  127. ");
  128. $oneLevelData = MentalityDrillCategory::query()
  129. ->where(array("pid"=>0))
  130. ->select(array("id","name"))
  131. ->get()
  132. ->toArray();
  133. if (!empty($oneLevelData)){
  134. $oneLevelData = array_column($oneLevelData,"name","id");
  135. }
  136. $form->select('category_id', '一级分类')
  137. ->options($oneLevelData)
  138. ->load('two_category_id', '/admin/lowerLevel')
  139. ->required();
  140. $form->select('two_category_id', '二级分类')->options([]);
  141. $form->text('title', __('标题'))->required();
  142. $form->text('subtitle', __('副标题'))->required();
  143. $form->image('pic', __('图片'))->removable()->uniqueName()->move('pics/'.date('Y').'/'.date('m'));
  144. $form->editor('content', __('内容'))->required();
  145. $form->number('number_of_studies', __('学习次数'))->default(0);
  146. $form->number('rank', __('排序 越大越前'))->default(0);
  147. $form->list('comment_tag', __('推荐标签'));
  148. return $form;
  149. }
  150. }