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