123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace App\Admin\Controllers;
- use App\Models\SpecialistCloumn;
- use App\Models\SpecialistCloumnTag;
- use App\Models\SpecialistInfo;
- use Encore\Admin\Controllers\AdminController;
- use Encore\Admin\Form;
- use Encore\Admin\Grid;
- use Encore\Admin\Show;
- class SpecialistContentController extends AdminController
- {
- /**
- * Title for current resource.
- *
- * @var string
- */
- protected $title = '健康课堂-内容管理';
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- $grid = new Grid(new SpecialistInfo);
- $grid->column('id', __('ID'))->sortable();
- $grid->column('firstCloumn.name', __('一级栏目'));
- $grid->column('secondCloumn.name', __('二级栏目'));
- $grid->column('name', __('名称'))->limit(40);
- $grid->column('type', __('类别'))->display(function ($type) {
- return SpecialistInfo::$typeMap[$type];
- });
- $grid->column('rank', __('排序(越大越靠前)'))->sortable();
- $grid->column('created_at', __('创建时间'));
- $grid->column('updated_at', __('更新时间'));
- //过滤用到的一级栏目
- $cloumns = SpecialistCloumn::where('pid', 0)->get();
- $firstCloumns = [];
- foreach ($cloumns as $cloumn){
- $firstCloumns[$cloumn->id] = $cloumn->name;
- }
- // 过滤
- $grid->filter(function ($filter) use($firstCloumns){
- $filter->like('name', '名称');
- $filter->equal('first_id', '一级栏目')->select($firstCloumns);
- $filter->equal('type', '类别')->select(SpecialistInfo::$typeMap);
- });
- return $grid;
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(SpecialistInfo::findOrFail($id));
- $show->field('id', __('ID'));
- $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 SpecialistInfo);
- $form->display('id', __('ID'))->rules('required');
- $form->select('first_id', '一级栏目')->options(SpecialistCloumn::where('pid', 0)->get()->pluck('name','id'))->rules("required")->load('second_id', '/admin/second_column');
- $id = 0;
- if($form->isEditing()){
- //获取id
- $parameters = request()->route()->parameters();
- $id = $parameters['specialist_content'];
- }
- if ($id > 0) {
- $info = $form->model()->find($id);
- $secondCategory = SpecialistCloumn::where('pid', $info->first_id)->get()->pluck('name','id');
- } else {
- $secondCategory = [];
- }
- $form->select('second_id', '二级栏目')->options($secondCategory)->load('tag_id', '/admin/tags')->default(0);
- if ($id > 0) {
- $tags = SpecialistCloumnTag::where('specialist_cloumn_id', $info->second_id)->get()->pluck('name','id');
- } else {
- $tags = [];
- }
- $form->select('tag_id', '标签')->options($tags)->default(0);
- $form->text('name', __('名称'))->rules('required');
- $form->textarea('subtitle', __('描述'))->rules('required');
- $form->editor('content', __('内容'))->required()->rules('required');;
- $form->multipleImage('cover', __('封面'))->removable()->uniqueName()->move('pics/'.date('Y').'/'.date('m'));
- $form->file('video', __('视频'))->removable()->uniqueName()->move('videos/'.date('Y').'/'.date('m'));
- $form->text('speaker', __('主讲'));
- $form->text('source', __('来源'));
- $form->number('rank', __('排序(越大越靠前)'))->default(0);
- $form->list('comment_tag', __('推荐标签'));
- $form->saved(function(Form $form){
- $id = $form->model()->id;
- $info = SpecialistInfo::find($id);
- $newUpdate = [];
- if ($info->video){
- $newUpdate['type'] = SpecialistInfo::TYPE_3;
- }elseif ($info->cover){
- $newUpdate['type'] = SpecialistInfo::TYPE_2;
- }else{
- $newUpdate['type'] = SpecialistInfo::TYPE_1;
- }
- SpecialistInfo::where('id', $id)->update($newUpdate);
- });
- return $form;
- }
- }
|