Passed
Push — task/batch-request-skills-step ( d5d8c8...80c6aa )
by Tristan
07:05 queued 02:01
created

SkillCategoryCrudController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 9
eloc 64
c 4
b 0
f 2
dl 0
loc 126
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setupUpdateOperation() 0 3 1
A setupReorderOperation() 0 4 1
A setup() 0 44 4
A setupCreateOperation() 0 3 1
A setupListOperation() 0 46 2
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
    use \Backpack\CRUD\app\Http\Controllers\Operations\ReorderOperation;
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\SkillCategory');
30
        // Custom backpack route.
31
        $this->crud->setRoute('admin/skill-category');
32
        // Custom strings to display within the backpack UI,
33
        // things like Create Skill Category, Delete Skill Categories, etc.
34
        $this->crud->setEntityNameStrings('skill category', 'skill categories');
35
36
        $this->crud->operation(['create', 'update'], function () {
37
            // Add custom fields to the create/update views.
38
            $this->crud->addField([
39
                'name' => 'key',
40
                'type' => 'text',
41
                'label' => 'Key',
42
            ]);
43
44
            $this->crud->addField([
45
                'name' => 'name',
46
                'type' => 'textarea',
47
                'label' => 'Name',
48
                'limit' => 70,
49
            ]);
50
51
            $this->crud->addField([
52
                'name' => 'parent_id',
53
                'label' => 'Parent Category',
54
                'type' => 'select_from_array',
55
                'allows_null' => true,
56
                'options' => $this->crud->getCurrentEntry() && $this->crud->getCurrentEntry()->depth == 1 ? [] : SkillCategory::where(
57
                    // Exclude self from options.
58
                    'id',
59
                    '!=',
60
                    $this->crud->getCurrentEntry() ? $this->crud->getCurrentEntry()->id : null
61
                )
62
                ->where(
63
                    // Include categories with depth of 1 (parent skill categories) in options.
64
                    'depth',
65
                    '=',
66
                    1
67
                )
68
                ->get()->pluck('name', 'id')->toArray(),
69
                // Exclude all categories if self has depth of 1 (parent skill category).
70
            ]);
71
        });
72
    }
73
74
    public function setupListOperation()
75
    {
76
        $locale = 'en';
77
        if (null !== $this->request->input('locale')) {
78
            $locale = $this->request->input('locale');
79
        }
80
        App::setLocale($locale);
81
82
        // Remove delete button.
83
        $this->crud->removeButton('delete');
84
85
        // Add custom columns to the Skill Category index view.
86
        $this->crud->addColumn([
87
            'name' => 'id',
88
            'type' => 'text',
89
            'label' => 'ID',
90
            'orderable' => true
91
        ]);
92
93
        $this->crud->addColumn([
94
            'name' => 'key',
95
            'type' => 'text',
96
            'label' => 'Key',
97
            'orderable' => true,
98
        ]);
99
100
        $this->crud->addColumn([
101
            'name' => 'name',
102
            'type' => 'text',
103
            'label' => 'Name',
104
            'searchLogic' => function ($query, $column, $searchTerm) use ($locale) : void {
105
                $query->orWhere('name->' . $locale, 'like', "%$searchTerm%");
106
            },
107
            'orderable' => false,
108
        ]);
109
110
        // Add filter for skill categories without a parent category.
111
        $this->crud->addFilter(
112
            [
113
                'type' => 'simple',
114
                'name' => 'noParentSkillCategory',
115
                'label'=> 'No parent skill category'
116
            ],
117
            false,
118
            function () {
119
                $this->crud->query = $this->crud->query->whereNull('parent_id');
120
            }
121
        );
122
    }
123
124
    protected function setupReorderOperation()
125
    {
126
        $this->crud->set('reorder.label', 'name');
127
        $this->crud->set('reorder.max_level', 2);
128
    }
129
130
    public function setupCreateOperation()
131
    {
132
        $this->crud->setValidation(StoreRequest::class);
133
    }
134
135
    public function setupUpdateOperation()
136
    {
137
        $this->crud->setValidation(UpdateRequest::class);
138
    }
139
}
140