-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathCustomField.php
155 lines (133 loc) · 3.57 KB
/
CustomField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Givebutter\LaravelCustomFields\Models;
use Givebutter\LaravelCustomFields\Collections\CustomFieldCollection;
use Givebutter\LaravelCustomFields\FieldTypes\FieldType;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\App;
class CustomField extends Model
{
use SoftDeletes, HasFactory;
/**
* The attributes that aren't mass assignable.
*
* @var string[]|bool
*/
protected $guarded = [
'id',
];
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'group',
'type',
'title',
'description',
'answers',
'required',
'default_value',
'order',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'answers' => 'array',
'archived_at' => 'datetime',
];
/**
* CustomField constructor.
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->table = config('custom-fields.tables.fields', 'custom_fields');
}
/**
* Bootstrap the model and its traits.
*
* @return void
*/
protected static function boot()
{
parent::boot();
self::creating(function ($field) {
$lastFieldOnCurrentModel = $field->model
->customFields()
->reorder()
->orderByDesc('order')
->first();
$field->order = ($lastFieldOnCurrentModel ? $lastFieldOnCurrentModel->order : 0) + 1;
});
}
public function model(): MorphTo
{
return $this->morphTo();
}
public function responses(): HasMany
{
return $this->hasMany(config('custom-fields.models.custom-field-response'), 'field_id');
}
public function archive(): self
{
$this->forceFill([
'archived_at' => now(),
])->save();
return $this;
}
public function unarchive(): self
{
$this->forceFill([
'archived_at' => null,
])->save();
return $this;
}
public function validationRules(): Attribute
{
return new Attribute(
get: fn ($value, $attributes) => $this->field_type->validationRules($attributes),
);
}
public function validationAttributes(): Attribute
{
return new Attribute(
get: fn ($value, $attributes) => $this->field_type->validationAttributes(),
);
}
public function validationMessages(): Attribute
{
return new Attribute(
get: fn ($value, $attributes) => $this->field_type->validationMessages(),
);
}
public function fieldType(): Attribute
{
return Attribute::get(
fn ($value, array $attributes) => App::makeWith(FieldType::class, [
'type' => $attributes['type'],
'field' => $this,
]),
);
}
/**
* Create a new Eloquent Collection instance.
*
* @param array $models
* @return \Illuminate\Database\Eloquent\Collection
*/
public function newCollection(array $models = [])
{
return new CustomFieldCollection($models);
}
}