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

176 lines
4.5 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-08-25 17:07:58 -07:00
use Illuminate\Http\Request;
use Log;
2016-03-25 01:18:05 -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
{
/**
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
*/
public function index()
2016-03-25 01:18:05 -07:00
{
$fieldsets = CustomFieldset::with("fields", "models")->get();
$fields = CustomField::with("fieldset")->get();
2016-03-25 01:18:05 -07:00
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 to create a new custom field.
*
* @see CustomFieldsController::storeField()
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v1.8]
* @return View
*/
public function create()
2016-03-25 01:18:05 -07:00
{
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
*/
public function store(Request $request)
2016-03-25 01:18:05 -07:00
{
2016-08-25 17:08:08 -07:00
$field = new CustomField([
"name" => e($request->get("name")),
"element" => e($request->get("element")),
"field_values" => e($request->get("field_values")),
"field_encrypted" => e($request->get("field_encrypted", 0)),
"user_id" => Auth::user()->id
]);
2016-03-25 01:18:05 -07:00
if (!in_array(Input::get('format'), array_keys(CustomField::$PredefinedFormats))) {
2016-08-25 17:08:08 -07:00
$field->format = e($request->get("custom_format"));
2016-03-25 01:18:05 -07:00
} else {
2016-08-25 17:08:08 -07:00
$field->format = e($request->get("format"));
2016-03-25 01:18:05 -07:00
}
2016-08-25 17:08:08 -07:00
2016-03-25 01:18:05 -07:00
$validator=Validator::make(Input::all(), $field->rules);
if ($validator->passes()) {
2016-08-25 17:08:08 -07:00
$results = $field->save();
2016-03-25 01:18:05 -07:00
if ($results) {
return redirect()->route("fields.index")->with("success", trans('admin/custom_fields/message.field.create.success'));
2016-03-25 01:18:05 -07:00
} else {
2016-08-25 17:08:08 -07:00
dd($field);
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-07-28 08:10:15 -07:00
/**
* Detach a custom field from a fieldset.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return Redirect
*/
public function deleteFieldFromFieldset($field_id, $fieldset_id)
{
$field = CustomField::find($field_id);
if ($field->fieldset()->detach($fieldset_id)) {
2016-12-29 14:02:18 -08:00
return redirect()->route('fieldsets.show', ['fieldset' => $fieldset_id])->with("success", trans('admin/custom_fields/message.field.delete.success'));
2016-07-28 08:10:15 -07:00
}
return redirect()->back()->withErrors(['message' => "Field is in-use"]);
}
2016-04-07 13:21:09 -07:00
/**
* Delete a custom field.
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v1.8]
* @return Redirect
*/
public function destroy($field_id)
2016-03-25 01:18:05 -07:00
{
2016-08-25 17:08:08 -07:00
$field = CustomField::find($field_id);
2016-07-28 08:10:15 -07:00
2016-03-25 01:18:05 -07:00
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();
return redirect()->route("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
* 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-03-25 01:18:05 -07:00
}