snipe-it/app/Http/Controllers/CustomFieldsController.php

278 lines
8 KiB
PHP
Raw Normal View History

2016-03-25 01:18:05 -07:00
<?php
namespace App\Http\Controllers;
use View;
use App\Models\CustomFieldset;
use App\Models\CustomField;
use Input;
use Validator;
use Redirect;
2016-03-25 19:26:22 -07:00
use App\Models\AssetModel;
2016-03-25 01:18:05 -07:00
use Lang;
use Auth;
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
{
/**
2016-04-07 13:21:09 -07:00
* Returns a view with a listing of custom fields.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v1.8]
* @return View
*/
2016-03-25 01:18:05 -07:00
public function index()
{
//
$fieldsets=CustomFieldset::with("fields", "models")->get();
//$fieldsets=CustomFieldset::all();
$fields=CustomField::with("fieldset")->get();
//$fields=CustomField::all();
return View::make("custom_fields.index")->with("custom_fieldsets", $fieldsets)->with("custom_fields", $fields);
}
/**
2016-04-07 13:21:09 -07:00
* Returns a view with a form for creating a new custom fieldset.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v1.8]
* @return View
*/
2016-03-25 01:18:05 -07:00
public function create()
{
//
return View::make("custom_fields.create");
}
/**
2016-04-07 13:21:09 -07:00
* Validates and stores a new custom fieldset.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v1.8]
* @return Redirect
*/
2016-03-25 01:18:05 -07:00
public function store()
{
//
$cfset=new CustomFieldset(["name" => Input::get("name"),"user_id" => Auth::user()->id]);
$validator=Validator::make(Input::all(), $cfset->rules);
if ($validator->passes()) {
$cfset->save();
2016-04-28 21:06:41 -07:00
return redirect()->route("admin.custom_fields.show", [$cfset->id])->with('success', trans('admin/custom_fields/message.fieldset.create.success'));
2016-03-25 01:18:05 -07:00
} else {
2016-04-28 21:06:41 -07:00
return redirect()->back()->withInput()->withErrors($validator);
2016-03-25 01:18:05 -07:00
}
}
2016-04-07 13:21:09 -07:00
/**
* Associate the custom field with a custom fieldset.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v1.8]
* @return View
*/
2016-03-25 01:18:05 -07:00
public function associate($id)
{
$set = CustomFieldset::find($id);
foreach ($set->fields as $field) {
if ($field->id == Input::get('field_id')) {
2016-04-28 21:06:41 -07:00
return redirect()->route("admin.custom_fields.show", [$id])->withInput()->withErrors(['field_id' => trans('admin/custom_fields/message.field.already_added')]);
2016-03-25 01:18:05 -07:00
}
}
$results=$set->fields()->attach(Input::get('field_id'), ["required" => (Input::get('required') == "on"),"order" => Input::get('order')]);
2016-04-28 21:06:41 -07:00
return redirect()->route("admin.custom_fields.show", [$id])->with("success", trans('admin/custom_fields/message.field.create.assoc_success'));
2016-03-25 01:18:05 -07:00
}
2016-04-07 13:21:09 -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 View
*/
2016-03-25 01:18:05 -07:00
public function createField()
{
return View::make("custom_fields.create_field");
}
2016-04-07 13:21:09 -07:00
/**
* Validates and stores a new custom field.
*
* @see CustomFieldsController::createField()
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v1.8]
* @return Redirect
*/
2016-03-25 01:18:05 -07:00
public function storeField()
{
$field=new CustomField(["name" => Input::get("name"),"element" => Input::get("element"),"user_id" => Auth::user()->id]);
if (!in_array(Input::get('format'), array_keys(CustomField::$PredefinedFormats))) {
$field->format=Input::get("custom_format");
} else {
$field->format=Input::get('format');
}
$validator=Validator::make(Input::all(), $field->rules);
if ($validator->passes()) {
$results=$field->save();
//return "postCreateField: $results";
if ($results) {
2016-04-28 21:06:41 -07:00
return redirect()->route("admin.custom_fields.index")->with("success", trans('admin/custom_fields/message.field.create.success'));
2016-03-25 01:18:05 -07:00
} else {
2016-04-28 21:06:41 -07:00
return redirect()->back()->withInput()->with('error', trans('admin/custom_fields/message.field.create.error'));
2016-03-25 01:18:05 -07:00
}
} else {
2016-04-28 21:06:41 -07:00
return redirect()->back()->withInput()->withErrors($validator);
2016-03-25 01:18:05 -07:00
}
}
2016-04-07 13:21:09 -07:00
/**
* Delete a custom field.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v1.8]
* @return Redirect
*/
2016-03-25 01:18:05 -07:00
public function deleteField($field_id)
{
$field=CustomField::find($field_id);
if ($field->fieldset->count()>0) {
2016-04-28 21:06:41 -07:00
return redirect()->back()->withErrors(['message' => "Field is in-use"]);
2016-03-25 01:18:05 -07:00
} else {
$field->delete();
2016-04-28 21:06:41 -07:00
return redirect()->route("admin.custom_fields.index")->with("success", trans('admin/custom_fields/message.field.delete.success'));
2016-03-25 01:18:05 -07:00
}
}
/**
2016-04-07 13:21:09 -07:00
* Validates and stores a new custom field.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @param int $id
* @since [v1.8]
* @return View
*/
2016-03-25 01:18:05 -07:00
public function show($id)
{
//$id=$parameters[0];
$cfset=CustomFieldset::find($id);
//print_r($parameters);
//
$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) {
$maxid=$field->pivot->order;
}
if (isset($custom_fields_list[$field->id])) {
// print "Found ".$field->id.", so removing it.<br>";
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);
}
/**
2016-04-07 13:21:09 -07:00
* What the actual fuck, Brady?
*
* @todo Uhh, build this?
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @param int $id
* @since [v1.8]
* @return Fuckall
*/
2016-03-25 01:18:05 -07:00
public function edit($id)
{
//
}
/**
2016-04-07 13:21:09 -07:00
* GET IN THE SEA BRADY.
*
* @todo Uhh, build this too?
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @param int $id
* @since [v1.8]
* @return Fuckall
*/
2016-03-25 01:18:05 -07:00
public function update($id)
{
//
}
/**
2016-04-07 13:21:09 -07:00
* 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
*/
2016-03-25 01:18:05 -07:00
public function destroy($id)
{
//
$fieldset=CustomFieldset::find($id);
2016-03-25 19:26:22 -07:00
$models = AssetModel::where("fieldset_id", "=", $id);
2016-03-25 01:18:05 -07:00
if ($models->count()==0) {
$fieldset->delete();
2016-04-28 21:06:41 -07:00
return redirect()->route("admin.custom_fields.index")->with("success", trans('admin/custom_fields/message.fieldset.delete.success'));
2016-03-25 01:18:05 -07:00
} else {
2016-04-28 21:06:41 -07:00
return redirect()->route("admin.custom_fields.index")->with("error", trans('admin/custom_fields/message.fieldset.delete.in_use')); //->with("models",$models);
2016-03-25 01:18:05 -07:00
}
}
/**
* Reorder the custom fields within a fieldset
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @param int $id
* @since [v3.0]
* @return Array
*/
2016-06-22 12:27:41 -07:00
public function postReorder($id)
{
$fieldset=CustomFieldset::find($id);
$fields = array();
$items = Input::get('item');
foreach ($fieldset->fields as $field) {
$value = array_shift($items);
$fields[$field->id] = ['required' => $field->pivot->required, 'order' => $value];
}
return $fieldset->fields()->sync($fields);
}
2016-03-25 01:18:05 -07:00
}