Passed
Push — task/skillcategory-admin ( 2464ec...a87176 )
by
unknown
04:24
created

setupReorderOperation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 4
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\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
52
            $this->crud->addField([
53
                'name' => 'parent_id',
54
                'label' => 'Parent Category',
55
                'type' => 'select_from_array',
56
                'allows_null' => true,
57
                'options' => $this->crud->getCurrentEntry()->depth == 1 ? [] : SkillCategory::where(
58
                    // Exclude self from options.
59
                    'id',
60
                    '!=',
61
                    $this->crud->getCurrentEntry() ? $this->crud->getCurrentEntry()->id : null
62
                )
63
                ->where(
64
                    // Include categories with depth of 1 (parent skill categories) in options.
65
                    'depth',
66
                    '=',
67
                    1
68
                )
69
                ->get()->pluck('name', 'id')->toArray(),
70
                // Exclude all categories if self has depth of 1 (parent skill category).
71
            ]);
72
        });
73
    }
74
75
    public function setupListOperation()
76
    {
77
        $locale = 'en';
78
        if (null !== $this->request->input('locale')) {
79
            $locale = $this->request->input('locale');
80
        }
81
        App::setLocale($locale);
82
83
        // Remove delete button.
84
        $this->crud->removeButton('delete');
85
86
        // Add custom columns to the Skill Category index view.
87
        $this->crud->addColumn([
88
            'name' => 'id',
89
            'type' => 'text',
90
            'label' => 'ID',
91
            'orderable' => true
92
        ]);
93
94
        $this->crud->addColumn([
95
            'name' => 'key',
96
            'type' => 'text',
97
            'label' => 'Key',
98
            'orderable' => true,
99
        ]);
100
101
        $this->crud->addColumn([
102
            'name' => 'name',
103
            'type' => 'text',
104
            'label' => 'Name',
105
            'searchLogic' => function ($query, $column, $searchTerm) use ($locale) : void {
106
                $query->orWhere('name->' . $locale, 'like', "%$searchTerm%");
107
            },
108
            'orderable' => false,
109
        ]);
110
111
        // Add filter for skill categories without a parent category.
112
        $this->crud->addFilter(
113
            [
114
                'type' => 'simple',
115
                'name' => 'noParentSkillCategory',
116
                'label'=> 'No parent skill category'
117
            ],
118
            false,
119
            function () {
120
                $this->crud->query = $this->crud->query->whereNull('parent_id');
121
            }
122
        );
123
    }
124
125
    protected function setupReorderOperation()
126
    {
127
        $this->crud->set('reorder.label', 'name');
128
        $this->crud->set('reorder.max_level', 2);
129
    }
130
131
    public function setupCreateOperation()
132
    {
133
        $this->crud->setValidation(StoreRequest::class);
134
    }
135
136
    public function setupUpdateOperation()
137
    {
138
        $this->crud->setValidation(UpdateRequest::class);
139
    }
140
}
141