API improvements

Components still need some work here
This commit is contained in:
snipe 2017-03-11 10:05:50 -08:00
parent a4697f046e
commit 742e3d044e
6 changed files with 163 additions and 208 deletions

View file

@ -92,6 +92,24 @@ class AccessoriesController extends Controller
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$this->authorize('view', Accessory::class);
$accessory = Accessory::findOrFail($id);
$accessory_users = $accessory->users;
return (new AccessoriesTransformer)->transformAccessory($accessory);
}
/**
* Display the specified resource.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0]
* @param int $id
* @return \Illuminate\Http\Response
*/
public function accessory_detail($id)
{
$this->authorize('view', Accessory::class);
$accessory = Accessory::findOrFail($id);
@ -151,7 +169,12 @@ class AccessoriesController extends Controller
{
$this->authorize('delete', Accessory::class);
$accessory = Accessory::findOrFail($id);
$this->authorize('delete', $accessory);
$this->authorize($accessory);
if ($accessory->hasUsers() > 0) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/accessories/message.assoc_users', array('count'=> $accessory->hasUsers()))));
}
$accessory->delete();
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/accessories/message.delete.success')));

View file

@ -189,24 +189,21 @@ class CategoriesController extends Controller
*/
public function show($id)
{
$category = Category::find($id);
if ($category = Category::find($id)) {
if (isset($category->id)) {
if ($category->category_type=='asset') {
$category_type = 'hardware';
$category_type_route = 'assets';
} elseif ($category->category_type=='accessory') {
$category_type = 'accessories';
$category_type_route = 'accessories';
} else {
$category_type = $category->category_type;
$category_type_route = $category->category_type.'s';
}
return View::make('categories/view', compact('category'))
->with('category_type',$category_type)
->with('category_type_route',$category_type_route);
if ($category->category_type=='asset') {
$category_type = 'hardware';
$category_type_route = 'assets';
} elseif ($category->category_type=='accessory') {
$category_type = 'accessories';
$category_type_route = 'accessories';
} else {
$category_type = $category->category_type;
$category_type_route = $category->category_type.'s';
}
return View::make('categories/view', compact('category'))
->with('category_type',$category_type)
->with('category_type_route',$category_type_route);
}
// Prepare the error message
@ -216,131 +213,4 @@ class CategoriesController extends Controller
}
public function getDataViewAssets(Request $request, $categoryID)
{
$category = Category::find($categoryID);
$category = $category->load('assets.company', 'assets.model', 'assets.assetstatus', 'assets.assignedTo');
$category_assets = $category->assets();
if (Input::has('search')) {
$category_assets = $category_assets->TextSearch(e($request->input('search')));
}
$offset = request('offset', 0);
$limit = request('limit', 50);
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$allowed_columns = ['id','name','serial','asset_tag'];
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
$count = $category_assets->count();
$category_assets = $category_assets->skip($offset)->take($limit)->get();
$rows = array();
$all_custom_fields = CustomField::all();
foreach ($category_assets as $asset) {
$rows[] = $asset->present()->forDataTable($all_custom_fields);
}
$data = array('total' => $count, 'rows' => $rows);
return $data;
}
/**
* @param $categoryID
* @return array
*/
public function getDataViewAccessories(Request $request, $categoryID)
{
$category = Category::with('accessories.company')->find($categoryID);
$category_accessories = $category->accessories();
if (Input::has('search')) {
$category_accessories = $category_accessories->TextSearch(e($request->input('search')));
}
$offset = request('offset', 0);
$limit = request('limit', 50);
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$allowed_columns = ['id','name','serial','asset_tag'];
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
$count = $category_accessories->count();
$category_accessories = $category_accessories->skip($offset)->take($limit)->get();
$rows = array();
foreach ($category_accessories as $accessory) {
$rows[] = $accessory->present()->forDataTable();
}
$data = array('total' => $count, 'rows' => $rows);
return $data;
}
/**
* @param $categoryID
* @param Request $request
* @return array
*/
public function getDataViewConsumables($categoryID, Request $request)
{
$category = Category::with('accessories.company')->find($categoryID);
$category_consumables = $category->consumables();
if (Input::has('search')) {
$category_consumables = $category_consumables->TextSearch(e($request->input('search')));
}
$offset = request('offset', 0);
$limit = request('limit', 50);
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$allowed_columns = ['id','name','serial','asset_tag'];
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
$count = $category_consumables->count();
$category_consumables = $category_consumables->skip($offset)->take($limit)->get();
$rows = array();
foreach ($category_consumables as $consumable) {
$rows[] = $consumable->present()->forDataTable();
}
$data = array('total' => $count, 'rows' => $rows);
return $data;
}
public function getDataViewComponent(Request $request, $categoryID)
{
$category = Category::with('accessories.company')->find($categoryID);
$category_components = $category->components();
if (Input::has('search')) {
$category_components = $category_components->TextSearch(e($request->input('search')));
}
$offset = request('offset', 0);
$limit = request('limit', 50);
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$allowed_columns = ['id','name','serial','asset_tag'];
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
$count = $category_components->count();
$category_components = $category_components->skip($offset)->take($limit)->get();
$rows = array();
foreach ($category_components as $component) {
$rows[] = $component->present()->forDataTable();
}
$data = array('total' => $count, 'rows' => $rows);
return $data;
}
}

View file

@ -43,7 +43,7 @@ class AccessoryPresenter extends Presenter
"field" => "name",
"searchable" => true,
"sortable" => true,
"title" => trans('admin/accessories/table.title'),
"title" => trans('general.name'),
"formatter" => "accessoriesLinkFormatter"
], [
"field" => "category",
@ -90,6 +90,7 @@ class AccessoryPresenter extends Presenter
"sortable" => true,
"visible" => false,
"title" => trans('general.purchase_date'),
"formatter" => "dateDisplayFormatter"
], [
"field" => "purchase_cost",
"searchable" => true,

View file

@ -0,0 +1,103 @@
<?php
namespace App\Presenters;
use App\Helpers\Helper;
/**
* Class ComponentPresenter
* @package App\Presenters
*/
class ComponentPresenter extends Presenter
{
/**
* Json Column Layout for bootstrap table
* @return string
*/
public static function dataTableLayout()
{
$layout = [
[
"field" => "id",
"searchable" => false,
"sortable" => true,
"switchable" => true,
"title" => trans('general.id'),
"visible" => false
],
[
"field" => "company",
"searchable" => true,
"sortable" => true,
"switchable" => true,
"title" => trans('general.company'),
"visible" => false,
"formatter" => 'companiesLinkObjFormatter',
],
[
"field" => "name",
"searchable" => true,
"sortable" => true,
"title" => trans('general.name'),
"visible" => true,
"formatter" => 'componentsLinkFormatter',
], [
"field" => "category",
"searchable" => true,
"sortable" => true,
"title" => trans('general.category'),
"formatter" => "categoriesLinkObjFormatter"
], [
"field" => "qty",
"searchable" => false,
"sortable" => false,
"title" => trans('admin/components/general.total'),
"visible" => true,
], [
"field" => "remaining",
"searchable" => false,
"sortable" => false,
"title" => trans('admin/components/general.remaining'),
"visible" => true,
], [
"field" => "min_amt",
"searchable" => false,
"sortable" => false,
"title" => trans('general.min_amt'),
"visible" => true,
], [
"field" => "order_number",
"searchable" => true,
"sortable" => true,
"title" => trans('general.order_number'),
"visible" => true,
],[
"field" => "purchase_date",
"searchable" => true,
"sortable" => true,
"title" => trans('general.purchase_date'),
"visible" => true,
"formatter" => "dateDisplayFormatter",
],[
"field" => "purchase_cost",
"searchable" => true,
"sortable" => true,
"title" => trans('general.purchase_cost'),
"visible" => true,
], [
"field" => "actions",
"searchable" => false,
"sortable" => false,
"switchable" => false,
"title" => trans('table.actions'),
"visible" => true,
"formatter" => "componentsActionsFormatter",
]
];
return json_encode($layout);
}
}

View file

@ -23,6 +23,7 @@
{{-- Page content --}}
@section('content')
<div class="row">
<div class="col-md-12">
<div class="box box-default">
@ -35,19 +36,7 @@
data-cookie="true"
data-click-to-select="true"
data-cookie-id-table="category{{ $category_type_route }}Table">
@if ($category->category_type!='asset')
<thead>
<tr>
<th data-searchable="false" data-sortable="false" data-field="id" data-visible="false">{{ trans('general.id') }}</th>
<th data-searchable="false" data-sortable="false" data-field="company" data-visible="false" data-formatter="companiesLinkObjFormatter">
{{ trans('admin/companies/table.title') }}
</th>
<th data-searchable="true" data-formatter="{{ $category_type }}LinkFormatter" data-sortable="true" data-field="name">{{ trans('general.name') }}</th>
<th data-searchable="false" data-sortable="false" data-formatter="{{ $category_type }}ActionsFormatter" data-field="actions" data-switchable="false">{{ trans('table.actions') }}</th>
</tr>
</thead>
@endif
</table>
</div>
</div>
</div>
@ -62,12 +51,24 @@
'exportFile' => 'category-' . $category->name . '-export',
'search' => true,
'columns' => \App\Presenters\AssetPresenter::dataTableLayout()])
@else
@elseif ($category->category_type=='accessory')
@include ('partials.bootstrap-table',
[
'exportFile' => 'category-' . $category->name . '-export',
'search' => false])
'search' => true,
'columns' => \App\Presenters\AccessoryPresenter::dataTableLayout()])
@elseif ($category->category_type=='consumable')
@include ('partials.bootstrap-table',
[
'exportFile' => 'category-' . $category->name . '-export',
'search' => true,
'columns' => \App\Presenters\ConsumablePresenter::dataTableLayout()])
@elseif ($category->category_type=='component')
@include ('partials.bootstrap-table',
[
'exportFile' => 'category-' . $category->name . '-export',
'search' => true,
'columns' => \App\Presenters\ComponentPresenter::dataTableLayout()])
@endif

