Passed
Push — task/user-api-endpoint ( 6e4649...f0b85b )
by Chris
04:32
created

JobPosterCrudController::setupListOperation()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 83
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 83
rs 8.5178
c 0
b 0
f 0
cc 5
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;
12
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { update as traitUpdate;
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()
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
            'searchLogic' => function ($query, $column, $searchTerm) use ($locale) : void {
58
                $query->orWhere('title->' . $locale, 'ilike', "%$searchTerm%");
59
            },
60
            'orderLogic' => function ($query, $column, $columnDirection) use ($locale) {
61
                return $query->orderBy('title->' . $locale, $columnDirection)->select('*');
62
            }
63
        ]);
64
        $this->crud->addColumn([
65
            'name' => 'status',
66
            'label' => 'Status',
67
            'type' => 'model_function',
68
            'function_name' => 'status'
69
        ]);
70
        $this->crud->addColumn([
71
            'name' => 'published',
72
            'label' => 'Published',
73
            'type' => 'check',
74
        ]);
75
        $this->crud->addColumn([
76
            'name' => 'manager_user_name',
77
            'type' => 'closure',
78
            'label' => 'Manager',
79
            'orderable' => false,
80
            'function' => function ($entry) {
81
                return '<a href="' . route('manager.profile.edit', $entry->manager->id) . '" target="_blank">' . $entry->manager->user->full_name . '</a>';
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
                return '<a href="' . /** @scrutinizer ignore-call */ route('manager.profile.edit', $entry->manager->id) . '" target="_blank">' . $entry->manager->user->full_name . '</a>';
Loading history...
82
            }
83
        ]);
84
        $this->crud->addColumn([
85
            'name' => 'department.name',
86
            'label' => 'Department',
87
            'type' => 'text'
88
        ]);
89
        $this->crud->addColumn([
90
            'name' => 'submitted_applications_count',
91
            'label' => 'Applications',
92
            'type' => 'closure',
93
            'function' =>
94
                function ($entry) {
95
                    return $entry->submitted_applications_count() > 0 ?
96
                        '<a target="_blank" href="' . route('manager.jobs.applications', $entry->id) . '">' . $entry->submitted_applications_count() . ' (View <i class="fa fa-external-link"></i>)</a>' :
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
                        '<a target="_blank" href="' . /** @scrutinizer ignore-call */ route('manager.jobs.applications', $entry->id) . '">' . $entry->submitted_applications_count() . ' (View <i class="fa fa-external-link"></i>)</a>' :
Loading history...
97
                        $entry->submitted_applications_count();
98
                }
99
        ]);
100
101
        // Filters.
102
        $this->crud->addFilter([
103
            'name' => 'departments',
104
            'type' => 'select2_multiple',
105
            'label' => 'Departments'
106
        ], function () {
107
            return Department::all()->pluck('name', 'id')->toArray();
108
        }, function ($values) {
109
            $this->crud->addClause('WhereHas', 'department', function ($query) use ($values) {
110
                foreach (json_decode($values) as $key => $value) {
111
                    if ($key === 0) {
112
                        $query->where('id', $value);
113
                    } else {
114
                        $query->orWhere('id', $value);
115
                    }
116
                }
117
            });
118
        });
119
    }
120
121
    public function setupUpdateOperation()
122
    {
123
        $this->crud->addField([
124
            'name' => 'title',
125
            'label' => 'Title',
126
            'type' => 'text',
127
            'attributes' => [
128
                'readonly' => 'readonly'
129
            ]
130
        ]);
131
        $this->crud->addField([
132
            'name' => 'salary_min',
133
            'type' => 'number',
134
            'label' => 'Minimum Salary',
135
        ]);
136
        $this->crud->addField([
137
            'name' => 'salary_max',
138
            'type' => 'number',
139
            'label' => 'Maximum Salary',
140
        ]);
141
        $this->crud->addField([
142
            'name' => 'noc',
143
            'type' => 'number',
144
            'label' => 'NOC Code',
145
        ]);
146
        $this->crud->addField([
147
            'name' => 'open_date_time',
148
            'label' => 'Open Date',
149
            'type' => 'date_picker',
150
            'date_picker_options' => [
151
                'todayBtn' => 'linked',
152
                'format' => 'yyyy-mm-dd',
153
            ],
154
        ]);
155
        $this->crud->addField([
156
            'name' => 'close_date_time',
157
            'label' => 'Close Date',
158
            'type' => 'date_picker',
159
            'date_picker_options' => [
160
                'todayBtn' => 'linked',
161
                'format' => 'yyyy-mm-dd',
162
            ],
163
        ]);
164
        $this->crud->addField([
165
            'name' => 'start_date_time',
166
            'label' => 'Start Date',
167
            'type' => 'date_picker',
168
            'date_picker_options' => [
169
                'todayBtn' => 'linked',
170
                'format' => 'yyyy-mm-dd',
171
            ],
172
        ]);
173
        $this->crud->addField([
174
            'name' => 'process_number',
175
            'type' => 'number',
176
            'label' => 'Process #',
177
        ]);
178
        $this->crud->addField([
179
            'name' => 'priority_clearance_number',
180
            'type' => 'number',
181
            'label' => 'Priority Clearance #',
182
        ]);
183
        $this->crud->addField([
184
            'name' => 'loo_issuance_date',
185
            'type' => 'date_picker',
186
            'label' => 'Letter of Offer Issuance Date',
187
            'date_picker_options' => [
188
                'todayBtn' => 'linked',
189
                'format' => 'yyyy-mm-dd',
190
            ],
191
        ]);
192
        if ($this->crud->getCurrentEntry() &&
193
            !$this->crud->getCurrentEntry()->published
194
        ) {
195
            $this->crud->addField([
196
                'name' => 'published',
197
                'label' => 'Publish',
198
                'type' => 'checkbox'
199
            ]);
200
        }
201
    }
202
203
    public function update()
204
    {
205
        $open_date = $this->crud->request->request->get('open_date_time');
206
        $close_date = $this->crud->request->request->get('close_date_time');
207
        $start_date = $this->crud->request->request->get('start_date_time');
208
        $this->crud->request->request->remove('open_date_time');
209
        $this->crud->request->request->remove('close_date_time');
210
        $this->crud->request->request->remove('start_date_time');
211
        // Manipulates the input fields to save the "end of day" timestamp for
212
        // open/close/start dates.
213
        $this->crud->request->request->add([
214
            'open_date_time' => $open_date !== null ? ptDayStartToUtcTime($open_date) : null,
215
            'close_date_time' => $close_date !== null ? ptDayEndToUtcTime($close_date) : null,
216
            'start_date_time' => $start_date !== null ? ptDayStartToUtcTime($start_date) : null,
217
        ]);
218
        $response = $this->traitUpdate();
219
220
        return $response;
221
    }
222
}
223