d2b7828569
This should allow us to add custom fields to just about anything we want to within Snipe-IT. Below are the commits that have been squashed together: Initial decoupling of custom field behavior from Assets for re-use Add new DB columns to Custom Fields and fieldsets for 'type' WIP: trying to figure out UI for custom fields for things other than Assets, find problematic places Real progress towards getting to where this stuff might actually work... Fix the table-name determining code for Custom Fields Getting it closer to where Assets at least work Rename the trait to it's new, even better name Solid progress on the new Trait! WIP: HasCustomFields, still working some stuff out Got some basics working; creating custom fields and stuff HasCustomFields now validates and saves Starting to yank the other boilerplate code as things start to work (!) Got the start of defaultValuesForCustomField() working More progress (squash me!) Add migrations for default_values_for_custom_fields table WIP: more towards hasCustomFields trait Progress cleaning up the PR, fixing FIXME's New, passing HasCustomFieldsTrait test! Fix date formatter helper for custom fields Fixed more FIXME's
124 lines
3.7 KiB
PHP
124 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Gate;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Validation\Rule;
|
|
use Watson\Validating\ValidatingTrait;
|
|
|
|
class CustomFieldset extends Model
|
|
{
|
|
use HasFactory;
|
|
use ValidatingTrait;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
/**
|
|
* Validation rules
|
|
* @var array
|
|
*/
|
|
public $rules = [
|
|
'name' => 'required|unique:custom_fieldsets',
|
|
''
|
|
];
|
|
|
|
/**
|
|
* Whether the model should inject it's identifier to the unique
|
|
* validation rules before attempting validation. If this property
|
|
* is not set in the model it will default to true.
|
|
*
|
|
* @var bool
|
|
*/
|
|
protected $injectUniqueIdentifier = true;
|
|
|
|
/**
|
|
* Establishes the fieldset -> field relationship
|
|
*
|
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
|
* @since [v3.0]
|
|
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
|
*/
|
|
public function fields()
|
|
{
|
|
return $this->belongsToMany(\App\Models\CustomField::class)->withPivot(['required', 'order'])->orderBy('pivot_order');
|
|
}
|
|
|
|
/**
|
|
* Establishes the fieldset -> models relationship
|
|
*
|
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
|
* @since [v3.0]
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public function customizables() // TODO - I don't like this name, but I can't think of anything better
|
|
{
|
|
$customizable_class_name = $this->type; //TODO - copypasta from Customizable trait?
|
|
\Log::debug("Customizable Class name is: ".$customizable_class_name);
|
|
return $customizable_class_name::getFieldsetUsers($this->id);
|
|
}
|
|
|
|
/**
|
|
* Establishes the fieldset -> admin user relationship
|
|
*
|
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
|
* @since [v3.0]
|
|
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
|
*/
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(\App\Models\User::class); //WARNING - not all CustomFieldsets have a User!!
|
|
}
|
|
|
|
/**
|
|
* Determine the validation rules we should apply based on the
|
|
* custom field format
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v3.0]
|
|
* @return array
|
|
*/
|
|
public function validation_rules()
|
|
{
|
|
\Log::debug("CALLING validation_rules FOR customfiledsets!");
|
|
$rules = [];
|
|
foreach ($this->fields as $field) {
|
|
$rule = [];
|
|
|
|
if (($field->field_encrypted != '1') ||
|
|
(($field->field_encrypted == '1') && (Gate::allows('admin')))) {
|
|
$rule[] = ($field->pivot->required == '1') ? 'required' : 'nullable';
|
|
}
|
|
|
|
if ($field->is_unique == '1') {
|
|
$rule[] = 'unique_undeleted';
|
|
}
|
|
|
|
\Log::debug("Field Format for".$field->name." is: ".$field->format);
|
|
if($field->format == 'DATE') { //we do a weird mutator thing, it's confusing - but, yes, it's all-caps
|
|
$rule[] = 'date_format:Y-m-d';
|
|
} else {
|
|
array_push($rule, $field->attributes['format']);
|
|
}
|
|
$rules[$field->db_column_name()] = $rule;
|
|
|
|
// add not_array to rules for all fields but checkboxes
|
|
if ($field->element != 'checkbox') {
|
|
$rules[$field->db_column_name()][] = 'not_array';
|
|
}
|
|
|
|
if ($field->element == 'checkbox') {
|
|
$rules[$field->db_column_name()][] = 'checkboxes';
|
|
}
|
|
|
|
if ($field->element == 'radio') {
|
|
$rules[$field->db_column_name()][] = 'radio_buttons';
|
|
}
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
}
|