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

309 lines
8.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-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 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-08-25 17:07:58 -07:00
public function store(Request $request)
2016-03-25 01:18:05 -07:00
{
//
2016-08-25 17:07:58 -07:00
$cfset = new CustomFieldset(
[
"name" => e($request->get("name")),
"user_id" => Auth::user()->id]
);
$validator = Validator::make(Input::all(), $cfset->rules);
2016-03-25 01:18:05 -07:00
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-08-25 17:08:08 -07:00
public function storeField(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) {
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-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)) {
return redirect()->route("admin.custom_fields.index")->with("success", trans('admin/custom_fields/message.field.delete.success'));
}
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
*/
2016-03-25 01:18:05 -07:00
public function deleteField($field_id)
{
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();
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
*/
public function getCustomFieldset($id)
2016-03-25 01:18:05 -07:00
{
$cfset = CustomFieldset::with('fields')->where('id','=',$id)->orderBy('id','ASC')->first();
$custom_fields_list = ["" => "Add New Field to Fieldset"] + CustomField::pluck("name", "id")->toArray();
2016-03-25 01:18:05 -07:00
$maxid = 0;
foreach ($cfset->fields() as $field) {
2016-03-25 01:18:05 -07:00
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);
}
/**
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 01:18:05 -07:00
2016-03-25 19:26:22 -07:00
$models = AssetModel::where("fieldset_id", "=", $id);
if ($models->count() == 0) {
2016-03-25 01:18:05 -07:00
$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 {
return redirect()->route("admin.custom_fields.index")->with("error", trans('admin/custom_fields/message.fieldset.delete.in_use'));
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
*/
public function postReorder(Request $request, $id)
2016-06-22 12:27:41 -07:00
{
$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);
}
2016-03-25 01:18:05 -07:00
}