Fixes #2912 - Reordering of custom fields now saving correctly

This commit is contained in:
snipe 2016-11-11 18:33:59 -08:00
parent f4fc783026
commit 2f05b83e0b

View file

@ -11,6 +11,7 @@ use App\Models\AssetModel;
use Lang; use Lang;
use Auth; use Auth;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Log;
/** /**
* This controller handles all actions related to Custom Asset Fields for * This controller handles all actions related to Custom Asset Fields for
@ -210,21 +211,15 @@ class CustomFieldsController extends Controller
*/ */
public function show($id) public function show($id)
{ {
//$id=$parameters[0]; $cfset = CustomFieldset::with('fields')->where('id','=',$id)->orderBy('id','ASC')->first();
$cfset=CustomFieldset::find($id); $custom_fields_list = ["" => "Add New Field to Fieldset"] + CustomField::lists("name", "id")->toArray();
//print_r($parameters); $maxid = 0;
// foreach ($cfset->fields() as $field) {
$custom_fields_list=["" => "Add New Field to Fieldset"] + CustomField::lists("name", "id")->toArray();
// print_r($custom_fields_list);
$maxid=0;
foreach ($cfset->fields as $field) {
// print "Looking for: ".$field->id;
if ($field->pivot->order > $maxid) { if ($field->pivot->order > $maxid) {
$maxid=$field->pivot->order; $maxid=$field->pivot->order;
} }
if (isset($custom_fields_list[$field->id])) { if (isset($custom_fields_list[$field->id])) {
// print "Found ".$field->id.", so removing it.<br>";
unset($custom_fields_list[$field->id]); unset($custom_fields_list[$field->id]);
} }
} }
@ -274,14 +269,14 @@ class CustomFieldsController extends Controller
public function destroy($id) public function destroy($id)
{ {
// //
$fieldset=CustomFieldset::find($id); $fieldset = CustomFieldset::find($id);
$models = AssetModel::where("fieldset_id", "=", $id); $models = AssetModel::where("fieldset_id", "=", $id);
if ($models->count()==0) { if ($models->count() == 0) {
$fieldset->delete(); $fieldset->delete();
return redirect()->route("admin.custom_fields.index")->with("success", trans('admin/custom_fields/message.fieldset.delete.success')); return redirect()->route("admin.custom_fields.index")->with("success", trans('admin/custom_fields/message.fieldset.delete.success'));
} else { } else {
return redirect()->route("admin.custom_fields.index")->with("error", trans('admin/custom_fields/message.fieldset.delete.in_use')); //->with("models",$models); return redirect()->route("admin.custom_fields.index")->with("error", trans('admin/custom_fields/message.fieldset.delete.in_use'));
} }
} }
@ -294,18 +289,23 @@ class CustomFieldsController extends Controller
* @since [v3.0] * @since [v3.0]
* @return Array * @return Array
*/ */
public function postReorder($id) public function postReorder(Request $request, $id)
{ {
$fieldset=CustomFieldset::find($id); $fieldset = CustomFieldset::find($id);
$fields = array(); $fields = array();
$order_array = array();
$items = Input::get('item'); $items = $request->input('item');
foreach ($fieldset->fields as $field) {
$value = array_shift($items); foreach ($items as $order => $field_id) {
$fields[$field->id] = ['required' => $field->pivot->required, 'order' => $value]; $order_array[$field_id] = $order;
} }
return $fieldset->fields()->sync($fields);
foreach ($fieldset->fields as $field) {
$fields[$field->id] = ['required' => $field->pivot->required, 'order' => $order_array[$field->id]];
}
return $fieldset->fields()->sync($fields);
} }
} }