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,75 +111,92 @@ 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');
$asset = new Asset(); $success = false;
$asset->model()->associate(AssetModel::find($request->input('model_id'))); for ($a = 1; $a <= count($asset_tags); $a++) {
$asset->name = $request->input('name'); $asset = new Asset();
$asset->serial = $request->input('serial'); $asset->model()->associate(AssetModel::find($request->input('model_id')));
$asset->company_id = Company::getIdForCurrentUser($request->input('company_id')); $asset->name = $request->input('name');
$asset->model_id = $request->input('model_id'); $asset->serial = $request->input('serial');
$asset->order_number = $request->input('order_number'); $asset->company_id = Company::getIdForCurrentUser($request->input('company_id'));
$asset->notes = $request->input('notes'); $asset->model_id = $request->input('model_id');
$asset->asset_tag = $request->input('asset_tag'); $asset->order_number = $request->input('order_number');
$asset->user_id = Auth::id(); $asset->notes = $request->input('notes');
$asset->archived = '0'; $asset->user_id = Auth::id();
$asset->physical = '1'; $asset->archived = '0';
$asset->depreciate = '0'; $asset->physical = '1';
$asset->status_id = request('status_id', 0); $asset->depreciate = '0';
$asset->warranty_months = request('warranty_months', null); $asset->status_id = request('status_id', 0);
$asset->purchase_cost = Helper::ParseFloat($request->get('purchase_cost')); $asset->warranty_months = request('warranty_months', null);
$asset->purchase_date = request('purchase_date', null); $asset->purchase_cost = Helper::ParseFloat($request->get('purchase_cost'));
$asset->assigned_to = request('assigned_to', null); $asset->purchase_date = request('purchase_date', null);
$asset->supplier_id = request('supplier_id', 0); $asset->assigned_to = request('assigned_to', null);
$asset->requestable = request('requestable', 0); $asset->supplier_id = request('supplier_id', 0);
$asset->rtd_location_id = request('rtd_location_id', null); $asset->requestable = request('requestable', 0);
$asset->rtd_location_id = request('rtd_location_id', null);
if ($asset->assigned_to=='') { if ($asset->assigned_to=='') {
$asset->location_id = $request->input('rtd_location_id', null); $asset->location_id = $request->input('rtd_location_id', null);
} }
$asset = $request->handleImages($asset);
// Update custom fields in the database.
// Validation for these fields is handled through the AssetRequest form request
$model = AssetModel::find($request->get('model_id'));
if (($model) && ($model->fieldset)) { $asset->asset_tag = $asset_tags[$a];
foreach ($model->fieldset->fields as $field) { $asset = $request->handleImages($asset);
if ($field->field_encrypted=='1') {
if (Gate::allows('admin')) {
$asset->{$field->convertUnicodeDbSlug()} = \Crypt::encrypt($request->input($field->convertUnicodeDbSlug())); // Update custom fields in the database.
// Validation for these fields is handled through the AssetRequest form request
$model = AssetModel::find($request->get('model_id'));
if (($model) && ($model->fieldset)) {
foreach ($model->fieldset->fields as $field) {
if ($field->field_encrypted=='1') {
if (Gate::allows('admin')) {
$asset->{$field->convertUnicodeDbSlug()} = \Crypt::encrypt($request->input($field->convertUnicodeDbSlug()));
}
} else {
$asset->{$field->convertUnicodeDbSlug()} = $request->input($field->convertUnicodeDbSlug());
} }
} else {
$asset->{$field->convertUnicodeDbSlug()} = $request->input($field->convertUnicodeDbSlug());
} }
} }
// Was the asset created?
if ($asset->save()) {
if (request('assigned_user')) {
$target = User::find(request('assigned_user'));
$location = $target->location_id;
} elseif (request('assigned_asset')) {
$target = Asset::find(request('assigned_asset'));
$location = $target->location_id;
} elseif (request('assigned_location')) {
$target = Location::find(request('assigned_location'));
$location = $target->id;
}
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);
}
$success = true;
}
} }
// Was the asset created? if ($success) {
if ($asset->save()) {
if (request('assigned_user')) {
$target = User::find(request('assigned_user'));
$location = $target->location_id;
} elseif (request('assigned_asset')) {
$target = Asset::find(request('assigned_asset'));
$location = $target->location_id;
} elseif (request('assigned_location')) {
$target = Location::find(request('assigned_location'));
$location = $target->id;
}
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);
}
// 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 -->