Passed
Pull Request — release/1.0.15 (#1655)
by Tristan
10:06 queued 02:47
created

SkillCrudController::setupCreateOperation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
// Validation.
6
use App\Http\Requests\SkillCrudRequest as StoreRequest;
7
use App\Http\Requests\SkillCrudRequest as UpdateRequest;
8
use App\Models\Classification;
9
use App\Models\Lookup\SkillType;
10
use Backpack\CRUD\app\Http\Controllers\CrudController;
11
use Illuminate\Support\Facades\App;
12
13
class SkillCrudController extends CrudController
14
{
15
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Http\C...perations\ListOperation requires some properties which are not provided by App\Http\Controllers\Admin\SkillCrudController: $model, $entity_name_plural
Loading history...
16
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
0 ignored issues
show
Bug introduced by
The trait Backpack\CRUD\app\Http\C...rations\CreateOperation requires the property $entity_name which is not provided by App\Http\Controllers\Admin\SkillCrudController.
Loading history...
17
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Http\C...rations\UpdateOperation requires some properties which are not provided by App\Http\Controllers\Admin\SkillCrudController: $model, $entity_name
Loading history...
18
19
    /**
20
     * Prepare the admin interface by setting the associated
21
     * model, setting the route, and adding custom columns/fields.
22
     *
23
     * @return void
24
     */
25
    public function setup() : void
26
    {
27
        // Eloquent model to associate with this collection
28
        // of views and controller actions.
29
        $this->crud->setModel('App\Models\Skill');
30
        // Custom backpack route.
31
        $this->crud->setRoute('admin/skill');
32
        // Custom strings to display within the backpack UI,
33
        // things like Create Skill, Delete Skills, etc.
34
        $this->crud->setEntityNameStrings('skill', 'skills');
35
36
        $this->crud->operation(['create', 'update'], function () {
37
            // Add custom fields to the create/update views.
38
            $this->crud->addField([
39
                'name' => 'name',
40
                'type' => 'text',
41
                'label' => 'Name',
42
            ]);
43
44
            $this->crud->addField([
45
                'name' => 'description',
46
                'type' => 'textarea',
47
                'label' => 'Description'
48
            ]);
49
50
            $this->crud->addField([
51
                'name' => 'skill_type_id',
52
                'label' => 'Type',
53
                'type' => 'select_from_array',
54
                'options' => SkillType::all()->pluck('name', 'id')->toArray(),
55
                'allow_null' => false,
56
            ]);
57
58
            $this->crud->addField([
59
                'name' => 'classifications',
60
                'type' => 'select2_multiple',
61
                'label' => 'Classifications (select all that apply)',
62
                'entity' => 'skills',
63
                'attribute' => 'key',
64
                'model' => 'App\Models\Classification',
65
                'pivot' => true,
66
            ]);
67
68
            $this->crud->addField([
69
                'name' => 'is_culture_skill',
70
                'label' => 'This is a culture skill',
71
                'type' => 'checkbox'
72
            ]);
73
74
            $this->crud->addField([
75
                'name' => 'is_future_skill',
76
                'label' => 'This is a future skill',
77
                'type' => 'checkbox'
78
            ]);
79
        });
80
    }
81
82
    public function setupListOperation()
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function setupListOperation()
Loading history...
83
    {
84
        // Workaround for how the unique_translation validation
85
        // works in App\Http\Requests\SkillCrudRequest.
86
        $locale = 'en';
87
        if (null !== $this->request->input('locale')) {
88
            $locale = $this->request->input('locale');
89
        }
90
        App::setLocale($locale);
91
92
        // Remove delete button.
93
        $this->crud->removeButton('delete');
94
95
        // Add custom columns to the Skill index view.
96
        $this->crud->addColumn([
97
            'name' => 'id',
98
            'type' => 'text',
99
            'label' => 'ID',
100
            'orderable' => true
101
        ]);
102
103
        $this->crud->addColumn([
104
            'name' => 'name',
105
            'type' => 'text',
106
            'label' => 'Name',
107
            'searchLogic' => function ($query, $column, $searchTerm) use ($locale) : void {
108
                $query->orWhere('name->' . $locale, 'like', "%$searchTerm%");
109
            },
110
            'orderLogic' => function ($query, $column, $columnDirection) use ($locale) {
111
                return $query->orderBy('name->' . $locale, $columnDirection)->select('*');
112
            }
113
        ]);
114
115
        $this->crud->addColumn([
116
            'name' => 'description',
117
            'type' => 'text',
118
            'label' => 'Description',
119
            'searchLogic' => function ($query, $column, $searchTerm) use ($locale) : void {
120
                $query->orWhere('description->' . $locale, 'like', "%$searchTerm%");
121
            },
122
            'orderable' => false,
123
        ]);
124
125
        $this->crud->addColumn([
126
            'name' => 'skill_type.name',
127
            'key' => 'skill_type_name',
128
            'type' => 'text',
129
            'label' => 'Type',
130
            'orderable' => true,
131
            'orderLogic' => function ($query, $column, $columnDirection) use ($locale) {
0 ignored issues
show
Unused Code introduced by
The import $locale is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
132
                return $query->orderBy('skill_type_id', $columnDirection)->select('*');
133
            }
134
        ]);
135
136
        $this->crud->addColumn([
137
            'label' => 'Classifications',
138
            'type' => 'select_multiple',
139
            'name' => 'classifications',
140
            'entity' => 'classifications',
141
            'attribute' => 'key',
142
            'model' => 'App\Models\Skill',
143
        ]);
144
145
        $this->crud->addColumn([
146
            'name' => 'is_culture_skill',
147
            'label' => 'Culture',
148
            'type' => 'boolean',
149
            'orderable' => true,
150
        ]);
151
152
        $this->crud->addColumn([
153
            'name' => 'is_future_skill',
154
            'label' => 'Future',
155
            'type' => 'boolean',
156
            'orderable' => true,
157
        ]);
158
159
        // Add select2_multiple filter for classifications.
160
        $this->crud->addFilter([
161
            'name' => 'classifications',
162
            'key' => 'classifications_filter',
163
            'type' => 'select2_multiple',
164
            'label' => 'Filter by classification'
165
        ], function () {
166
            // The options that show up in the select2.
167
            return Classification::all()->pluck('key', 'id')->toArray();
168
        }, function ($values) {
169
            // If the filter is active.
170
            foreach (json_decode($values) as $key => $value) {
171
                $this->crud->query = $this->crud->query->whereHas('classifications', function ($query) use ($value) {
172
                    $query->where('id', $value);
173
                });
174
            }
175
        });
176
177
        // Add filter for skills without classifications.
178
        $this->crud->addFilter(
179
            [
180
                'type' => 'simple',
181
                'name' => 'noClassification',
182
                'label'=> 'No classification'
183
            ],
184
            false,
185
            function () {
186
                $this->crud->query = $this->crud->query->doesntHave('classifications');
187
            }
188
        );
189
    }
190
191
    public function setupCreateOperation()
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function setupCreateOperation()
Loading history...
192
    {
193
        $this->crud->setValidation(StoreRequest::class);
194
    }
195
196
    public function setupUpdateOperation()
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function setupUpdateOperation()
Loading history...
197
    {
198
        $this->crud->setValidation(UpdateRequest::class);
199
    }
200
}
201