SpecialistContentController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\SpecialistCloumn;
  4. use App\Models\SpecialistCloumnTag;
  5. use App\Models\SpecialistInfo;
  6. use Encore\Admin\Controllers\AdminController;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Show;
  10. class SpecialistContentController extends AdminController
  11. {
  12. /**
  13. * Title for current resource.
  14. *
  15. * @var string
  16. */
  17. protected $title = '健康课堂-内容管理';
  18. /**
  19. * Make a grid builder.
  20. *
  21. * @return Grid
  22. */
  23. protected function grid()
  24. {
  25. $grid = new Grid(new SpecialistInfo);
  26. $grid->column('id', __('ID'))->sortable();
  27. $grid->column('firstCloumn.name', __('一级栏目'));
  28. $grid->column('secondCloumn.name', __('二级栏目'));
  29. $grid->column('name', __('名称'))->limit(40);
  30. $grid->column('type', __('类别'))->display(function ($type) {
  31. return SpecialistInfo::$typeMap[$type];
  32. });
  33. $grid->column('rank', __('排序(越大越靠前)'))->sortable();
  34. $grid->column('created_at', __('创建时间'));
  35. $grid->column('updated_at', __('更新时间'));
  36. //过滤用到的一级栏目
  37. $cloumns = SpecialistCloumn::where('pid', 0)->get();
  38. $firstCloumns = [];
  39. foreach ($cloumns as $cloumn){
  40. $firstCloumns[$cloumn->id] = $cloumn->name;
  41. }
  42. // 过滤
  43. $grid->filter(function ($filter) use($firstCloumns){
  44. $filter->like('name', '名称');
  45. $filter->equal('first_id', '一级栏目')->select($firstCloumns);
  46. $filter->equal('type', '类别')->select(SpecialistInfo::$typeMap);
  47. });
  48. return $grid;
  49. }
  50. /**
  51. * Make a show builder.
  52. *
  53. * @param mixed $id
  54. * @return Show
  55. */
  56. protected function detail($id)
  57. {
  58. $show = new Show(SpecialistInfo::findOrFail($id));
  59. $show->field('id', __('ID'));
  60. $show->field('created_at', __('Created at'));
  61. $show->field('updated_at', __('Updated at'));
  62. return $show;
  63. }
  64. /**
  65. * Make a form builder.
  66. *
  67. * @return Form
  68. */
  69. protected function form()
  70. {
  71. $form = new Form(new SpecialistInfo);
  72. $form->display('id', __('ID'))->rules('required');
  73. $form->select('first_id', '一级栏目')->options(SpecialistCloumn::where('pid', 0)->get()->pluck('name','id'))->rules("required")->load('second_id', '/admin/second_column');
  74. $id = 0;
  75. if($form->isEditing()){
  76. //获取id
  77. $parameters = request()->route()->parameters();
  78. $id = $parameters['specialist_content'];
  79. }
  80. if ($id > 0) {
  81. $info = $form->model()->find($id);
  82. $secondCategory = SpecialistCloumn::where('pid', $info->first_id)->get()->pluck('name','id');
  83. } else {
  84. $secondCategory = [];
  85. }
  86. $form->select('second_id', '二级栏目')->options($secondCategory)->load('tag_id', '/admin/tags')->default(0);
  87. if ($id > 0) {
  88. $tags = SpecialistCloumnTag::where('specialist_cloumn_id', $info->second_id)->get()->pluck('name','id');
  89. } else {
  90. $tags = [];
  91. }
  92. $form->select('tag_id', '标签')->options($tags)->default(0);
  93. $form->text('name', __('名称'))->rules('required');
  94. $form->textarea('subtitle', __('描述'))->rules('required');
  95. $form->editor('content', __('内容'))->required()->rules('required');;
  96. $form->multipleImage('cover', __('封面'))->removable()->uniqueName()->move('pics/'.date('Y').'/'.date('m'));
  97. $form->file('video', __('视频'))->removable()->uniqueName()->move('videos/'.date('Y').'/'.date('m'));
  98. $form->text('speaker', __('主讲'));
  99. $form->text('source', __('来源'));
  100. $form->number('rank', __('排序(越大越靠前)'))->default(0);
  101. $form->list('comment_tag', __('推荐标签'));
  102. $form->saved(function(Form $form){
  103. $id = $form->model()->id;
  104. $info = SpecialistInfo::find($id);
  105. $newUpdate = [];
  106. if ($info->video){
  107. $newUpdate['type'] = SpecialistInfo::TYPE_3;
  108. }elseif ($info->cover){
  109. $newUpdate['type'] = SpecialistInfo::TYPE_2;
  110. }else{
  111. $newUpdate['type'] = SpecialistInfo::TYPE_1;
  112. }
  113. SpecialistInfo::where('id', $id)->update($newUpdate);
  114. });
  115. return $form;
  116. }
  117. }