Passed
Push — task/skillcategory-admin ( e35471 )
by
unknown
05:33
created

SkillCategoryCrudController::setup()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 38
rs 9.552
cc 2
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
// Validation.
6
use App\Http\Requests\SkillCategoryCrudRequest as StoreRequest;
7
use App\Http\Requests\SkillCategoryCrudRequest as UpdateRequest;
8
use App\Models\SkillCategory;
9
use Backpack\CRUD\app\Http\Controllers\CrudController;
10
use Illuminate\Support\Facades\App;
11
12
class SkillCategoryCrudController extends CrudController
13
{
14
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
15
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
16
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
17
18
    /**
19
     * Prepare the admin interface by setting the associated
20
     * model, setting the route, and adding custom columns/fields.
21
     *
22
     * @return void
23
     */
24
    public function setup() : void
25
    {
26
        // Eloquent model to associate with this collection
27
        // of views and controller actions.
28
        $this->crud->setModel('App\Models\SkillCategory');
29
        // Custom backpack route.
30
        $this->crud->setRoute('admin/skill-category');
31
        // Custom strings to display within the backpack UI,
32
        // things like Create Skill Category, Delete Skill Categories, etc.
33
        $this->crud->setEntityNameStrings('skill category', 'skill categories');
34
35
        $this->crud->operation(['create', 'update'], function () {
36
            // Add custom fields to the create/update views.
37
            $this->crud->addField([
38
                'name' => 'key',
39
                'type' => 'text',
40
                'label' => 'Key',
41
            ]);
42
43
            $this->crud->addField([
44
                'name' => 'name',
45
                'type' => 'textarea',
46
                'label' => 'Name',
47
                'limit' => 70,
48
            ]);
49
50
            $this->crud->addField([
51
                'name' => 'parent_category_id',
52
                'label' => 'Parent Category',
53
                'type' => 'select_from_array',
54
                'allow_null' => false,
55
                'options' => SkillCategory::where(
56
                    // Exclude self from options.
57
                    'id',
58
                    '!=',
59
                    $this->crud->getCurrentEntry() ? $this->crud->getCurrentEntry()->id : null
60
                )
61
                ->get()->pluck('name', 'id')->toArray(),
62
            ]);
63
        });
64
    }
65
66
    public function setupListOperation()
67
    {
68
        $locale = 'en';
69
        if (null !== $this->request->input('locale')) {
70
            $locale = $this->request->input('locale');
71
        }
72
        App::setLocale($locale);
73
74
        // Remove delete button.
75
        $this->crud->removeButton('delete');
76
77
        // Add custom columns to the Skill Category index view.
78
        $this->crud->addColumn([
79
            'name' => 'id',
80
            'type' => 'text',
81
            'label' => 'ID',
82
            'orderable' => true
83
        ]);
84
85
        $this->crud->addColumn([
86
            'name' => 'key',
87
            'type' => 'text',
88
            'label' => 'Key',
89
            'orderable' => true,
90
        ]);
91
92
        $this->crud->addColumn([
93
            'name' => 'name',
94
            'type' => 'text',
95
            'label' => 'Name',
96
            'searchLogic' => function ($query, $column, $searchTerm) use ($locale) : void {
97
                $query->orWhere('name->' . $locale, 'like', "%$searchTerm%");
98
            },
99
            'orderable' => false,
100
        ]);
101
102
        // Add filter for skill categories without a parent category.
103
        $this->crud->addFilter(
104
            [
105
                'type' => 'simple',
106
                'name' => 'noParentSkillCategory',
107
                'label'=> 'No parent skill category'
108
            ],
109
            false,
110
            function () {
111
                $this->crud->query = $this->crud->query->where('parent_category_id', 0);
112
            }
113
        );
114
    }
115
116
    public function setupCreateOperation()
117
    {
118
        $this->crud->setValidation(StoreRequest::class);
119
    }
120
121
    public function setupUpdateOperation()
122
    {
123
        $this->crud->setValidation(UpdateRequest::class);
124
    }
125
}
126