Set session urls

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe
2023-01-02 13:31:52 -08:00
parent 36858d1c64
commit a605dc16b6
2 changed files with 47 additions and 20 deletions
@@ -10,6 +10,7 @@ use App\Http\Requests\AssetCheckoutRequest;
use App\Models\Asset;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
class AssetCheckoutController extends Controller
{
@@ -24,8 +25,9 @@ class AssetCheckoutController extends Controller
* @since [v1.0]
* @return View
*/
public function create($assetId)
public function create(Request $request, $assetId)
{
// Check if the asset exists
if (is_null($asset = Asset::find(e($assetId)))) {
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
@@ -34,10 +36,28 @@ class AssetCheckoutController extends Controller
$this->authorize('checkout', $asset);
if ($asset->availableForCheckout()) {
/**
* Set a session variable here for the possible return url - this may or may not be used
* in the store() method below, but we need to set it here, so we know what "back" means.
* We set this here so that we don't end up with UX weirdness if our referrer isn't what we're
* expecting (for example a checkout that happens from a place we didn't plan for yet.
* - @snipe - 2022-12-22
*/
$request->session()->forget('backto_item_type');
$request->session()->forget('backto_item_id');
\Log::debug('dropping session data for backto_item_type and backto_item_id');
// Hard-coding this for now, since we're only workiing with assets, not other first-class items yet
$request->session()->put('backto_item_type', '\App\Models\Asset');
$request->session()->put('backto_item_id', $assetId);
\Log::debug('setting new session data for asset '. $assetId . ' via backto_item_type and backto_item_id');
return view('hardware/checkout', compact('asset'))
->with('statusLabel_list', Helper::deployableStatusLabelList());
}
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkout.not_available'));
}
@@ -53,13 +73,11 @@ class AssetCheckoutController extends Controller
public function store(AssetCheckoutRequest $request, $assetId)
{
$bulk_back_url = request()->headers->get('referer');
session(['return_to' => $request->input('return-to')]);
$asset = Asset::find($assetId);
$this->authorize('checkout', $asset);
try {
// Check if the asset exists
if (!$asset) {
@@ -67,19 +85,19 @@ class AssetCheckoutController extends Controller
} elseif (!$asset->availableForCheckout()) {
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkout.not_available'));
}
$this->authorize('checkout', $asset);
$admin = Auth::user();
$target = $this->determineCheckoutTarget($asset);
$asset = $this->updateAssetLocation($asset, $target);
$expected_checkin = '';
$checkout_at = date('Y-m-d H:i:s');
if (($request->filled('checkout_at')) && ($request->get('checkout_at') != date('Y-m-d'))) {
$checkout_at = $request->get('checkout_at');
}
$expected_checkin = '';
if ($request->filled('expected_checkin')) {
$expected_checkin = $request->get('expected_checkin');
}
@@ -88,20 +106,32 @@ class AssetCheckoutController extends Controller
$asset->status_id = $request->get('status_id');
}
if(!empty($asset->licenseseats->all())){
if(request('checkout_to_type') == 'user') {
if ($request->filled('next_action')) {
$back_to_route = $request->get('next_action');
}
if(!empty($asset->licenseseats->all())) {
if (request('checkout_to_type') == 'user') {
foreach ($asset->licenseseats as $seat){
$seat->assigned_to = $target->id;
$seat->save();
}
}
}
if ($asset->checkOut($target, $admin, $checkout_at, $expected_checkin, e($request->get('note')), $request->get('name'))) {
return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.checkout.success'));
if ($request->get('accept_in_person')=='1') {
return view('hardware/checkout', compact('asset'))
->with('statusLabel_list', Helper::deployableStatusLabelList());
}
return redirect()->route($back_to_route)->with('success', trans('admin/hardware/message.checkout.success'));
}
// Redirect to the asset management page with error
// Redirect to the asset page with error
return redirect()->to("hardware/$assetId/checkout")->with('error', trans('admin/hardware/message.checkout.error').$asset->getErrors());
} catch (ModelNotFoundException $e) {
return redirect()->back()->with('error', trans('admin/hardware/message.checkout.error'))->withErrors($asset->getErrors());
+5 -8
View File
@@ -161,9 +161,11 @@
<div class="col-md-8 text-right">
<span id="return-to">
<span class="form-inline">Return to:</span>
<select name="next_action" class="select2 select2-hide-search text-left" style="min-width: 150px;">
<option class="text-left" value="listings" role="option">Listings</option>
<option value="item" role="option">Asset</option>
<select name="return_to" class="select2 select2-hide-search text-left" style="min-width: 150px;">
<option class="text-left" value="" role="option">Assets</option>
<option value="" role="option" {{ request()->session()->get('backto_item_type') == '\App\Models\Asset' ? ' selected=selected' : '' }}>Asset</option>
</select>
</span>
@@ -175,11 +177,6 @@
</div>
</div>
</form>
</div>