ScaleController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\Scale;
  4. use Encore\Admin\Controllers\AdminController;
  5. use Encore\Admin\Form;
  6. use Encore\Admin\Grid;
  7. use Encore\Admin\Show;
  8. use App\Models\ScaleCategory;
  9. use App\Models\ThirdScale;
  10. use Illuminate\Http\Request;
  11. class ScaleController extends AdminController
  12. {
  13. /**
  14. * Title for current resource.
  15. *
  16. * @var string
  17. */
  18. // protected $title = '量表内容';
  19. protected $title = '心理评估内容';
  20. /**
  21. * Make a grid builder.
  22. *
  23. * @return Grid
  24. */
  25. protected function grid()
  26. {
  27. $grid = new Grid(new Scale());
  28. $grid->column('id', __('Id'));
  29. $grid->column('category_id', __('分类'))->display(function ($pid) {
  30. if ($pid>0){
  31. $scaleCategoryData = ScaleCategory::find($pid);
  32. if ($scaleCategoryData){
  33. return $scaleCategoryData->name;
  34. }
  35. }
  36. return '';
  37. });
  38. $grid->column('second_id', __('二级分类'))->display(function ($pid) {
  39. if ($pid>0){
  40. $scaleCategoryData = ScaleCategory::find($pid);
  41. if ($scaleCategoryData){
  42. return $scaleCategoryData->name;
  43. }
  44. }
  45. return '';
  46. });
  47. $grid->column('title', __('标题'));
  48. $grid->column('time', __('测试时间'));
  49. $grid->column('suitable_for_the_crowd', __('适合人群'));
  50. $grid->column('number_of_questions', __('问题数'));
  51. $grid->column('number_of_replay', __('报告数'));
  52. $grid->column('number_of_test', __('测试次数'));
  53. $grid->column('rank', __('排序 越大越前'));
  54. $grid->column('created_at', __('创建时间'));
  55. $grid->column('updated_at', __('更新时间'));
  56. return $grid;
  57. }
  58. /**
  59. * Make a show builder.
  60. *
  61. * @param mixed $id
  62. * @return Show
  63. */
  64. protected function detail($id)
  65. {
  66. $show = new Show(Scale::findOrFail($id));
  67. $show->field('id', __('Id'));
  68. $show->field('category_id', __('Category id'));
  69. $show->field('third_id', __('Third id'));
  70. $show->field('title', __('Title'));
  71. $show->field('subtitle', __('Subtitle'));
  72. $show->field('pic', __('Pic'));
  73. $show->field('content', __('Content'));
  74. $show->field('time', __('Time'));
  75. $show->field('suitable_for_the_crowd', __('Suitable for the crowd'));
  76. $show->field('number_of_questions', __('Number of questions'));
  77. $show->field('number_of_replay', __('Number of replay'));
  78. $show->field('number_of_test', __('Number of test'));
  79. $show->field('notice', __('Notice'));
  80. $show->field('rank', __('Rank'));
  81. $show->field('created_at', __('Created at'));
  82. $show->field('updated_at', __('Updated at'));
  83. $show->field('deleted_at', __('Deleted at'));
  84. return $show;
  85. }
  86. /**
  87. * Make a form builder.
  88. *
  89. * @return Form
  90. */
  91. protected function form()
  92. {
  93. $form = new Form(new Scale());
  94. $thirdScale = ThirdScale::get(['id', 'title as name'])->pluck('name','id');
  95. $form->select('third_id', __('远程量表'))->options($thirdScale)->required();
  96. $cloumns = ScaleCategory::where('pid', 0)->where('deleted_at',null)->get(['id','name'])->pluck('name','id');
  97. $form->select('category_id', __('分类'))->options($cloumns)->rules("required")->load('second_id', '/admin/scale_second_category');
  98. $second = [];
  99. if($form->isEditing()){
  100. $model = Scale::find(request()->route()->parameters()['scale']);
  101. if($model) {
  102. $second = ScaleCategory::where('pid','=',$model->category_id)->where('deleted_at',null)->get(['id','name'])->pluck('name','id');
  103. }
  104. }
  105. $form->select('second_id', __('二级分类'))->options($second);;
  106. $form->text('title', __('标题'))->required();
  107. $form->text('subtitle', __('副标题'))->required();
  108. $form->image('pic', __('图片'))->required();
  109. $form->editor('content', __('内容'))->required();
  110. $form->number('time', __('测试时间(分钟)'))->required();
  111. $form->text('suitable_for_the_crowd', __('适合人群'))->required();
  112. $form->number('number_of_questions', __('问题数'))->default(0);
  113. $form->number('number_of_replay', __('报告数'))->default(0);
  114. $form->number('number_of_test', __('测试次数'))->default(0);
  115. $form->textarea('notice', __('测试须知'))->required();
  116. $form->number('rank', __('排序 越大越前'))->default(0);
  117. $form->list('comment_tag', __('推荐标签'));
  118. return $form;
  119. }
  120. public function getSecondCategory(Request $request )
  121. {
  122. $pid = $request->input('q');
  123. return ScaleCategory::where('pid', $pid)->select('name as text','id')->get()->toArray();
  124. }
  125. }