Completed
Pull Request — master (#4037)
by Muhlis
02:38
created

UserController::grid()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 53
rs 9.0254
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Tests\Controllers;
4
5
use App\Http\Controllers\Controller;
6
use Encore\Admin\Controllers\ModelForm;
7
use Encore\Admin\Facades\Admin;
8
use Encore\Admin\Form;
9
use Encore\Admin\Grid;
10
use Encore\Admin\Layout\Content;
11
use Tests\Models\Tag;
12
use Tests\Models\User;
13
14
class UserController extends Controller
15
{
16
    use ModelForm;
17
18
    /**
19
     * Index interface.
20
     *
21
     * @return Content
22
     */
23
    public function index()
24
    {
25
        return Admin::content(function (Content $content) {
26
            $content->header('All users');
27
            $content->description('description');
28
29
            $content->body($this->grid());
30
        });
31
    }
32
33
    /**
34
     * Edit interface.
35
     *
36
     * @param $id
37
     *
38
     * @return Content
39
     */
40
    public function edit($id)
41
    {
42
        return Admin::content(function (Content $content) use ($id) {
43
            $content->header('Edit user');
44
            $content->description('description');
45
46
            $content->body($this->form()->edit($id));
47
        });
48
    }
49
50
    /**
51
     * Create interface.
52
     *
53
     * @return Content
54
     */
55
    public function create()
56
    {
57
        return Admin::content(function (Content $content) {
58
            $content->header('Create user');
59
60
            $content->body($this->form());
61
        });
62
    }
63
64
    /**
65
     * Make a grid builder.
66
     *
67
     * @return Grid
68
     */
69
    protected function grid()
70
    {
71
        return Admin::grid(User::class, function (Grid $grid) {
72
            $grid->id('ID')->sortable();
73
74
            $grid->username();
75
            $grid->email();
76
            $grid->mobile();
77
            $grid->full_name();
78
            $grid->avatar()->display(function ($avatar) {
79
                return "<img src='{$avatar}' />";
80
            });
81
            $grid->profile()->postcode('Post code');
82
            $grid->profile()->address();
83
            $grid->position('Position');
84
            $grid->column('profile.color');
85
            $grid->profile()->start_at('开始时间');
86
            $grid->profile()->end_at('结束时间');
87
88
            $grid->column('column1_not_in_table')->display(function () {
89
                return 'full name:'.$this->full_name;
90
            });
91
92
            $grid->column('column2_not_in_table')->display(function () {
93
                return $this->email.'#'.$this->profile['color'];
94
            });
95
96
            $grid->tags()->display(function ($tags) {
97
                $tags = collect($tags)->map(function ($tag) {
98
                    return "<code>{$tag['name']}</code>";
99
                })->toArray();
100
101
                return implode('', $tags);
102
            });
103
104
            $grid->created_at();
105
            $grid->updated_at();
106
107
            $grid->filter(function ($filter) {
108
                $filter->like('username');
109
                $filter->like('email');
110
                $filter->like('profile.postcode');
111
                $filter->between('profile.start_at')->datetime();
112
                $filter->between('profile.end_at')->datetime();
113
            });
114
115
            $grid->actions(function ($actions) {
116
                if ($actions->getKey() % 2 == 0) {
117
                    $actions->append('<a href="/" class="btn btn-xs btn-danger">detail</a>');
118
                }
119
            });
120
        });
121
    }
122
123
    /**
124
     * Make a form builder.
125
     *
126
     * @return Form
127
     */
128
    protected function form()
129
    {
130
        Form::extend('map', Form\Field\Map::class);
131
        Form::extend('editor', Form\Field\Editor::class);
132
133
        return Admin::form(User::class, function (Form $form) {
134
            $form->disableDeletion();
135
136
            $form->display('id', 'ID');
137
            $form->text('username');
138
            $form->email('email')->rules('required');
139
            $form->mobile('mobile');
140
            $form->image('avatar')->help('上传头像', 'fa-image');
141
            $form->ignore(['password_confirmation']);
142
            $form->password('password')->rules('confirmed');
143
            $form->password('password_confirmation');
144
145
            $form->divider();
146
147
            $form->text('profile.first_name');
148
            $form->text('profile.last_name');
149
            $form->text('profile.postcode')->help('Please input your postcode');
150
            $form->textarea('profile.address')->rows(15);
151
            $form->map('profile.latitude', 'profile.longitude', 'Position');
152
            $form->color('profile.color');
153
            $form->datetime('profile.start_at');
154
            $form->datetime('profile.end_at');
155
156
            $form->multipleSelect('tags', 'Tags')->options(Tag::all()->pluck('name', 'id')); //->rules('max:10|min:3');
157
158
            $form->display('created_at', 'Created At');
159
            $form->display('updated_at', 'Updated At');
160
161
            $form->html('<a html-field>html...</a>');
162
        });
163
    }
164
}
165