snipe-it/app/Models/CustomFieldset.php

59 lines
1.5 KiB
PHP
Raw Normal View History

2016-03-25 01:18:05 -07:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Gate;
use Watson\Validating\ValidatingTrait;
2016-03-25 01:18:05 -07:00
class CustomFieldset extends Model
{
protected $guarded=["id"];
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 boolean
*/
protected $injectUniqueIdentifier = true;
use ValidatingTrait;
2016-03-25 01:18:05 -07:00
public function fields()
{
return $this->belongsToMany('\App\Models\CustomField')->withPivot(["required","order"])->orderBy("pivot_order");
}
public function models()
{
return $this->hasMany('\App\Models\AssetModel', "fieldset_id");
}
public function user()
{
return $this->belongsTo('\App\Models\User'); //WARNING - not all CustomFieldsets have a User!!
}
public function validation_rules()
{
$rules=[];
foreach ($this->fields as $field) {
$rule = [];
if (($field->field_encrypted!='1') ||
2016-12-29 14:02:18 -08:00
(($field->field_encrypted =='1') && (Gate::allows('admin')) )) {
2017-09-29 03:44:23 -07:00
$rule[] = ($field->pivot->required=='1') ? "required" : "nullable";
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;
}
return $rules;
}
}