Cleaned up custom fields controllers, views, names, etc

This commit is contained in:
snipe 2016-12-15 19:17:07 -08:00
parent 040774d646
commit e685e0f019
10 changed files with 219 additions and 171 deletions

View file

@ -42,67 +42,8 @@ class CustomFieldsController extends Controller
}
/**
* Returns a view with a form for creating a new custom fieldset.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v1.8]
* @return View
*/
public function create()
{
return View::make("custom_fields.create");
}
/**
* Validates and stores a new custom fieldset.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v1.8]
* @return Redirect
*/
public function store(Request $request)
{
//
$cfset = new CustomFieldset(
[
"name" => e($request->get("name")),
"user_id" => Auth::user()->id]
);
$validator = Validator::make(Input::all(), $cfset->rules);
if ($validator->passes()) {
$cfset->save();
return redirect()->route("admin.custom_fields.show", [$cfset->id])->with('success', trans('admin/custom_fields/message.fieldset.create.success'));
} else {
return redirect()->back()->withInput()->withErrors($validator);
}
}
/**
* Associate the custom field with a custom fieldset.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v1.8]
* @return View
*/
public function associate($id)
{
$set = CustomFieldset::find($id);
foreach ($set->fields as $field) {
if ($field->id == Input::get('field_id')) {
return redirect()->route("admin.custom_fields.show", [$id])->withInput()->withErrors(['field_id' => trans('admin/custom_fields/message.field.already_added')]);
}
}
$results=$set->fields()->attach(Input::get('field_id'), ["required" => (Input::get('required') == "on"),"order" => Input::get('order')]);
return redirect()->route("admin.custom_fields.show", [$id])->with("success", trans('admin/custom_fields/message.field.create.assoc_success'));
}
/**
* Returns a view with a form to create a new custom field.
@ -112,7 +53,7 @@ class CustomFieldsController extends Controller
* @since [v1.8]
* @return View
*/
public function createField()
public function create()
{
return View::make("custom_fields.create_field");
}
@ -126,7 +67,7 @@ class CustomFieldsController extends Controller
* @since [v1.8]
* @return Redirect
*/
public function storeField(Request $request)
public function store(Request $request)
{
$field = new CustomField([
"name" => e($request->get("name")),
@ -150,7 +91,7 @@ class CustomFieldsController extends Controller
if ($validator->passes()) {
$results = $field->save();
if ($results) {
return redirect()->route("admin.custom_fields.index")->with("success", trans('admin/custom_fields/message.field.create.success'));
return redirect()->route("fields.index")->with("success", trans('admin/custom_fields/message.field.create.success'));
} else {
dd($field);
return redirect()->back()->withInput()->with('error', trans('admin/custom_fields/message.field.create.error'));
@ -173,7 +114,7 @@ class CustomFieldsController extends Controller
$field = CustomField::find($field_id);
if ($field->fieldset()->detach($fieldset_id)) {
return redirect()->route("admin.custom_fields.index")->with("success", trans('admin/custom_fields/message.field.delete.success'));
return redirect()->route('fieldsets.show',['fieldset' => $fieldset_id])->with("success", trans('admin/custom_fields/message.field.delete.success'));
}
return redirect()->back()->withErrors(['message' => "Field is in-use"]);
@ -186,7 +127,7 @@ class CustomFieldsController extends Controller
* @since [v1.8]
* @return Redirect
*/
public function deleteField($field_id)
public function destroy($field_id)
{
$field = CustomField::find($field_id);
@ -194,35 +135,10 @@ class CustomFieldsController extends Controller
return redirect()->back()->withErrors(['message' => "Field is in-use"]);
} else {
$field->delete();
return redirect()->route("admin.custom_fields.index")->with("success", trans('admin/custom_fields/message.field.delete.success'));
return redirect()->route("fields.index")->with("success", trans('admin/custom_fields/message.field.delete.success'));
}
}
/**
* Validates and stores a new custom field.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @param int $id
* @since [v1.8]
* @return View
*/
public function getCustomFieldset($id)
{
$cfset = CustomFieldset::with('fields')->where('id','=',$id)->orderBy('id','ASC')->first();
$custom_fields_list = ["" => "Add New Field to Fieldset"] + CustomField::pluck("name", "id")->toArray();
$maxid = 0;
foreach ($cfset->fields() as $field) {
if ($field->pivot->order > $maxid) {
$maxid=$field->pivot->order;
}
if (isset($custom_fields_list[$field->id])) {
unset($custom_fields_list[$field->id]);
}
}
return View::make("custom_fields.show")->with("custom_fieldset", $cfset)->with("maxid", $maxid+1)->with("custom_fields_list", $custom_fields_list);
}
/**
@ -255,29 +171,6 @@ class CustomFieldsController extends Controller
}
/**
* Validates a custom fieldset and then deletes if it has no models associated.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @param int $id
* @since [v1.8]
* @return View
*/
public function destroy($id)
{
//
$fieldset = CustomFieldset::find($id);
$models = AssetModel::where("fieldset_id", "=", $id);
if ($models->count() == 0) {
$fieldset->delete();
return redirect()->route("admin.custom_fields.index")->with("success", trans('admin/custom_fields/message.fieldset.delete.success'));
} else {
return redirect()->route("admin.custom_fields.index")->with("error", trans('admin/custom_fields/message.fieldset.delete.in_use'));
}
}
/**
* Reorder the custom fields within a fieldset
*

View file

@ -0,0 +1,173 @@
<?php
namespace App\Http\Controllers;
use View;
use App\Models\CustomFieldset;
use App\Models\CustomField;
use Input;
use Validator;
use Redirect;
use App\Models\AssetModel;
use Lang;
use Auth;
use Illuminate\Http\Request;
use Log;
/**
* 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>]
*/
class CustomFieldsetsController extends Controller
{
/**
* Validates and stores a new custom field.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @param int $id
* @since [v1.8]
* @return View
*/
public function show($id)
{
$cfset = CustomFieldset::with('fields')->where('id','=',$id)->orderBy('id','ASC')->first();
$custom_fields_list = ["" => "Add New Field to Fieldset"] + CustomField::pluck("name", "id")->toArray();
$maxid = 0;
foreach ($cfset->fields() as $field) {
if ($field->pivot->order > $maxid) {
$maxid=$field->pivot->order;
}
if (isset($custom_fields_list[$field->id])) {
unset($custom_fields_list[$field->id]);
}
}
return View::make("custom_fields.fieldset")->with("custom_fieldset", $cfset)->with("maxid", $maxid+1)->with("custom_fields_list", $custom_fields_list);
}
/**
* Returns a view with a form for creating a new custom fieldset.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v1.8]
* @return View
*/
public function create()
{
return View::make("custom_fields.create");
}
/**
* Validates and stores a new custom fieldset.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v1.8]
* @return Redirect
*/
public function store(Request $request)
{
//
$cfset = new CustomFieldset(
[
"name" => e($request->get("name")),
"user_id" => Auth::user()->id]
);
$validator = Validator::make(Input::all(), $cfset->rules);
if ($validator->passes()) {
$cfset->save();
return redirect()->route("fieldsets.show", [$cfset->id])->with('success', trans('admin/custom_fields/message.fieldset.create.success'));
} else {
return redirect()->back()->withInput()->withErrors($validator);
}
}
/**
* What the actual fuck, Brady?
*
* @todo Uhh, build this?
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @param int $id
* @since [v1.8]
* @return Fuckall
*/
public function edit($id)
{
//
}
/**
* GET IN THE SEA BRADY.
*
* @todo Uhh, build this too?
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @param int $id
* @since [v1.8]
* @return Fuckall
*/
public function update($id)
{
//
}
/**
* Validates a custom fieldset and then deletes if it has no models associated.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @param int $id
* @since [v1.8]
* @return View
*/
public function destroy($id)
{
//
$fieldset = CustomFieldset::find($id);
$models = AssetModel::where("fieldset_id", "=", $id);
if ($models->count() == 0) {
$fieldset->delete();
return redirect()->route("fields.show")->with("success", trans('admin/custom_fields/message.fieldset.delete.success'));
} else {
return redirect()->route("fields.show")->with("error", trans('admin/custom_fields/message.fieldset.delete.in_use'));
}
}
/**
* Associate the custom field with a custom fieldset.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v1.8]
* @return View
*/
public function associate($id)
{
$set = CustomFieldset::find($id);
foreach ($set->fields as $field) {
if ($field->id == Input::get('field_id')) {
return redirect()->route("fieldsets.show", [$id])->withInput()->withErrors(['field_id' => trans('admin/custom_fields/message.field.already_added')]);
}
}
$results=$set->fields()->attach(Input::get('field_id'), ["required" => (Input::get('required') == "on"),"order" => Input::get('order')]);
return redirect()->route("fieldsets.show", [$id])->with("success", trans('admin/custom_fields/message.field.create.assoc_success'));
}
}

View file

@ -60,6 +60,7 @@ class RouteServiceProvider extends ServiceProvider
require base_path('routes/web/accessories.php');
require base_path('routes/web/licenses.php');
require base_path('routes/web/consumables.php');
require base_path('routes/web/fields.php');
require base_path('routes/web.php');
});
}

View file

@ -22,7 +22,7 @@
<div class="box-body">
{{ Form::open(['route' => 'admin.custom_fields.store', 'class'=>'form-horizontal']) }}
{{ Form::open(['route' => 'fieldsets.store', 'class'=>'form-horizontal']) }}
<!-- CSRF Token -->
<input type="hidden" name="_token" value="{{ csrf_token() }}" />

View file

@ -23,7 +23,7 @@
<div class="box-body">
{{ Form::open(['route' => 'admin.custom_fields.store-field', 'class'=>'form-horizontal']) }}
{{ Form::open(['route' => 'fields.store', 'class'=>'form-horizontal']) }}
<!-- Name -->

View file

@ -7,7 +7,7 @@
@stop
@section('header_right')
<a href="{{ route('admin.custom_fields.index') }}" class="btn btn-primary pull-right">
<a href="{{ route('fields.index') }}" class="btn btn-primary pull-right">
{{ trans('general.back') }}</a>
@stop
@ -49,7 +49,7 @@
<tr>
<td colspan="5" class="text-right">
{{ Form::open(['route' =>
["admin.custom_fields.associate",$custom_fieldset->id],
["fieldsets.associate",$custom_fieldset->id],
'class'=>'form-horizontal',
'id' => 'ordering']) }}
{{ Form::checkbox("required","on") }}
@ -77,7 +77,7 @@
<td>{{$field->element}}</td>
<td>{{ $field->field_encrypted=='1' ? trans('general.yes') : trans('general.no') }}</td>
<td>{{$field->pivot->required ? "REQUIRED" : "OPTIONAL"}}</td>
<td><a href="{{ route('admin.custom_fields.disassociate', [$field,$custom_fieldset->id]) }}" class="btn btn-sm btn-danger">Remove</a></td>
<td><a href="{{ route('fields.disassociate', [$field,$custom_fieldset->id]) }}" class="btn btn-sm btn-danger">Remove</a></td>
</tr>
@endforeach
</tbody>

View file

@ -17,7 +17,7 @@
<div class="box-header with-border">
<h3 class="box-title">{{ trans('admin/custom_fields/general.fieldsets') }}</h3>
<div class="box-tools pull-right">
<a href="{{ route('admin.custom_fields.create-fieldset') }}" class="btn btn-sm btn-primary" data-toggle="tooltip" title="Create a new fieldset">{{ trans('admin/custom_fields/general.create_fieldset') }}</a>
<a href="{{ route('fieldsets.create') }}" class="btn btn-sm btn-primary" data-toggle="tooltip" title="Create a new fieldset">{{ trans('admin/custom_fields/general.create_fieldset') }}</a>
</div>
</div><!-- /.box-header -->
<div class="box-body">
@ -39,18 +39,18 @@
@foreach($custom_fieldsets AS $fieldset)
<tr>
<td>
{{ link_to_route("admin.custom_fields.show",$fieldset->name,['id' => $fieldset->id]) }}
{{ link_to_route("fieldsets.show",$fieldset->name,['id' => $fieldset->id]) }}
</td>
<td>
{{ $fieldset->fields->count() }}
</td>
<td>
@foreach($fieldset->models as $model)
{{ link_to_route("view/model",$model->name,[$model->id]) }}
{{ link_to_route("models.show",$model->name,[$model->id]) }}
@endforeach
</td>
<td>
{{ Form::open(array('route' => array('admin.custom_fields.delete-field', $fieldset->id), 'method' => 'delete')) }}
{{ Form::open(array('route' => array('fieldsets.destroy', $fieldset->id), 'method' => 'delete')) }}
@if($fieldset->models->count() > 0)
<button type="submit" class="btn btn-danger btn-sm disabled" disabled><i class="fa fa-trash"></i></button>
@ -86,7 +86,7 @@
<div class="box-header with-border">
<h3 class="box-title">{{ trans('admin/custom_fields/general.custom_fields') }}</h3>
<div class="box-tools pull-right">
<a href="{{ route('admin.custom_fields.create-field') }}" class="btn btn-sm btn-primary" data-toggle="tooltip" title="Create a new custom field">{{ trans('admin/custom_fields/general.create_field') }}</a>
<a href="{{ route('fields.create') }}" class="btn btn-sm btn-primary" data-toggle="tooltip" title="Create a new custom field">{{ trans('admin/custom_fields/general.create_field') }}</a>
</div>
</div><!-- /.box-header -->
<div class="box-body">
@ -112,11 +112,11 @@
<td>{{ $field->element }}</td>
<td>
@foreach($field->fieldset as $fieldset)
{{link_to_route("admin.custom_fields.show",$fieldset->name,[$fieldset->id])}}
{{ link_to_route("fieldsets.show",$fieldset->name,[$fieldset->id])}}
@endforeach
</td>
<td>
{{ Form::open(array('route' => array('admin.custom_fields.delete-field', $field->id), 'method' => 'delete')) }}
{{ Form::open(array('route' => array('fields.destroy', $field->id), 'method' => 'delete')) }}
@if($field->fieldset->count()>0)
<button type="submit" class="btn btn-danger btn-sm disabled" disabled><i class="fa fa-trash"></i></button>

View file

@ -367,7 +367,7 @@
</a>
</li>
<li {!! (Request::is('custom_fields*') ? ' class="active"' : '') !!}>
<a href="{{ route('admin.custom_fields.index') }}">
<a href="{{ route('fields.index') }}">
<i class="fa fa-wrench fa-fw"></i> @lang('admin/custom_fields/general.custom_fields')
</a>
</li>

View file

@ -3,12 +3,6 @@ use App\Models\CheckoutRequest;
use App\Models\Location;
use App\Models\Statuslabel;
/*
* Custom Fields Routes
*/
Route::resource('fields', 'CustomFieldsController', [
'parameters' => ['customfield' => 'field_id', 'fieldset' => 'fieldset_id']
]);
/*
* Companies
@ -188,46 +182,7 @@ Route::group([ 'prefix' => 'admin','middleware' => ['web','auth']], function ()
});
# Custom fields support
Route::get('customfields/field/create',
['uses' =>'CustomFieldsController@createField',
'as' => 'admin.custom_fields.create-field']
);
Route::get('customfields/fieldset/create',
['as' => 'admin.custom_fields.create-fieldset',
'uses' => 'CustomFieldsController@create']
);
Route::post('customfields/field/create',
['uses' => 'CustomFieldsController@storeField',
'as' => 'admin.custom_fields.store-field']
);
Route::post('customfields/field/{id}/associate',
['uses' => 'CustomFieldsController@associate',
' as' => 'admin.custom_fields.associate']
);
Route::get('customfields/fieldset/{fieldset_id}/{field_id}/disassociate',
['uses' => 'CustomFieldsController@deleteFieldFromFieldset',
'as' => 'admin.custom_fields.disassociate']
);
Route::get('custom_fields/field/{id}/delete',
['uses' =>'CustomFieldsController@deleteField',
'as' => 'admin.custom_fields.delete-field']
);
Route::get('customfields/fieldset/{id}/view',
['uses' =>'CustomFieldsController@getCustomFieldset',
'as' => 'admin.custom_fields.show']
);
Route::get('customfields',
['uses' =>'CustomFieldsController@getIndex',
'as' => 'admin.custom_fields.index']
);
# User Management

26
routes/web/fields.php Normal file
View file

@ -0,0 +1,26 @@
<?php
/*
* Custom Fields Routes
*/
Route::resource('fields', 'CustomFieldsController', [
'parameters' => ['field' => 'field_id', 'fieldset' => 'fieldset_id']
]);
Route::group([ 'prefix' => 'fields' ], function () {
Route::get('{field_id}/fieldset/{fieldset_id}/disassociate',
['uses' => 'CustomFieldsController@deleteFieldFromFieldset',
'as' => 'fields.disassociate']
);
Route::post('fieldsets/{id}/associate',
['uses' => 'CustomFieldsetsController@associate',
'as' => 'fieldsets.associate']
);
Route::resource('fieldsets', 'CustomFieldsetsController', [
'parameters' => ['fieldset' => 'field_id', 'field' => 'field_id']
]);
});