2016-03-25 01:18:05 -07:00
< ? php
2021-06-10 13:15:52 -07:00
2016-03-25 01:18:05 -07:00
namespace App\Http\Controllers ;
2018-12-06 20:05:04 -08:00
use App\Helpers\Helper ;
2019-03-13 20:12:03 -07:00
use App\Http\Requests\CustomFieldRequest ;
2018-12-06 20:05:04 -08:00
use App\Models\CustomField ;
use App\Models\CustomFieldset ;
2018-07-24 19:35:26 -07:00
use Illuminate\Support\Facades\Auth ;
2023-04-25 21:07:50 -07:00
use Illuminate\Http\Request ;
2016-03-25 01:18:05 -07:00
2024-06-06 05:35:38 -07:00
2016-04-07 13:21:09 -07:00
/**
* This controller handles all actions related to Custom Asset Fields for
* the Snipe - IT Asset Management application .
*
* @ todo Improve documentation here .
* @ todo Check for raw DB queries and try to convert them to query builder statements
* @ version v2 . 0
* @ author [ Brady Wetherington ] [ < uberbrady @ gmail . com > ]
*/
2016-03-25 01:18:05 -07:00
class CustomFieldsController extends Controller
{
2024-06-06 05:35:38 -07:00
2016-03-25 01:18:05 -07:00
/**
2018-07-24 19:35:26 -07:00
* Returns a view with a listing of custom fields .
*
* @ author [ Brady Wetherington ] [ < uberbrady @ gmail . com > ]
* @ since [ v1 . 8 ]
* @ return \Illuminate\Support\Facades\View
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
2024-06-06 05:35:38 -07:00
public function index ( Request $request )
2016-03-25 01:18:05 -07:00
{
2018-07-12 18:28:20 -07:00
$this -> authorize ( 'view' , CustomField :: class );
2016-12-14 09:56:23 -08:00
2024-06-06 05:35:38 -07:00
$fieldsets = CustomFieldset :: with ( 'fields' ) -> where ( " type " , Helper :: $itemtypes_having_custom_fields [ $request -> get ( 'tab' , 0 )]) -> get (); //cannot eager-load 'customizable' because it's not a relation
$fields = CustomField :: with ( 'fieldset' ) -> where ( " type " , Helper :: $itemtypes_having_custom_fields [ $request -> get ( 'tab' , 0 )]) -> get ();
2018-12-06 20:05:04 -08:00
2021-06-10 13:15:52 -07:00
return view ( 'custom_fields.index' ) -> with ( 'custom_fieldsets' , $fieldsets ) -> with ( 'custom_fields' , $fields );
2016-03-25 01:18:05 -07:00
}
2021-04-20 15:09:23 -07:00
/**
* Just redirect the user back if they try to view the details of a field .
* We already show those details on the listing page .
*
* @ see CustomFieldsController :: storeField ()
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v5 . 1.5 ]
2023-04-25 21:07:50 -07:00
* @ return \Illuminate\Http\RedirectResponse
2021-04-20 15:09:23 -07:00
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
public function show ()
{
2021-06-10 13:15:52 -07:00
return redirect () -> route ( 'fields.index' );
2021-04-20 15:09:23 -07:00
}
2016-04-07 13:21:09 -07:00
/**
2018-07-24 19:35:26 -07:00
* Returns a view with a form to create a new custom field .
*
* @ see CustomFieldsController :: storeField ()
* @ author [ Brady Wetherington ] [ < uberbrady @ gmail . com > ]
* @ since [ v1 . 8 ]
* @ return \Illuminate\Support\Facades\View
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
2023-04-25 21:08:26 -07:00
public function create ( Request $request )
2016-03-25 01:18:05 -07:00
{
2018-07-12 18:28:20 -07:00
$this -> authorize ( 'create' , CustomField :: class );
2023-04-25 21:08:26 -07:00
$fieldsets = CustomFieldset :: get ();
2017-01-25 04:34:11 -08:00
2021-06-10 13:15:52 -07:00
return view ( 'custom_fields.fields.edit' , [
2019-01-15 13:56:56 -08:00
'predefinedFormats' => Helper :: predefined_formats (),
2023-05-03 10:57:02 -07:00
'customFormat' => '' ,
'fieldsets' => $fieldsets ,
'field' => new CustomField (),
]);
2016-03-25 01:18:05 -07:00
}
2016-04-07 13:21:09 -07:00
/**
2018-07-24 19:35:26 -07:00
* Validates and stores a new custom field .
*
* @ see CustomFieldsController :: createField ()
* @ author [ Brady Wetherington ] [ < uberbrady @ gmail . com > ]
* @ since [ v1 . 8 ]
2023-04-25 21:08:26 -07:00
* @ return \Illuminate\Http\RedirectResponse
2018-07-24 19:35:26 -07:00
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
2017-11-04 17:06:14 -07:00
public function store ( CustomFieldRequest $request )
2016-03-25 01:18:05 -07:00
{
2018-07-12 18:28:20 -07:00
$this -> authorize ( 'create' , CustomField :: class );
2022-11-14 14:41:31 -08:00
$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' ;
}
2016-08-25 17:08:08 -07:00
$field = new CustomField ([
2021-11-22 16:43:21 -08:00
" name " => trim ( $request -> get ( " name " )),
2017-01-26 04:52:11 -08:00
" element " => $request -> get ( " element " ),
" help_text " => $request -> get ( " help_text " ),
2024-01-26 09:56:02 -08:00
" field_values " => $request -> get ( " field_values " ),
2017-01-26 04:52:11 -08:00
" field_encrypted " => $request -> get ( " field_encrypted " , 0 ),
2022-11-14 14:41:31 -08:00
" show_in_email " => $show_in_email ,
2022-02-16 13:47:48 -08:00
" is_unique " => $request -> get ( " is_unique " , 0 ),
2022-11-14 14:41:31 -08:00
" display_in_user_view " => $display_in_user_view ,
2023-04-25 21:08:26 -07:00
" auto_add_to_fieldsets " => $request -> get ( " auto_add_to_fieldsets " , 0 ),
2023-07-14 01:09:43 -07:00
" show_in_listview " => $request -> get ( " show_in_listview " , 0 ),
2023-10-25 09:27:36 -07:00
" show_in_requestable_list " => $request -> get ( " show_in_requestable_list " , 0 ),
2024-06-06 05:35:38 -07:00
" user_id " => Auth :: id (),
2016-08-25 17:08:08 -07:00
]);
2024-06-06 05:35:38 -07:00
// not mass-assignable; must be manual
$field -> type = Helper :: $itemtypes_having_custom_fields [ $request -> get ( 'tab' )];
2016-08-25 17:08:08 -07:00
2016-03-25 01:18:05 -07:00
2021-06-10 13:15:52 -07:00
if ( $request -> filled ( 'custom_format' )) {
2023-02-12 14:27:37 -08:00
$field -> format = $request -> get ( 'custom_format' );
2016-03-25 01:18:05 -07:00
} else {
2023-02-12 14:27:37 -08:00
$field -> format = $request -> get ( 'format' );
2016-03-25 01:18:05 -07:00
}
2017-11-04 17:06:14 -07:00
if ( $field -> save ()) {
2023-04-25 21:08:26 -07:00
// Sync fields with fieldsets
2023-04-25 21:31:23 -07:00
$fieldset_array = $request -> input ( 'associate_fieldsets' );
if ( $request -> has ( 'associate_fieldsets' ) && ( is_array ( $fieldset_array ))) {
$field -> fieldset () -> sync ( array_keys ( $fieldset_array ));
} else {
$field -> fieldset () -> sync ([]);
2023-04-25 21:08:26 -07:00
}
2024-06-06 05:35:38 -07:00
return redirect () -> route ( 'fields.index' , [ 'tab' => $request -> get ( 'tab' , 0 )]) -> with ( 'success' , trans ( 'admin/custom_fields/message.field.create.success' ));
2016-03-25 01:18:05 -07:00
}
2017-11-04 17:06:14 -07:00
2023-04-25 21:08:26 -07:00
return redirect () -> back () -> with ( 'selected_fieldsets' , $request -> input ( 'associate_fieldsets' )) -> withInput ()
2018-07-24 19:35:26 -07:00
-> with ( 'error' , trans ( 'admin/custom_fields/message.field.create.error' ));
2016-03-25 01:18:05 -07:00
}
2016-07-28 08:10:15 -07:00
/**
* Detach a custom field from a fieldset .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v3 . 0 ]
2023-04-25 21:08:26 -07:00
* @ return \Illuminate\Http\RedirectResponse
2018-07-24 19:35:26 -07:00
* @ throws \Illuminate\Auth\Access\AuthorizationException
2016-07-28 08:10:15 -07:00
*/
public function deleteFieldFromFieldset ( $field_id , $fieldset_id )
{
$field = CustomField :: find ( $field_id );
2018-07-12 18:28:20 -07:00
$this -> authorize ( 'update' , $field );
2022-02-09 16:16:16 -08:00
// Check that the field exists - this is mostly related to the demo, where we
// rewrite the data every x minutes, so it's possible someone might be disassociating
// a field from a fieldset just as we're wiping the database
if (( $field ) && ( $fieldset_id )) {
2016-07-28 08:10:15 -07:00
if ( $field -> fieldset () -> detach ( $fieldset_id )) {
2018-07-24 19:35:26 -07:00
return redirect () -> route ( 'fieldsets.show' , [ 'fieldset' => $fieldset_id ])
2021-06-10 13:15:52 -07:00
-> with ( 'success' , trans ( 'admin/custom_fields/message.field.delete.success' ));
2022-02-09 16:30:42 -08:00
} else {
return redirect () -> back () -> withErrors ([ 'message' => " Field is in use and cannot be deleted. " ]);
2023-04-25 21:08:26 -07:00
}
2016-07-28 08:10:15 -07:00
}
2022-02-09 16:16:16 -08:00
return redirect () -> back () -> withErrors ([ 'message' => " Error deleting field from fieldset " ]);
2022-02-09 16:30:42 -08:00
2016-07-28 08:10:15 -07:00
}
2016-04-07 13:21:09 -07:00
/**
2018-07-24 19:35:26 -07:00
* Delete a custom field .
*
* @ author [ Brady Wetherington ] [ < uberbrady @ gmail . com > ]
* @ since [ v1 . 8 ]
2023-04-25 21:08:26 -07:00
* @ return \Illuminate\Http\RedirectResponse
2018-07-24 19:35:26 -07:00
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
2016-12-15 19:17:07 -08:00
public function destroy ( $field_id )
2016-03-25 01:18:05 -07:00
{
2021-04-20 15:10:28 -07:00
if ( $field = CustomField :: find ( $field_id )) {
2021-04-20 15:09:23 -07:00
$this -> authorize ( 'delete' , $field );
2018-07-12 18:28:20 -07:00
2021-04-20 15:10:28 -07:00
if (( $field -> fieldset ) && ( $field -> fieldset -> count () > 0 )) {
2021-06-10 13:15:52 -07:00
return redirect () -> back () -> withErrors ([ 'message' => 'Field is in-use' ]);
2021-04-20 15:09:23 -07:00
}
$field -> delete ();
2024-06-06 05:35:38 -07:00
return redirect () -> route ( 'fields.index' , [ 'tab' => Request :: query ( 'tab' , 0 )])
-> with ( 'success' , trans ( 'admin/custom_fields/message.field.delete.success' ));
2016-03-25 01:18:05 -07:00
}
2021-06-10 13:15:52 -07:00
return redirect () -> back () -> withErrors ([ 'message' => 'Field does not exist' ]);
2021-04-20 15:10:28 -07:00
}
2016-03-25 01:18:05 -07:00
/**
2018-07-24 19:35:26 -07:00
* Return a view to edit a custom field
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $id
* @ since [ v4 . 0 ]
* @ return \Illuminate\Support\Facades\View
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
2023-04-25 21:08:26 -07:00
public function edit ( Request $request , $id )
2016-03-25 01:18:05 -07:00
{
2022-01-03 19:14:50 -08:00
if ( $field = CustomField :: find ( $id )) {
2018-07-12 18:28:20 -07:00
$this -> authorize ( 'update' , $field );
2023-04-25 21:08:26 -07:00
$fieldsets = CustomFieldset :: get ();
2018-12-06 20:05:04 -08:00
$customFormat = '' ;
2021-06-10 13:15:52 -07:00
if (( stripos ( $field -> format , 'regex' ) === 0 ) && ( $field -> format !== CustomField :: PREDEFINED_FORMATS [ 'MAC' ])) {
2018-12-06 20:05:04 -08:00
$customFormat = $field -> format ;
}
2021-06-10 13:15:52 -07:00
return view ( 'custom_fields.fields.edit' , [
2018-12-06 20:05:04 -08:00
'field' => $field ,
'customFormat' => $customFormat ,
2023-04-25 21:08:26 -07:00
'fieldsets' => $fieldsets ,
2021-06-10 13:15:52 -07:00
'predefinedFormats' => Helper :: predefined_formats (),
2018-12-06 20:05:04 -08:00
]);
2022-01-03 19:14:50 -08:00
}
return redirect () -> route ( " fields.index " )
-> with ( " error " , trans ( 'admin/custom_fields/message.field.invalid' ));
2016-03-25 01:18:05 -07:00
}
/**
2018-07-24 19:35:26 -07:00
* Store the updated field
*
* @ todo Allow encrypting / decrypting if encryption status changes
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $id
* @ since [ v4 . 0 ]
2023-04-25 21:08:26 -07:00
* @ return \Illuminate\Http\RedirectResponse
2018-07-24 19:35:26 -07:00
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
2017-11-04 17:06:14 -07:00
public function update ( CustomFieldRequest $request , $id )
2016-03-25 01:18:05 -07:00
{
2021-06-10 13:15:52 -07:00
$field = CustomField :: find ( $id );
2018-07-12 18:28:20 -07:00
$this -> authorize ( 'update' , $field );
2022-11-14 14:41:31 -08:00
$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' ;
}
2021-11-22 16:43:21 -08:00
$field -> name = trim ( e ( $request -> get ( " name " )));
2018-12-06 20:05:04 -08:00
$field -> element = e ( $request -> get ( " element " ));
2024-01-26 09:56:02 -08:00
$field -> field_values = $request -> get ( " field_values " );
2018-12-06 20:05:04 -08:00
$field -> user_id = Auth :: id ();
$field -> help_text = $request -> get ( " help_text " );
2022-11-14 14:41:31 -08:00
$field -> show_in_email = $show_in_email ;
2022-02-16 13:47:48 -08:00
$field -> is_unique = $request -> get ( " is_unique " , 0 );
2022-11-14 14:41:31 -08:00
$field -> display_in_user_view = $display_in_user_view ;
2023-04-25 21:08:26 -07:00
$field -> auto_add_to_fieldsets = $request -> get ( " auto_add_to_fieldsets " , 0 );
2023-07-14 01:09:43 -07:00
$field -> show_in_listview = $request -> get ( " show_in_listview " , 0 );
2023-10-25 09:27:36 -07:00
$field -> show_in_requestable_list = $request -> get ( " show_in_requestable_list " , 0 );
2017-01-25 04:34:11 -08:00
2018-12-06 20:05:04 -08:00
if ( $request -> get ( 'format' ) == 'CUSTOM REGEX' ) {
2021-06-10 13:15:52 -07:00
$field -> format = e ( $request -> get ( 'custom_format' ));
2017-01-25 04:34:11 -08:00
} else {
2021-06-10 13:15:52 -07:00
$field -> format = e ( $request -> get ( 'format' ));
2017-01-25 04:34:11 -08:00
}
2023-04-25 21:08:26 -07:00
if ( $field -> element == 'checkbox' || $field -> element == 'radio' ){
2022-09-15 09:21:02 -07:00
$field -> format = 'ANY' ;
}
2017-01-25 04:52:47 -08:00
if ( $field -> save ()) {
2023-04-25 21:31:23 -07:00
2023-04-25 21:08:26 -07:00
// Sync fields with fieldsets
2023-04-25 21:31:23 -07:00
$fieldset_array = $request -> input ( 'associate_fieldsets' );
if ( $request -> has ( 'associate_fieldsets' ) && ( is_array ( $fieldset_array ))) {
$field -> fieldset () -> sync ( array_keys ( $fieldset_array ));
} else {
$field -> fieldset () -> sync ([]);
}
2024-06-06 05:35:38 -07:00
return redirect () -> route ( 'fields.index' , [ 'tab' => $request -> get ( 'tab' , 0 )]) -> with ( 'success' , trans ( 'admin/custom_fields/message.field.update.success' ));
2017-01-25 04:34:11 -08:00
}
2017-11-04 17:06:14 -07:00
return redirect () -> back () -> withInput () -> with ( 'error' , trans ( 'admin/custom_fields/message.field.update.error' ));
2016-03-25 01:18:05 -07:00
}
}