snipe-it/app/Models/CustomFieldset.php

100 lines
2.6 KiB
PHP
Raw Normal View History

2016-03-25 01:18:05 -07:00
<?php
2016-03-25 01:18:05 -07:00
namespace App\Models;
use Gate;
2021-06-10 13:19:27 -07:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Watson\Validating\ValidatingTrait;
2016-03-25 01:18:05 -07:00
class CustomFieldset extends Model
{
2021-06-10 13:17:44 -07:00
use HasFactory;
use ValidatingTrait;
protected $guarded = ['id'];
2016-03-25 01:18:05 -07:00
/**
* Validation rules
* @var array
*/
public $rules = [
'name' => 'required|unique:custom_fieldsets',
2016-03-25 01:18:05 -07:00
];
/**
* 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
*/
2016-03-25 01:18:05 -07:00
public function fields()
{
return $this->belongsToMany(\App\Models\CustomField::class)->withPivot(['required', 'order'])->orderBy('pivot_order');
2016-03-25 01:18:05 -07:00
}
/**
* Establishes the fieldset -> models relationship
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v3.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
2016-03-25 01:18:05 -07:00
public function models()
{
return $this->hasMany(\App\Models\AssetModel::class, 'fieldset_id');
2016-03-25 01:18:05 -07:00
}
/**
* Establishes the fieldset -> admin user relationship
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v3.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
2016-03-25 01:18:05 -07:00
public function user()
{
return $this->belongsTo(\App\Models\User::class); //WARNING - not all CustomFieldsets have a User!!
2016-03-25 01:18:05 -07:00
}
/**
* Determine the validation rules we should apply based on the
* custom field format
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return array
*/
2016-03-25 01:18:05 -07:00
public function validation_rules()
{
$rules = [];
2016-03-25 01:18:05 -07:00
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';
2016-03-25 01:18:05 -07:00
}
2016-03-25 01:18:05 -07:00
array_push($rule, $field->attributes['format']);
$rules[$field->db_column_name()] = $rule;
2016-03-25 01:18:05 -07:00
}
2016-03-25 01:18:05 -07:00
return $rules;
}
}