Conditions | 14 |
Paths | 768 |
Total Lines | 202 |
Code Lines | 117 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
60 | public function setupListOperation() |
||
61 | { |
||
62 | |||
63 | if ($this->crud->getRequest()->has('course_id')) { |
||
64 | $this->mode = 'course'; |
||
65 | $this->course = Course::findOrFail($this->crud->getRequest()->course_id); |
||
66 | |||
67 | if (Gate::forUser(backpack_user())->denies('view-course', $this->course)) { |
||
68 | abort(403); |
||
69 | } |
||
70 | CRUD::addClause('course', $this->course->id); |
||
71 | } |
||
72 | |||
73 | if ($this->mode === 'course') { |
||
74 | CRUD::denyAccess(['create', 'update', 'delete']); |
||
75 | } |
||
76 | |||
77 | if (backpack_user()->hasRole('admin')) { |
||
78 | CRUD::enableExportButtons(); |
||
79 | } |
||
80 | |||
81 | if ($this->mode === 'global' && $this->crud->getOperation() === 'list') { |
||
82 | $pendingBalance = Enrollment::pending()->sum('balance'); |
||
83 | |||
84 | Widget::add()->type('view')->view('enrollments.total_balance_widget')->value($pendingBalance)->to('before_content'); |
||
85 | // Widget::add()->type('view')->view('enrollments.total_income_widget')->value($periodIncome)->to('before_content'); |
||
86 | } |
||
87 | |||
88 | if ($this->mode === 'course') { |
||
89 | Widget::add(['type' => 'view', 'view' => 'partials.course_info', 'course' => $this->course])->to('before_content'); |
||
90 | |||
91 | CRUD::denyAccess(['show']); |
||
92 | CRUD::addButtonFromView('line', 'showStudent', 'showStudentForEnrollment'); |
||
93 | |||
94 | CRUD::addButtonFromView('top', 'enroll-student-in-course', 'enroll-student-in-course', 'end'); |
||
95 | CRUD::addButtonFromView('top', 'switch-to-photo-roster', 'switch-to-photo-roster', 'end'); |
||
96 | } |
||
97 | |||
98 | if (config('app.currency_position') === 'before') { |
||
99 | $currency = ['prefix' => config('app.currency_symbol')]; |
||
100 | } else { |
||
101 | $currency = ['suffix' => config('app.currency_symbol')]; |
||
102 | } |
||
103 | |||
104 | CRUD::addColumns([ |
||
105 | |||
106 | [ |
||
107 | 'name' => 'id', |
||
108 | 'label' => 'ID', |
||
109 | ], |
||
110 | |||
111 | [ |
||
112 | 'label' => __('ID number'), |
||
113 | 'type' => 'text', |
||
114 | 'name' => 'student.idnumber', |
||
115 | ], |
||
116 | |||
117 | [ |
||
118 | 'name' => 'user', |
||
119 | 'key' => 'user_lastname', |
||
120 | 'attribute' => 'lastname', |
||
121 | 'label' => __('Last Name'), |
||
122 | 'type' => 'relationship', |
||
123 | 'searchLogic' => function ($query, $column, $searchTerm) { |
||
124 | $query->orWhereHas('student', function ($q) use ($searchTerm) { |
||
125 | $q->whereHas('user', function ($q) use ($searchTerm) { |
||
126 | $q->where('lastname', 'like', '%'.$searchTerm.'%'); |
||
127 | }); |
||
128 | }); |
||
129 | }, |
||
130 | ], |
||
131 | |||
132 | [ |
||
133 | 'name' => 'user', |
||
134 | 'key' => 'user_firstname', |
||
135 | 'attribute' => 'firstname', |
||
136 | 'label' => __('First Name'), |
||
137 | 'type' => 'relationship', |
||
138 | 'searchLogic' => function ($query, $column, $searchTerm) { |
||
139 | $query->orWhereHas('student', function ($q) use ($searchTerm) { |
||
140 | $q->whereHas('user', function ($q) use ($searchTerm) { |
||
141 | $q->where('firstname', 'like', '%'.$searchTerm.'%'); |
||
142 | }); |
||
143 | }); |
||
144 | }, |
||
145 | ], |
||
146 | |||
147 | [ |
||
148 | 'label' => __('Age'), |
||
149 | 'name' => 'student_age', |
||
150 | ], |
||
151 | |||
152 | [ |
||
153 | 'label' => __('Birthdate'), |
||
154 | 'name' => 'student_birthdate', |
||
155 | ], |
||
156 | ]); |
||
157 | |||
158 | if ($this->mode === 'global') { |
||
159 | CRUD::addColumns([[// COURSE NAME |
||
160 | 'label' => __('Course'), // Table column heading |
||
161 | 'type' => 'select', 'name' => 'course_id', // the column that contains the ID of that connected entity; |
||
162 | 'entity' => 'course', // the method that defines the relationship in your Model |
||
163 | 'attribute' => 'name', // foreign key attribute that is shown to user |
||
164 | 'model' => Course::class, // foreign key model |
||
165 | ], |
||
166 | |||
167 | ['type' => 'relationship', 'name' => 'course.period', 'label' => __('Period'), 'attribute' => 'name'], ]); |
||
168 | } |
||
169 | |||
170 | CRUD::addColumns([ |
||
171 | [ |
||
172 | // STATUS |
||
173 | 'label' => __('Status'), // Table column heading |
||
174 | 'type' => 'select', |
||
175 | 'name' => 'status_id', // the column that contains the ID of that connected entity; |
||
176 | 'entity' => 'enrollmentStatus', // the method that defines the relationship in your Model |
||
177 | 'attribute' => 'name', // foreign key attribute that is shown to user |
||
178 | 'model' => EnrollmentStatusType::class, // foreign key model |
||
179 | ], |
||
180 | |||
181 | array_merge([ |
||
182 | 'name' => 'balance', |
||
183 | 'label' => __('Balance'), |
||
184 | 'type' => 'number', |
||
185 | ], |
||
186 | $currency), |
||
187 | ]); |
||
188 | |||
189 | if (config('invoicing.allow_scheduled_payments')) { |
||
190 | CRUD::addColumn([ |
||
191 | 'name' => 'scheduledPayments', // name of relationship method in the model |
||
192 | 'type' => 'relationship', 'label' => __('Scheduled Payments'), // OPTIONAL |
||
193 | 'attribute' => 'date', 'model' => ScheduledPayment::class, // foreign key model |
||
194 | ]); |
||
195 | } |
||
196 | |||
197 | CRUD::addColumns([ |
||
198 | |||
199 | [ |
||
200 | // any type of relationship |
||
201 | 'name' => 'scholarships', // name of relationship method in the model |
||
202 | 'type' => 'relationship', |
||
203 | 'label' => __('Scholarship'), |
||
204 | // OPTIONAL |
||
205 | // 'entity' => 'tags', // the method that defines the relationship in your Model |
||
206 | 'attribute' => 'name', // foreign key attribute that is shown to user |
||
207 | 'model' => Scholarship::class, // foreign key model |
||
208 | ], |
||
209 | |||
210 | [ |
||
211 | 'label' => __('Email'), // Table column heading |
||
212 | 'name' => 'user', |
||
213 | 'attribute' => 'email', |
||
214 | 'type' => 'relationship', |
||
215 | ], |
||
216 | |||
217 | [ |
||
218 | 'label' => __('Phone number'), // Table column heading |
||
219 | 'type' => 'select_multiple', |
||
220 | 'name' => 'student.phone', // the method that defines the relationship in your Model |
||
221 | 'attribute' => 'phone_number', // foreign key attribute that is shown to user |
||
222 | 'model' => PhoneNumber::class, // foreign key model |
||
223 | ], |
||
224 | |||
225 | ]); |
||
226 | |||
227 | if ($this->mode === 'global') { |
||
228 | CRUD::addFilter([ |
||
229 | 'name' => 'status_id', |
||
230 | 'type' => 'select2_multiple', |
||
231 | 'label'=> __('Status'), |
||
232 | ], fn () => EnrollmentStatusType::all()->pluck('name', 'id')->toArray(), |
||
233 | function ($values) { |
||
234 | // if the filter is active |
||
235 | foreach (json_decode($values, null, 512, JSON_THROW_ON_ERROR) as $value) { |
||
236 | CRUD::addClause('orWhere', 'status_id', $value); |
||
237 | } |
||
238 | }); |
||
239 | |||
240 | CRUD::addFilter([ |
||
241 | 'name' => 'period_id', |
||
242 | 'type' => 'select2', |
||
243 | 'label'=> __('Period'), |
||
244 | ], fn () => Period::all()->pluck('name', 'id')->toArray(), function ($value) { |
||
245 | // if the filter is active |
||
246 | CRUD::addClause('period', $value); |
||
247 | }); |
||
248 | |||
249 | CRUD::addFilter( |
||
250 | [ |
||
251 | 'name' => 'scholarship', |
||
252 | 'type' => 'select2', |
||
253 | 'label'=> __('Scholarship'), |
||
254 | ], |
||
255 | fn () => Scholarship::all()->pluck('name', 'id')->toArray(), |
||
256 | function ($value) { // if the filter is active |
||
257 | if ($value == 'all') { |
||
258 | CRUD::addClause('whereHas', 'scholarships'); |
||
259 | } else { |
||
260 | CRUD::addClause('whereHas', 'scholarships', function ($q) use ($value) { |
||
261 | $q->where('scholarships.id', $value); |
||
262 | }); |
||
360 |