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 ;
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 ;
2023-01-10 16:17:03 -08:00
use Illuminate\Support\Facades\Validator ;
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
*/
public function index ( Request $request )
{
$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' ,
'created_at' ,
'updated_at' ,
'name' , 'category_type' ,
'use_default_eula' ,
'eula_text' ,
'require_acceptance' ,
'checkin_email' ,
'image'
2023-03-08 13:06:14 -08:00
]) -> 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' );
}
2017-01-13 09:01:10 -08:00
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' ));
}
2023-04-12 11:28:46 -07:00
// Make sure the offset and limit are actually integers and do not exceed system limits
$offset = ( $request -> input ( 'offset' ) > $categories -> count ()) ? $categories -> count () : intval ( request ( 'offset' ));
$limit = ( $request -> input ( 'limit' ) > config ( 'app.max_results' )) ? config ( 'app.max_results' ) : max ( intval ( request ( 'offset' )), config ( 'app.max_results' ));
2019-09-03 14:02:08 -07:00
2017-01-13 09:01:10 -08:00
$order = $request -> input ( 'order' ) === 'asc' ? 'asc' : 'desc' ;
2017-10-19 11:48:09 -07:00
$sort = in_array ( $request -> input ( 'sort' ), $allowed_columns ) ? $request -> input ( 'sort' ) : 'assets_count' ;
2017-01-13 09:01:10 -08:00
$categories -> orderBy ( $sort , $order );
$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
*/
2021-06-29 02:26:45 -07:00
public function store ( ImageUploadRequest $request )
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
* @ return \Illuminate\Http\Response
*/
public function show ( $id )
{
$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
*/
2021-06-29 02:26:45 -07:00
public function update ( ImageUploadRequest $request , $id )
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 (
Helper :: formatStandardApiResponse ( 'error' , null , trans ( 'admin/categories/message.update.cannot_change_category_type' ))
);
}
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
*/
public function destroy ( $id )
{
$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
*/
2017-10-28 11:17:52 -07:00
public function selectlist ( Request $request , $category_type = 'asset' )
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
}