2017-01-13 09:01:10 -08:00
< ? php
namespace App\Http\Controllers\Api ;
use App\Helpers\Helper ;
2019-03-13 20:12:03 -07:00
use App\Http\Controllers\Controller ;
2017-02-16 00:32:33 -08:00
use App\Http\Transformers\CategoriesTransformer ;
2017-10-26 22:09:08 -07:00
use App\Http\Transformers\SelectlistTransformer ;
2019-03-13 20:12:03 -07:00
use App\Models\Category ;
use Illuminate\Http\Request ;
2024-07-04 18:36:55 -07:00
use Illuminate\Http\JsonResponse ;
2021-06-29 02:26:45 -07:00
use App\Http\Requests\ImageUploadRequest ;
2018-09-29 21:33:52 -07:00
use Illuminate\Support\Facades\Storage ;
2017-01-13 09:01:10 -08:00
class CategoriesController extends Controller
{
/**
* Display a listing of the resource .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v4 . 0 ]
* @ return \Illuminate\Http\Response
*/
2024-07-04 18:36:55 -07:00
public function index ( Request $request ) : array
2017-01-13 09:01:10 -08:00
{
$this -> authorize ( 'view' , Category :: class );
2023-03-08 13:01:38 -08:00
$allowed_columns = [
'id' ,
'name' ,
'category_type' ,
'category_type' ,
'use_default_eula' ,
'eula_text' ,
'require_acceptance' ,
'checkin_email' ,
'assets_count' ,
'accessories_count' ,
'consumables_count' ,
'components_count' ,
'licenses_count' ,
'image' ,
];
$categories = Category :: select ([
'id' ,
2024-09-19 09:56:05 -07:00
'created_by' ,
2023-03-08 13:01:38 -08:00
'created_at' ,
'updated_at' ,
'name' , 'category_type' ,
'use_default_eula' ,
'eula_text' ,
'require_acceptance' ,
'checkin_email' ,
2024-09-19 09:56:05 -07:00
'image' ,
2024-09-19 11:56:39 -07:00
])
-> with ( 'adminuser' )
-> withCount ( 'accessories as accessories_count' , 'consumables as consumables_count' , 'components as components_count' , 'licenses as licenses_count' );
2023-03-08 13:01:38 -08:00
/*
* This checks to see if we should override the Admin Setting to show archived assets in list .
* We don ' t currently use it within the Snipe - IT GUI , but will be useful for API integrations where they
* may actually need to fetch assets that are archived .
*
* @ see \App\Models\Category :: showableAssets ()
*/
if ( $request -> input ( 'archived' ) == 'true' ) {
$categories = $categories -> withCount ( 'assets as assets_count' );
} else {
$categories = $categories -> withCount ( 'showableAssets as assets_count' );
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'search' )) {
2017-01-13 09:01:10 -08:00
$categories = $categories -> TextSearch ( $request -> input ( 'search' ));
}
2022-06-28 19:59:45 -07:00
if ( $request -> filled ( 'name' )) {
$categories -> where ( 'name' , '=' , $request -> input ( 'name' ));
}
if ( $request -> filled ( 'category_type' )) {
$categories -> where ( 'category_type' , '=' , $request -> input ( 'category_type' ));
}
if ( $request -> filled ( 'use_default_eula' )) {
$categories -> where ( 'use_default_eula' , '=' , $request -> input ( 'use_default_eula' ));
}
if ( $request -> filled ( 'require_acceptance' )) {
$categories -> where ( 'require_acceptance' , '=' , $request -> input ( 'require_acceptance' ));
}
if ( $request -> filled ( 'checkin_email' )) {
$categories -> where ( 'checkin_email' , '=' , $request -> input ( 'checkin_email' ));
}
2024-09-19 09:56:05 -07:00
if ( $request -> filled ( 'created_by' )) {
$categories -> where ( 'created_by' , '=' , $request -> input ( 'created_by' ));
}
if ( $request -> filled ( 'created_at' )) {
$categories -> where ( 'created_at' , '=' , $request -> input ( 'created_at' ));
}
if ( $request -> filled ( 'updated_at' )) {
$categories -> where ( 'updated_at' , '=' , $request -> input ( 'updated_at' ));
}
2023-04-12 11:28:46 -07:00
// Make sure the offset and limit are actually integers and do not exceed system limits
2023-10-14 12:39:52 -07:00
$offset = ( $request -> input ( 'offset' ) > $categories -> count ()) ? $categories -> count () : app ( 'api_offset_value' );
2023-04-16 08:46:39 -07:00
$limit = app ( 'api_limit_value' );
2017-01-13 09:01:10 -08:00
$order = $request -> input ( 'order' ) === 'asc' ? 'asc' : 'desc' ;
2024-09-19 09:56:05 -07:00
$sort_override = $request -> input ( 'sort' );
2024-09-19 11:56:39 -07:00
$column_sort = in_array ( $sort_override , $allowed_columns ) ? $sort_override : 'assets_count' ;
2024-09-19 09:56:05 -07:00
switch ( $sort_override ) {
case 'created_by' :
$categories = $categories -> OrderByCreatedBy ( $order );
break ;
default :
$categories = $categories -> orderBy ( $column_sort , $order );
break ;
}
2017-01-13 09:01:10 -08:00
$total = $categories -> count ();
$categories = $categories -> skip ( $offset ) -> take ( $limit ) -> get ();
2017-02-16 00:32:33 -08:00
return ( new CategoriesTransformer ) -> transformCategories ( $categories , $total );
2017-01-13 09:01:10 -08:00
}
/**
* Store a newly created resource in storage .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v4 . 0 ]
2021-06-29 02:26:45 -07:00
* @ param \App\Http\Requests\ImageUploadRequest $request
2017-01-13 09:01:10 -08:00
* @ return \Illuminate\Http\Response
*/
2024-07-04 18:36:55 -07:00
public function store ( ImageUploadRequest $request ) : JsonResponse
2017-01-13 09:01:10 -08:00
{
$this -> authorize ( 'create' , Category :: class );
$category = new Category ;
$category -> fill ( $request -> all ());
2021-09-21 15:51:41 -07:00
$category -> category_type = strtolower ( $request -> input ( 'category_type' ));
2021-06-29 02:26:45 -07:00
$category = $request -> handleImages ( $category );
2017-01-13 09:01:10 -08:00
if ( $category -> save ()) {
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , $category , trans ( 'admin/categories/message.create.success' )));
}
2021-06-10 13:15:52 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , $category -> getErrors ()));
2017-01-13 09:01:10 -08:00
}
/**
* Display the specified resource .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v4 . 0 ]
* @ param int $id
*/
2024-07-04 18:36:55 -07:00
public function show ( $id ) : array
2017-01-13 09:01:10 -08:00
{
$this -> authorize ( 'view' , Category :: class );
2023-01-10 16:17:03 -08:00
$category = Category :: withCount ( 'assets as assets_count' , 'accessories as accessories_count' , 'consumables as consumables_count' , 'components as components_count' , 'licenses as licenses_count' ) -> findOrFail ( $id );
2021-06-10 13:15:52 -07:00
return ( new CategoriesTransformer ) -> transformCategory ( $category );
2017-08-22 14:26:08 -07:00
2017-01-13 09:01:10 -08:00
}
/**
* Update the specified resource in storage .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v4 . 0 ]
2021-06-29 02:26:45 -07:00
* @ param \App\Http\Requests\ImageUploadRequest $request
2017-01-13 09:01:10 -08:00
* @ param int $id
* @ return \Illuminate\Http\Response
*/
2024-07-04 18:36:55 -07:00
public function update ( ImageUploadRequest $request , $id ) : JsonResponse
2017-01-13 09:01:10 -08:00
{
2018-07-12 18:28:02 -07:00
$this -> authorize ( 'update' , Category :: class );
2017-01-13 09:01:10 -08:00
$category = Category :: findOrFail ( $id );
2023-01-10 16:17:03 -08:00
2023-01-10 16:24:46 -08:00
// Don't allow the user to change the category_type once it's been created
if (( $request -> filled ( 'category_type' )) && ( $category -> category_type != $request -> input ( 'category_type' ))) {
2023-01-10 16:17:03 -08:00
return response () -> json (
2024-07-04 19:23:48 -07:00
Helper :: formatStandardApiResponse ( 'error' , null , [ 'category_type' => trans ( 'admin/categories/message.update.cannot_change_category_type' )], 422 )
2023-01-10 16:17:03 -08:00
);
}
2017-01-13 09:01:10 -08:00
$category -> fill ( $request -> all ());
2021-06-29 02:26:45 -07:00
$category = $request -> handleImages ( $category );
2017-01-13 09:01:10 -08:00
if ( $category -> save ()) {
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , $category , trans ( 'admin/categories/message.update.success' )));
}
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , $category -> getErrors ()));
}
/**
* Remove the specified resource from storage .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v4 . 0 ]
* @ param int $id
* @ return \Illuminate\Http\Response
*/
2024-07-04 23:07:20 -07:00
public function destroy ( $id ) : JsonResponse
2017-01-13 09:01:10 -08:00
{
$this -> authorize ( 'delete' , Category :: class );
2023-01-10 16:17:03 -08:00
$category = Category :: withCount ( 'assets as assets_count' , 'accessories as accessories_count' , 'consumables as consumables_count' , 'components as components_count' , 'licenses as licenses_count' ) -> findOrFail ( $id );
2020-05-23 10:36:02 -07:00
2021-06-10 13:15:52 -07:00
if ( ! $category -> isDeletable ()) {
2020-05-23 10:36:02 -07:00
return response () -> json (
2021-06-10 13:15:52 -07:00
Helper :: formatStandardApiResponse ( 'error' , null , trans ( 'admin/categories/message.assoc_items' , [ 'asset_type' => $category -> category_type ]))
2020-05-23 10:36:02 -07:00
);
2017-03-11 08:03:16 -08:00
}
2017-01-13 09:01:10 -08:00
$category -> delete ();
2021-06-10 13:15:52 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , null , trans ( 'admin/categories/message.delete.success' )));
2017-01-13 09:01:10 -08:00
}
2017-10-26 22:09:08 -07:00
/**
* Gets a paginated collection for the select2 menus
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v4 . 0.16 ]
* @ see \App\Http\Transformers\SelectlistTransformer
*/
2024-07-04 23:07:20 -07:00
public function selectlist ( Request $request , $category_type = 'asset' ) : array
2017-10-26 22:09:08 -07:00
{
2022-02-11 11:46:14 -08:00
$this -> authorize ( 'view.selectlists' );
2017-10-26 22:09:08 -07:00
$categories = Category :: select ([
'id' ,
'name' ,
'image' ,
]);
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'search' )) {
2017-10-26 22:09:08 -07:00
$categories = $categories -> where ( 'name' , 'LIKE' , '%' . $request -> get ( 'search' ) . '%' );
}
2017-10-28 11:17:52 -07:00
$categories = $categories -> where ( 'category_type' , $category_type ) -> orderBy ( 'name' , 'ASC' ) -> paginate ( 50 );
2017-10-26 22:09:08 -07:00
// Loop through and set some custom properties for the transformer to use.
// This lets us have more flexibility in special cases like assets, where
// they may not have a ->name value but we want to display something anyway
foreach ( $categories as $category ) {
2018-09-29 21:33:52 -07:00
$category -> use_image = ( $category -> image ) ? Storage :: disk ( 'public' ) -> url ( 'categories/' . $category -> image , $category -> image ) : null ;
2017-10-26 22:09:08 -07:00
}
return ( new SelectlistTransformer ) -> transformSelectlist ( $categories );
}
2017-01-13 09:01:10 -08:00
}