View file

@ -24,12 +24,6 @@
'class' => 'form-inline' ]) }}
<div id="toolbar">
<!-- <select name="bulk_actions" class="form-control select2" style="width: 130px;">
<option value="checkout">Checkout</option>
<option value="checkin">Checkin</option>
</select>
<button class="btn btn-default" id="bulkEdit" disabled>Go</button>
-->
</div>
<table
@ -40,27 +34,7 @@
data-url="{{route('api.components.index') }}"
data-cookie="true"
data-click-to-select="true"
data-cookie-id-table="componentsTable-{{ config('version.hash_version') }}-{{ config('version.hash_version') }}"
>
<thead>
<tr>
{{--<th data-checkbox="true" data-field="checkbox"></th>--}}
<th data-sortable="true" data-field="id" data-visible="false">{{ trans('general.id') }}</th>
<th data-switchable="true" data-visible="false" data-searchable="true" data-sortable="true" data-field="company" data-formatter="companiesLinkObjFormatter">{{ trans('admin/companies/table.title') }}</th>
<th data-sortable="true" data-searchable="true" data-field="name" data-formatter="componentsLinkFormatter">{{ trans('admin/components/table.title') }}</th>
<th data-sortable="true" data-searchable="true" data-field="serial_number" data-visible="false">{{ trans('admin/hardware/form.serial') }}</th>
<th data-searchable="true" data-sortable="true" data-field="location" data-formatter="locationsLinkObjFormatter">{{ trans('general.location') }}</th>
<th data-searchable="true" data-sortable="true" data-field="category" data-formatter="categoriesLinkObjFormatter">{{ trans('general.category') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="true" data-field="qty"> {{ trans('admin/components/general.total') }}</th>
<th data-switchable="true" data-searchable="false" data-sortable="true" data-field="min_amt"> {{ trans('general.min_amt') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="remaining"> {{ trans('admin/components/general.remaining') }}</th>
<th data-sortable="true" data-searchable="true" data-field="order_number" data-visible="false">{{ trans('admin/components/general.order') }}</th>
<th data-sortable="true" data-searchable="true" data-field="purchase_date" data-visible="false">{{ trans('admin/components/general.date') }}</th>
<th data-sortable="true" data-searchable="true" data-field="purchase_cost" data-visible="false">{{ trans('admin/components/general.cost') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="actions" data-formatter="componentsActionsFormatter"> {{ trans('table.actions') }}</th>
</tr>
</thead>
data-cookie-id-table="componentsTable-{{ config('version.hash_version') }}">
</table>
{{ Form::close() }}
</div><!-- /.box-body -->
@ -71,25 +45,8 @@
@stop
@section('moar_scripts')
@include ('partials.bootstrap-table', ['exportFile' => 'components-export', 'search' => true])
@include ('partials.bootstrap-table', ['exportFile' => 'components-export', 'search' => true, 'columns' => \App\Presenters\ComponentPresenter::dataTableLayout()])
<script>
$(function() {
function checkForChecked() {
var check_checked = $('input.one_required:checked').length;
if (check_checked > 0) {
$('#bulkEdit').removeAttr('disabled');
}
else {
$('#bulkEdit').attr('disabled', 'disabled');
}
}
$('#table').on('change','input.one_required',checkForChecked);
$("#checkAll").change(function () {
$("input:checkbox").prop('checked', $(this).prop("checked"));
checkForChecked();
});
});
</script>
@stop