| Conditions | 15 |
| Paths | 192 |
| Total Lines | 208 |
| Code Lines | 125 |
| 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 | CRUD::addColumns([ |
||
| 91 | [ |
||
| 92 | 'name' => 'id', |
||
| 93 | 'label' => 'ID', |
||
| 94 | 'wrapper' => [ |
||
| 95 | 'element' => function ($crud, $column, $entry) { |
||
| 96 | return $entry->status_id > 2 ? 'del' : 'span'; |
||
| 97 | }, |
||
| 98 | ], |
||
| 99 | ], |
||
| 100 | |||
| 101 | [ |
||
| 102 | 'label' => __('ID number'), |
||
| 103 | 'type' => 'text', |
||
| 104 | 'name' => 'student.idnumber', |
||
| 105 | 'wrapper' => [ |
||
| 106 | 'element' => function ($crud, $column, $entry) { |
||
| 107 | return $entry->status_id > 2 ? 'del' : 'span'; |
||
| 108 | }, |
||
| 109 | ], |
||
| 110 | ], |
||
| 111 | |||
| 112 | [ |
||
| 113 | 'name' => 'user', |
||
| 114 | 'key' => 'user_lastname', |
||
| 115 | 'attribute' => 'lastname', |
||
| 116 | 'label' => __('Last Name'), |
||
| 117 | 'type' => 'relationship', |
||
| 118 | 'wrapper' => [ |
||
| 119 | 'element' => function ($crud, $column, $entry) { |
||
| 120 | return $entry->status_id > 2 ? 'del' : 'span'; |
||
| 121 | }, |
||
| 122 | ], |
||
| 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 | 'wrapper' => [ |
||
| 139 | 'element' => function ($crud, $column, $entry) { |
||
| 140 | return $entry->status_id > 2 ? 'del' : 'span'; |
||
| 141 | }, |
||
| 142 | ], |
||
| 143 | 'searchLogic' => function ($query, $column, $searchTerm) { |
||
| 144 | $query->orWhereHas('student', function ($q) use ($searchTerm) { |
||
| 145 | $q->whereHas('user', function ($q) use ($searchTerm) { |
||
| 146 | $q->where('firstname', 'like', '%'.$searchTerm.'%'); |
||
| 147 | }); |
||
| 148 | }); |
||
| 149 | }, |
||
| 150 | ], |
||
| 151 | |||
| 152 | [ |
||
| 153 | 'label' => __('Age'), |
||
| 154 | 'name' => 'student_age', |
||
| 155 | ], |
||
| 156 | |||
| 157 | [ |
||
| 158 | 'label' => __('Birthdate'), |
||
| 159 | 'name' => 'student_birthdate', |
||
| 160 | ], |
||
| 161 | ]); |
||
| 162 | |||
| 163 | if ($this->mode === 'global') { |
||
| 164 | CRUD::addColumns([ |
||
| 165 | [ |
||
| 166 | 'label' => __('Course'), |
||
| 167 | 'type' => 'select', |
||
| 168 | 'name' => 'course_id', |
||
| 169 | 'entity' => 'course', |
||
| 170 | 'attribute' => 'name', |
||
| 171 | 'model' => Course::class, |
||
| 172 | ], |
||
| 173 | [ |
||
| 174 | 'type' => 'relationship', |
||
| 175 | 'name' => 'course.period', |
||
| 176 | 'label' => __('Period'), |
||
| 177 | 'attribute' => 'name', ], |
||
| 178 | ]); |
||
| 179 | } |
||
| 180 | |||
| 181 | CRUD::addColumns([ |
||
| 182 | [ |
||
| 183 | 'label' => __('Status'), |
||
| 184 | 'type' => 'select', |
||
| 185 | 'name' => 'status_id', |
||
| 186 | 'entity' => 'enrollmentStatus', |
||
| 187 | 'attribute' => 'name', |
||
| 188 | 'model' => EnrollmentStatusType::class, |
||
| 189 | 'wrapper' => [ |
||
| 190 | 'element' => 'span', |
||
| 191 | 'class' => function ($crud, $column, $entry) { |
||
| 192 | return 'badge badge-pill badge-'.$entry->enrollmentStatus->styling(); |
||
| 193 | }, |
||
| 194 | ], |
||
| 195 | ], |
||
| 196 | ]); |
||
| 197 | |||
| 198 | if (config('invoicing.allow_scheduled_payments')) { |
||
| 199 | CRUD::addColumn([ |
||
| 200 | 'name' => 'scheduledPayments', |
||
| 201 | 'type' => 'relationship', 'label' => __('Scheduled Payments'), // OPTIONAL |
||
| 202 | 'attribute' => 'date', 'model' => ScheduledPayment::class, |
||
| 203 | ]); |
||
| 204 | } |
||
| 205 | |||
| 206 | CRUD::addColumns([ |
||
| 207 | |||
| 208 | [ |
||
| 209 | 'name' => 'scholarships', |
||
| 210 | 'type' => 'relationship', |
||
| 211 | 'label' => __('Scholarship'), |
||
| 212 | 'attribute' => 'name', |
||
| 213 | 'model' => Scholarship::class, |
||
| 214 | ], |
||
| 215 | |||
| 216 | [ |
||
| 217 | 'label' => __('Email'), |
||
| 218 | 'name' => 'user', |
||
| 219 | 'attribute' => 'email', |
||
| 220 | 'type' => 'relationship', |
||
| 221 | ], |
||
| 222 | |||
| 223 | [ |
||
| 224 | 'label' => __('Phone Number'), |
||
| 225 | 'type' => 'select_multiple', |
||
| 226 | 'name' => 'student.phone', |
||
| 227 | 'attribute' => 'phone_number', |
||
| 228 | 'model' => PhoneNumber::class, |
||
| 229 | ], |
||
| 230 | ]); |
||
| 231 | |||
| 232 | if ($this->mode === 'global') { |
||
| 233 | CRUD::addFilter( |
||
| 234 | [ |
||
| 235 | 'name' => 'status_id', |
||
| 236 | 'type' => 'select2_multiple', |
||
| 237 | 'label'=> __('Status'), |
||
| 238 | ], |
||
| 239 | fn () => EnrollmentStatusType::all()->pluck('name', 'id')->toArray(), |
||
| 240 | 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 | |||
| 247 | CRUD::addFilter([ |
||
| 248 | 'name' => 'period_id', |
||
| 249 | 'type' => 'select2', |
||
| 250 | 'label'=> __('Period'), |
||
| 251 | ], fn () => Period::all()->pluck('name', 'id')->toArray(), function ($value) { |
||
| 252 | CRUD::addClause('period', $value); |
||
| 253 | }); |
||
| 254 | |||
| 255 | CRUD::addFilter( |
||
| 256 | [ |
||
| 257 | 'name' => 'scholarship', |
||
| 258 | 'type' => 'select2', |
||
| 259 | 'label'=> __('Scholarship'), |
||
| 260 | ], |
||
| 261 | fn () => Scholarship::all()->pluck('name', 'id')->toArray(), |
||
| 262 | function ($value) { // if the filter is active |
||
| 263 | if ($value == 'all') { |
||
| 264 | CRUD::addClause('whereHas', 'scholarships'); |
||
| 265 | } else { |
||
| 266 | CRUD::addClause('whereHas', 'scholarships', function ($q) use ($value) { |
||
| 267 | $q->where('scholarships.id', $value); |
||
| 268 | }); |
||
| 355 |