Skip to content

Commit c714114

Browse files
committed
First pass at better-handling those annoying Rollbars we keep getting
1 parent ab0c009 commit c714114

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

app/Http/Controllers/Assets/AssetsController.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ public function store(ImageUploadRequest $request) : RedirectResponse
112112

113113
$settings = Setting::getSettings();
114114

115-
$success = false;
115+
$successes = 0;
116+
$failures = 0;
116117
$serials = $request->input('serials');
118+
$last_succesful_asset = null;
117119

118120
for ($a = 1; $a <= count($asset_tags); $a++) {
119121
$asset = new Asset();
@@ -200,20 +202,36 @@ public function store(ImageUploadRequest $request) : RedirectResponse
200202
$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);
201203
}
202204

203-
$success = true;
204-
205+
$last_succesful_asset = $asset;
206+
$successes++;
207+
208+
} else {
209+
$failures++;
205210
}
206211
}
207212

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

210215

211-
if ($success) {
216+
if ($successes > 0) {
217+
if ($failures > 0) {
218+
//some succeeded, some failed
219+
return redirect()->to(Helper::getRedirectOption($request, $last_succesful_asset->id, 'Assets'))
220+
->with('success-unescaped', trans_choice('admin/hardware/message.create.multi_success_linked', $successes, ['link' => route('hardware.show', ['hardware' => $last_succesful_asset->id]), 'id', 'tag' => e($last_succesful_asset->asset_tag)]))
221+
->with('warning', trans_choice('admin/hardware/message.create.partial_failure', $failures));
222+
} else {
223+
if ($successes == 1) {
224+
//the most common case, keeping it so we don't have to make every use of that translation string be trans_choice'ed
225+
//and re-translated
226+
return redirect()->to(Helper::getRedirectOption($request, $last_succesful_asset->id, 'Assets'))
227+
->with('success-unescaped', trans('admin/hardware/message.create.success_linked', ['link' => route('hardware.show', ['hardware' => $last_succesful_asset->id]), 'id', 'tag' => e($last_succesful_asset->asset_tag)]));
228+
} else {
229+
//multi-success
230+
return redirect()->to(Helper::getRedirectOption($request, $last_succesful_asset->id, 'Assets'))
231+
->with('success-unescaped', trans_choice('admin/hardware/message.create.multi_success_linked', $successes, ['link' => route('hardware.show', ['hardware' => $last_succesful_asset->id]), 'id', 'tag' => e($last_succesful_asset->asset_tag)]));
232+
}
233+
}
212234

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

219237
return redirect()->back()->withInput()->withErrors($asset->getErrors());

resources/lang/en-US/admin/hardware/message.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
'error' => 'Asset was not created, please try again. :(',
1515
'success' => 'Asset created successfully. :)',
1616
'success_linked' => 'Asset with tag :tag was created successfully. <strong><a href=":link" style="color: white;">Click here to view</a></strong>.',
17+
'multi_success_linked' => 'Asset with tag :tag was created successfully. <strong><a href=":link" style="color: white;">Click here to view</a></strong>.|:count assets were created succesfully. The last one was :tag. <strong><a href=":link" style="color: white;">Click here to view</a></strong>.',
18+
'partial_success_linked' => 'Asset with tag :tag was created successfully. <strong><a href=":link" style="color: white;">Click here to view</a></strong>.',
19+
'partial_failure' => 'An asset was unable to be created.|:count assets were unable to be created.'
1720
],
1821

1922
'update' => [

0 commit comments

Comments
 (0)