Passed
Push — master ( 73dd95...b2df94 )
by John
04:30
created

ProblemController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
nc 1
nop 1
dl 0
loc 6
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace App\Admin\Controllers;
4
5
use App\Models\ProblemModel;
6
use App\Http\Controllers\Controller;
7
use Encore\Admin\Controllers\HasResourceActions;
8
use Encore\Admin\Form;
9
use Encore\Admin\Grid;
10
use Encore\Admin\Layout\Content;
11
use Encore\Admin\Show;
12
13
class ProblemController extends Controller
14
{
15
    use HasResourceActions;
16
17
    /**
18
     * Index interface.
19
     *
20
     * @param Content $content
21
     * @return Content
22
     */
23
    public function index(Content $content)
24
    {
25
        return $content
26
            ->header('Problems')
27
            ->description('all problems')
28
            ->body($this->grid());
29
    }
30
31
    /**
32
     * Show interface.
33
     *
34
     * @param mixed $id
35
     * @param Content $content
36
     * @return Content
37
     */
38
    public function show($id, Content $content)
39
    {
40
        return $content
41
            ->header('Problem Detail')
42
            ->description('the detail of problems')
43
            ->body($this->detail($id));
44
    }
45
46
    /**
47
     * Edit interface.
48
     *
49
     * @param mixed $id
50
     * @param Content $content
51
     * @return Content
52
     */
53
    public function edit($id, Content $content)
54
    {
55
        return $content
56
            ->header('Edit Problem')
57
            ->description('edit the detail of problems')
58
            ->body($this->form()->edit($id));
59
    }
60
61
    /**
62
     * Create interface.
63
     *
64
     * @param Content $content
65
     * @return Content
66
     */
67
    public function create(Content $content)
68
    {
69
        return $content
70
            ->header('Create New Problem')
71
            ->description('create a new problem')
72
            ->body($this->form());
73
    }
74
75
    /**
76
     * Make a grid builder.
77
     *
78
     * @return Grid
79
     */
80
    protected function grid()
81
    {
82
        $grid = new Grid(new ProblemModel);
83
        $grid->column('pid',"ID")->sortable();
84
        $grid->column('pcode',"PCode")->editable();
85
        $grid->title("Title")->editable();
86
        $grid->time_limit("Time/ms")->editable();
87
        $grid->memory_limit("Memory/kb")->editable();
88
        $grid->OJ();
89
        $grid->update_date();
90
        $grid->tot_score("Score");
91
        $grid->partial("Partial")->display(function ($partial) {
92
            return $partial ? '是' : '否';
93
        });
94
        $grid->markdown("Markdown")->display(function ($markdown) {
95
            return $markdown ? '是' : '否';
96
        });
97
        $grid->filter(function (Grid\Filter $filter) {
98
            $filter->disableIdFilter();
99
            $filter->like('pcode');
100
            $filter->like('title');
101
        });
102
        return $grid;
103
    }
104
105
    /**
106
     * Make a show builder.
107
     *
108
     * @param mixed $id
109
     * @return Show
110
     */
111
    protected function detail($id)
112
    {
113
        $show = new Show(ProblemModel::findOrFail($id));
114
        return $show;
115
    }
116
117
    /**
118
     * Make a form builder.
119
     *
120
     * @return Form
121
     */
122
    protected function form()
123
    {
124
        $form = new Form(new ProblemModel);
125
        $form->model()->makeVisible('password');
126
        $form->tab('Basic', function (Form $form) {
127
            // $form->display('pid');
128
            // $form->text('pcode')->rules('required');
129
            $form->text('title')->rules('required');
130
            $form->text('time_limit')->rules('required');
131
            $form->text('memory_limit')->rules('required');
132
            $form->display('OJ');
133
            $form->display('update_date');
134
            $form->text('tot_score')->rules('required');
135
            $form->text('partial')->rules('required');
136
            $form->text('markdown')->rules('required');
137
        });
138
        return $form;
139
    }
140
}
141