Merge pull request #12096 from snipe/features/add_custom_field_option_to_view_assets

Added custom field option to view assets
This commit is contained in:
snipe 2022-11-14 22:45:26 +00:00 committed by GitHub
commit 8122236944
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 118 additions and 52 deletions

View file

@ -86,14 +86,24 @@ class CustomFieldsController extends Controller
{ {
$this->authorize('create', CustomField::class); $this->authorize('create', CustomField::class);
$show_in_email = $request->get("show_in_email", 0);
$display_in_user_view = $request->get("display_in_user_view", 0);
// Override the display settings if the field is encrypted
if ($request->get("field_encrypted") == '1') {
$show_in_email = '0';
$display_in_user_view = '0';
}
$field = new CustomField([ $field = new CustomField([
"name" => trim($request->get("name")), "name" => trim($request->get("name")),
"element" => $request->get("element"), "element" => $request->get("element"),
"help_text" => $request->get("help_text"), "help_text" => $request->get("help_text"),
"field_values" => $request->get("field_values"), "field_values" => $request->get("field_values"),
"field_encrypted" => $request->get("field_encrypted", 0), "field_encrypted" => $request->get("field_encrypted", 0),
"show_in_email" => $request->get("show_in_email", 0), "show_in_email" => $show_in_email,
"is_unique" => $request->get("is_unique", 0), "is_unique" => $request->get("is_unique", 0),
"display_in_user_view" => $display_in_user_view,
"user_id" => Auth::id() "user_id" => Auth::id()
]); ]);
@ -221,13 +231,24 @@ class CustomFieldsController extends Controller
$this->authorize('update', $field); $this->authorize('update', $field);
$show_in_email = $request->get("show_in_email", 0);
$display_in_user_view = $request->get("display_in_user_view", 0);
// Override the display settings if the field is encrypted
if ($request->get("field_encrypted") == '1') {
$show_in_email = '0';
$display_in_user_view = '0';
}
$field->name = trim(e($request->get("name"))); $field->name = trim(e($request->get("name")));
$field->element = e($request->get("element")); $field->element = e($request->get("element"));
$field->field_values = e($request->get("field_values")); $field->field_values = e($request->get("field_values"));
$field->user_id = Auth::id(); $field->user_id = Auth::id();
$field->help_text = $request->get("help_text"); $field->help_text = $request->get("help_text");
$field->show_in_email = $request->get("show_in_email", 0); $field->show_in_email = $show_in_email;
$field->is_unique = $request->get("is_unique", 0); $field->is_unique = $request->get("is_unique", 0);
$field->display_in_user_view = $display_in_user_view;
if ($request->get('format') == 'CUSTOM REGEX') { if ($request->get('format') == 'CUSTOM REGEX') {
$field->format = e($request->get('custom_format')); $field->format = e($request->get('custom_format'));

View file

@ -8,6 +8,7 @@ use App\Models\AssetModel;
use App\Models\Company; use App\Models\Company;
use App\Models\Setting; use App\Models\Setting;
use App\Models\User; use App\Models\User;
use App\Models\CustomField;
use App\Notifications\RequestAssetCancelation; use App\Notifications\RequestAssetCancelation;
use App\Notifications\RequestAssetNotification; use App\Notifications\RequestAssetNotification;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -29,23 +30,32 @@ class ViewAssetsController extends Controller
public function getIndex() public function getIndex()
{ {
$user = User::with( $user = User::with(
'assets',
'assets.model', 'assets.model',
'assets.model.fieldset.fields',
'consumables', 'consumables',
'accessories', 'accessories',
'licenses', 'licenses',
'userloc', )->find(Auth::user()->id);
'userlog'
)->withTrashed()->find(Auth::user()->id);
$userlog = $user->userlog->load('item', 'user', 'target'); // Loop through all the custom fields that are applied to any model the user has assigned
foreach ($user->assets as $asset) {
foreach ($asset->model->fieldset->fields as $field) {
// check and make sure they're allowed to see the value of the custom field
if ($field->display_in_user_view == '1') {
$field_array[$field->db_column] = $field->name;
}
}
}
// Since some models may re-use the same fieldsets/fields, let's make the array unique so we don't repeat columns
array_unique($field_array);
if (isset($user->id)) { if (isset($user->id)) {
return view('account/view-assets', compact('user', 'userlog')) return view('account/view-assets', compact('user', 'field_array' ))
->with('settings', Setting::getSettings()); ->with('settings', Setting::getSettings());
} else {
// Redirect to the user management page
return redirect()->route('users.index')->with('error', trans('admin/users/message.user_not_found', compact('id')));
} }
// Redirect to the user management page // Redirect to the user management page
return redirect()->route('users.index') return redirect()->route('users.index')
->with('error', trans('admin/users/message.user_not_found', $user->id)); ->with('error', trans('admin/users/message.user_not_found', $user->id));

View file

@ -47,6 +47,7 @@ class CustomFieldsTransformer
'field_values_array' => ($field->field_values) ? explode("\r\n", e($field->field_values)) : null, 'field_values_array' => ($field->field_values) ? explode("\r\n", e($field->field_values)) : null,
'type' => e($field->element), 'type' => e($field->element),
'required' => (($field->pivot) && ($field->pivot->required=='1')) ? true : false, 'required' => (($field->pivot) && ($field->pivot->required=='1')) ? true : false,
'display_in_user_view' => ($field->display_in_user_view =='1') ? true : false,
'created_at' => Helper::getFormattedDateObject($field->created_at, 'datetime'), 'created_at' => Helper::getFormattedDateObject($field->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($field->updated_at, 'datetime'), 'updated_at' => Helper::getFormattedDateObject($field->updated_at, 'datetime'),
]; ];

View file

@ -64,6 +64,7 @@ class CustomField extends Model
'help_text', 'help_text',
'show_in_email', 'show_in_email',
'is_unique', 'is_unique',
'display_in_user_view',
]; ];
/** /**

View file

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddDisplayToUserInCustomFields extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('custom_fields', function (Blueprint $table) {
$table->boolean('display_in_user_view')->nullable()->default(0);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('custom_fields', function (Blueprint $table) {
$table->dropColumn('display_in_user_view');
});
}
}

View file

@ -44,4 +44,6 @@ return [
'db_convert_warning' => 'WARNING. This field is in the custom fields table as <code>:db_column</code> but should be <code>:expected</code>.', 'db_convert_warning' => 'WARNING. This field is in the custom fields table as <code>:db_column</code> but should be <code>:expected</code>.',
'is_unique' => 'This value must be unique across all assets', 'is_unique' => 'This value must be unique across all assets',
'unique' => 'Unique', 'unique' => 'Unique',
'display_in_user_view' => 'Allow the checked out user to view these values in their View Assigned Assets page',
'display_in_user_view_table' => 'Visible to User',
]; ];

View file

@ -397,13 +397,18 @@
}'> }'>
<thead> <thead>
<tr> <tr>
<th>#</th> <th class="col-md-1">#</th>
<th class="col-md-1">{{ trans('general.image') }}</th> <th class="col-md-1">{{ trans('general.image') }}</th>
<th class="col-md-2" data-switchable="true" data-visible="true">{{ trans('general.category') }}</th> <th class="col-md-2" data-switchable="true" data-visible="true">{{ trans('general.category') }}</th>
<th class="col-md-2" data-switchable="true" data-visible="true">{{ trans('admin/hardware/table.asset_tag') }}</th> <th class="col-md-2" data-switchable="true" data-visible="true">{{ trans('admin/hardware/table.asset_tag') }}</th>
<th class="col-md-2" data-switchable="true" data-visible="true">{{ trans('general.name') }}</th> <th class="col-md-2" data-switchable="true" data-visible="true">{{ trans('general.name') }}</th>
<th class="col-md-2" data-switchable="true" data-visible="true">{{ trans('admin/hardware/table.asset_model') }}</th> <th class="col-md-2" data-switchable="true" data-visible="true">{{ trans('admin/hardware/table.asset_model') }}</th>
<th class="col-md-3" data-switchable="true" data-visible="true">{{ trans('admin/hardware/table.serial') }}</th> <th class="col-md-3" data-switchable="true" data-visible="true">{{ trans('admin/hardware/table.serial') }}</th>
@foreach ($field_array as $db_column => $field_name)
<th class="col-md-1" data-switchable="true" data-visible="true">{{ $field_name }}</th>
@endforeach
</tr> </tr>
</thead> </thead>
@ -434,37 +439,15 @@
@endif @endif
</td> </td>
<td>{{ $asset->serial }}</td> <td>{{ $asset->serial }}</td>
</tr>
@if($settings->show_assigned_assets)
@php
$assignedCounter = 1
@endphp
@foreach ($asset->assignedAssets as $asset)
<tr>
<td>{{ $counter }}.{{ $assignedCounter }}</td>
<td>{{ $asset->model->category->name }}</td>
<td>{{ $asset->asset_tag }}</td>
<td>{{ $asset->name }}</td>
<td>
@if ($asset->physical=='1')
{{ $asset->model->name }}
@endif
</td>
<td>{{ $asset->serial }}</td>
<td>
@if (($asset->image) && ($asset->image!=''))
<img src="{{ Storage::disk('public')->url(app('assets_upload_path').e($asset->image)) }}" height="50" width="50">
@elseif (($asset->model) && ($asset->model->image!='')) @foreach ($field_array as $db_column => $field_value)
<img src="{{ Storage::disk('public')->url(app('models_upload_path').e($asset->model->image)) }}" height="50" width="50"> <td>
@endif {{ $asset->{$db_column} }}
</td> </td>
</tr>
@php
$assignedCounter++
@endphp
@endforeach @endforeach
@endif
</tr>
@php @php
$counter++ $counter++
@endphp @endphp
@ -551,7 +534,7 @@
<tbody> <tbody>
@foreach ($user->accessories as $accessory) @foreach ($user->accessories as $accessory)
<tr> <tr>
<td>{!! $accessory->name !!}</td> <td>{{ $accessory->name }}</td>
<td> <td>
{!! Helper::formatCurrencyOutput($accessory->purchase_cost) !!} {!! Helper::formatCurrencyOutput($accessory->purchase_cost) !!}
</td> </td>
@ -599,7 +582,7 @@
<tbody> <tbody>
@foreach ($user->consumables as $consumable) @foreach ($user->consumables as $consumable)
<tr> <tr>
<td>{!! $consumable->name !!}</td> <td>{{ $consumable->name }}</td>
<td> <td>
{!! Helper::formatCurrencyOutput($consumable->purchase_cost) !!} {!! Helper::formatCurrencyOutput($consumable->purchase_cost) !!}
</td> </td>

View file

@ -116,6 +116,17 @@
</div> </div>
<!-- Show in View All Assets profile view -->
<div class="form-group {{ $errors->has('display_in_user_view') ? ' has-error' : '' }}" id="display_in_user_view">
<div class="col-md-8 col-md-offset-4">
<label for="display_in_user_view">
<input type="checkbox" name="display_in_user_view" aria-label="display_in_user_view" value="1" class="minimal"{{ (old('display_in_user_view') || $field->display_in_user_view) ? ' checked="checked"' : '' }}>
{{ trans('admin/custom_fields/general.display_in_user_view') }}
</label>
</div>
</div>
<!-- Value Must be Unique --> <!-- Value Must be Unique -->
<div class="form-group {{ $errors->has('is_unique') ? ' has-error' : '' }}" id="is_unique"> <div class="form-group {{ $errors->has('is_unique') ? ' has-error' : '' }}" id="is_unique">
<div class="col-md-8 col-md-offset-4"> <div class="col-md-8 col-md-offset-4">
@ -206,11 +217,13 @@
$('#field_encrypted').on('ifChecked', function(event){ $('#field_encrypted').on('ifChecked', function(event){
$("#encrypt_warning").show(); $("#encrypt_warning").show();
$("#show_in_email").hide(); $("#show_in_email").hide();
$("#display_in_user_view").hide();
}); });
$('#field_encrypted').on('ifUnchecked', function(event){ $('#field_encrypted').on('ifUnchecked', function(event){
$("#encrypt_warning").hide(); $("#encrypt_warning").hide();
$("#show_in_email").show(); $("#show_in_email").show();
$("#display_in_user_view").show();
}); });
</script> </script>

View file

@ -126,16 +126,17 @@
}'> }'>
<thead> <thead>
<tr> <tr>
<th data-searchable="true">{{ trans('general.name') }}</th> <th data-sortable="true" data-searchable="true">{{ trans('general.name') }}</th>
<th data-searchable="true">{{ trans('admin/custom_fields/general.help_text')}}</th> <th data-sortable="true" data-searchable="true">{{ trans('admin/custom_fields/general.help_text')}}</th>
<th data-searchable="true">{{ trans('general.email') }}</th> <th data-sortable="true" data-searchable="true">{{ trans('admin/custom_fields/general.unique') }}</th>
<th data-searchable="true">{{ trans('admin/custom_fields/general.unique') }}</th> <th data-sortable="true" data-visible="false">{{ trans('admin/custom_fields/general.db_field') }}</th>
<th data-visible="false">{{ trans('admin/custom_fields/general.db_field') }}</th> <th data-sortable="true" data-searchable="true">{{ trans('admin/custom_fields/general.field_format') }}</th>
<th data-searchable="true">{{ trans('admin/custom_fields/general.field_format') }}</th> <th data-sortable="true"><i class="fa fa-lock" aria-hidden="true"></i>
<th><i class="fa fa-lock" aria-hidden="true"></i>
<span class="hidden-xs hidden-sm hidden-md hidden-lg">{{ trans('admin/custom_fields/general.encrypted') }}</span> <span class="hidden-xs hidden-sm hidden-md hidden-lg">{{ trans('admin/custom_fields/general.encrypted') }}</span>
</th> </th>
<th data-searchable="true">{{ trans('admin/custom_fields/general.field_element_short') }}</th> <th data-visible="false" data-sortable="true"><i class="fa fa-eye" aria-hidden="true"><span class="sr-only">Visible to User</span></i></th>
<th data-sortable="true" data-searchable="true"><i class="fa fa-envelope" aria-hidden="true"><span class="sr-only">Show in Email</span></i></th>
<th data-sortable="true" data-searchable="true">{{ trans('admin/custom_fields/general.field_element_short') }}</th>
<th data-searchable="true">{{ trans('admin/custom_fields/general.fieldsets') }}</th> <th data-searchable="true">{{ trans('admin/custom_fields/general.fieldsets') }}</th>
<th>{{ trans('button.actions') }}</th> <th>{{ trans('button.actions') }}</th>
</tr> </tr>
@ -145,7 +146,7 @@
<tr> <tr>
<td>{{ $field->name }}</td> <td>{{ $field->name }}</td>
<td>{{ $field->help_text }}</td> <td>{{ $field->help_text }}</td>
<td class='text-center'>{!! ($field->show_in_email=='1') ? '<i class="fas fa-check text-success" aria-hidden="true"><span class="sr-only">'.trans('general.yes').'</span></i>' : '<i class="fas fa-times text-danger" aria-hidden="true"><span class="sr-only">'.trans('general.no').'</span></i>' !!}</td>
<td class='text-center'>{!! ($field->is_unique=='1') ? '<i class="fas fa-check text-success" aria-hidden="true"><span class="sr-only">'.trans('general.yes').'</span></i>' : '<i class="fas fa-times text-danger" aria-hidden="true"><span class="sr-only">'.trans('general.no').'</span></i>' !!}</td> <td class='text-center'>{!! ($field->is_unique=='1') ? '<i class="fas fa-check text-success" aria-hidden="true"><span class="sr-only">'.trans('general.yes').'</span></i>' : '<i class="fas fa-times text-danger" aria-hidden="true"><span class="sr-only">'.trans('general.no').'</span></i>' !!}</td>
<td> <td>
<code>{{ $field->convertUnicodeDbSlug() }}</code> <code>{{ $field->convertUnicodeDbSlug() }}</code>
@ -156,6 +157,8 @@
</td> </td>
<td>{{ $field->format }}</td> <td>{{ $field->format }}</td>
<td>{!! ($field->field_encrypted=='1' ? '<i class="fa fa-check text-success"></i>' : '<i class="fa fa-times text-danger"></i>') !!}</td> <td>{!! ($field->field_encrypted=='1' ? '<i class="fa fa-check text-success"></i>' : '<i class="fa fa-times text-danger"></i>') !!}</td>
<td>{!! ($field->display_in_user_view=='1' ? '<i class="fa fa-check text-success"></i>' : '<i class="fa fa-times text-danger"></i>') !!}</td>
<td class='text-center'>{!! ($field->show_in_email=='1') ? '<i class="fas fa-check text-success" aria-hidden="true"><span class="sr-only">'.trans('general.yes').'</span></i>' : '<i class="fas fa-times text-danger" aria-hidden="true"><span class="sr-only">'.trans('general.no').'</span></i>' !!}</td>
<td>{{ $field->element }}</td> <td>{{ $field->element }}</td>
<td> <td>
@foreach($field->fieldset as $fieldset) @foreach($field->fieldset as $fieldset)