2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
namespace App\Models;
|
|
|
|
|
2016-08-25 21:03:52 -07:00
|
|
|
use Gate;
|
2021-06-10 13:19:27 -07:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-02-13 17:35:37 -08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Validation\Rule;
|
2017-07-12 19:34:45 -07:00
|
|
|
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;
|
2018-09-12 22:49:50 -07:00
|
|
|
use ValidatingTrait;
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
protected $guarded = ['id'];
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2018-09-12 22:49:50 -07:00
|
|
|
/**
|
|
|
|
* Validation rules
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-09-28 12:34:52 -07:00
|
|
|
public $rules = [
|
2021-06-10 13:15:52 -07:00
|
|
|
'name' => 'required|unique:custom_fieldsets',
|
2016-03-25 01:18:05 -07:00
|
|
|
];
|
|
|
|
|
2017-07-12 19:34:45 -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.
|
|
|
|
*
|
2021-06-10 13:15:52 -07:00
|
|
|
* @var bool
|
2017-07-12 19:34:45 -07:00
|
|
|
*/
|
|
|
|
protected $injectUniqueIdentifier = true;
|
|
|
|
|
2018-08-01 00:06:41 -07:00
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
{
|
2021-06-10 13:16:56 -07:00
|
|
|
return $this->belongsToMany(\App\Models\CustomField::class)->withPivot(['required', 'order'])->orderBy('pivot_order');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2018-08-01 00:06:41 -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()
|
|
|
|
{
|
2021-06-10 13:16:56 -07:00
|
|
|
return $this->hasMany(\App\Models\AssetModel::class, 'fieldset_id');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2018-08-01 00:06:41 -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()
|
|
|
|
{
|
2021-06-10 13:16:56 -07:00
|
|
|
return $this->belongsTo(\App\Models\User::class); //WARNING - not all CustomFieldsets have a User!!
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2018-08-01 00:06:41 -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()
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$rules = [];
|
2016-03-25 01:18:05 -07:00
|
|
|
foreach ($this->fields as $field) {
|
2016-08-25 21:03:52 -07:00
|
|
|
$rule = [];
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (($field->field_encrypted != '1') ||
|
|
|
|
(($field->field_encrypted == '1') && (Gate::allows('admin')))) {
|
2022-02-16 13:46:22 -08:00
|
|
|
$rule[] = ($field->pivot->required == '1') ? 'required' : 'nullable';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($field->is_unique == '1') {
|
2022-08-01 17:10:39 -07:00
|
|
|
$rule[] = 'unique_undeleted';
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-08-25 21:03:52 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
array_push($rule, $field->attributes['format']);
|
2021-06-10 13:15:52 -07:00
|
|
|
$rules[$field->db_column_name()] = $rule;
|
2024-02-14 09:41:46 -08:00
|
|
|
|
|
|
|
// add not_array to rules for all fields but checkboxes
|
2024-02-07 18:03:37 -08:00
|
|
|
if ($field->element != 'checkbox') {
|
|
|
|
$rules[$field->db_column_name()][] = 'not_array';
|
|
|
|
}
|
2024-02-14 09:41:46 -08:00
|
|
|
|
2024-02-13 17:35:37 -08:00
|
|
|
if ($field->element == 'checkbox') {
|
2024-02-14 00:12:31 -08:00
|
|
|
$rules[$field->db_column_name()][] = 'checkboxes';
|
2024-02-13 17:35:37 -08:00
|
|
|
}
|
2024-03-25 19:03:13 -07:00
|
|
|
|
|
|
|
if ($field->element == 'radio') {
|
|
|
|
$rules[$field->db_column_name()][] = 'radio_buttons';
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
}
|