Skip to content

Commit

Permalink
Merge pull request #15550 from uberbrady/fix_multi_create_partial_fai…
Browse files Browse the repository at this point in the history
…lure

Fix multi create partial failure (fixes: [RB-18591])
  • Loading branch information
snipe authored Oct 24, 2024
2 parents 07a51ec + b634053 commit dfc6364
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
33 changes: 25 additions & 8 deletions app/Http/Controllers/Assets/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ public function store(ImageUploadRequest $request) : RedirectResponse

$settings = Setting::getSettings();

$success = false;
$successes = [];
$failures = [];
$serials = $request->input('serials');
$asset = null;

for ($a = 1; $a <= count($asset_tags); $a++) {
$asset = new Asset();
Expand Down Expand Up @@ -199,20 +201,35 @@ public function store(ImageUploadRequest $request) : RedirectResponse
$asset->checkOut($target, auth()->user(), date('Y-m-d H:i:s'), $request->input('expected_checkin', null), 'Checked out on asset creation', $request->get('name'), $location);
}

$success = true;

$successes[] = "<a href='" . route('hardware.show', ['hardware' => $asset->id]) . "' style='color: white;'>" . e($asset->asset_tag) . "</a>";

} else {
$failures[] = join(",", $asset->getErrors()->all());
}
}

session()->put(['redirect_option' => $request->get('redirect_option'), 'checkout_to_type' => $request->get('checkout_to_type')]);


if ($success) {
if ($successes) {
if ($failures) {
//some succeeded, some failed
return redirect()->to(Helper::getRedirectOption($request, $asset->id, 'Assets')) //FIXME - not tested
->with('success-unescaped', trans_choice('admin/hardware/message.create.multi_success_linked', $successes, ['links' => join(", ", $successes)]))
->with('warning', trans_choice('admin/hardware/message.create.partial_failure', $failures, ['failures' => join("; ", $failures)]));
} else {
if (count($successes) == 1) {
//the most common case, keeping it so we don't have to make every use of that translation string be trans_choice'ed
//and re-translated
return redirect()->to(Helper::getRedirectOption($request, $asset->id, 'Assets'))
->with('success-unescaped', trans('admin/hardware/message.create.success_linked', ['link' => route('hardware.show', ['hardware' => $asset->id]), 'id', 'tag' => e($asset->asset_tag)]));
} else {
//multi-success
return redirect()->to(Helper::getRedirectOption($request, $asset->id, 'Assets'))
->with('success-unescaped', trans_choice('admin/hardware/message.create.multi_success_linked', $successes, ['links' => join(", ", $successes)]));
}
}

return redirect()->to(Helper::getRedirectOption($request, $asset->id, 'Assets'))
->with('success-unescaped', trans('admin/hardware/message.create.success_linked', ['link' => route('hardware.show', ['hardware' => $asset->id]), 'id', 'tag' => e($asset->asset_tag)]));


}

return redirect()->back()->withInput()->withErrors($asset->getErrors());
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/en-US/admin/hardware/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
'error' => 'Asset was not created, please try again. :(',
'success' => 'Asset created successfully. :)',
'success_linked' => 'Asset with tag :tag was created successfully. <strong><a href=":link" style="color: white;">Click here to view</a></strong>.',
'multi_success_linked' => 'Asset with tag :links was created successfully.|:count assets were created succesfully. :links.',
'partial_failure' => 'An asset was unable to be created. Reason: :failures|:count assets were unable to be created. Reasons: :failures',
],

'update' => [
Expand Down

0 comments on commit dfc6364

Please sign in to comment.