From 9bcfe0748b50a474e7ddb8ca3ee66208e948edf8 Mon Sep 17 00:00:00 2001 From: snipe Date: Sat, 8 Jul 2017 18:44:28 -0700 Subject: [PATCH] Make custom fields into their own JSON array --- app/Http/Transformers/AssetsTransformer.php | 24 +++++++++---------- app/Presenters/AssetPresenter.php | 6 +++-- .../views/partials/bootstrap-table.blade.php | 17 ++++++++++++- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php index 3d7f69c3d6..c1ed67183d 100644 --- a/app/Http/Transformers/AssetsTransformer.php +++ b/app/Http/Transformers/AssetsTransformer.php @@ -88,25 +88,25 @@ class AssetsTransformer $decrypted = \App\Helpers\Helper::gracefulDecrypt($field,$asset->{$field->convertUnicodeDbSlug()}); $value = (Gate::allows('superadmin')) ? $decrypted : strtoupper(trans('admin/custom_fields/general.encrypted')); - $fields_array = [$field->convertUnicodeDbSlug() => $value]; + // $fields_array = [$field->convertUnicodeDbSlug() => $value]; -// $fields_array[$field->name] = [ -// 'field' => $field->convertUnicodeDbSlug(), -// 'value' => $value -// ]; + $fields_array[$field->name] = [ + 'field' => $field->convertUnicodeDbSlug(), + 'value' => $value + ]; } else { -// $fields_array[$field->name] = [ -// 'field' => $field->convertUnicodeDbSlug(), -// 'value' => $asset->{$field->convertUnicodeDbSlug()} -// ]; - $fields_array = [$field->convertUnicodeDbSlug() => $asset->{$field->convertUnicodeDbSlug()}]; + $fields_array[$field->name] = [ + 'field' => $field->convertUnicodeDbSlug(), + 'value' => $asset->{$field->convertUnicodeDbSlug()} + ]; + //$fields_array = [$field->convertUnicodeDbSlug() => $asset->{$field->convertUnicodeDbSlug()}]; } - $array += $fields_array; - //$array['custom_fields'] = $fields_array; + //array += $fields_array; + $array['custom_fields'] = $fields_array; } } diff --git a/app/Presenters/AssetPresenter.php b/app/Presenters/AssetPresenter.php index 4941784c1b..47bdaa1fb8 100644 --- a/app/Presenters/AssetPresenter.php +++ b/app/Presenters/AssetPresenter.php @@ -178,11 +178,13 @@ class AssetPresenter extends Presenter $fields = CustomField::all(); foreach ($fields as $field) { - $layout[] = ["field" => $field->convertUnicodeDbSlug(), + $layout[] = [ + "field" => 'custom_fields.'.$field->convertUnicodeDbSlug(), "searchable" => true, "sortable" => true, "switchable" => true, - "title" => ($field->field_encrypted=='1') ?' '.e($field->name) : e($field->name) + "title" => ($field->field_encrypted=='1') ?' '.e($field->name) : e($field->name), + "formatter" => "customFieldsFormatter" ]; } diff --git a/resources/views/partials/bootstrap-table.blade.php b/resources/views/partials/bootstrap-table.blade.php index fe046bab17..ff0a123758 100644 --- a/resources/views/partials/bootstrap-table.blade.php +++ b/resources/views/partials/bootstrap-table.blade.php @@ -247,8 +247,23 @@ $('.snipe-table').bootstrapTable({ } + // This is gross, but necessary so that we can package the API response + // for custom fields in a more useful way. + function customFieldsFormatter(value, row) { - function createdAtFormatter(value, row) { + var field_column = this.title; + + // Pull out any HTMl that might be passed via the presenter + // (for example, the locked icon for encrypted fields) + var field_column_plain = field_column.replace(/<(?:.|\n)*?> ?/gm, ''); + if (row.custom_fields[field_column_plain]) { + return row.custom_fields[field_column_plain].value; + } + + } + + + function createdAtFormatter(value) { if ((value) && (value.date)) { return value.date; }