APIified custom fields reorder

This commit is contained in:
snipe 2017-01-18 04:58:56 -08:00
parent db7b1b586c
commit 297034c037
4 changed files with 54 additions and 37 deletions

View file

@ -0,0 +1,38 @@
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\CustomFieldset;
class CustomFieldsController extends Controller
{
/**
* Reorder the custom fields within a fieldset
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @param int $id
* @since [v3.0]
* @return Array
*/
public function postReorder(Request $request, $id)
{
$fieldset = CustomFieldset::find($id);
$fields = array();
$order_array = array();
$items = $request->input('item');
foreach ($items as $order => $field_id) {
$order_array[$field_id] = $order;
}
foreach ($fieldset->fields as $field) {
$fields[$field->id] = ['required' => $field->pivot->required, 'order' => $order_array[$field->id]];
}
return $fieldset->fields()->sync($fields);
}
}

View file

@ -171,31 +171,5 @@ class CustomFieldsController extends Controller
}
/**
* Reorder the custom fields within a fieldset
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @param int $id
* @since [v3.0]
* @return Array
*/
public function postReorder(Request $request, $id)
{
$fieldset = CustomFieldset::find($id);
$fields = array();
$order_array = array();
$items = $request->input('item');
foreach ($items as $order => $field_id) {
$order_array[$field_id] = $order;
}
foreach ($fieldset->fields as $field) {
$fields[$field->id] = ['required' => $field->pivot->required, 'order' => $order_array[$field->id]];
}
return $fieldset->fields()->sync($fields);
}
}

View file

@ -22,9 +22,7 @@
</div><!-- /.box-header -->
<div class="box-body">
<table
name="fieldsets"
id="sort" class="table table-responsive todo-list"
>
name="fieldsets" id="sort" class="table table-responsive todo-list">
<thead>
<tr>
<th class="col-md-1"></th>
@ -99,6 +97,10 @@
$.ajax({
method: "POST",
url: "{{ route('api.customfields.order', $custom_fieldset->id) }}",
headers: {
"X-Requested-With": 'XMLHttpRequest',
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
},
data: $("#sort tbody").sortable('serialize', {
}),

View file

@ -243,14 +243,6 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
]);
/*---Accessories API---*/
Route::group([ 'prefix' => 'accessories' ], function () {
@ -307,6 +299,17 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
});
Route::group([ 'prefix' => 'fields' ], function () {
Route::post(
'fieldsets/{id}/order',
[ 'as' => 'api.customfields.order', 'uses' => 'CustomFieldsController@postReorder' ]
);
});
});