UserCrudController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 86.21%

Importance

Changes 0
Metric Value
wmc 3
eloc 79
dl 0
loc 116
ccs 25
cts 29
cp 0.8621
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 5 1
A setupUpdateOperation() 0 30 1
A setupListOperation() 0 65 1
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Models\UserRole;
6
use Backpack\CRUD\app\Http\Controllers\CrudController;
7
8
class UserCrudController extends CrudController
9
{
10
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
11
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
12
13
    /**
14
     * Prepare the admin interface by setting the associated
15
     * model, setting the route, and adding custom columns/fields.
16 1
     *
17
     * @return void
18 1
     */
19 1
    public function setup() : void
20 1
    {
21
        $this->crud->setModel('App\Models\User');
22 1
        $this->crud->setRoute('admin/user');
23 1
        $this->crud->setEntityNameStrings('user', 'users');
24
    }
25 1
26 1
    public function setupListOperation()
27
    {
28
        $this->crud->addColumn([
29
            'name' => 'id',
30 1
            'type' => 'number',
31 1
            'label' => 'ID'
32
        ]);
33
        $this->crud->addColumn([
34
            'name' => 'first_name',
35 1
            'type' => 'text',
36 1
            'label' => 'First Name'
37
        ]);
38
        $this->crud->addColumn([
39
            'name' => 'last_name',
40 1
            'type' => 'text',
41 1
            'label' => 'Last Name'
42
        ]);
43
        $this->crud->addColumn([
44
            'name' => 'user_role.name',
45
            'type' => 'text',
46 1
            'key' => 'user_role_name',
47 1
            'label' => 'Role'
48
        ]);
49
        $this->crud->addColumn([
50
            'name' => 'email',
51 1
            'type' => 'text',
52
            'label' => 'Email'
53
        ]);
54 1
        $this->crud->addColumn([
55
            'name' => 'gov_email',
56 1
            'type' => 'text',
57 1
            'label' => 'Government Email'
58
        ]);
59
        $this->crud->addColumn([
60
            'name' => 'not_in_gov',
61
            'type' => 'check',
62
            'label' => 'In Government'
63
        ]);
64 1
65 1
        $this->crud->addColumn([
66
            'name' => 'is_priority',
67
            'type' => 'check',
68
            'label' => 'Priority'
69
        ]);
70
71
        $this->crud->addColumn([
72 1
            'name' => 'contact_language',
73 1
            'type' => 'text',
74
            'label' => 'Contact Language'
75
        ]);
76
77 1
        $this->crud->addColumn([
78
            'name' => 'job_alerts',
79
            'type' => 'check',
80
            'label' => 'Job Alerts'
81
        ]);
82
83
        $this->crud->addFilter([
84
            'name' => 'user_role',
85
            'type' => 'select2',
86
            'label' => 'Role'
87
            ], function () {
88
                return UserRole::all()->keyBy('id')->pluck('name', 'id')->toArray();
89
            }, function ($value) : void {
90
                $this->crud->addClause('where', 'user_role_id', $value);
91
            });
92
    }
93
94
    public function setupUpdateOperation()
95
    {
96
        $this->crud->addField([
97
            'name' => 'full_name',
98
            'label' => 'Name',
99
            'type' => 'text',
100
            'attributes' => [
101
                'readonly'=>'readonly'
102
            ]
103
        ]);
104
        $this->crud->addField([
105
            'label' => 'Role',
106
            'type' => 'select',
107
            'name' => 'user_role_id', // The db column for the foreign key.
108
            'entity' => 'user_role', // The method that defines the relationship in your Model.
109
            'attribute' => 'name', // Foreign key attribute that is shown to user.
110
            'model' => 'App\Models\UserRole' // Foreign key model.
111
        ]);
112
        $this->crud->addField([
113
            'label' => 'Department',
114
            'type' => 'select',
115
            'name' => 'department_id', // The db column for the foreign key.
116
            'entity' => 'department', // The method that defines the relationship in your Model.
117
            'attribute' => 'name', // Foreign key attribute that is shown to user.
118
            'model' => 'App\Models\Lookup\Department' // Foreign key model.
119
        ]);
120
        $this->crud->addField([
121
            'name' => 'is_priority',
122
            'type' => 'checkbox',
123
            'label' => 'Priority'
124
        ]);
125
    }
126
}
127