OnePoemController.php 5.33 KB
<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Verse;
use App\Models\Author;
use App\Models\OnePoem2;
use App\Models\Poetry;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Table;

class OnePoemController extends AdminController
{
    protected $title = '一言';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
//        return Grid::make(new OnePoem(), function (Grid $grid) {
//            $grid->column('id')->sortable();
//            $grid->column('title');
//            $grid->column('content');
//            $grid->column('annotate');
//            $grid->column('spelling');
//            $grid->column('en');
//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();
//
//            $grid->filter(function (Grid\Filter $filter) {
//                $filter->equal('id');
//            });
//        });

        return Grid::make(new OnePoem2(), function (Grid $grid) {
            $grid->model()->with('verses');
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('author');
            $grid->column('','诗句组')
                ->display('展开')
                ->expand(function (){
                    $th = ['id','一言Id','正文','注释','拼音','英文','出处','创建时间','修改时间'];
                    return Table::make($th, $this->verses->toArray())->withBorder();
                });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new OnePoem2(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('content');
            $show->field('annotate');
            $show->field('spelling');
            $show->field('en');
            $show->field('source');
            $show->field('poetry_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }


    public function create(Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title($this->title())
            ->description('注意:基础库中内容只做参考,创建的一言与其他表数据不耦合!理论上可自行组装句子,也可按诗词原文复制。')
//            ->description($this->description()['create'] ?? trans('admin.create'))
            ->body(Card::make($this->PoetryGrid()))
            ->body($this->form());
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $builder = OnePoem2::with(['verses']);
        return Form::make($builder, function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->text('author');

            $form->hasMany('verses','诗句组', function (Form\NestedForm $form) {
                $form->textarea('content');
                $form->textarea('annotate');
                $form->textarea('spelling');
                $form->textarea('en');
                $form->textarea('source');
            })->useTable();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }


    protected function PoetryGrid()
    {
        return Grid::make(new Verse(), function (Grid $grid) {
            $grid->model()->with(['poetry']);

            $grid->column('id')->sortable();
            $grid->column('author_id','作者')->display(function (){
                $id = $this->poetry_id;
                $poetry = Poetry::query()->find($id);
                $author_id = $poetry->author_id;
                $author = Author::query()->find($author_id);
                return $author->name;
            })->copyable();
            $grid->column('poetry.name')->copyable();

            $grid->column('stanza')->copyable();
            $grid->column('annotate')->copyable();
            $grid->column('spelling')->copyable();
            $grid->column('en')->copyable();

            $grid->withBorder();
            $grid->quickSearch(['stanza'])->placeholder('快捷搜索诗节(支持模糊查询)');

            $grid->paginate(5);
            $grid->simplePaginate();

            $grid->disableActions();
            $grid->disableCreateButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->where('author_id',function ($query){
                    $author = Author::query()->where('name','like',"%{$this->input}%")->get()->pluck('id');
                    $poetry =Poetry::query()->whereIn('author_id',$author)->get()->pluck('id');
                    $query->whereIn('poetry_id',$poetry);
                },'作者')->width(3);
                $filter->like('poetry.name')->width(3);
                $filter->like('stanza')->width(4);
                $filter->panel();
                $filter->expand();
            });
        });
    }
}