123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace App\Admin\Controllers;
- use App\Admin\Controllers\RowAction\CustomAction;
- use App\Models\MentalityDrill;
- use App\Models\MentalityDrillCategory;
- use Encore\Admin\Admin;
- use Encore\Admin\Controllers\AdminController;
- use Encore\Admin\Form;
- use Encore\Admin\Grid;
- use Encore\Admin\Show;
- use Illuminate\Support\Facades\Log;
- class TrainDrillController extends AdminController
- {
- /**
- * Title for current resource.
- *
- * @var string
- */
- protected $title = '心理训练 内容管理';
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- $grid = new Grid(new MentalityDrill());
- $oneLevelData = MentalityDrillCategory::query()
- ->where(array("pid"=>0))
- ->select(array("id","name"))
- ->get()
- ->toArray();
- if (!empty($oneLevelData)){
- $oneLevelData = array_column($oneLevelData,"name","id");
- }
- $grid->column('id', __('Id'));
- $subjectData = MentalityDrillCategory::query()
- ->where(array("is_subject"=>MentalityDrillCategory::isSubjectYes))
- ->select(array("id","name"))
- ->get()
- ->toArray();
- if (!empty($subjectData)){
- $subjectData = array_column($subjectData,"name","id");
- }
- $grid->filter(function(\Encore\Admin\Grid\Filter $filter) use ($oneLevelData){
- // 在这里添加字段过滤器
- $filter->like('title', '标题');
- $filter->equal('category_id', '训练')->select(
- $oneLevelData
- );
- $filter->expand();
- });
- $grid->column('category_id', __('一级分类'))->display(function ($categoryId) use ($oneLevelData,$subjectData){
- if ($categoryId > 0 && isset($oneLevelData[$categoryId])){
- return isset($subjectData[$categoryId]) ? $oneLevelData[$categoryId]." [课程]" : $oneLevelData[$categoryId];
- }
- return '';
- });
- $grid->column('two_category_id', __('二级分类'))->display(function ($twoCategoryId) use ($oneLevelData){
- if ($twoCategoryId>0){
- $twoLevelData = MentalityDrillCategory::query()
- ->where(array("id"=>$twoCategoryId))
- ->select(array("id","name"))
- ->first();
- if ($twoLevelData){
- return $twoLevelData->name;
- }
- }
- return '';
- });
- $grid->column('title', __('标题'));
- $grid->column('number_of_studies', __('学习次数'));
- $grid->column('rank', __('排序 越大越前'));
- $grid->column('rank', __('排序 越大越前'));
- $grid->column('created_at', __('创建时间'));
- $grid->column('updated_at', __('更新时间'));
- $grid->actions(function (Grid\Displayers\Actions $actions) use ($subjectData) {
- $categoryId = $actions->row->category_id;
- if (isset($subjectData[$categoryId])){
- $actions->add(new CustomAction(
- "课程列表",
- admin_url("/mentality_drill_subject")."?".
- http_build_query(array("mentality_drills_id"=>$actions->row->id))
- ));
- }
- // 去掉删除
- $actions->disableDelete();
- });
- return $grid;
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(MentalityDrill::findOrFail($id));
- $show->field('id', __('Id'));
- $show->field('category_id', __('Category id'));
- $show->field('title', __('Title'));
- $show->field('subtitle', __('Subtitle'));
- $show->field('pic', __('Pic'));
- $show->field('content', __('Content'));
- $show->field('number_of_studies', __('Number of studies'));
- $show->field('rank', __('Rank'));
- $show->field('created_at', __('Created at'));
- $show->field('updated_at', __('Updated at'));
- $show->field('deleted_at', __('Deleted at'));
- return $show;
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- protected function form()
- {
- $form = new Form(new MentalityDrill());
- Admin::script("
- $(document).ready(function() {
- // 页面加载完毕后执行的代码
- console.log('页面已加载完毕');
- $('.category_id').trigger('change');
- });
- ");
- $oneLevelData = MentalityDrillCategory::query()
- ->where(array("pid"=>0))
- ->select(array("id","name"))
- ->get()
- ->toArray();
- if (!empty($oneLevelData)){
- $oneLevelData = array_column($oneLevelData,"name","id");
- }
- $form->select('category_id', '一级分类')
- ->options($oneLevelData)
- ->load('two_category_id', '/admin/lowerLevel')
- ->required();
- $form->select('two_category_id', '二级分类')->options([]);
- $form->text('title', __('标题'))->required();
- $form->text('subtitle', __('副标题'))->required();
- $form->image('pic', __('图片'))->removable()->uniqueName()->move('pics/'.date('Y').'/'.date('m'));
- $form->editor('content', __('内容'))->required();
- $form->number('number_of_studies', __('学习次数'))->default(0);
- $form->number('rank', __('排序 越大越前'))->default(0);
- $form->list('comment_tag', __('推荐标签'));
- return $form;
- }
- }
|