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