| Total Complexity | 27 |
| Total Lines | 333 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 26 | class ImportCrudController extends CrudController |
||
| 27 | { |
||
| 28 | use UsesPolicies, UsesEnums; |
||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | public $steps = [ |
||
| 33 | 'spreadsheet' => SetSpreadsheetStep::class, |
||
| 34 | 'worksheets' => SelectWorksheetsStep::class, |
||
| 35 | 'approve' => ApproveImportStep::class, |
||
| 36 | 'report' => DisplayResultsStep::class, |
||
| 37 | ]; |
||
| 38 | /** |
||
| 39 | * @var Wizard |
||
| 40 | */ |
||
| 41 | protected $wizard; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * ImportCrudController constructor. |
||
| 45 | * |
||
| 46 | * @throws \Smajti1\Laravel\Exceptions\StepNotFoundException |
||
| 47 | */ |
||
| 48 | public function __construct() |
||
| 49 | { |
||
| 50 | parent::__construct(); |
||
| 51 | $this->wizard = new Wizard($this->steps, $sessionKeyName = 'project-import'); |
||
| 52 | |||
| 53 | view()->share(['wizard' => $this->wizard]); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @throws \Exception |
||
| 58 | */ |
||
| 59 | public function setup() |
||
| 100 | |||
| 101 | // ------ CRUD REORDER |
||
| 102 | // $this->crud->enableReorder('label_name', MAX_TREE_LEVEL); |
||
|
1 ignored issue
–
show
|
|||
| 103 | // NOTE: you also need to do allow access to the right users: $this->crud->allowAccess('reorder'); |
||
| 104 | |||
| 105 | // ------ CRUD DETAILS ROW |
||
| 106 | // $this->crud->enableDetailsRow(); |
||
|
1 ignored issue
–
show
|
|||
| 107 | // NOTE: you also need to do allow access to the right users: $this->crud->allowAccess('details_row'); |
||
| 108 | // NOTE: you also need to do overwrite the showDetailsRow($id) method in your EntityCrudController to show whatever you'd like in the details row OR overwrite the views/backpack/crud/details_row.blade.php |
||
| 109 | |||
| 110 | // ------ REVISIONS |
||
| 111 | // You also need to use \Venturecraft\Revisionable\RevisionableTrait; |
||
| 112 | // Please check out: https://laravel-backpack.readme.io/docs/crud#revisions |
||
| 113 | // $this->crud->allowAccess('revisions'); |
||
|
1 ignored issue
–
show
|
|||
| 114 | |||
| 115 | // ------ AJAX TABLE VIEW |
||
| 116 | // Please note the drawbacks of this though: |
||
| 117 | // - 1-n and n-n columns are not searchable |
||
| 118 | // - date and datetime columns won't be sortable anymore |
||
| 119 | // $this->crud->enableAjaxTable(); |
||
|
1 ignored issue
–
show
|
|||
| 120 | |||
| 121 | // ------ DATATABLE EXPORT BUTTONS |
||
| 122 | // Show export to PDF, CSV, XLS and Print buttons on the table view. |
||
| 123 | // Does not work well with AJAX datatables. |
||
| 124 | // $this->crud->enableExportButtons(); |
||
|
1 ignored issue
–
show
|
|||
| 125 | |||
| 126 | // ------ ADVANCED QUERIES |
||
| 127 | // $this->crud->addClause('active'); |
||
|
1 ignored issue
–
show
|
|||
| 128 | // $this->crud->addClause('type', 'car'); |
||
|
1 ignored issue
–
show
|
|||
| 129 | // $this->crud->addClause('where', 'name', '==', 'car'); |
||
|
1 ignored issue
–
show
|
|||
| 130 | // $this->crud->addClause('whereName', 'car'); |
||
|
1 ignored issue
–
show
|
|||
| 131 | // $this->crud->addClause('whereHas', 'posts', function($query) { |
||
|
1 ignored issue
–
show
|
|||
| 132 | // $query->activePosts(); |
||
|
1 ignored issue
–
show
|
|||
| 133 | // }); |
||
| 134 | // $this->crud->addClause('withoutGlobalScopes'); |
||
|
1 ignored issue
–
show
|
|||
| 135 | // $this->crud->addClause('withoutGlobalScope', VisibleScope::class); |
||
|
1 ignored issue
–
show
|
|||
| 136 | // $this->crud->with(); // eager load relationships |
||
|
1 ignored issue
–
show
|
|||
| 137 | // $this->crud->orderBy(); |
||
|
1 ignored issue
–
show
|
|||
| 138 | // $this->crud->groupBy(); |
||
|
1 ignored issue
–
show
|
|||
| 139 | // $this->crud->limit(); |
||
|
1 ignored issue
–
show
|
|||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param UpdateRequest $request |
||
| 144 | * @param Project $project |
||
| 145 | * |
||
| 146 | * @return RedirectResponse |
||
| 147 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 148 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 149 | */ |
||
| 150 | public function createBatch(ImportRequest $request, Project $project) |
||
| 151 | { |
||
| 152 | $this->policyAuthorize('import', $project, $project->id); |
||
| 153 | /* @var Batch $batch */ |
||
| 154 | if ($this->wizard->dataHas('batch_id')) { |
||
| 155 | $batch = Batch::findOrFail($this->wizard->dataGet('batch_id')); |
||
| 156 | } else { |
||
| 157 | $batch = Batch::make(['project_id' => $project->id]); |
||
| 158 | $this->setWizardData('batch_id', $batch->id); |
||
| 159 | } |
||
| 160 | |||
| 161 | return $this->processImportProject($request, $project, $batch, $this->wizard->first()->key); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param Request $request |
||
| 166 | * @param Project $project |
||
| 167 | * @param Batch $batch |
||
| 168 | * @param null $step |
||
| 169 | * |
||
| 170 | * @return \Backpack\CRUD\app\Http\Controllers\Response |
||
| 171 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 172 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 173 | */ |
||
| 174 | public function importProject(Request $request, Project $project, Batch $batch, $step = null) |
||
| 175 | { |
||
| 176 | $this->policyAuthorize('import', $project, $project->id); |
||
| 177 | //if we get to here we're authorized to import a project, so we authorize create |
||
| 178 | $this->crud->allowAccess(['create', 'edit']); |
||
| 179 | try { |
||
| 180 | if (null === $step) { |
||
| 181 | if (null === $batch->id) { |
||
| 182 | $step = $this->wizard->first(); |
||
| 183 | $this->wizard->data([]); |
||
| 184 | } else { |
||
| 185 | $step = $this->wizard->firstOrLastProcessed(); |
||
| 186 | } |
||
| 187 | } else { |
||
| 188 | $step = $this->wizard->getBySlug($step); |
||
| 189 | } |
||
| 190 | } catch (StepNotFoundException $e) { |
||
| 191 | abort(404); |
||
| 192 | } |
||
| 193 | //we've jumped into a middle step of the sequence (we should always have worksheets after step 1) |
||
| 194 | if ($step->number > 1 && ! $batch->dataHas('googlesheets')) { |
||
| 195 | $step = $this->wizard->first(); |
||
| 196 | } |
||
| 197 | if ($this->wizard->data() != $batch->step_data) { |
||
| 198 | //we've returned from an incomplete import |
||
| 199 | //and have to load the previous step data into the wizard from the database |
||
| 200 | $this->wizard->data($batch->step_data); |
||
| 201 | } |
||
| 202 | |||
| 203 | $this->setWizardData('batch_id', $batch->id); |
||
| 204 | $this->setWizardData('project_id', $project->id); |
||
| 205 | |||
| 206 | if (method_exists($step, 'preProcess')) { |
||
| 207 | $step->preProcess($request, $this->wizard); |
||
| 208 | } |
||
| 209 | |||
| 210 | $this->crud->setCreateView('frontend.import.project.wizard'); |
||
| 211 | $this->crud->setRoute(config('backpack.base.route_prefix') . 'projects/' . $project->id); |
||
| 212 | $this->crud->addFields($step->fields(), 'create'); |
||
| 213 | $this->data['step'] = $step; |
||
| 214 | $this->data['project'] = $project; |
||
| 215 | $this->data['batch'] = $batch; |
||
| 216 | $this->data['wizard_data'] = $this->wizard->data(); |
||
| 217 | if (isset($this->wizard->data()[$step->key])) { |
||
| 218 | $data = $this->wizard->data()[$step->key]; |
||
| 219 | if ($step->key === 'spreadsheet') { |
||
| 220 | foreach ($data as $key => $datum) { |
||
| 221 | $this->crud->create_fields[$key]['value'] = $datum; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | if ($step->key === 'worksheets') { |
||
| 225 | $this->crud->create_fields['worksheets']['value'] = $data; |
||
| 226 | } |
||
| 227 | if ($step->key === 'approve') { |
||
| 228 | $this->crud->create_fields['approve']['value'] = $data; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | return parent::create(); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param UpdateRequest $request |
||
| 237 | * @param Project $project |
||
| 238 | * @param Batch $batch |
||
| 239 | * @param null $step |
||
| 240 | * |
||
| 241 | * @return RedirectResponse |
||
| 242 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 243 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 244 | */ |
||
| 245 | public function processImportProject(ImportRequest $request, Project $project, Batch $batch, $step = null): RedirectResponse |
||
| 246 | { |
||
| 247 | $this->policyAuthorize('import', $project, $project->id); |
||
| 248 | //if we get to here we're authorized to import a project, so we authorize create |
||
| 249 | $this->crud->allowAccess(['create']); |
||
| 250 | try { |
||
| 251 | $step = $this->wizard->getBySlug($step); |
||
| 252 | } catch (StepNotFoundException $e) { |
||
| 253 | abort(404); |
||
| 254 | } |
||
| 255 | |||
| 256 | //here we validate the input from the step |
||
| 257 | |||
| 258 | if (method_exists($step, 'validate')) { |
||
| 259 | $step->validate($request); |
||
| 260 | } else { |
||
| 261 | $this->validate($request, $step->rules($request)); |
||
| 262 | } |
||
| 263 | |||
| 264 | //$request->flash(); |
||
|
1 ignored issue
–
show
|
|||
| 265 | |||
| 266 | //handle the next/last step |
||
| 267 | $step->process($request); |
||
| 268 | |||
| 269 | $batch->step_data = $this->wizard->data(); |
||
| 270 | $batch->next_step = $this->wizard->nextSlug(); |
||
| 271 | $batch->run_description = $batch->run_description ?? $this->wizard->dataGet('title'); |
||
| 272 | $batch->save(); |
||
| 273 | |||
| 274 | //and redirect to the next step if valid |
||
| 275 | //$request->session()->save(); |
||
|
1 ignored issue
–
show
|
|||
| 276 | return redirect()->route('frontend.project.import', |
||
| 277 | ['project' => $project->id, 'batch' => $batch, 'step' => $this->wizard->nextSlug()]); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param UpdateRequest $request |
||
| 282 | * |
||
| 283 | * @return RedirectResponse |
||
| 284 | */ |
||
| 285 | public function store(StoreRequest $request) |
||
|
1 ignored issue
–
show
|
|||
| 286 | { |
||
| 287 | // your additional operations before save here |
||
| 288 | $redirect_location = parent::storeCrud(); |
||
| 289 | // your additional operations after save here |
||
| 290 | // use $this->data['entry'] or $this->crud->entry |
||
|
1 ignored issue
–
show
|
|||
| 291 | return $redirect_location; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param UpdateRequest $request |
||
| 296 | * |
||
| 297 | * @return RedirectResponse |
||
| 298 | */ |
||
| 299 | public function update(UpdateRequest $request) |
||
|
1 ignored issue
–
show
|
|||
| 300 | { |
||
| 301 | // your additional operations before save here |
||
| 302 | $redirect_location = parent::updateCrud(); |
||
| 303 | // your additional operations after save here |
||
| 304 | // use $this->data['entry'] or $this->crud->entry |
||
|
1 ignored issue
–
show
|
|||
| 305 | return $redirect_location; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param $step |
||
| 310 | * |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | private static function getWizardStep($step): array |
||
| 314 | { |
||
| 315 | $steps = [ |
||
| 316 | [ |
||
| 317 | 'name' => 'spreadsheet', |
||
| 318 | 'step' => 1, |
||
| 319 | 'title' => 'Start with a Spreadsheet URL...', |
||
| 320 | 'instructions' => 'Some instructions', |
||
| 321 | ], |
||
| 322 | [ |
||
| 323 | 'name' => 'worksheets', |
||
| 324 | 'step' => 2, |
||
| 325 | 'title' => 'Choose the Worksheets...', |
||
| 326 | 'instructions' => 'Some instructions', |
||
| 327 | ], |
||
| 328 | [ |
||
| 329 | 'name' => 'approve', |
||
| 330 | 'step' => 3, |
||
| 331 | 'title' => 'Approve the changes..', |
||
| 332 | 'instructions' => 'Some instructions', |
||
| 333 | ], |
||
| 334 | [ |
||
| 335 | 'name' => 'import', |
||
| 336 | 'step' => 4, |
||
| 337 | 'title' => 'Start with a Spreadsheet URL', |
||
| 338 | 'instructions' => 'Some instructions', |
||
| 339 | ], |
||
| 340 | ]; |
||
| 341 | $key = $step !== null ? array_search($step, array_column($steps, 'name')) : 0; |
||
| 342 | if ($key === false) { |
||
| 343 | abort(404, 'Invalid Step'); |
||
| 344 | } |
||
| 345 | $steps[$key]['next'] = ($key < count($steps)) ? $steps[$key + 1]['name'] : false; |
||
| 346 | |||
| 347 | return $steps[$key]; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $key |
||
| 352 | * @param mixed $value |
||
| 353 | */ |
||
| 354 | private function setWizardData($key, $value): void |
||
| 359 | } |
||
| 360 | } |
||
| 361 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.