snipe-it/app/Models/CustomFieldset.php
Laravel Shift 934afa036f Adopt Laravel coding style
Shift automatically applies the Laravel coding style - which uses the PSR-2 coding style as a base with some minor additions.

You may customize the adopted coding style by adding your own [PHP CS Fixer][1] `.php_cs` config file to your project root. Feel free to use [Shift's Laravel ruleset][2] to help you get started.

[1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer
[2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200
2021-06-10 20:15:52 +00:00

94 lines
2.4 KiB
PHP

<?php
namespace App\Models;
use Gate;
use Illuminate\Database\Eloquent\Model;
use Watson\Validating\ValidatingTrait;
class CustomFieldset extends Model
{
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')->withPivot(['required', 'order'])->orderBy('pivot_order');
}
/**
* Establishes the fieldset -> models relationship
*
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
* @since [v3.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function models()
{
return $this->hasMany('\App\Models\AssetModel', 'fieldset_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'); //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()
{
$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';
}
array_push($rule, $field->attributes['format']);
$rules[$field->db_column_name()] = $rule;
}
return $rules;
}
}