Passed
Push — task/common-translation-packag... ( 1a550f...cc3697 )
by Grant
18:07 queued 07:02
created

JobPosterCrudController::setupListOperation()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 75
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 75
rs 8.9599
c 0
b 0
f 0
cc 4
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Models\Lookup\Department;
6
use Backpack\CRUD\app\Http\Controllers\CrudController;
7
use Illuminate\Support\Facades\App;
8
9
class JobPosterCrudController extends CrudController
10
{
11
    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\JobPosterCrudController: $model, $query, $entity_name_plural
Loading history...
12
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { update as traitUpdate;
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\JobPosterCrudController: $model, $entity_name
Loading history...
13
    }
14
15
    /**
16
     * Prepare the admin interface by setting the associated
17
     * model, setting the route, and adding custom columns/fields.
18
     *
19
     * @return void
20
     */
21
    public function setup() : void
22
    {
23
        $this->crud->setModel('App\Models\JobPoster');
24
        $this->crud->setRoute('admin/job-poster');
25
        $this->crud->setEntityNameStrings('Job Poster', 'Job Posters');
26
27
        if (!$this->request->has('order')) {
28
            $this->crud->orderBy('close_date_time', 'desc');
29
        }
30
    }
31
32
    public function setupListOperation()
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function setupListOperation()
Loading history...
33
    {
34
        // Required for order logic.
35
        $locale = 'en';
36
        if (null !== $this->request->input('locale')) {
37
            $locale = $this->request->input('locale');
38
        }
39
        App::setLocale($locale);
40
41
        // Add the custom blade buttons found in resources/views/vendor/backpack/crud/buttons/.
42
        $this->crud->addButtonFromView('line', 'job_admin_edit', 'job_admin_edit', 'end');
43
        $this->crud->addButtonFromView('line', 'spb_link', 'spb_link', 'end');
44
        $this->crud->addButtonFromView('line', 'jpb_link', 'jpb_link', 'end');
45
        $this->crud->addButtonFromView('line', 'job_poster_link', 'job_poster_link', 'end');
46
47
48
        $this->crud->addColumn([
49
            'name' => 'id',
50
            'type' => 'number',
51
            'label' => 'ID'
52
        ]);
53
        $this->crud->addColumn([
54
            'name' => 'title',
55
            'type' => 'text',
56
            'label' => 'Title',
57
            'orderLogic' => function ($query, $column, $columnDirection) use ($locale) {
58
                return $query->orderBy('title->' . $locale, $columnDirection)->select('*');
59
            }
60
        ]);
61
        $this->crud->addColumn([
62
            'name' => 'status',
63
            'label' => 'Status',
64
            'type' => 'model_function',
65
            'function_name' => 'status'
66
        ]);
67
        $this->crud->addColumn([
68
            'name' => 'published',
69
            'label' => 'Published',
70
            'type' => 'check',
71
        ]);
72
        $this->crud->addColumn([
73
            'name' => 'manager_user_name',
74
            'type' => 'closure',
75
            'label' => 'Manager',
76
            'orderable' => false,
77
            'function' => function ($entry) {
78
                return '<a href="' . route('manager.profile.edit', $entry->manager->user->id) . '" target="_blank">' . $entry->manager->user->full_name . '</a>';
79
            }
80
        ]);
81
        $this->crud->addColumn([
82
            'name' => 'department.name',
83
            'label' => 'Department',
84
            'type' => 'text'
85
        ]);
86
        $this->crud->addColumn([
87
            'name' => 'submitted_applications_count',
88
            'label' => 'Total Applications',
89
            'type' => 'model_function',
90
            'function_name' => 'submitted_applications_count'
91
        ]);
92
93
        // Filters.
94
        $this->crud->addFilter([
95
            'name' => 'departments',
96
            'type' => 'select2_multiple',
97
            'label' => 'Departments'
98
        ], function () {
99
            return Department::all()->pluck('name', 'id')->toArray();
100
        }, function ($values) {
101
            $this->crud->addClause('WhereHas', 'department', function ($query) use ($values) {
102
                foreach (json_decode($values) as $key => $value) {
103
                    if ($key === 0) {
104
                        $query->where('id', $value);
105
                    } else {
106
                        $query->orWhere('id', $value);
107
                    }
108
                }
109
            });
110
        });
111
    }
112
113
    public function setupUpdateOperation()
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function setupUpdateOperation()
Loading history...
114
    {
115
        $this->crud->addField([
116
            'name' => 'title',
117
            'label' => 'Title',
118
            'type' => 'text',
119
            'attributes' => [
120
                'readonly' => 'readonly'
121
            ]
122
        ]);
123
        $this->crud->addField([
124
            'name' => 'salary_min',
125
            'type' => 'number',
126
            'label' => 'Minimum Salary',
127
        ]);
128
        $this->crud->addField([
129
            'name' => 'salary_max',
130
            'type' => 'number',
131
            'label' => 'Maximum Salary',
132
        ]);
133
        $this->crud->addField([
134
            'name' => 'noc',
135
            'type' => 'number',
136
            'label' => 'NOC Code',
137
        ]);
138
        $this->crud->addField([
139
            'name' => 'open_date_time',
140
            'label' => 'Open Date',
141
            'type' => 'date_picker',
142
            'date_picker_options' => [
143
                'todayBtn' => 'linked',
144
                'format' => 'yyyy-mm-dd',
145
            ],
146
        ]);
147
        $this->crud->addField([
148
            'name' => 'close_date_time',
149
            'label' => 'Close Date',
150
            'type' => 'date_picker',
151
            'date_picker_options' => [
152
                'todayBtn' => 'linked',
153
                'format' => 'yyyy-mm-dd',
154
            ],
155
        ]);
156
        $this->crud->addField([
157
            'name' => 'start_date_time',
158
            'label' => 'Start Date',
159
            'type' => 'date_picker',
160
            'date_picker_options' => [
161
                'todayBtn' => 'linked',
162
                'format' => 'yyyy-mm-dd',
163
            ],
164
        ]);
165
        $this->crud->addField([
166
            'name' => 'process_number',
167
            'type' => 'number',
168
            'label' => 'Process #',
169
        ]);
170
        $this->crud->addField([
171
            'name' => 'priority_clearance_number',
172
            'type' => 'number',
173
            'label' => 'Priority Clearance #',
174
        ]);
175
        $this->crud->addField([
176
            'name' => 'loo_issuance_date',
177
            'type' => 'date_picker',
178
            'label' => 'Letter of Offer Issuance Date',
179
            'date_picker_options' => [
180
               'todayBtn' => 'linked',
181
               'format' => 'yyyy-mm-dd',
182
            ],
183
        ]);
184
        if ($this->crud->getCurrentEntry() &&
185
            !$this->crud->getCurrentEntry()->published
186
        ) {
187
            $this->crud->addField([
188
                'name' => 'published',
189
                'label' => 'Publish',
190
                'type' => 'checkbox'
191
            ]);
192
        }
193
    }
194
195
    public function update()
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function update()
Loading history...
196
    {
197
        $open_date = $this->crud->request->request->get('open_date_time');
198
        $close_date = $this->crud->request->request->get('close_date_time');
199
        $start_date = $this->crud->request->request->get('start_date_time');
200
        $this->crud->request->request->remove('open_date_time');
201
        $this->crud->request->request->remove('close_date_time');
202
        $this->crud->request->request->remove('start_date_time');
203
        // Manipulates the input fields to save the "end of day" timestamp for
204
        // open/close/start dates.
205
        $this->crud->request->request->add([
206
            'open_date_time' => ptDayStartToUtcTime($open_date),
207
            'close_date_time' => ptDayEndToUtcTime($close_date),
208
            'start_date_time' => ptDayStartToUtcTime($start_date),
209
        ]);
210
        $response = $this->traitUpdate();
211
212
        return $response;
213
    }
214
}
215