Passed
Push — feature/application-urls ( 912c99...540418 )
by Tristan
07:23 queued 02:05
created

DepartmentCrudController::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\DepartmentCrudRequest as StoreRequest;
7
use App\Http\Requests\DepartmentCrudRequest as UpdateRequest;
8
use Backpack\CRUD\app\Http\Controllers\CrudController;
9
use Illuminate\Support\Facades\App;
10
11
class DepartmentCrudController extends CrudController
12
{
13
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
14
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
15
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
16
    use \Backpack\CRUD\app\Http\Controllers\Operations\ReorderOperation;
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 of views and controller actions.
27
        $this->crud->setModel('App\Models\Lookup\Department');
28
        // Custom backpack route.
29
        $this->crud->setRoute('admin/department');
30
        // Custom strings to display within the backpack UI.
31
        $this->crud->setEntityNameStrings('department', 'departments');
32
33
        $this->crud->operation(['create', 'update'], function () {
34
            $this->crud->addField([
35
                'name' => 'name',
36
                'type' => 'text',
37
                'label' => 'Name',
38
            ]);
39
40
            $this->crud->addField([
41
                'name' => 'impact',
42
                'type' => 'textarea',
43
                'label' => 'Impact',
44
            ]);
45
46
            $this->crud->addField([
47
                'name' => 'preference',
48
                'type' => 'textarea',
49
                'label' => 'Preference',
50
            ]);
51
52
            $this->crud->addField([
53
                'name' => 'allow_indeterminate',
54
                'type' => 'checkbox',
55
                'label' => 'Allow Indeterminate: allow Indeterminate length jobs to be created within this department.',
56
            ]);
57
58
            $this->crud->addField([
59
                'name' => 'is_partner',
60
                'type' => 'checkbox',
61
                'label' => 'Is this department a Talent Cloud partner?',
62
            ]);
63
64
            $this->crud->addField([
65
                'name' => 'is_host',
66
                'type' => 'checkbox',
67
                'label' => 'Is this department a Talent Cloud host?',
68
            ]);
69
        });
70
    }
71
72
    public function setupListOperation()
73
    {
74
        // Required for order logic.
75
        $locale = 'en';
76
        if (null !== $this->request->input('locale')) {
77
            $locale = $this->request->input('locale');
78
        }
79
        App::setLocale($locale);
80
81
        // Remove delete button.
82
        $this->crud->removeButton('delete');
83
84
        // Add custom columns to the Department index view.
85
        $this->crud->addColumn([
86
            'name' => 'id',
87
            'type' => 'text',
88
            'label' => 'ID',
89
            'orderable' => true,
90
        ]);
91
92
        $this->crud->addColumn([
93
            'name' => 'name',
94
            'type' => 'text',
95
            'label' => 'Name',
96
            'orderable' => true,
97
            'limit' => 70,
98
            'orderLogic' => function ($query, $column, $columnDirection) use ($locale) {
99
                return $query->orderBy('name->' . $locale, $columnDirection)->select('*');
100
            }
101
        ]);
102
103
        $this->crud->addColumn([
104
            'name' => 'impact',
105
            'type' => 'text',
106
            'label' => 'Impact',
107
            'orderable' => false,
108
            'limit' => 70,
109
        ]);
110
111
        $this->crud->addColumn([
112
            'name' => 'preference',
113
            'type' => 'text',
114
            'label' => 'Preference',
115
            'orderable' => false,
116
            'limit' => 70,
117
        ]);
118
119
        $this->crud->addColumn([
120
            'name' => 'allow_indeterminate',
121
            'type' => 'check',
122
            'label' => 'Allow Indeterminate',
123
        ]);
124
125
        $this->crud->addColumn([
126
            'name' => 'is_partner',
127
            'type' => 'check',
128
            'label' => 'Partner Department',
129
        ]);
130
131
        $this->crud->addColumn([
132
            'name' => 'is_host',
133
            'type' => 'check',
134
            'label' => 'Talent Cloud Host',
135
        ]);
136
137
        // Add filter for departments that are partners
138
        $this->crud->addFilter(
139
            [
140
                'type' => 'simple',
141
                'name' => 'partners',
142
                'label'=> 'Partner Departments'
143
            ],
144
            false,
145
            function () {
146
                $this->crud->addClause('where', 'is_partner', '=', true);
147
            }
148
        );
149
150
    }
151
152
    public function setupCreateOperation()
153
    {
154
        $this->crud->setValidation(StoreRequest::class);
155
    }
156
157
    public function setupUpdateOperation()
158
    {
159
        $this->crud->setValidation(UpdateRequest::class);
160
    }
161
162
    protected function setupReorderOperation()
163
    {
164
        $this->crud->set('reorder.label', 'name');
165
        $this->crud->set('reorder.max_level', 1);
166
    }
167
}
168