Passed
Push — master ( a9d1f9...e92e16 )
by John
04:09
created

SubmissionController::codify()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 25
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 1
nop 2
dl 0
loc 25
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace App\Admin\Controllers;
4
5
use App\Models\Eloquent\SubmissionModel as EloquentSubmissionModel;
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 SubmissionController 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('Submissions')
27
            ->description('all submissions')
28
            ->body($this->grid()->render());
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('Submission Detail')
42
            ->description('the detail of submissions')
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 Submission')
57
            ->description('edit the detail of submissions')
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 Submission')
71
            ->description('create a new submission')
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 EloquentSubmissionModel);
83
        $grid->column('sid', "ID")->sortable();
84
        $grid->time("Time");
85
        $grid->memory("Memory");
86
        $grid->verdict("Verdict")->display(function($verdict) {
87
            return '<i class="fa fa-circle '.$this->color.'"></i> '.$verdict;
0 ignored issues
show
Bug Best Practice introduced by
The property color does not exist on App\Admin\Controllers\SubmissionController. Did you maybe forget to declare it?
Loading history...
88
        });
89
        $grid->language("Language");
90
        $grid->submission_date("Submission Date")->display(function($submission_date) {
91
            return date("Y-m-d H:i:s", $submission_date);
92
        }); ;
93
        $grid->uid("UID");
94
        $grid->cid("CID");
95
        $grid->pid("PID");
96
        $grid->jid("JID");
97
        $grid->coid("COID");
98
        $grid->score("Raw Score");
99
        $grid->filter(function(Grid\Filter $filter) {
100
            $filter->column(6, function ($filter) {
101
                $filter->like('verdict');
102
            });
103
            $filter->column(6, function ($filter) {
104
                $filter->equal('cid', 'Contest ID');
105
                $filter->equal('uid', 'User ID');
106
                $filter->equal('pid', 'Problem ID');
107
            });
108
        });
109
        return $grid;
110
    }
111
112
    /**
113
     * Make a show builder.
114
     *
115
     * @param mixed $id
116
     * @return Show
117
     */
118
    protected function detail($id)
119
    {
120
        $show=new Show(EloquentSubmissionModel::findOrFail($id));
121
        $show->sid('SID');
122
        $show->time();
123
        $show->memory();
124
        $show->verdict();
125
        $show->color();
126
        $show->language();
127
        $show->submission_date();
128
        $show->remote_id();
129
        $this->codify($show->solution(),$show->getModel()->compiler->lang);
0 ignored issues
show
Bug introduced by
The property lang does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
130
        if(!blank($show->getModel()->compile_info))$this->codify($show->compile_info());
131
        $show->uid('UID');
132
        $show->pid('PID');
133
        $show->cid('CID');
134
        $show->jid('JID');
135
        $show->coid('COID');
136
        $show->vcid('VCID');
137
        $show->score();
138
        $show->share();
139
        return $show;
140
    }
141
142
    private function codify($field, $lang=null){
143
        $field->unescape()->as(function ($value) use ($field,$lang) {
144
            $field->border = false;
145
            if($value===null || $value==="") $value=" ";
146
            return "
147
                <style>
148
                #x".md5($value)." {
149
                    background: #ffffff;
150
                    border-top-left-radius: 0;
151
                    border-top-right-radius: 0;
152
                    border-bottom-right-radius: 3px;
153
                    border-bottom-left-radius: 3px;
154
                    padding: 10px;
155
                    border: 1px solid #d2d6de;
156
                }
157
                #x".md5($value)." code {
158
                    background: #ffffff;
159
                }
160
                </style>
161
                <pre id='x".md5($value)."'><code class='$lang'>".htmlspecialchars($value)."</code></pre>
162
                <script>
163
                    try{
164
                        hljs.highlightBlock(document.querySelector('#x".md5($value)." code'));
165
                    }catch(err){
166
                        window.addEventListener('load', function(){hljs.highlightBlock(document.querySelector('#x".md5($value)." code'));});
167
                    }
168
                </script>
169
            ";
170
        });
171
    }
172
173
    /**
174
     * Make a form builder.
175
     *
176
     * @return Form
177
     */
178
    protected function form()
179
    {
180
        $form=new Form(new EloquentSubmissionModel);
181
        $form->model()->makeVisible('password');
182
        $form->tab('Basic', function(Form $form) {
183
            $form->display('sid');
184
            $form->text('time')->rules('required');
185
            $form->text('memory')->rules('required');
186
            $form->text('verdict')->rules('required');
187
            $form->text('color')->rules('required');
188
            $form->text('language')->rules('required');
189
            $form->display('submission_date');
190
            $form->number('uid')->rules('required');
191
            $form->number('cid');
192
            $form->number('pid')->rules('required');
193
            $form->number('jid')->rules('required');
194
            $form->number('coid')->rules('required');
195
            $form->number('score')->rules('required');
196
        });
197
        return $form;
198
    }
199
}
200