Handle array of asset tags

TODO: figure out how to display validation errors more sanely
This commit is contained in:
snipe 2018-10-05 04:34:47 -07:00
parent d4a97d0431
commit 476b58632b
2 changed files with 72 additions and 55 deletions

View file

@ -111,17 +111,22 @@ class AssetsController extends Controller
{ {
$this->authorize(Asset::class); $this->authorize(Asset::class);
// Handle asset tags - there could be one, or potentially many.
// This is only necessary on create, not update, since bulk editing is handled
// differently
$asset_tags = $request->input('asset_tags');
$success = false;
for ($a = 1; $a <= count($asset_tags); $a++) {
$asset = new Asset(); $asset = new Asset();
$asset->model()->associate(AssetModel::find($request->input('model_id'))); $asset->model()->associate(AssetModel::find($request->input('model_id')));
$asset->name = $request->input('name'); $asset->name = $request->input('name');
$asset->serial = $request->input('serial'); $asset->serial = $request->input('serial');
$asset->company_id = Company::getIdForCurrentUser($request->input('company_id')); $asset->company_id = Company::getIdForCurrentUser($request->input('company_id'));
$asset->model_id = $request->input('model_id'); $asset->model_id = $request->input('model_id');
$asset->order_number = $request->input('order_number'); $asset->order_number = $request->input('order_number');
$asset->notes = $request->input('notes'); $asset->notes = $request->input('notes');
$asset->asset_tag = $request->input('asset_tag');
$asset->user_id = Auth::id(); $asset->user_id = Auth::id();
$asset->archived = '0'; $asset->archived = '0';
$asset->physical = '1'; $asset->physical = '1';
@ -139,6 +144,9 @@ class AssetsController extends Controller
$asset->location_id = $request->input('rtd_location_id', null); $asset->location_id = $request->input('rtd_location_id', null);
} }
$asset->asset_tag = $asset_tags[$a];
$asset = $request->handleImages($asset); $asset = $request->handleImages($asset);
@ -161,7 +169,6 @@ class AssetsController extends Controller
// Was the asset created? // Was the asset created?
if ($asset->save()) { if ($asset->save()) {
if (request('assigned_user')) { if (request('assigned_user')) {
$target = User::find(request('assigned_user')); $target = User::find(request('assigned_user'));
$location = $target->location_id; $location = $target->location_id;
@ -176,10 +183,20 @@ class AssetsController extends Controller
if (isset($target)) { if (isset($target)) {
$asset->checkOut($target, Auth::user(), date('Y-m-d H:i:s'), '', 'Checked out on asset creation', e($request->get('name')), $location); $asset->checkOut($target, Auth::user(), date('Y-m-d H:i:s'), '', 'Checked out on asset creation', e($request->get('name')), $location);
} }
$success = true;
}
}
if ($success) {
// Redirect to the asset listing page // Redirect to the asset listing page
return redirect()->route('hardware.index') return redirect()->route('hardware.index')
->with('success', trans('admin/hardware/message.create.success')); ->with('success', trans('admin/hardware/message.create.success'));
} }
return redirect()->back()->withInput()->withErrors($asset->getErrors()); return redirect()->back()->withInput()->withErrors($asset->getErrors());
} }

View file

@ -23,7 +23,7 @@
<!-- we are editing an existing asset --> <!-- we are editing an existing asset -->
@if ($item->id) @if ($item->id)
<div class="col-md-7 col-sm-12{{ (\App\Helpers\Helper::checkIfRequired($item, 'asset_tag')) ? ' required' : '' }}"> <div class="col-md-7 col-sm-12{{ (\App\Helpers\Helper::checkIfRequired($item, 'asset_tag')) ? ' required' : '' }}">
<input class="form-control" type="text" name="asset_tag" id="asset_tag" value="{{ Input::old('asset_tag', $item->asset_tag) }}" data-validation="required"> <input class="form-control" type="text" name="asset_tags[1]" id="asset_tag" value="{{ Input::old('asset_tag', $item->asset_tag) }}" data-validation="required">
</div> </div>
@else @else
<!-- we are creating a new asset - let people use more than one asset tag --> <!-- we are creating a new asset - let people use more than one asset tag -->